diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2022-02-08 20:39:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-08 20:39:07 (GMT) |
commit | 81c72044a181dbbfbf689d7a977d0d99090f26a8 (patch) | |
tree | 14329746bd6f179cf2ae7c9818e1ae881eb46360 /Modules | |
parent | c018d3037b5b62e6d48d5985d1a37b91762fbffb (diff) | |
download | cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.zip cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.gz cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.bz2 |
bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized global objects. (gh-30928)
We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules.
The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings).
https://bugs.python.org/issue46541#msg411799 explains the rationale for this change.
The core of the change is in:
* (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros
* Include/internal/pycore_runtime_init.h - added the static initializers for the global strings
* Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState
* Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers
I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config.
The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *.
The following are not changed (yet):
* stop using _Py_IDENTIFIER() in the stdlib modules
* (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API
* (maybe) intern the strings during runtime init
https://bugs.python.org/issue46541
Diffstat (limited to 'Modules')
43 files changed, 244 insertions, 334 deletions
diff --git a/Modules/_abc.c b/Modules/_abc.c index a043961..641d619 100644 --- a/Modules/_abc.c +++ b/Modules/_abc.c @@ -4,8 +4,9 @@ #endif #include "Python.h" -#include "pycore_object.h" // _PyType_GetSubclasses() #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_object.h" // _PyType_GetSubclasses() +#include "pycore_runtime.h" // _Py_ID() #include "clinic/_abc.c.h" /*[clinic input] @@ -16,15 +17,6 @@ module _abc PyDoc_STRVAR(_abc__doc__, "Module contains faster C implementation of abc.ABCMeta"); -_Py_IDENTIFIER(__abstractmethods__); -_Py_IDENTIFIER(__class__); -_Py_IDENTIFIER(__dict__); -_Py_IDENTIFIER(__abc_tpflags__); -_Py_IDENTIFIER(__bases__); -_Py_IDENTIFIER(_abc_impl); -_Py_IDENTIFIER(__subclasscheck__); -_Py_IDENTIFIER(__subclasshook__); - typedef struct { PyTypeObject *_abc_data_type; unsigned long long abc_invalidation_counter; @@ -122,7 +114,7 @@ static _abc_data * _get_impl(PyObject *module, PyObject *self) { _abcmodule_state *state = get_abc_state(module); - PyObject *impl = _PyObject_GetAttrId(self, &PyId__abc_impl); + PyObject *impl = PyObject_GetAttr(self, &_Py_ID(_abc_impl)); if (impl == NULL) { return NULL; } @@ -311,7 +303,7 @@ compute_abstract_methods(PyObject *self) PyObject *ns = NULL, *items = NULL, *bases = NULL; // Py_XDECREF()ed on error. /* Stage 1: direct abstract methods. */ - ns = _PyObject_GetAttrId(self, &PyId___dict__); + ns = PyObject_GetAttr(self, &_Py_ID(__dict__)); if (!ns) { goto error; } @@ -355,7 +347,7 @@ compute_abstract_methods(PyObject *self) } /* Stage 2: inherited abstract methods. */ - bases = _PyObject_GetAttrId(self, &PyId___bases__); + bases = PyObject_GetAttr(self, &_Py_ID(__bases__)); if (!bases) { goto error; } @@ -368,8 +360,8 @@ compute_abstract_methods(PyObject *self) PyObject *item = PyTuple_GET_ITEM(bases, pos); // borrowed PyObject *base_abstracts, *iter; - if (_PyObject_LookupAttrId(item, &PyId___abstractmethods__, - &base_abstracts) < 0) { + if (_PyObject_LookupAttr(item, &_Py_ID(__abstractmethods__), + &base_abstracts) < 0) { goto error; } if (base_abstracts == NULL) { @@ -409,7 +401,7 @@ compute_abstract_methods(PyObject *self) } } - if (_PyObject_SetAttrId(self, &PyId___abstractmethods__, abstracts) < 0) { + if (PyObject_SetAttr(self, &_Py_ID(__abstractmethods__), abstracts) < 0) { goto error; } @@ -448,7 +440,7 @@ _abc__abc_init(PyObject *module, PyObject *self) if (data == NULL) { return NULL; } - if (_PyObject_SetAttrId(self, &PyId__abc_impl, data) < 0) { + if (PyObject_SetAttr(self, &_Py_ID(_abc_impl), data) < 0) { Py_DECREF(data); return NULL; } @@ -459,7 +451,8 @@ _abc__abc_init(PyObject *module, PyObject *self) * their special status w.r.t. pattern matching. */ if (PyType_Check(self)) { PyTypeObject *cls = (PyTypeObject *)self; - PyObject *flags = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___abc_tpflags__); + PyObject *flags = PyDict_GetItemWithError(cls->tp_dict, + &_Py_ID(__abc_tpflags__)); if (flags == NULL) { if (PyErr_Occurred()) { return NULL; @@ -477,7 +470,7 @@ _abc__abc_init(PyObject *module, PyObject *self) } ((PyTypeObject *)self)->tp_flags |= (val & COLLECTION_FLAGS); } - if (_PyDict_DelItemId(cls->tp_dict, &PyId___abc_tpflags__) < 0) { + if (PyDict_DelItem(cls->tp_dict, &_Py_ID(__abc_tpflags__)) < 0) { return NULL; } } @@ -593,7 +586,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, return NULL; } - subclass = _PyObject_GetAttrId(instance, &PyId___class__); + subclass = PyObject_GetAttr(instance, &_Py_ID(__class__)); if (subclass == NULL) { Py_DECREF(impl); return NULL; @@ -622,12 +615,12 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, } } /* Fall back to the subclass check. */ - result = _PyObject_CallMethodIdOneArg(self, &PyId___subclasscheck__, - subclass); + result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__), + subclass); goto end; } - result = _PyObject_CallMethodIdOneArg(self, &PyId___subclasscheck__, - subclass); + result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__), + subclass); if (result == NULL) { goto end; } @@ -639,8 +632,8 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self, break; case 0: Py_DECREF(result); - result = _PyObject_CallMethodIdOneArg(self, &PyId___subclasscheck__, - subtype); + result = PyObject_CallMethodOneArg(self, &_Py_ID(__subclasscheck__), + subtype); break; case 1: // Nothing to do. break; @@ -723,8 +716,8 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self, } /* 3. Check the subclass hook. */ - ok = _PyObject_CallMethodIdOneArg((PyObject *)self, &PyId___subclasshook__, - subclass); + ok = PyObject_CallMethodOneArg( + (PyObject *)self, &_Py_ID(__subclasshook__), subclass); if (ok == NULL) { goto end; } diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 2216dd0..72dbdb8 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1,6 +1,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "pycore_pyerrors.h" // _PyErr_ClearExcState() diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index 26c4b9b..f884d94 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -4,6 +4,7 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru). */ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" /*[clinic input] diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 4c4e1f7..e7bd8bc 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1348,9 +1348,8 @@ static PyObject * deque_reduce(dequeobject *deque, PyObject *Py_UNUSED(ignored)) { PyObject *dict, *it; - _Py_IDENTIFIER(__dict__); - if (_PyObject_LookupAttrId((PyObject *)deque, &PyId___dict__, &dict) < 0) { + if (_PyObject_LookupAttr((PyObject *)deque, &_Py_ID(__dict__), &dict) < 0) { return NULL; } if (dict == NULL) { @@ -2064,7 +2063,6 @@ defdict_reduce(defdictobject *dd, PyObject *Py_UNUSED(ignored)) PyObject *items; PyObject *iter; PyObject *result; - _Py_IDENTIFIER(items); if (dd->default_factory == NULL || dd->default_factory == Py_None) args = PyTuple_New(0); @@ -2072,7 +2070,7 @@ defdict_reduce(defdictobject *dd, PyObject *Py_UNUSED(ignored)) args = PyTuple_Pack(1, dd->default_factory); if (args == NULL) return NULL; - items = _PyObject_CallMethodIdNoArgs((PyObject *)dd, &PyId_items); + items = PyObject_CallMethodNoArgs((PyObject *)dd, &_Py_ID(items)); if (items == NULL) { Py_DECREF(args); return NULL; @@ -2310,8 +2308,6 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, PyObject *iterable) /*[clinic end generated code: output=7e0c1789636b3d8f input=e79fad04534a0b45]*/ { - _Py_IDENTIFIER(get); - _Py_IDENTIFIER(__setitem__); PyObject *it, *oldval; PyObject *newval = NULL; PyObject *key = NULL; @@ -2329,10 +2325,10 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, /* Only take the fast path when get() and __setitem__() * have not been overridden. */ - mapping_get = _PyType_LookupId(Py_TYPE(mapping), &PyId_get); - dict_get = _PyType_LookupId(&PyDict_Type, &PyId_get); - mapping_setitem = _PyType_LookupId(Py_TYPE(mapping), &PyId___setitem__); - dict_setitem = _PyType_LookupId(&PyDict_Type, &PyId___setitem__); + mapping_get = _PyType_Lookup(Py_TYPE(mapping), &_Py_ID(get)); + dict_get = _PyType_Lookup(&PyDict_Type, &_Py_ID(get)); + mapping_setitem = _PyType_Lookup(Py_TYPE(mapping), &_Py_ID(__setitem__)); + dict_setitem = _PyType_Lookup(&PyDict_Type, &_Py_ID(__setitem__)); if (mapping_get != NULL && mapping_get == dict_get && mapping_setitem != NULL && mapping_setitem == dict_setitem && @@ -2381,7 +2377,7 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping, } } else { - bound_get = _PyObject_GetAttrId(mapping, &PyId_get); + bound_get = PyObject_GetAttr(mapping, &_Py_ID(get)); if (bound_get == NULL) goto done; diff --git a/Modules/_csv.c b/Modules/_csv.c index 1c2f504..d2cb77f 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -10,6 +10,8 @@ module instead. #define MODULE_VERSION "1.0" +#define NEEDS_PY_IDENTIFIER + #include "Python.h" #include "structmember.h" // PyMemberDef #include <stdbool.h> diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 15eb752..481de85 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -101,6 +101,7 @@ bytes(cdata) #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index a8fee0d..f2d9a53 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -1,6 +1,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" // windows.h must be included before pycore internal headers diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index da29567..6dba0ff 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -54,6 +54,8 @@ */ +#define NEEDS_PY_IDENTIFIER + #include "Python.h" #include "structmember.h" // PyMemberDef diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 3470cf1..a819ce9 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -1,6 +1,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" // windows.h must be included before pycore internal headers diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 423b042..7e2699e 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -103,6 +103,7 @@ static const char PyCursesVersion[] = "2.2"; #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index fda8401..ae97190 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -10,6 +10,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "pycore_long.h" // _PyLong_GetOne() diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index c0371fb..65778fa 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -3,6 +3,7 @@ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include <sys/types.h> diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 9dadeef..f155dc7 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -12,6 +12,7 @@ */ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "structmember.h" // PyMemberDef diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 445500c..2d690c0 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -4,6 +4,7 @@ /* Doc strings: Mitch Chapman */ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "gdbm.h" diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c index 116688d..9ecc671 100644 --- a/Modules/_io/_iomodule.c +++ b/Modules/_io/_iomodule.c @@ -241,11 +241,6 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL, *path_or_fd = NULL; - _Py_IDENTIFIER(_blksize); - _Py_IDENTIFIER(isatty); - _Py_IDENTIFIER(mode); - _Py_IDENTIFIER(close); - is_number = PyNumber_Check(file); if (is_number) { @@ -381,7 +376,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, /* buffering */ if (buffering < 0) { - PyObject *res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty); + PyObject *res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty)); if (res == NULL) goto error; isatty = PyLong_AsLong(res); @@ -399,7 +394,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, if (buffering < 0) { PyObject *blksize_obj; - blksize_obj = _PyObject_GetAttrId(raw, &PyId__blksize); + blksize_obj = PyObject_GetAttr(raw, &_Py_ID(_blksize)); if (blksize_obj == NULL) goto error; buffering = PyLong_AsLong(blksize_obj); @@ -466,7 +461,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, result = wrapper; Py_DECREF(buffer); - if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0) + if (PyObject_SetAttr(wrapper, &_Py_ID(mode), modeobj) < 0) goto error; Py_DECREF(modeobj); return result; @@ -475,7 +470,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode, if (result != NULL) { PyObject *exc, *val, *tb, *close_result; PyErr_Fetch(&exc, &val, &tb); - close_result = _PyObject_CallMethodIdNoArgs(result, &PyId_close); + close_result = PyObject_CallMethodNoArgs(result, &_Py_ID(close)); _PyErr_ChainExceptions(exc, val, tb); Py_XDECREF(close_result); Py_DECREF(result); diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 2899505..bb93ae3 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -25,21 +25,6 @@ class _io.BufferedRandom "buffered *" "&PyBufferedRandom_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=59460b9c5639984d]*/ -_Py_IDENTIFIER(close); -_Py_IDENTIFIER(_dealloc_warn); -_Py_IDENTIFIER(flush); -_Py_IDENTIFIER(isatty); -_Py_IDENTIFIER(mode); -_Py_IDENTIFIER(name); -_Py_IDENTIFIER(peek); -_Py_IDENTIFIER(read); -_Py_IDENTIFIER(read1); -_Py_IDENTIFIER(readable); -_Py_IDENTIFIER(readinto); -_Py_IDENTIFIER(readinto1); -_Py_IDENTIFIER(writable); -_Py_IDENTIFIER(write); - /* * BufferedIOBase class, inherits from IOBase. */ @@ -65,9 +50,10 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint Py_ssize_t len; PyObject *data; - data = _PyObject_CallMethodId(self, - readinto1 ? &PyId_read1 : &PyId_read, - "n", buffer->len); + PyObject *attr = readinto1 + ? &_Py_ID(read1) + : &_Py_ID(read); + data = _PyObject_CallMethod(self, attr, "n", buffer->len); if (data == NULL) return NULL; @@ -436,8 +422,7 @@ buffered_dealloc_warn(buffered *self, PyObject *source) { if (self->ok && self->raw) { PyObject *r; - r = _PyObject_CallMethodIdOneArg(self->raw, &PyId__dealloc_warn, - source); + r = PyObject_CallMethodOneArg(self->raw, &_Py_ID(_dealloc_warn), source); if (r) Py_DECREF(r); else @@ -583,14 +568,14 @@ static PyObject * buffered_name_get(buffered *self, void *context) { CHECK_INITIALIZED(self) - return _PyObject_GetAttrId(self->raw, &PyId_name); + return PyObject_GetAttr(self->raw, &_Py_ID(name)); } static PyObject * buffered_mode_get(buffered *self, void *context) { CHECK_INITIALIZED(self) - return _PyObject_GetAttrId(self->raw, &PyId_mode); + return PyObject_GetAttr(self->raw, &_Py_ID(mode)); } /* Lower-level APIs */ @@ -1381,7 +1366,7 @@ buffered_repr(buffered *self) { PyObject *nameobj, *res; - if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { + if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { if (!PyErr_ExceptionMatches(PyExc_ValueError)) { return NULL; } @@ -2153,7 +2138,7 @@ bufferedrwpair_dealloc(rwpair *self) } static PyObject * -_forward_call(buffered *self, _Py_Identifier *name, PyObject *args) +_forward_call(buffered *self, PyObject *name, PyObject *args) { PyObject *func, *ret; if (self == NULL) { @@ -2162,9 +2147,9 @@ _forward_call(buffered *self, _Py_Identifier *name, PyObject *args) return NULL; } - func = _PyObject_GetAttrId((PyObject *)self, name); + func = PyObject_GetAttr((PyObject *)self, name); if (func == NULL) { - PyErr_SetString(PyExc_AttributeError, name->string); + PyErr_SetObject(PyExc_AttributeError, name); return NULL; } @@ -2176,67 +2161,67 @@ _forward_call(buffered *self, _Py_Identifier *name, PyObject *args) static PyObject * bufferedrwpair_read(rwpair *self, PyObject *args) { - return _forward_call(self->reader, &PyId_read, args); + return _forward_call(self->reader, &_Py_ID(read), args); } static PyObject * bufferedrwpair_peek(rwpair *self, PyObject *args) { - return _forward_call(self->reader, &PyId_peek, args); + return _forward_call(self->reader, &_Py_ID(peek), args); } static PyObject * bufferedrwpair_read1(rwpair *self, PyObject *args) { - return _forward_call(self->reader, &PyId_read1, args); + return _forward_call(self->reader, &_Py_ID(read1), args); } static PyObject * bufferedrwpair_readinto(rwpair *self, PyObject *args) { - return _forward_call(self->reader, &PyId_readinto, args); + return _forward_call(self->reader, &_Py_ID(readinto), args); } static PyObject * bufferedrwpair_readinto1(rwpair *self, PyObject *args) { - return _forward_call(self->reader, &PyId_readinto1, args); + return _forward_call(self->reader, &_Py_ID(readinto1), args); } static PyObject * bufferedrwpair_write(rwpair *self, PyObject *args) { - return _forward_call(self->writer, &PyId_write, args); + return _forward_call(self->writer, &_Py_ID(write), args); } static PyObject * bufferedrwpair_flush(rwpair *self, PyObject *Py_UNUSED(ignored)) { - return _forward_call(self->writer, &PyId_flush, NULL); + return _forward_call(self->writer, &_Py_ID(flush), NULL); } static PyObject * bufferedrwpair_readable(rwpair *self, PyObject *Py_UNUSED(ignored)) { - return _forward_call(self->reader, &PyId_readable, NULL); + return _forward_call(self->reader, &_Py_ID(readable), NULL); } static PyObject * bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored)) { - return _forward_call(self->writer, &PyId_writable, NULL); + return _forward_call(self->writer, &_Py_ID(writable), NULL); } static PyObject * bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored)) { PyObject *exc = NULL, *val, *tb; - PyObject *ret = _forward_call(self->writer, &PyId_close, NULL); + PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL); if (ret == NULL) PyErr_Fetch(&exc, &val, &tb); else Py_DECREF(ret); - ret = _forward_call(self->reader, &PyId_close, NULL); + ret = _forward_call(self->reader, &_Py_ID(close), NULL); if (exc != NULL) { _PyErr_ChainExceptions(exc, val, tb); Py_CLEAR(ret); @@ -2247,7 +2232,7 @@ bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored)) static PyObject * bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored)) { - PyObject *ret = _forward_call(self->writer, &PyId_isatty, NULL); + PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL); if (ret != Py_False) { /* either True or exception */ @@ -2255,7 +2240,7 @@ bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored)) } Py_DECREF(ret); - return _forward_call(self->reader, &PyId_isatty, NULL); + return _forward_call(self->reader, &_Py_ID(isatty), NULL); } static PyObject * diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index dd215e8..8b1cff5 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -72,8 +72,6 @@ typedef struct { PyTypeObject PyFileIO_Type; -_Py_IDENTIFIER(name); - #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) /* Forward declarations */ @@ -146,9 +144,8 @@ _io_FileIO_close_impl(fileio *self) PyObject *res; PyObject *exc, *val, *tb; int rc; - _Py_IDENTIFIER(close); - res = _PyObject_CallMethodIdOneArg((PyObject*)&PyRawIOBase_Type, - &PyId_close, (PyObject *)self); + res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type, + &_Py_ID(close), (PyObject *)self); if (!self->closefd) { self->fd = -1; return res; @@ -476,7 +473,7 @@ _Py_COMP_DIAG_POP _setmode(self->fd, O_BINARY); #endif - if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0) + if (PyObject_SetAttr((PyObject *)self, &_Py_ID(name), nameobj) < 0) goto error; if (self->appending) { @@ -1085,7 +1082,7 @@ fileio_repr(fileio *self) if (self->fd < 0) return PyUnicode_FromFormat("<_io.FileIO [closed]>"); - if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { + if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { return NULL; } if (nameobj == NULL) { diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c index 5b687b7..74663e0 100644 --- a/Modules/_io/iobase.c +++ b/Modules/_io/iobase.c @@ -69,9 +69,6 @@ PyDoc_STRVAR(iobase_doc, of the IOBase object rather than the virtual `closed` attribute as returned by whatever subclass. */ -_Py_IDENTIFIER(__IOBase_closed); -_Py_IDENTIFIER(read); - /* Internal methods */ static PyObject * @@ -114,9 +111,7 @@ static PyObject * _io__IOBase_tell_impl(PyObject *self) /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/ { - _Py_IDENTIFIER(seek); - - return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); + return _PyObject_CallMethod(self, &_Py_ID(seek), "ii", 0, 1); } PyDoc_STRVAR(iobase_truncate_doc, @@ -138,7 +133,7 @@ iobase_is_closed(PyObject *self) int ret; /* This gets the derived attribute, which is *not* __IOBase_closed in most cases! */ - ret = _PyObject_LookupAttrId(self, &PyId___IOBase_closed, &res); + ret = _PyObject_LookupAttr(self, &_Py_ID(__IOBase_closed), &res); Py_XDECREF(res); return ret; } @@ -239,7 +234,7 @@ _io__IOBase_close_impl(PyObject *self) res = PyObject_CallMethodNoArgs(self, _PyIO_str_flush); PyErr_Fetch(&exc, &val, &tb); - rc = _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True); + rc = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True); _PyErr_ChainExceptions(exc, val, tb); if (rc < 0) { Py_CLEAR(res); @@ -260,7 +255,6 @@ iobase_finalize(PyObject *self) PyObject *res; PyObject *error_type, *error_value, *error_traceback; int closed; - _Py_IDENTIFIER(_finalizing); /* Save the current exception, if any. */ PyErr_Fetch(&error_type, &error_value, &error_traceback); @@ -280,7 +274,7 @@ iobase_finalize(PyObject *self) if (closed == 0) { /* Signal close() that it was called as part of the object finalization process. */ - if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True)) + if (PyObject_SetAttr(self, &_Py_ID(_finalizing), Py_True)) PyErr_Clear(); res = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_close); /* Silencing I/O errors is bad, but printing spurious tracebacks is @@ -597,7 +591,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) Py_DECREF(readahead); } - b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); + b = _PyObject_CallMethod(self, &_Py_ID(read), "n", nreadahead); if (b == NULL) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ @@ -697,10 +691,8 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) /* XXX special-casing this made sense in the Python version in order to remove the bytecode interpretation overhead, but it could probably be removed here. */ - _Py_IDENTIFIER(extend); - PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend, - self, NULL); - + PyObject *ret = PyObject_CallMethodObjArgs(result, &_Py_ID(extend), + self, NULL); if (ret == NULL) { goto error; } @@ -919,9 +911,7 @@ _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) PyObject *b, *res; if (n < 0) { - _Py_IDENTIFIER(readall); - - return _PyObject_CallMethodIdNoArgs(self, &PyId_readall); + return PyObject_CallMethodNoArgs(self, &_Py_ID(readall)); } /* TODO: allocate a bytes object directly instead and manually construct @@ -967,8 +957,8 @@ _io__RawIOBase_readall_impl(PyObject *self) return NULL; while (1) { - PyObject *data = _PyObject_CallMethodId(self, &PyId_read, - "i", DEFAULT_BUFFER_SIZE); + PyObject *data = _PyObject_CallMethod(self, &_Py_ID(read), + "i", DEFAULT_BUFFER_SIZE); if (!data) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index eb05ae1..d5ceec7 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -23,26 +23,6 @@ class _io.TextIOWrapper "textio *" "&TextIOWrapper_TYpe" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2097a4fc85670c26]*/ -_Py_IDENTIFIER(close); -_Py_IDENTIFIER(_dealloc_warn); -_Py_IDENTIFIER(decode); -_Py_IDENTIFIER(fileno); -_Py_IDENTIFIER(flush); -_Py_IDENTIFIER(isatty); -_Py_IDENTIFIER(mode); -_Py_IDENTIFIER(name); -_Py_IDENTIFIER(raw); -_Py_IDENTIFIER(read); -_Py_IDENTIFIER(readable); -_Py_IDENTIFIER(replace); -_Py_IDENTIFIER(reset); -_Py_IDENTIFIER(seek); -_Py_IDENTIFIER(seekable); -_Py_IDENTIFIER(setstate); -_Py_IDENTIFIER(strict); -_Py_IDENTIFIER(tell); -_Py_IDENTIFIER(writable); - /* TextIOBase */ PyDoc_STRVAR(textiobase_doc, @@ -255,9 +235,7 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, Py_INCREF(decoder); if (errors == NULL) { - self->errors = _PyUnicode_FromId(&PyId_strict); - if (self->errors == NULL) - return -1; + self->errors = &_Py_ID(strict); } else { self->errors = errors; @@ -586,11 +564,13 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self, self->pendingcr = (int) (flag & 1); flag >>= 1; - if (self->decoder != Py_None) - return _PyObject_CallMethodId(self->decoder, - &PyId_setstate, "((OK))", buffer, flag); - else + if (self->decoder != Py_None) { + return _PyObject_CallMethod(self->decoder, &_Py_ID(setstate), + "((OK))", buffer, flag); + } + else { Py_RETURN_NONE; + } } /*[clinic input] @@ -865,7 +845,7 @@ _textiowrapper_set_decoder(textio *self, PyObject *codec_info, PyObject *res; int r; - res = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_readable); + res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable)); if (res == NULL) return -1; @@ -920,7 +900,7 @@ _textiowrapper_set_encoder(textio *self, PyObject *codec_info, PyObject *res; int r; - res = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_writable); + res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable)); if (res == NULL) return -1; @@ -939,7 +919,7 @@ _textiowrapper_set_encoder(textio *self, PyObject *codec_info, return -1; /* Get the normalized named of the codec */ - if (_PyObject_LookupAttrId(codec_info, &PyId_name, &res) < 0) { + if (_PyObject_LookupAttr(codec_info, &_Py_ID(name), &res) < 0) { return -1; } if (res != NULL && PyUnicode_Check(res)) { @@ -1099,10 +1079,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, } if (errors == Py_None) { - errors = _PyUnicode_FromId(&PyId_strict); /* borrowed */ - if (errors == NULL) { - return -1; - } + errors = &_Py_ID(strict); } else if (!PyUnicode_Check(errors)) { // Check 'errors' argument here because Argument Clinic doesn't support @@ -1142,7 +1119,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, state = IO_STATE(); if (state == NULL) goto error; - fileno = _PyObject_CallMethodIdNoArgs(buffer, &PyId_fileno); + fileno = PyObject_CallMethodNoArgs(buffer, &_Py_ID(fileno)); /* Ignore only AttributeError and UnsupportedOperation */ if (fileno == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError) || @@ -1228,7 +1205,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, Py_IS_TYPE(buffer, &PyBufferedWriter_Type) || Py_IS_TYPE(buffer, &PyBufferedRandom_Type)) { - if (_PyObject_LookupAttrId(buffer, &PyId_raw, &raw) < 0) + if (_PyObject_LookupAttr(buffer, &_Py_ID(raw), &raw) < 0) goto error; /* Cache the raw FileIO object to speed up 'closed' checks */ if (raw != NULL) { @@ -1239,7 +1216,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, } } - res = _PyObject_CallMethodIdNoArgs(buffer, &PyId_seekable); + res = PyObject_CallMethodNoArgs(buffer, &_Py_ID(seekable)); if (res == NULL) goto error; r = PyObject_IsTrue(res); @@ -1302,10 +1279,7 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding, } } else if (errors == Py_None) { - errors = _PyUnicode_FromId(&PyId_strict); - if (errors == NULL) { - return -1; - } + errors = &_Py_ID(strict); } const char *c_errors = PyUnicode_AsUTF8(errors); @@ -1640,8 +1614,8 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) haslf = 1; if (haslf && self->writetranslate && self->writenl != NULL) { - PyObject *newtext = _PyObject_CallMethodId( - text, &PyId_replace, "ss", "\n", self->writenl); + PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), + "ss", "\n", self->writenl); Py_DECREF(text); if (newtext == NULL) return NULL; @@ -1740,7 +1714,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text) Py_CLEAR(self->snapshot); if (self->decoder) { - ret = _PyObject_CallMethodIdNoArgs(self->decoder, &PyId_reset); + ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); if (ret == NULL) return NULL; Py_DECREF(ret); @@ -1944,7 +1918,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n) if (n < 0) { /* Read everything */ - PyObject *bytes = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_read); + PyObject *bytes = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(read)); PyObject *decoded; if (bytes == NULL) goto fail; @@ -2404,13 +2378,16 @@ _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie) at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of utf-16, that we are expecting a BOM). */ - if (cookie->start_pos == 0 && cookie->dec_flags == 0) + if (cookie->start_pos == 0 && cookie->dec_flags == 0) { res = PyObject_CallMethodNoArgs(self->decoder, _PyIO_str_reset); - else - res = _PyObject_CallMethodId(self->decoder, &PyId_setstate, - "((yi))", "", cookie->dec_flags); - if (res == NULL) + } + else { + res = _PyObject_CallMethod(self->decoder, &_Py_ID(setstate), + "((yi))", "", cookie->dec_flags); + } + if (res == NULL) { return -1; + } Py_DECREF(res); return 0; } @@ -2487,7 +2464,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) * sync the underlying buffer with the current position. */ Py_DECREF(cookieObj); - cookieObj = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_tell); + cookieObj = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(tell)); if (cookieObj == NULL) goto fail; break; @@ -2503,7 +2480,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) goto fail; } - res = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); + res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush)); if (res == NULL) goto fail; Py_DECREF(res); @@ -2511,13 +2488,13 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) textiowrapper_set_decoded_chars(self, NULL); Py_CLEAR(self->snapshot); if (self->decoder) { - res = _PyObject_CallMethodIdNoArgs(self->decoder, &PyId_reset); + res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); if (res == NULL) goto fail; Py_DECREF(res); } - res = _PyObject_CallMethodId(self->buffer, &PyId_seek, "ii", 0, 2); + res = _PyObject_CallMethod(self->buffer, &_Py_ID(seek), "ii", 0, 2); Py_CLEAR(cookieObj); if (res == NULL) goto fail; @@ -2583,8 +2560,8 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) if (cookie.chars_to_skip) { /* Just like _read_chunk, feed the decoder and save a snapshot. */ - PyObject *input_chunk = _PyObject_CallMethodId( - self->buffer, &PyId_read, "i", cookie.bytes_to_feed); + PyObject *input_chunk = _PyObject_CallMethod(self->buffer, &_Py_ID(read), + "i", cookie.bytes_to_feed); PyObject *decoded; if (input_chunk == NULL) @@ -2605,7 +2582,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) } Py_XSETREF(self->snapshot, snapshot); - decoded = _PyObject_CallMethodIdObjArgs(self->decoder, &PyId_decode, + decoded = PyObject_CallMethodObjArgs(self->decoder, &_Py_ID(decode), input_chunk, cookie.need_eof ? Py_True : Py_False, NULL); if (check_decoded(decoded) < 0) @@ -2673,12 +2650,12 @@ _io_TextIOWrapper_tell_impl(textio *self) if (_textiowrapper_writeflush(self) < 0) return NULL; - res = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); + res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush)); if (res == NULL) goto fail; Py_DECREF(res); - posobj = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_tell); + posobj = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(tell)); if (posobj == NULL) goto fail; @@ -2750,8 +2727,8 @@ _io_TextIOWrapper_tell_impl(textio *self) } while (0) #define DECODER_DECODE(start, len, res) do { \ - PyObject *_decoded = _PyObject_CallMethodId( \ - self->decoder, &PyId_decode, "y#", start, len); \ + PyObject *_decoded = _PyObject_CallMethod( \ + self->decoder, &_Py_ID(decode), "y#", start, len); \ if (check_decoded(_decoded) < 0) \ goto fail; \ res = PyUnicode_GET_LENGTH(_decoded); \ @@ -2832,8 +2809,8 @@ _io_TextIOWrapper_tell_impl(textio *self) } if (input == input_end) { /* We didn't get enough decoded data; signal EOF to get more. */ - PyObject *decoded = _PyObject_CallMethodId( - self->decoder, &PyId_decode, "yO", "", /* final = */ Py_True); + PyObject *decoded = _PyObject_CallMethod( + self->decoder, &_Py_ID(decode), "yO", "", /* final = */ Py_True); if (check_decoded(decoded) < 0) goto fail; chars_decoded += PyUnicode_GET_LENGTH(decoded); @@ -2848,7 +2825,8 @@ _io_TextIOWrapper_tell_impl(textio *self) } finally: - res = _PyObject_CallMethodIdOneArg(self->decoder, &PyId_setstate, saved_state); + res = PyObject_CallMethodOneArg( + self->decoder, &_Py_ID(setstate), saved_state); Py_DECREF(saved_state); if (res == NULL) return NULL; @@ -2862,7 +2840,8 @@ fail: if (saved_state) { PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); - res = _PyObject_CallMethodIdOneArg(self->decoder, &PyId_setstate, saved_state); + res = PyObject_CallMethodOneArg( + self->decoder, &_Py_ID(setstate), saved_state); _PyErr_ChainExceptions(type, value, traceback); Py_DECREF(saved_state); Py_XDECREF(res); @@ -2913,7 +2892,7 @@ textiowrapper_repr(textio *self) } goto error; } - if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { + if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { if (!PyErr_ExceptionMatches(PyExc_ValueError)) { goto error; } @@ -2929,7 +2908,7 @@ textiowrapper_repr(textio *self) if (res == NULL) goto error; } - if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) { + if (_PyObject_LookupAttr((PyObject *) self, &_Py_ID(mode), &modeobj) < 0) { goto error; } if (modeobj != NULL) { @@ -2969,7 +2948,7 @@ _io_TextIOWrapper_fileno_impl(textio *self) /*[clinic end generated code: output=21490a4c3da13e6c input=c488ca83d0069f9b]*/ { CHECK_ATTACHED(self); - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_fileno); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(fileno)); } /*[clinic input] @@ -2981,7 +2960,7 @@ _io_TextIOWrapper_seekable_impl(textio *self) /*[clinic end generated code: output=ab223dbbcffc0f00 input=8b005ca06e1fca13]*/ { CHECK_ATTACHED(self); - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_seekable); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(seekable)); } /*[clinic input] @@ -2993,7 +2972,7 @@ _io_TextIOWrapper_readable_impl(textio *self) /*[clinic end generated code: output=72ff7ba289a8a91b input=0704ea7e01b0d3eb]*/ { CHECK_ATTACHED(self); - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_readable); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable)); } /*[clinic input] @@ -3005,7 +2984,7 @@ _io_TextIOWrapper_writable_impl(textio *self) /*[clinic end generated code: output=a728c71790d03200 input=c41740bc9d8636e8]*/ { CHECK_ATTACHED(self); - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_writable); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable)); } /*[clinic input] @@ -3017,7 +2996,7 @@ _io_TextIOWrapper_isatty_impl(textio *self) /*[clinic end generated code: output=12be1a35bace882e input=fb68d9f2c99bbfff]*/ { CHECK_ATTACHED(self); - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_isatty); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(isatty)); } /*[clinic input] @@ -3033,7 +3012,7 @@ _io_TextIOWrapper_flush_impl(textio *self) self->telling = self->seekable; if (_textiowrapper_writeflush(self) < 0) return NULL; - return _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_flush); + return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush)); } /*[clinic input] @@ -3062,21 +3041,20 @@ _io_TextIOWrapper_close_impl(textio *self) else { PyObject *exc = NULL, *val, *tb; if (self->finalizing) { - res = _PyObject_CallMethodIdOneArg(self->buffer, - &PyId__dealloc_warn, - (PyObject *)self); + res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn), + (PyObject *)self); if (res) Py_DECREF(res); else PyErr_Clear(); } - res = _PyObject_CallMethodIdNoArgs((PyObject *)self, &PyId_flush); + res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(flush)); if (res == NULL) PyErr_Fetch(&exc, &val, &tb); else Py_DECREF(res); - res = _PyObject_CallMethodIdNoArgs(self->buffer, &PyId_close); + res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close)); if (exc != NULL) { _PyErr_ChainExceptions(exc, val, tb); Py_CLEAR(res); @@ -3127,7 +3105,7 @@ static PyObject * textiowrapper_name_get(textio *self, void *context) { CHECK_ATTACHED(self); - return _PyObject_GetAttrId(self->buffer, &PyId_name); + return PyObject_GetAttr(self->buffer, &_Py_ID(name)); } static PyObject * diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c index 4002d28..5c1a6dd 100644 --- a/Modules/_io/winconsoleio.c +++ b/Modules/_io/winconsoleio.c @@ -156,8 +156,6 @@ typedef struct { PyTypeObject PyWindowsConsoleIO_Type; -_Py_IDENTIFIER(name); - int _PyWindowsConsoleIO_closed(PyObject *self) { @@ -196,9 +194,8 @@ _io__WindowsConsoleIO_close_impl(winconsoleio *self) PyObject *res; PyObject *exc, *val, *tb; int rc; - _Py_IDENTIFIER(close); - res = _PyObject_CallMethodIdOneArg((PyObject*)&PyRawIOBase_Type, - &PyId_close, (PyObject*)self); + res = PyObject_CallMethodOneArg((PyObject*)&PyRawIOBase_Type, + &_Py_ID(close), (PyObject*)self); if (!self->closefd) { self->fd = -1; return res; @@ -394,7 +391,7 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj, self->blksize = DEFAULT_BUFFER_SIZE; memset(self->buf, 0, 4); - if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0) + if (PyObject_SetAttr((PyObject *)self, &_Py_ID(name), nameobj) < 0) goto error; goto done; diff --git a/Modules/_json.c b/Modules/_json.c index 1c9c506..f7a3e38 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -7,6 +7,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "structmember.h" // PyMemberDef diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index e50f55b..e3fc90e 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -6,6 +6,7 @@ */ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "structmember.h" // PyMemberDef diff --git a/Modules/_operator.c b/Modules/_operator.c index b3a8bef..10adabc 100644 --- a/Modules/_operator.c +++ b/Modules/_operator.c @@ -1,5 +1,6 @@ #include "Python.h" #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_runtime.h" // _Py_ID() #include "clinic/_operator.c.h" typedef struct { @@ -1693,11 +1694,10 @@ methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored)) PyObject *constructor; PyObject *newargs[2]; - _Py_IDENTIFIER(partial); functools = PyImport_ImportModule("functools"); if (!functools) return NULL; - partial = _PyObject_GetAttrId(functools, &PyId_partial); + partial = PyObject_GetAttr(functools, &_Py_ID(partial)); Py_DECREF(functools); if (!partial) return NULL; diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 0d9e57a..19e8a71 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -11,6 +11,8 @@ #include "Python.h" #include "pycore_floatobject.h" // _PyFloat_Pack8() #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_runtime.h" // _Py_ID() +#include "pycore_pystate.h" // _PyThreadState_GET() #include "structmember.h" // PyMemberDef #include <stdlib.h> // strtol() @@ -35,6 +37,13 @@ enum { DEFAULT_PROTOCOL = 4 }; +#ifdef MS_WINDOWS +// These are already typedefs from windows.h, pulled in via pycore_runtime.h. +#define FLOAT FLOAT_ +#define INT INT_ +#define LONG LONG_ +#endif + /* Pickle opcodes. These must be kept updated with pickle.py. Extensive docs are in pickletools.py. */ enum opcode { @@ -225,9 +234,8 @@ _Pickle_InitState(PickleState *st) PyObject *compat_pickle = NULL; PyObject *codecs = NULL; PyObject *functools = NULL; - _Py_IDENTIFIER(getattr); - st->getattr = _PyEval_GetBuiltinId(&PyId_getattr); + st->getattr = _PyEval_GetBuiltin(&_Py_ID(getattr)); if (st->getattr == NULL) goto error; @@ -373,7 +381,7 @@ _Pickle_FastCall(PyObject *func, PyObject *obj) /* Retrieve and deconstruct a method for avoiding a reference cycle (pickler -> bound method of pickler -> pickler) */ static int -init_method_ref(PyObject *self, _Py_Identifier *name, +init_method_ref(PyObject *self, PyObject *name, PyObject **method_func, PyObject **method_self) { PyObject *func, *func2; @@ -381,7 +389,7 @@ init_method_ref(PyObject *self, _Py_Identifier *name, /* *method_func and *method_self should be consistent. All refcount decrements should be occurred after setting *method_self and *method_func. */ - ret = _PyObject_LookupAttrId(self, name, &func); + ret = _PyObject_LookupAttr(self, name, &func); if (func == NULL) { *method_self = NULL; Py_CLEAR(*method_func); @@ -1175,9 +1183,8 @@ _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) static int _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) { - _Py_IDENTIFIER(write); assert(file != NULL); - if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { + if (_PyObject_LookupAttr(file, &_Py_ID(write), &self->write) < 0) { return -1; } if (self->write == NULL) { @@ -1636,20 +1643,15 @@ _Unpickler_New(void) static int _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) { - _Py_IDENTIFIER(peek); - _Py_IDENTIFIER(read); - _Py_IDENTIFIER(readinto); - _Py_IDENTIFIER(readline); - /* Optional file methods */ - if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { + if (_PyObject_LookupAttr(file, &_Py_ID(peek), &self->peek) < 0) { return -1; } - if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) { + if (_PyObject_LookupAttr(file, &_Py_ID(readinto), &self->readinto) < 0) { return -1; } - (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); - (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); + (void)_PyObject_LookupAttr(file, &_Py_ID(read), &self->read); + (void)_PyObject_LookupAttr(file, &_Py_ID(readline), &self->readline); if (!self->readline || !self->read) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, @@ -1809,11 +1811,10 @@ memo_put(PicklerObject *self, PyObject *obj) static PyObject * get_dotted_path(PyObject *obj, PyObject *name) { - _Py_static_string(PyId_dot, "."); PyObject *dotted_path; Py_ssize_t i, n; - dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); + dotted_path = PyUnicode_Split(name, &_Py_STR(dot), -1); if (dotted_path == NULL) return NULL; n = PyList_GET_SIZE(dotted_path); @@ -1914,11 +1915,8 @@ whichmodule(PyObject *global, PyObject *dotted_path) PyObject *module = NULL; Py_ssize_t i; PyObject *modules; - _Py_IDENTIFIER(__module__); - _Py_IDENTIFIER(modules); - _Py_IDENTIFIER(__main__); - if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { + if (_PyObject_LookupAttr(global, &_Py_ID(__module__), &module_name) < 0) { return NULL; } if (module_name) { @@ -1932,7 +1930,8 @@ whichmodule(PyObject *global, PyObject *dotted_path) assert(module_name == NULL); /* Fallback on walking sys.modules */ - modules = _PySys_GetObjectId(&PyId_modules); + PyThreadState *tstate = _PyThreadState_GET(); + modules = _PySys_GetAttr(tstate, &_Py_ID(modules)); if (modules == NULL) { PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); return NULL; @@ -1977,8 +1976,8 @@ whichmodule(PyObject *global, PyObject *dotted_path) } /* If no module is found, use __main__. */ - module_name = _PyUnicode_FromId(&PyId___main__); - Py_XINCREF(module_name); + module_name = &_Py_ID(__main__); + Py_INCREF(module_name); return module_name; } @@ -2427,13 +2426,12 @@ save_bytes(PicklerObject *self, PyObject *obj) PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj), "strict"); - _Py_IDENTIFIER(latin1); if (unicode_str == NULL) return -1; reduce_value = Py_BuildValue("(O(OO))", st->codecs_encode, unicode_str, - _PyUnicode_FromId(&PyId_latin1)); + &_Py_ID(latin1)); Py_DECREF(unicode_str); } @@ -3335,9 +3333,7 @@ save_dict(PicklerObject *self, PyObject *obj) status = batch_dict_exact(self, obj); Py_LeaveRecursiveCall(); } else { - _Py_IDENTIFIER(items); - - items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items); + items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items)); if (items == NULL) goto error; iter = PyObject_GetIter(items); @@ -3589,8 +3585,6 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name) PyObject *cls; PickleState *st = _Pickle_GetGlobalState(); int status = 0; - _Py_IDENTIFIER(__name__); - _Py_IDENTIFIER(__qualname__); const char global_op = GLOBAL; @@ -3599,10 +3593,10 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name) global_name = name; } else { - if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) + if (_PyObject_LookupAttr(obj, &_Py_ID(__qualname__), &global_name) < 0) goto error; if (global_name == NULL) { - global_name = _PyObject_GetAttrId(obj, &PyId___name__); + global_name = PyObject_GetAttr(obj, &_Py_ID(__name__)); if (global_name == NULL) goto error; } @@ -3923,9 +3917,8 @@ static PyObject * get_class(PyObject *obj) { PyObject *cls; - _Py_IDENTIFIER(__class__); - if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &cls) == 0) { cls = (PyObject *) Py_TYPE(obj); Py_INCREF(cls); } @@ -4008,18 +4001,14 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) if (self->proto >= 2) { PyObject *name; - _Py_IDENTIFIER(__name__); - if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { + if (_PyObject_LookupAttr(callable, &_Py_ID(__name__), &name) < 0) { return -1; } if (name != NULL && PyUnicode_Check(name)) { - _Py_IDENTIFIER(__newobj_ex__); - use_newobj_ex = _PyUnicode_EqualToASCIIId( - name, &PyId___newobj_ex__); + use_newobj_ex = _PyUnicode_Equal(name, &_Py_ID(__newobj_ex__)); if (!use_newobj_ex) { - _Py_IDENTIFIER(__newobj__); - use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); + use_newobj = _PyUnicode_Equal(name, &_Py_ID(__newobj__)); } } Py_XDECREF(name); @@ -4071,13 +4060,12 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) PyObject *newargs; PyObject *cls_new; Py_ssize_t i; - _Py_IDENTIFIER(__new__); newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); if (newargs == NULL) return -1; - cls_new = _PyObject_GetAttrId(cls, &PyId___new__); + cls_new = PyObject_GetAttr(cls, &_Py_ID(__new__)); if (cls_new == NULL) { Py_DECREF(newargs); return -1; @@ -4412,9 +4400,6 @@ save(PicklerObject *self, PyObject *obj, int pers_save) goto done; } else { - _Py_IDENTIFIER(__reduce__); - _Py_IDENTIFIER(__reduce_ex__); - /* XXX: If the __reduce__ method is defined, __reduce_ex__ is automatically defined as __reduce__. While this is convenient, this make it impossible to know which method was actually called. Of @@ -4424,7 +4409,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save) don't actually have to check for a __reduce__ method. */ /* Check for a __reduce_ex__ method. */ - if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__reduce_ex__), &reduce_func) < 0) { goto error; } if (reduce_func != NULL) { @@ -4436,7 +4421,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save) } else { /* Check for a __reduce__ method. */ - if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__reduce__), &reduce_func) < 0) { goto error; } if (reduce_func != NULL) { @@ -4489,10 +4474,9 @@ dump(PicklerObject *self, PyObject *obj) const char stop_op = STOP; int status = -1; PyObject *tmp; - _Py_IDENTIFIER(reducer_override); - if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override, - &tmp) < 0) { + if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(reducer_override), + &tmp) < 0) { goto error; } /* Cache the reducer_override method, if it exists. */ @@ -4727,9 +4711,6 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *buffer_callback) /*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/ { - _Py_IDENTIFIER(persistent_id); - _Py_IDENTIFIER(dispatch_table); - /* In case of multiple __init__() calls, clear previous content. */ if (self->write != NULL) (void)Pickler_clear(self); @@ -4762,14 +4743,14 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, self->fast_nesting = 0; self->fast_memo = NULL; - if (init_method_ref((PyObject *)self, &PyId_persistent_id, + if (init_method_ref((PyObject *)self, &_Py_ID(persistent_id), &self->pers_func, &self->pers_func_self) < 0) { return -1; } - if (_PyObject_LookupAttrId((PyObject *)self, - &PyId_dispatch_table, &self->dispatch_table) < 0) { + if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(dispatch_table), + &self->dispatch_table) < 0) { return -1; } @@ -5123,10 +5104,8 @@ static PyTypeObject Pickler_Type = { static PyObject * find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) { - _Py_IDENTIFIER(find_class); - - return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, - module_name, global_name, NULL); + return PyObject_CallMethodObjArgs((PyObject *)self, &_Py_ID(find_class), + module_name, global_name, NULL); } static Py_ssize_t @@ -5813,14 +5792,12 @@ instantiate(PyObject *cls, PyObject *args) into a newly created tuple. */ assert(PyTuple_Check(args)); if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { - _Py_IDENTIFIER(__getinitargs__); - _Py_IDENTIFIER(__new__); PyObject *func; - if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { + if (_PyObject_LookupAttr(cls, &_Py_ID(__getinitargs__), &func) < 0) { return NULL; } if (func == NULL) { - return _PyObject_CallMethodIdOneArg(cls, &PyId___new__, cls); + return PyObject_CallMethodOneArg(cls, &_Py_ID(__new__), cls); } Py_DECREF(func); } @@ -6465,9 +6442,8 @@ do_append(UnpicklerObject *self, Py_ssize_t x) } else { PyObject *extend_func; - _Py_IDENTIFIER(extend); - if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) { + if (_PyObject_LookupAttr(list, &_Py_ID(extend), &extend_func) < 0) { return -1; } if (extend_func != NULL) { @@ -6484,12 +6460,11 @@ do_append(UnpicklerObject *self, Py_ssize_t x) } else { PyObject *append_func; - _Py_IDENTIFIER(append); /* Even if the PEP 307 requires extend() and append() methods, fall back on append() if the object has no extend() method for backward compatibility. */ - append_func = _PyObject_GetAttrId(list, &PyId_append); + append_func = PyObject_GetAttr(list, &_Py_ID(append)); if (append_func == NULL) return -1; for (i = x; i < len; i++) { @@ -6612,9 +6587,8 @@ load_additems(UnpicklerObject *self) } else { PyObject *add_func; - _Py_IDENTIFIER(add); - add_func = _PyObject_GetAttrId(set, &PyId_add); + add_func = PyObject_GetAttr(set, &_Py_ID(add)); if (add_func == NULL) return -1; for (i = mark; i < len; i++) { @@ -6642,7 +6616,6 @@ load_build(UnpicklerObject *self) PyObject *state, *inst, *slotstate; PyObject *setstate; int status = 0; - _Py_IDENTIFIER(__setstate__); /* Stack is ... instance, state. We want to leave instance at * the stack top, possibly mutated via instance.__setstate__(state). @@ -6656,7 +6629,7 @@ load_build(UnpicklerObject *self) inst = self->stack->data[Py_SIZE(self->stack) - 1]; - if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { + if (_PyObject_LookupAttr(inst, &_Py_ID(__setstate__), &setstate) < 0) { Py_DECREF(state); return -1; } @@ -6692,14 +6665,13 @@ load_build(UnpicklerObject *self) PyObject *dict; PyObject *d_key, *d_value; Py_ssize_t i; - _Py_IDENTIFIER(__dict__); if (!PyDict_Check(state)) { PickleState *st = _Pickle_GetGlobalState(); PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); goto error; } - dict = _PyObject_GetAttrId(inst, &PyId___dict__); + dict = PyObject_GetAttr(inst, &_Py_ID(__dict__)); if (dict == NULL) goto error; @@ -7251,8 +7223,6 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, const char *errors, PyObject *buffers) /*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/ { - _Py_IDENTIFIER(persistent_load); - /* In case of multiple __init__() calls, clear previous content. */ if (self->read != NULL) (void)Unpickler_clear(self); @@ -7268,7 +7238,7 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, self->fix_imports = fix_imports; - if (init_method_ref((PyObject *)self, &PyId_persistent_load, + if (init_method_ref((PyObject *)self, &_Py_ID(persistent_load), &self->pers_func, &self->pers_func_self) < 0) { return -1; diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index caefdf4..cb8cd2a 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -21,6 +21,8 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#define NEEDS_PY_IDENTIFIER + #include "module.h" #include "structmember.h" // PyMemberDef #include "connection.h" diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 4700afb..395b10f 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -21,6 +21,8 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#define NEEDS_PY_IDENTIFIER + #include "cursor.h" #include "module.h" #include "util.h" diff --git a/Modules/_sqlite/microprotocols.c b/Modules/_sqlite/microprotocols.c index 95c799d..697d1a7 100644 --- a/Modules/_sqlite/microprotocols.c +++ b/Modules/_sqlite/microprotocols.c @@ -23,6 +23,8 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#define NEEDS_PY_IDENTIFIER + #include <Python.h> #include "cursor.h" diff --git a/Modules/_sqlite/module.c b/Modules/_sqlite/module.c index 3b9f797..cfa4b97 100644 --- a/Modules/_sqlite/module.c +++ b/Modules/_sqlite/module.c @@ -21,6 +21,8 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#define NEEDS_PY_IDENTIFIER + #include "connection.h" #include "statement.h" #include "cursor.h" diff --git a/Modules/_ssl.c b/Modules/_ssl.c index e17876e..07f0580 100644 --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -22,6 +22,7 @@ #define OPENSSL_NO_DEPRECATED 1 #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 498a7c2..0be42f3 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -13,6 +13,7 @@ #undef Py_BUILD_CORE_MODULE #undef Py_BUILD_CORE_BUILTIN +#define NEEDS_PY_IDENTIFIER /* Always enable assertions */ #undef NDEBUG @@ -3865,7 +3866,7 @@ slot_tp_del(PyObject *self) PyErr_Fetch(&error_type, &error_value, &error_traceback); /* Execute __del__ method, if any. */ - del = _PyObject_LookupSpecial(self, &PyId___tp_del__); + del = _PyObject_LookupSpecialId(self, &PyId___tp_del__); if (del != NULL) { res = PyObject_CallNoArgs(del); if (res == NULL) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 7052d4c..072b79a 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -17,11 +17,6 @@ // ThreadError is just an alias to PyExc_RuntimeError #define ThreadError PyExc_RuntimeError -_Py_IDENTIFIER(__dict__); - -_Py_IDENTIFIER(stderr); -_Py_IDENTIFIER(flush); - // Forward declarations static struct PyModuleDef thread_module; @@ -938,12 +933,7 @@ local_setattro(localobject *self, PyObject *name, PyObject *v) return -1; } - PyObject *str_dict = _PyUnicode_FromId(&PyId___dict__); // borrowed ref - if (str_dict == NULL) { - return -1; - } - - int r = PyObject_RichCompareBool(name, str_dict, Py_EQ); + int r = PyObject_RichCompareBool(name, &_Py_ID(__dict__), Py_EQ); if (r == -1) { return -1; } @@ -994,12 +984,7 @@ local_getattro(localobject *self, PyObject *name) if (ldict == NULL) return NULL; - PyObject *str_dict = _PyUnicode_FromId(&PyId___dict__); // borrowed ref - if (str_dict == NULL) { - return NULL; - } - - int r = PyObject_RichCompareBool(name, str_dict, Py_EQ); + int r = PyObject_RichCompareBool(name, &_Py_ID(__dict__), Py_EQ); if (r == 1) { return Py_NewRef(ldict); } @@ -1413,7 +1398,6 @@ static int thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value, PyObject *exc_traceback, PyObject *thread) { - _Py_IDENTIFIER(name); /* print(f"Exception in thread {thread.name}:", file=file) */ if (PyFile_WriteString("Exception in thread ", file) < 0) { return -1; @@ -1421,7 +1405,7 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value, PyObject *name = NULL; if (thread != Py_None) { - if (_PyObject_LookupAttrId(thread, &PyId_name, &name) < 0) { + if (_PyObject_LookupAttr(thread, &_Py_ID(name), &name) < 0) { return -1; } } @@ -1459,7 +1443,7 @@ thread_excepthook_file(PyObject *file, PyObject *exc_type, PyObject *exc_value, _PyErr_Display(file, exc_type, exc_value, exc_traceback); /* Call file.flush() */ - PyObject *res = _PyObject_CallMethodIdNoArgs(file, &PyId_flush); + PyObject *res = PyObject_CallMethodNoArgs(file, &_Py_ID(flush)); if (!res) { return -1; } @@ -1514,7 +1498,8 @@ thread_excepthook(PyObject *module, PyObject *args) PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2); PyObject *thread = PyStructSequence_GET_ITEM(args, 3); - PyObject *file = _PySys_GetObjectId(&PyId_stderr); + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr)); if (file == NULL || file == Py_None) { if (thread == Py_None) { /* do nothing if sys.stderr is None and thread is None */ diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 8e43063..4aaab0c 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -6,6 +6,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN #include "Python.h" diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index ba558d0..df48a0d 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -5,6 +5,7 @@ */ #define PY_SSIZE_T_CLEAN +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "structmember.h" // PyMemberDef #include "multibytecodec.h" diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 1888337..46e18c1 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -32,11 +32,6 @@ #define PUTS(fd, str) _Py_write_noraise(fd, str, strlen(str)) -_Py_IDENTIFIER(enable); -_Py_IDENTIFIER(fileno); -_Py_IDENTIFIER(flush); -_Py_IDENTIFIER(stderr); - #ifdef HAVE_SIGACTION typedef struct sigaction _Py_sighandler_t; #else @@ -155,7 +150,8 @@ faulthandler_get_fileno(PyObject **file_ptr) PyObject *file = *file_ptr; if (file == NULL || file == Py_None) { - file = _PySys_GetObjectId(&PyId_stderr); + PyThreadState *tstate = _PyThreadState_GET(); + file = _PySys_GetAttr(tstate, &_Py_ID(stderr)); if (file == NULL) { PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr"); return -1; @@ -178,7 +174,7 @@ faulthandler_get_fileno(PyObject **file_ptr) return fd; } - result = _PyObject_CallMethodIdNoArgs(file, &PyId_fileno); + result = PyObject_CallMethodNoArgs(file, &_Py_ID(fileno)); if (result == NULL) return -1; @@ -196,7 +192,7 @@ faulthandler_get_fileno(PyObject **file_ptr) return -1; } - result = _PyObject_CallMethodIdNoArgs(file, &PyId_flush); + result = PyObject_CallMethodNoArgs(file, &_Py_ID(flush)); if (result != NULL) Py_DECREF(result); else { @@ -1336,7 +1332,7 @@ faulthandler_init_enable(void) return -1; } - PyObject *res = _PyObject_CallMethodIdNoArgs(module, &PyId_enable); + PyObject *res = PyObject_CallMethodNoArgs(module, &_Py_ID(enable)); Py_DECREF(module); if (res == NULL) { return -1; diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 342a3e6..0ec65d5 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -503,9 +503,8 @@ _grouper_next(_grouperobject *igo) static PyObject * _grouper_reduce(_grouperobject *lz, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); if (((groupbyobject *)lz->parent)->currgrouper != lz) { - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->parent, lz->tgtkey); } @@ -1015,7 +1014,6 @@ itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) { Py_ssize_t i; PyObject *it, *copyable, *copyfunc, *result; - _Py_IDENTIFIER(__copy__); if (n < 0) { PyErr_SetString(PyExc_ValueError, "n must be >= 0"); @@ -1032,7 +1030,7 @@ itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) return NULL; } - if (_PyObject_LookupAttrId(it, &PyId___copy__, ©func) < 0) { + if (_PyObject_LookupAttr(it, &_Py_ID(__copy__), ©func) < 0) { Py_DECREF(it); Py_DECREF(result); return NULL; @@ -1047,7 +1045,7 @@ itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) Py_DECREF(result); return NULL; } - copyfunc = _PyObject_GetAttrId(copyable, &PyId___copy__); + copyfunc = PyObject_GetAttr(copyable, &_Py_ID(__copy__)); if (copyfunc == NULL) { Py_DECREF(copyable); Py_DECREF(result); @@ -1179,9 +1177,8 @@ cycle_reduce(cycleobject *lz, PyObject *Py_UNUSED(ignored)) if (it == NULL) return NULL; if (lz->index != 0) { - _Py_IDENTIFIER(__setstate__); - PyObject *res = _PyObject_CallMethodId(it, &PyId___setstate__, - "n", lz->index); + PyObject *res = _PyObject_CallMethod(it, &_Py_ID(__setstate__), + "n", lz->index); if (res == NULL) { Py_DECREF(it); return NULL; @@ -4545,7 +4542,6 @@ static PyTypeObject ziplongest_type; static PyObject * zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - _Py_IDENTIFIER(fillvalue); ziplongestobject *lz; Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ @@ -4556,7 +4552,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { fillvalue = NULL; if (PyDict_GET_SIZE(kwds) == 1) { - fillvalue = _PyDict_GetItemIdWithError(kwds, &PyId_fillvalue); + fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); } if (fillvalue == NULL) { if (!PyErr_Occurred()) { diff --git a/Modules/main.c b/Modules/main.c index b9bcea39..2443f56 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -158,11 +158,10 @@ error: static int pymain_sys_path_add_path0(PyInterpreterState *interp, PyObject *path0) { - _Py_IDENTIFIER(path); PyObject *sys_path; PyObject *sysdict = interp->sysdict; if (sysdict != NULL) { - sys_path = _PyDict_GetItemIdWithError(sysdict, &PyId_path); + sys_path = PyDict_GetItemWithError(sysdict, &_Py_ID(path)); if (sys_path == NULL && PyErr_Occurred()) { return -1; } diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 0c7d4de..721c9a6 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -55,6 +55,7 @@ raised for division by zero and mod by zero. #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #include "Python.h" #include "pycore_bitutils.h" // _Py_bit_length() @@ -1216,7 +1217,7 @@ math_ceil(PyObject *module, PyObject *number) _Py_IDENTIFIER(__ceil__); if (!PyFloat_CheckExact(number)) { - PyObject *method = _PyObject_LookupSpecial(number, &PyId___ceil__); + PyObject *method = _PyObject_LookupSpecialId(number, &PyId___ceil__); if (method != NULL) { PyObject *result = _PyObject_CallNoArgs(method); Py_DECREF(method); @@ -1288,7 +1289,7 @@ math_floor(PyObject *module, PyObject *number) } else { - PyObject *method = _PyObject_LookupSpecial(number, &PyId___floor__); + PyObject *method = _PyObject_LookupSpecialId(number, &PyId___floor__); if (method != NULL) { PyObject *result = _PyObject_CallNoArgs(method); Py_DECREF(method); @@ -2166,7 +2167,7 @@ math_trunc(PyObject *module, PyObject *x) return NULL; } - trunc = _PyObject_LookupSpecial(x, &PyId___trunc__); + trunc = _PyObject_LookupSpecialId(x, &PyId___trunc__); if (trunc == NULL) { if (!PyErr_Occurred()) PyErr_Format(PyExc_TypeError, diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 7c9c28f..0a21694 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -21,6 +21,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN #include <Python.h> diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c index c9e788f..172a6e4 100644 --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -20,6 +20,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN #include "Python.h" diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index ea0435d..6ef3610 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -22,11 +22,12 @@ # include "pycore_bitutils.h" // _Py_popcount32() #endif #include "pycore_call.h" // _PyObject_CallNoArgs() -#include "pycore_fileutils.h" // _Py_closerange() -#include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_ceval.h" // _PyEval_ReInitThreads() +#include "pycore_fileutils.h" // _Py_closerange() #include "pycore_import.h" // _PyImport_ReInitLock() #include "pycore_initconfig.h" // _PyStatus_EXCEPTION() +#include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_object.h" // _PyObject_LookupSpecial() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "structmember.h" // PyMemberDef @@ -323,8 +324,6 @@ corresponding Unix manual entries for more information on calls."); # endif /* _MSC_VER */ #endif /* ! __WATCOMC__ || __QNX__ */ -_Py_IDENTIFIER(__fspath__); - /*[clinic input] # one of the few times we lie about this name! module os @@ -1159,7 +1158,7 @@ path_converter(PyObject *o, void *p) /* Inline PyOS_FSPath() for better error messages. */ PyObject *func, *res; - func = _PyObject_LookupSpecial(o, &PyId___fspath__); + func = _PyObject_LookupSpecial(o, &_Py_ID(__fspath__)); if (NULL == func) { goto error_format; } @@ -14437,7 +14436,7 @@ PyOS_FSPath(PyObject *path) return path; } - func = _PyObject_LookupSpecial(path, &PyId___fspath__); + func = _PyObject_LookupSpecial(path, &_Py_ID(__fspath__)); if (NULL == func) { return PyErr_Format(PyExc_TypeError, "expected str, bytes or os.PathLike object, " diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index f2baab7..f224f91 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1,3 +1,5 @@ +#define NEEDS_PY_IDENTIFIER + #include "Python.h" #include <ctype.h> diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 4b7500a..5b2d9b7 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -4,6 +4,7 @@ #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_namespace.h" // _PyNamespace_New() +#include "pycore_runtime.h" // _Py_ID() #include <ctype.h> @@ -910,13 +911,12 @@ static PyObject * time_strptime(PyObject *self, PyObject *args) { PyObject *module, *func, *result; - _Py_IDENTIFIER(_strptime_time); module = PyImport_ImportModule("_strptime"); if (!module) return NULL; - func = _PyObject_GetAttrId(module, &PyId__strptime_time); + func = PyObject_GetAttr(module, &_Py_ID(_strptime_time)); Py_DECREF(module); if (!func) { return NULL; diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c index bdbddcf..7369db5 100644 --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -15,6 +15,7 @@ #ifndef Py_BUILD_CORE_BUILTIN # define Py_BUILD_CORE_MODULE 1 #endif +#define NEEDS_PY_IDENTIFIER #define PY_SSIZE_T_CLEAN |