Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55c861f

Browse files
Issue #26745: Removed redundant code in _PyObject_GenericSetAttrWithDict.
Based on patch by Xiang Zhang.
1 parent a858bbd commit 55c861f

1 file changed

Lines changed: 21 additions & 30 deletions

File tree

Objects/object.c

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,19 +1040,18 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
10401040
name->ob_type->tp_name);
10411041
return NULL;
10421042
}
1043-
else
1044-
Py_INCREF(name);
1043+
Py_INCREF(name);
10451044

10461045
if (tp->tp_dict == NULL) {
10471046
if (PyType_Ready(tp) < 0)
10481047
goto done;
10491048
}
10501049

10511050
descr = _PyType_Lookup(tp, name);
1052-
Py_XINCREF(descr);
10531051

10541052
f = NULL;
10551053
if (descr != NULL) {
1054+
Py_INCREF(descr);
10561055
f = descr->ob_type->tp_descr_get;
10571056
if (f != NULL && PyDescr_IsData(descr)) {
10581057
res = f(descr, obj, (PyObject *)obj->ob_type);
@@ -1072,8 +1071,9 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
10721071
if (tsize < 0)
10731072
tsize = -tsize;
10741073
size = _PyObject_VAR_SIZE(tp, tsize);
1074+
assert(size <= PY_SSIZE_T_MAX);
10751075

1076-
dictoffset += (long)size;
1076+
dictoffset += (Py_ssize_t)size;
10771077
assert(dictoffset > 0);
10781078
assert(dictoffset % SIZEOF_VOID_P == 0);
10791079
}
@@ -1141,53 +1141,44 @@ _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
11411141
Py_INCREF(name);
11421142

11431143
descr = _PyType_Lookup(tp, name);
1144-
Py_XINCREF(descr);
11451144

1146-
f = NULL;
11471145
if (descr != NULL) {
1146+
Py_INCREF(descr);
11481147
f = descr->ob_type->tp_descr_set;
1149-
if (f != NULL && PyDescr_IsData(descr)) {
1148+
if (f != NULL) {
11501149
res = f(descr, obj, value);
11511150
goto done;
11521151
}
11531152
}
11541153

11551154
if (dict == NULL) {
11561155
dictptr = _PyObject_GetDictPtr(obj);
1157-
if (dictptr != NULL) {
1158-
res = _PyObjectDict_SetItem(Py_TYPE(obj), dictptr, name, value);
1159-
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1160-
PyErr_SetObject(PyExc_AttributeError, name);
1156+
if (dictptr == NULL) {
1157+
if (descr == NULL) {
1158+
PyErr_Format(PyExc_AttributeError,
1159+
"'%.100s' object has no attribute '%U'",
1160+
tp->tp_name, name);
1161+
}
1162+
else {
1163+
PyErr_Format(PyExc_AttributeError,
1164+
"'%.50s' object attribute '%U' is read-only",
1165+
tp->tp_name, name);
1166+
}
11611167
goto done;
11621168
}
1169+
res = _PyObjectDict_SetItem(tp, dictptr, name, value);
11631170
}
1164-
if (dict != NULL) {
1171+
else {
11651172
Py_INCREF(dict);
11661173
if (value == NULL)
11671174
res = PyDict_DelItem(dict, name);
11681175
else
11691176
res = PyDict_SetItem(dict, name, value);
11701177
Py_DECREF(dict);
1171-
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1172-
PyErr_SetObject(PyExc_AttributeError, name);
1173-
goto done;
11741178
}
1179+
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1180+
PyErr_SetObject(PyExc_AttributeError, name);
11751181

1176-
if (f != NULL) {
1177-
res = f(descr, obj, value);
1178-
goto done;
1179-
}
1180-
1181-
if (descr == NULL) {
1182-
PyErr_Format(PyExc_AttributeError,
1183-
"'%.100s' object has no attribute '%U'",
1184-
tp->tp_name, name);
1185-
goto done;
1186-
}
1187-
1188-
PyErr_Format(PyExc_AttributeError,
1189-
"'%.50s' object attribute '%U' is read-only",
1190-
tp->tp_name, name);
11911182
done:
11921183
Py_XDECREF(descr);
11931184
Py_DECREF(name);

0 commit comments

Comments
 (0)