Skip to content

Commit be1b968

Browse files
gh-106521: Remove _PyObject_LookupAttr() function (GH-106642)
1 parent e8ab009 commit be1b968

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+351
-352
lines changed

Include/cpython/object.h

-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
294294
PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
295295
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
296296
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
297-
#define _PyObject_LookupAttr PyObject_GetOptionalAttr
298297

299298
PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
300299

Modules/_abc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ compute_abstract_methods(PyObject *self)
361361
PyObject *item = PyTuple_GET_ITEM(bases, pos); // borrowed
362362
PyObject *base_abstracts, *iter;
363363

364-
if (_PyObject_LookupAttr(item, &_Py_ID(__abstractmethods__),
364+
if (PyObject_GetOptionalAttr(item, &_Py_ID(__abstractmethods__),
365365
&base_abstracts) < 0) {
366366
goto error;
367367
}
@@ -375,7 +375,7 @@ compute_abstract_methods(PyObject *self)
375375
Py_DECREF(base_abstracts);
376376
PyObject *key, *value;
377377
while ((key = PyIter_Next(iter))) {
378-
if (_PyObject_LookupAttr(self, key, &value) < 0) {
378+
if (PyObject_GetOptionalAttr(self, key, &value) < 0) {
379379
Py_DECREF(key);
380380
Py_DECREF(iter);
381381
goto error;

Modules/_asynciomodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ get_future_loop(asyncio_state *state, PyObject *fut)
248248
return Py_NewRef(loop);
249249
}
250250

251-
if (_PyObject_LookupAttr(fut, &_Py_ID(get_loop), &getloop) < 0) {
251+
if (PyObject_GetOptionalAttr(fut, &_Py_ID(get_loop), &getloop) < 0) {
252252
return NULL;
253253
}
254254
if (getloop != NULL) {
@@ -2966,7 +2966,7 @@ task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *resu
29662966
}
29672967

29682968
/* Check if `result` is a Future-compatible object */
2969-
if (_PyObject_LookupAttr(result, &_Py_ID(_asyncio_future_blocking), &o) < 0) {
2969+
if (PyObject_GetOptionalAttr(result, &_Py_ID(_asyncio_future_blocking), &o) < 0) {
29702970
goto fail;
29712971
}
29722972
if (o != NULL && o != Py_None) {

Modules/_csv.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
14501450
Py_DECREF(self);
14511451
return NULL;
14521452
}
1453-
if (_PyObject_LookupAttr(output_file,
1453+
if (PyObject_GetOptionalAttr(output_file,
14541454
module_state->str_write,
14551455
&self->write) < 0) {
14561456
Py_DECREF(self);

Modules/_ctypes/_ctypes.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ CDataType_from_param(PyObject *type, PyObject *value)
851851
return NULL;
852852
}
853853

854-
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
854+
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
855855
return NULL;
856856
}
857857
if (as_parameter) {
@@ -1495,7 +1495,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14951495
stgdict = NULL;
14961496
type_attr = NULL;
14971497

1498-
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_length_), &length_attr) < 0) {
1498+
if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_length_), &length_attr) < 0) {
14991499
goto error;
15001500
}
15011501
if (!length_attr) {
@@ -1528,7 +1528,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15281528
goto error;
15291529
}
15301530

1531-
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_type_), &type_attr) < 0) {
1531+
if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_type_), &type_attr) < 0) {
15321532
goto error;
15331533
}
15341534
if (!type_attr) {
@@ -1720,7 +1720,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
17201720
}
17211721
}
17221722

1723-
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
1723+
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
17241724
return NULL;
17251725
}
17261726
if (as_parameter) {
@@ -1784,7 +1784,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
17841784
}
17851785
}
17861786

1787-
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
1787+
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
17881788
return NULL;
17891789
}
17901790
if (as_parameter) {
@@ -1919,7 +1919,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
19191919
}
19201920
}
19211921

1922-
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
1922+
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
19231923
return NULL;
19241924
}
19251925
if (as_parameter) {
@@ -2054,7 +2054,7 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20542054
if (result == NULL)
20552055
return NULL;
20562056

2057-
if (_PyObject_LookupAttr((PyObject *)result, &_Py_ID(_type_), &proto) < 0) {
2057+
if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_type_), &proto) < 0) {
20582058
return NULL;
20592059
}
20602060
if (!proto) {
@@ -2266,7 +2266,7 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
22662266
PyObject *exc = PyErr_GetRaisedException();
22672267
Py_DECREF(parg);
22682268

2269-
if (_PyObject_LookupAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
2269+
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) {
22702270
Py_XDECREF(exc);
22712271
return NULL;
22722272
}
@@ -2429,7 +2429,7 @@ converters_from_argtypes(PyObject *ob)
24292429
}
24302430
*/
24312431

2432-
if (_PyObject_LookupAttr(tp, &_Py_ID(from_param), &cnv) <= 0) {
2432+
if (PyObject_GetOptionalAttr(tp, &_Py_ID(from_param), &cnv) <= 0) {
24332433
Py_DECREF(converters);
24342434
Py_DECREF(ob);
24352435
if (!PyErr_Occurred()) {
@@ -2489,7 +2489,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24892489
return -1;
24902490
}
24912491
stgdict->restype = Py_NewRef(ob);
2492-
if (_PyObject_LookupAttr(ob, &_Py_ID(_check_retval_),
2492+
if (PyObject_GetOptionalAttr(ob, &_Py_ID(_check_retval_),
24932493
&stgdict->checker) < 0)
24942494
{
24952495
return -1;
@@ -3275,7 +3275,7 @@ PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ign
32753275
"restype must be a type, a callable, or None");
32763276
return -1;
32773277
}
3278-
if (_PyObject_LookupAttr(ob, &_Py_ID(_check_retval_), &checker) < 0) {
3278+
if (PyObject_GetOptionalAttr(ob, &_Py_ID(_check_retval_), &checker) < 0) {
32793279
return -1;
32803280
}
32813281
oldchecker = self->checker;

Modules/_ctypes/callproc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
727727

728728
{
729729
PyObject *arg;
730-
if (_PyObject_LookupAttr(obj, &_Py_ID(_as_parameter_), &arg) < 0) {
730+
if (PyObject_GetOptionalAttr(obj, &_Py_ID(_as_parameter_), &arg) < 0) {
731731
return -1;
732732
}
733733
/* Which types should we exactly allow here?

Modules/_ctypes/stgdict.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ MakeAnonFields(PyObject *type)
295295
PyObject *anon_names;
296296
Py_ssize_t i;
297297

298-
if (_PyObject_LookupAttr(type, &_Py_ID(_anonymous_), &anon) < 0) {
298+
if (PyObject_GetOptionalAttr(type, &_Py_ID(_anonymous_), &anon) < 0) {
299299
return -1;
300300
}
301301
if (anon == NULL) {
@@ -385,7 +385,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
385385
if (fields == NULL)
386386
return 0;
387387

388-
if (_PyObject_LookupAttr(type, &_Py_ID(_swappedbytes_), &tmp) < 0) {
388+
if (PyObject_GetOptionalAttr(type, &_Py_ID(_swappedbytes_), &tmp) < 0) {
389389
return -1;
390390
}
391391
if (tmp) {
@@ -396,7 +396,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
396396
big_endian = PY_BIG_ENDIAN;
397397
}
398398

399-
if (_PyObject_LookupAttr(type, &_Py_ID(_pack_), &tmp) < 0) {
399+
if (PyObject_GetOptionalAttr(type, &_Py_ID(_pack_), &tmp) < 0) {
400400
return -1;
401401
}
402402
if (tmp) {

Modules/_datetimemodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3791,7 +3791,7 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
37913791
PyObject *args, *state;
37923792
PyObject *getinitargs;
37933793

3794-
if (_PyObject_LookupAttr(self, &_Py_ID(__getinitargs__), &getinitargs) < 0) {
3794+
if (PyObject_GetOptionalAttr(self, &_Py_ID(__getinitargs__), &getinitargs) < 0) {
37953795
return NULL;
37963796
}
37973797
if (getinitargs != NULL) {

Modules/_elementtree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3530,7 +3530,7 @@ expat_start_doctype_handler(XMLParserObject *self,
35303530
sysid_obj, NULL);
35313531
Py_XDECREF(res);
35323532
}
3533-
else if (_PyObject_LookupAttr((PyObject *)self, st->str_doctype, &res) > 0) {
3533+
else if (PyObject_GetOptionalAttr((PyObject *)self, st->str_doctype, &res) > 0) {
35343534
(void)PyErr_WarnEx(PyExc_RuntimeWarning,
35353535
"The doctype() method of XMLParser is ignored. "
35363536
"Define doctype() method on the TreeBuilder target.",

Modules/_io/bufferedio.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ buffered_repr(buffered *self)
14631463
{
14641464
PyObject *nameobj, *res;
14651465

1466-
if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
1466+
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
14671467
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
14681468
return NULL;
14691469
}
@@ -1630,7 +1630,7 @@ _bufferedreader_read_all(buffered *self)
16301630
}
16311631
_bufferedreader_reset_buf(self);
16321632

1633-
if (_PyObject_LookupAttr(self->raw, &_Py_ID(readall), &readall) < 0) {
1633+
if (PyObject_GetOptionalAttr(self->raw, &_Py_ID(readall), &readall) < 0) {
16341634
goto cleanup;
16351635
}
16361636
if (readall) {

Modules/_io/fileio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ fileio_repr(fileio *self)
10991099
if (self->fd < 0)
11001100
return PyUnicode_FromFormat("<_io.FileIO [closed]>");
11011101

1102-
if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
1102+
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
11031103
return NULL;
11041104
}
11051105
if (nameobj == NULL) {

Modules/_io/iobase.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ iobase_is_closed(PyObject *self)
148148
int ret;
149149
/* This gets the derived attribute, which is *not* __IOBase_closed
150150
in most cases! */
151-
ret = _PyObject_LookupAttr(self, &_Py_ID(__IOBase_closed), &res);
151+
ret = PyObject_GetOptionalAttr(self, &_Py_ID(__IOBase_closed), &res);
152152
Py_XDECREF(res);
153153
return ret;
154154
}
@@ -196,7 +196,7 @@ iobase_check_closed(PyObject *self)
196196
int closed;
197197
/* This gets the derived attribute, which is *not* __IOBase_closed
198198
in most cases! */
199-
closed = _PyObject_LookupAttr(self, &_Py_ID(closed), &res);
199+
closed = PyObject_GetOptionalAttr(self, &_Py_ID(closed), &res);
200200
if (closed > 0) {
201201
closed = PyObject_IsTrue(res);
202202
Py_DECREF(res);
@@ -303,7 +303,7 @@ iobase_finalize(PyObject *self)
303303

304304
/* If `closed` doesn't exist or can't be evaluated as bool, then the
305305
object is probably in an unusable state, so ignore. */
306-
if (_PyObject_LookupAttr(self, &_Py_ID(closed), &res) <= 0) {
306+
if (PyObject_GetOptionalAttr(self, &_Py_ID(closed), &res) <= 0) {
307307
PyErr_Clear();
308308
closed = -1;
309309
}
@@ -571,7 +571,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
571571
PyObject *peek, *buffer, *result;
572572
Py_ssize_t old_size = -1;
573573

574-
if (_PyObject_LookupAttr(self, &_Py_ID(peek), &peek) < 0) {
574+
if (PyObject_GetOptionalAttr(self, &_Py_ID(peek), &peek) < 0) {
575575
return NULL;
576576
}
577577

Modules/_io/textio.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ _textiowrapper_set_encoder(textio *self, PyObject *codec_info,
946946
return -1;
947947

948948
/* Get the normalized named of the codec */
949-
if (_PyObject_LookupAttr(codec_info, &_Py_ID(name), &res) < 0) {
949+
if (PyObject_GetOptionalAttr(codec_info, &_Py_ID(name), &res) < 0) {
950950
return -1;
951951
}
952952
if (res != NULL && PyUnicode_Check(res)) {
@@ -1202,7 +1202,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
12021202
Py_IS_TYPE(buffer, state->PyBufferedWriter_Type) ||
12031203
Py_IS_TYPE(buffer, state->PyBufferedRandom_Type))
12041204
{
1205-
if (_PyObject_LookupAttr(buffer, &_Py_ID(raw), &raw) < 0)
1205+
if (PyObject_GetOptionalAttr(buffer, &_Py_ID(raw), &raw) < 0)
12061206
goto error;
12071207
/* Cache the raw FileIO object to speed up 'closed' checks */
12081208
if (raw != NULL) {
@@ -1222,7 +1222,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
12221222
goto error;
12231223
self->seekable = self->telling = r;
12241224

1225-
r = _PyObject_LookupAttr(buffer, &_Py_ID(read1), &res);
1225+
r = PyObject_GetOptionalAttr(buffer, &_Py_ID(read1), &res);
12261226
if (r < 0) {
12271227
goto error;
12281228
}
@@ -2897,7 +2897,7 @@ textiowrapper_repr(textio *self)
28972897
}
28982898
goto error;
28992899
}
2900-
if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
2900+
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
29012901
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
29022902
goto error;
29032903
}
@@ -2913,7 +2913,7 @@ textiowrapper_repr(textio *self)
29132913
if (res == NULL)
29142914
goto error;
29152915
}
2916-
if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(mode), &modeobj) < 0) {
2916+
if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(mode), &modeobj) < 0) {
29172917
goto error;
29182918
}
29192919
if (modeobj != NULL) {
@@ -3130,7 +3130,7 @@ textiowrapper_newlines_get(textio *self, void *context)
31303130
PyObject *res;
31313131
CHECK_ATTACHED(self);
31323132
if (self->decoder == NULL ||
3133-
_PyObject_LookupAttr(self->decoder, &_Py_ID(newlines), &res) == 0)
3133+
PyObject_GetOptionalAttr(self->decoder, &_Py_ID(newlines), &res) == 0)
31343134
{
31353135
Py_RETURN_NONE;
31363136
}

0 commit comments

Comments
 (0)