| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -536,11 +536,20 @@ PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); | |||
| 536 | 536 | PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); | |
| 537 | 537 | #ifndef Py_LIMITED_API | |
| 538 | 538 | PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); | |
| 539 | - /* Same as PyObject_GetAttr(), but don't raise AttributeError. */ | ||
| 540 | - PyAPI_FUNC(PyObject *) _PyObject_GetAttrWithoutError(PyObject *, PyObject *); | ||
| 541 | 539 | PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); | |
| 542 | 540 | PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); | |
| 543 | 541 | PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *); | |
| 542 | + /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which | ||
| 543 | + don't raise AttributeError. | ||
| 544 | + | ||
| 545 | + Return 1 and set *result != NULL if an attribute is found. | ||
| 546 | + Return 0 and set *result == NULL if an attribute is not found; | ||
| 547 | + an AttributeError is silenced. | ||
| 548 | + Return -1 and set *result == NULL if an error other than AttributeError | ||
| 549 | + is raised. | ||
| 550 | + */ | ||
| 551 | + PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **); | ||
| 552 | + PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **); | ||
| 544 | 553 | PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); | |
| 545 | 554 | #endif | |
| 546 | 555 | PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1339,12 +1339,10 @@ deque_reduce(dequeobject *deque) | |||
| 1339 | 1339 | PyObject *dict, *it; | |
| 1340 | 1340 | _Py_IDENTIFIER(__dict__); | |
| 1341 | 1341 | ||
| 1342 | - dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__); | ||
| 1342 | + if (_PyObject_LookupAttrId((PyObject *)deque, &PyId___dict__, &dict) < 0) { | ||
| 1343 | + return NULL; | ||
| 1344 | + } | ||
| 1343 | 1345 | if (dict == NULL) { | |
| 1344 | - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { | ||
| 1345 | - return NULL; | ||
| 1346 | - } | ||
| 1347 | - PyErr_Clear(); | ||
| 1348 | 1346 | dict = Py_None; | |
| 1349 | 1347 | Py_INCREF(dict); | |
| 1350 | 1348 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1541,7 +1541,9 @@ _bufferedreader_read_all(buffered *self) | |||
| 1541 | 1541 | } | |
| 1542 | 1542 | _bufferedreader_reset_buf(self); | |
| 1543 | 1543 | ||
| 1544 | - readall = _PyObject_GetAttrWithoutError(self->raw, _PyIO_str_readall); | ||
| 1544 | + if (_PyObject_LookupAttr(self->raw, _PyIO_str_readall, &readall) < 0) { | ||
| 1545 | + goto cleanup; | ||
| 1546 | + } | ||
| 1545 | 1547 | if (readall) { | |
| 1546 | 1548 | tmp = _PyObject_CallNoArg(readall); | |
| 1547 | 1549 | Py_DECREF(readall); | |
@@ -1561,9 +1563,6 @@ _bufferedreader_read_all(buffered *self) | |||
| 1561 | 1563 | } | |
| 1562 | 1564 | goto cleanup; | |
| 1563 | 1565 | } | |
| 1564 | - else if (PyErr_Occurred()) { | ||
| 1565 | - goto cleanup; | ||
| 1566 | - } | ||
| 1567 | 1566 | ||
| 1568 | 1567 | chunks = PyList_New(0); | |
| 1569 | 1568 | if (chunks == NULL) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1073,12 +1073,10 @@ fileio_repr(fileio *self) | |||
| 1073 | 1073 | if (self->fd < 0) | |
| 1074 | 1074 | return PyUnicode_FromFormat("<_io.FileIO [closed]>"); | |
| 1075 | 1075 | ||
| 1076 | - nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name); | ||
| 1076 | + if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { | ||
| 1077 | + return NULL; | ||
| 1078 | + } | ||
| 1077 | 1079 | if (nameobj == NULL) { | |
| 1078 | - if (PyErr_ExceptionMatches(PyExc_AttributeError)) | ||
| 1079 | - PyErr_Clear(); | ||
| 1080 | - else | ||
| 1081 | - return NULL; | ||
| 1082 | 1080 | res = PyUnicode_FromFormat( | |
| 1083 | 1081 | "<_io.FileIO fd=%d mode='%s' closefd=%s>", | |
| 1084 | 1082 | self->fd, mode_string(self), self->closefd ? "True" : "False"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -133,18 +133,12 @@ static int | |||
| 133 | 133 | iobase_is_closed(PyObject *self) | |
| 134 | 134 | { | |
| 135 | 135 | PyObject *res; | |
| 136 | + int ret; | ||
| 136 | 137 | /* This gets the derived attribute, which is *not* __IOBase_closed | |
| 137 | 138 | in most cases! */ | |
| 138 | - res = _PyObject_GetAttrId(self, &PyId___IOBase_closed); | ||
| 139 | - if (res == NULL) { | ||
| 140 | - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { | ||
| 141 | - return -1; | ||
| 142 | - } | ||
| 143 | - PyErr_Clear(); | ||
| 144 | - return 0; | ||
| 145 | - } | ||
| 146 | - Py_DECREF(res); | ||
| 147 | - return 1; | ||
| 139 | + ret = _PyObject_LookupAttrId(self, &PyId___IOBase_closed, &res); | ||
| 140 | + Py_XDECREF(res); | ||
| 141 | + return ret; | ||
| 148 | 142 | } | |
| 149 | 143 | ||
| 150 | 144 | /* Flush and close methods */ | |
@@ -190,20 +184,16 @@ iobase_check_closed(PyObject *self) | |||
| 190 | 184 | int closed; | |
| 191 | 185 | /* This gets the derived attribute, which is *not* __IOBase_closed | |
| 192 | 186 | in most cases! */ | |
| 193 | - res = _PyObject_GetAttrWithoutError(self, _PyIO_str_closed); | ||
| 194 | - if (res == NULL) { | ||
| 195 | - if (PyErr_Occurred()) { | ||
| 187 | + closed = _PyObject_LookupAttr(self, _PyIO_str_closed, &res); | ||
| 188 | + if (closed > 0) { | ||
| 189 | + closed = PyObject_IsTrue(res); | ||
| 190 | + Py_DECREF(res); | ||
| 191 | + if (closed > 0) { | ||
| 192 | + PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); | ||
| 196 | 193 | return -1; | |
| 197 | 194 | } | |
| 198 | - return 0; | ||
| 199 | - } | ||
| 200 | - closed = PyObject_IsTrue(res); | ||
| 201 | - Py_DECREF(res); | ||
| 202 | - if (closed <= 0) { | ||
| 203 | - return closed; | ||
| 204 | 195 | } | |
| 205 | - PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); | ||
| 206 | - return -1; | ||
| 196 | + return closed; | ||
| 207 | 197 | } | |
| 208 | 198 | ||
| 209 | 199 | PyObject * | |
@@ -273,8 +263,7 @@ iobase_finalize(PyObject *self) | |||
| 273 | 263 | ||
| 274 | 264 | /* If `closed` doesn't exist or can't be evaluated as bool, then the | |
| 275 | 265 | object is probably in an unusable state, so ignore. */ | |
| 276 | - res = _PyObject_GetAttrWithoutError(self, _PyIO_str_closed); | ||
| 277 | - if (res == NULL) { | ||
| 266 | + if (_PyObject_LookupAttr(self, _PyIO_str_closed, &res) <= 0) { | ||
| 278 | 267 | PyErr_Clear(); | |
| 279 | 268 | closed = -1; | |
| 280 | 269 | } | |
@@ -538,8 +527,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) | |||
| 538 | 527 | PyObject *peek, *buffer, *result; | |
| 539 | 528 | Py_ssize_t old_size = -1; | |
| 540 | 529 | ||
| 541 | - peek = _PyObject_GetAttrWithoutError(self, _PyIO_str_peek); | ||
| 542 | - if (peek == NULL && PyErr_Occurred()) { | ||
| 530 | + if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) { | ||
| 543 | 531 | return NULL; | |
| 544 | 532 | } | |
| 545 | 533 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -924,14 +924,10 @@ _textiowrapper_set_encoder(textio *self, PyObject *codec_info, | |||
| 924 | 924 | return -1; | |
| 925 | 925 | ||
| 926 | 926 | /* Get the normalized named of the codec */ | |
| 927 | - res = _PyObject_GetAttrId(codec_info, &PyId_name); | ||
| 928 | - if (res == NULL) { | ||
| 929 | - if (PyErr_ExceptionMatches(PyExc_AttributeError)) | ||
| 930 | - PyErr_Clear(); | ||
| 931 | - else | ||
| 932 | - return -1; | ||
| 927 | + if (_PyObject_LookupAttrId(codec_info, &PyId_name, &res) < 0) { | ||
| 928 | + return -1; | ||
| 933 | 929 | } | |
| 934 | - else if (PyUnicode_Check(res)) { | ||
| 930 | + if (res != NULL && PyUnicode_Check(res)) { | ||
| 935 | 931 | const encodefuncentry *e = encodefuncs; | |
| 936 | 932 | while (e->name != NULL) { | |
| 937 | 933 | if (_PyUnicode_EqualToASCIIString(res, e->name)) { | |
@@ -1177,19 +1173,17 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, | |||
| 1177 | 1173 | ||
| 1178 | 1174 | if (Py_TYPE(buffer) == &PyBufferedReader_Type || | |
| 1179 | 1175 | Py_TYPE(buffer) == &PyBufferedWriter_Type || | |
| 1180 | - Py_TYPE(buffer) == &PyBufferedRandom_Type) { | ||
| 1181 | - raw = _PyObject_GetAttrId(buffer, &PyId_raw); | ||
| 1176 | + Py_TYPE(buffer) == &PyBufferedRandom_Type) | ||
| 1177 | + { | ||
| 1178 | + if (_PyObject_LookupAttrId(buffer, &PyId_raw, &raw) < 0) | ||
| 1179 | + goto error; | ||
| 1182 | 1180 | /* Cache the raw FileIO object to speed up 'closed' checks */ | |
| 1183 | - if (raw == NULL) { | ||
| 1184 | - if (PyErr_ExceptionMatches(PyExc_AttributeError)) | ||
| 1185 | - PyErr_Clear(); | ||
| 1181 | + if (raw != NULL) { | ||
| 1182 | + if (Py_TYPE(raw) == &PyFileIO_Type) | ||
| 1183 | + self->raw = raw; | ||
| 1186 | 1184 | else | |
| 1187 | - goto error; | ||
| 1185 | + Py_DECREF(raw); | ||
| 1188 | 1186 | } | |
| 1189 | - else if (Py_TYPE(raw) == &PyFileIO_Type) | ||
| 1190 | - self->raw = raw; | ||
| 1191 | - else | ||
| 1192 | - Py_DECREF(raw); | ||
| 1193 | 1187 | } | |
| 1194 | 1188 | ||
| 1195 | 1189 | res = _PyObject_CallMethodId(buffer, &PyId_seekable, NULL); | |
@@ -1201,17 +1195,12 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, | |||
| 1201 | 1195 | goto error; | |
| 1202 | 1196 | self->seekable = self->telling = r; | |
| 1203 | 1197 | ||
| 1204 | - res = _PyObject_GetAttrWithoutError(buffer, _PyIO_str_read1); | ||
| 1205 | - if (res != NULL) { | ||
| 1206 | - Py_DECREF(res); | ||
| 1207 | - self->has_read1 = 1; | ||
| 1208 | - } | ||
| 1209 | - else if (!PyErr_Occurred()) { | ||
| 1210 | - self->has_read1 = 0; | ||
| 1211 | - } | ||
| 1212 | - else { | ||
| 1198 | + r = _PyObject_LookupAttr(buffer, _PyIO_str_read1, &res); | ||
| 1199 | + if (r < 0) { | ||
| 1213 | 1200 | goto error; | |
| 1214 | 1201 | } | |
| 1202 | + Py_XDECREF(res); | ||
| 1203 | + self->has_read1 = r; | ||
| 1215 | 1204 | ||
| 1216 | 1205 | self->encoding_start_of_stream = 0; | |
| 1217 | 1206 | if (_textiowrapper_fix_encoder_state(self) < 0) { | |
@@ -3020,10 +3009,9 @@ textiowrapper_newlines_get(textio *self, void *context) | |||
| 3020 | 3009 | { | |
| 3021 | 3010 | PyObject *res; | |
| 3022 | 3011 | CHECK_ATTACHED(self); | |
| 3023 | - if (self->decoder == NULL) | ||
| 3024 | - Py_RETURN_NONE; | ||
| 3025 | - res = _PyObject_GetAttrWithoutError(self->decoder, _PyIO_str_newlines); | ||
| 3026 | - if (res == NULL && !PyErr_Occurred()) { | ||
| 3012 | + if (self->decoder == NULL || | ||
| 3013 | + _PyObject_LookupAttr(self->decoder, _PyIO_str_newlines, &res) == 0) | ||
| 3014 | + { | ||
| 3027 | 3015 | Py_RETURN_NONE; | |
| 3028 | 3016 | } | |
| 3029 | 3017 | return res; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments