diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 48 | ||||
-rw-r--r-- | Objects/bytearrayobject.c | 8 | ||||
-rw-r--r-- | Objects/bytesobject.c | 11 | ||||
-rw-r--r-- | Objects/call.c | 36 | ||||
-rw-r--r-- | Objects/classobject.c | 15 | ||||
-rw-r--r-- | Objects/complexobject.c | 3 | ||||
-rw-r--r-- | Objects/descrobject.c | 35 | ||||
-rw-r--r-- | Objects/dictobject.c | 40 | ||||
-rw-r--r-- | Objects/enumobject.c | 3 | ||||
-rw-r--r-- | Objects/exceptions.c | 6 | ||||
-rw-r--r-- | Objects/fileobject.c | 22 | ||||
-rw-r--r-- | Objects/frameobject.c | 4 | ||||
-rw-r--r-- | Objects/funcobject.c | 10 | ||||
-rw-r--r-- | Objects/genericaliasobject.c | 19 | ||||
-rw-r--r-- | Objects/genobject.c | 6 | ||||
-rw-r--r-- | Objects/iterobject.c | 10 | ||||
-rw-r--r-- | Objects/listobject.c | 14 | ||||
-rw-r--r-- | Objects/longobject.c | 11 | ||||
-rw-r--r-- | Objects/methodobject.c | 7 | ||||
-rw-r--r-- | Objects/moduleobject.c | 61 | ||||
-rw-r--r-- | Objects/object.c | 36 | ||||
-rw-r--r-- | Objects/odictobject.c | 19 | ||||
-rw-r--r-- | Objects/rangeobject.c | 10 | ||||
-rw-r--r-- | Objects/setobject.c | 6 | ||||
-rw-r--r-- | Objects/structseq.c | 37 | ||||
-rw-r--r-- | Objects/tupleobject.c | 5 | ||||
-rw-r--r-- | Objects/typeobject.c | 487 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 45 | ||||
-rw-r--r-- | Objects/unionobject.c | 12 | ||||
-rw-r--r-- | Objects/weakrefobject.c | 6 |
30 files changed, 422 insertions, 610 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index ed99fd6..6ad66a8 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -92,7 +92,6 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) { PyObject *hint, *result; Py_ssize_t res; - _Py_IDENTIFIER(__length_hint__); if (_PyObject_HasLen(o)) { res = PyObject_Length(o); if (res < 0) { @@ -107,7 +106,7 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) return res; } } - hint = _PyObject_LookupSpecial(o, &PyId___length_hint__); + hint = _PyObject_LookupSpecial(o, &_Py_ID(__length_hint__)); if (hint == NULL) { if (PyErr_Occurred()) { return -1; @@ -177,14 +176,13 @@ PyObject_GetItem(PyObject *o, PyObject *key) if (PyType_Check(o)) { PyObject *meth, *result; - _Py_IDENTIFIER(__class_getitem__); // Special case type[int], but disallow other types so str[int] fails if ((PyTypeObject*)o == &PyType_Type) { return Py_GenericAlias(o, key); } - if (_PyObject_LookupAttrId(o, &PyId___class_getitem__, &meth) < 0) { + if (_PyObject_LookupAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) { return NULL; } if (meth) { @@ -770,7 +768,6 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) PyObject *meth; PyObject *empty = NULL; PyObject *result = NULL; - _Py_IDENTIFIER(__format__); if (format_spec != NULL && !PyUnicode_Check(format_spec)) { PyErr_Format(PyExc_SystemError, @@ -797,7 +794,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) } /* Find the (unbound!) __format__ method */ - meth = _PyObject_LookupSpecial(obj, &PyId___format__); + meth = _PyObject_LookupSpecial(obj, &_Py_ID(__format__)); if (meth == NULL) { PyThreadState *tstate = _PyThreadState_GET(); if (!_PyErr_Occurred(tstate)) { @@ -1520,7 +1517,6 @@ PyNumber_Long(PyObject *o) PyNumberMethods *m; PyObject *trunc_func; Py_buffer view; - _Py_IDENTIFIER(__trunc__); if (o == NULL) { return null_error(); @@ -1562,7 +1558,7 @@ PyNumber_Long(PyObject *o) if (m && m->nb_index) { return PyNumber_Index(o); } - trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__); + trunc_func = _PyObject_LookupSpecial(o, &_Py_ID(__trunc__)); if (trunc_func) { if (PyErr_WarnEx(PyExc_DeprecationWarning, "The delegation of int() to __trunc__ is deprecated.", 1)) { @@ -2406,12 +2402,12 @@ PyMapping_HasKey(PyObject *o, PyObject *key) a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values(). */ static PyObject * -method_output_as_list(PyObject *o, _Py_Identifier *meth_id) +method_output_as_list(PyObject *o, PyObject *meth) { PyObject *it, *result, *meth_output; assert(o != NULL); - meth_output = _PyObject_CallMethodIdNoArgs(o, meth_id); + meth_output = PyObject_CallMethodNoArgs(o, meth); if (meth_output == NULL || PyList_CheckExact(meth_output)) { return meth_output; } @@ -2422,7 +2418,7 @@ method_output_as_list(PyObject *o, _Py_Identifier *meth_id) _PyErr_Format(tstate, PyExc_TypeError, "%.200s.%U() returned a non-iterable (type %.200s)", Py_TYPE(o)->tp_name, - _PyUnicode_FromId(meth_id), + meth, Py_TYPE(meth_output)->tp_name); } Py_DECREF(meth_output); @@ -2437,43 +2433,37 @@ method_output_as_list(PyObject *o, _Py_Identifier *meth_id) PyObject * PyMapping_Keys(PyObject *o) { - _Py_IDENTIFIER(keys); - if (o == NULL) { return null_error(); } if (PyDict_CheckExact(o)) { return PyDict_Keys(o); } - return method_output_as_list(o, &PyId_keys); + return method_output_as_list(o, &_Py_ID(keys)); } PyObject * PyMapping_Items(PyObject *o) { - _Py_IDENTIFIER(items); - if (o == NULL) { return null_error(); } if (PyDict_CheckExact(o)) { return PyDict_Items(o); } - return method_output_as_list(o, &PyId_items); + return method_output_as_list(o, &_Py_ID(items)); } PyObject * PyMapping_Values(PyObject *o) { - _Py_IDENTIFIER(values); - if (o == NULL) { return null_error(); } if (PyDict_CheckExact(o)) { return PyDict_Values(o); } - return method_output_as_list(o, &PyId_values); + return method_output_as_list(o, &_Py_ID(values)); } /* isinstance(), issubclass() */ @@ -2505,10 +2495,9 @@ PyMapping_Values(PyObject *o) static PyObject * abstract_get_bases(PyObject *cls) { - _Py_IDENTIFIER(__bases__); PyObject *bases; - (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases); + (void)_PyObject_LookupAttr(cls, &_Py_ID(__bases__), &bases); if (bases != NULL && !PyTuple_Check(bases)) { Py_DECREF(bases); return NULL; @@ -2589,11 +2578,10 @@ object_isinstance(PyObject *inst, PyObject *cls) { PyObject *icls; int retval; - _Py_IDENTIFIER(__class__); if (PyType_Check(cls)) { retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); if (retval == 0) { - retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls); + retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls); if (icls != NULL) { if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) { retval = PyType_IsSubtype( @@ -2611,7 +2599,7 @@ object_isinstance(PyObject *inst, PyObject *cls) if (!check_class(cls, "isinstance() arg 2 must be a type, a tuple of types, or a union")) return -1; - retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls); + retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls); if (icls != NULL) { retval = abstract_issubclass(icls, cls); Py_DECREF(icls); @@ -2624,8 +2612,6 @@ object_isinstance(PyObject *inst, PyObject *cls) static int object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls) { - _Py_IDENTIFIER(__instancecheck__); - /* Quick test for an exact match */ if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) { return 1; @@ -2656,7 +2642,7 @@ object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls return r; } - PyObject *checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__); + PyObject *checker = _PyObject_LookupSpecial(cls, &_Py_ID(__instancecheck__)); if (checker != NULL) { if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) { Py_DECREF(checker); @@ -2715,7 +2701,6 @@ recursive_issubclass(PyObject *derived, PyObject *cls) static int object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls) { - _Py_IDENTIFIER(__subclasscheck__); PyObject *checker; /* We know what type's __subclasscheck__ does. */ @@ -2744,7 +2729,7 @@ object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls) return r; } - checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__); + checker = _PyObject_LookupSpecial(cls, &_Py_ID(__subclasscheck__)); if (checker != NULL) { int ok = -1; if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) { @@ -2879,7 +2864,6 @@ PyIter_Next(PyObject *iter) PySendResult PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result) { - _Py_IDENTIFIER(send); assert(arg != NULL); assert(result != NULL); if (Py_TYPE(iter)->tp_as_async && Py_TYPE(iter)->tp_as_async->am_send) { @@ -2891,7 +2875,7 @@ PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result) *result = Py_TYPE(iter)->tp_iternext(iter); } else { - *result = _PyObject_CallMethodIdOneArg(iter, &PyId_send, arg); + *result = PyObject_CallMethodOneArg(iter, &_Py_ID(send), arg); } if (*result != NULL) { return PYGEN_NEXT; diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 8fce83d..0ebb2ec 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2112,10 +2112,9 @@ static PyObject * _common_reduce(PyByteArrayObject *self, int proto) { PyObject *dict; - _Py_IDENTIFIER(__dict__); char *buf; - if (_PyObject_LookupAttrId((PyObject *)self, &PyId___dict__, &dict) < 0) { + if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(__dict__), &dict) < 0) { return NULL; } if (dict == NULL) { @@ -2428,12 +2427,11 @@ PyDoc_STRVAR(length_hint_doc, static PyObject * bytearrayiter_reduce(bytesiterobject *it, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); if (it->it_seq != NULL) { - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); } else { - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } } diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index cfc358f..3d8a216 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -23,8 +23,6 @@ class bytes "PyBytesObject *" "&PyBytes_Type" #include "clinic/bytesobject.c.h" -_Py_IDENTIFIER(__bytes__); - /* PyBytesObject_SIZE gives the basic size of a bytes object; any memory allocation for a bytes object of length n should request PyBytesObject_SIZE + n bytes. @@ -530,7 +528,7 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) return v; } /* does it support __bytes__? */ - func = _PyObject_LookupSpecial(v, &PyId___bytes__); + func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__)); if (func != NULL) { result = _PyObject_CallNoArgs(func); Py_DECREF(func); @@ -2582,7 +2580,7 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding, /* We'd like to call PyObject_Bytes here, but we need to check for an integer argument before deferring to PyBytes_FromObject, something PyObject_Bytes doesn't do. */ - else if ((func = _PyObject_LookupSpecial(x, &PyId___bytes__)) != NULL) { + else if ((func = _PyObject_LookupSpecial(x, &_Py_ID(__bytes__))) != NULL) { bytes = _PyObject_CallNoArgs(func); Py_DECREF(func); if (bytes == NULL) @@ -3122,12 +3120,11 @@ PyDoc_STRVAR(length_hint_doc, static PyObject * striter_reduce(striterobject *it, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); if (it->it_seq != NULL) { - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); } else { - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } } diff --git a/Objects/call.c b/Objects/call.c index 310a2d7..c3939ea 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -607,7 +607,6 @@ callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_lis return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t); } - PyObject * PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...) { @@ -659,6 +658,30 @@ PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...) PyObject * +_PyObject_CallMethod(PyObject *obj, PyObject *name, + const char *format, ...) +{ + PyThreadState *tstate = _PyThreadState_GET(); + if (obj == NULL || name == NULL) { + return null_error(tstate); + } + + PyObject *callable = PyObject_GetAttr(obj, name); + if (callable == NULL) { + return NULL; + } + + va_list va; + va_start(va, format); + PyObject *retval = callmethod(tstate, callable, format, va, 1); + va_end(va); + + Py_DECREF(callable); + return retval; +} + + +PyObject * _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name, const char *format, ...) { @@ -682,6 +705,17 @@ _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name, } +PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable, + const char *format, ...) +{ + va_list va; + va_start(va, format); + PyObject *retval = callmethod(tstate, callable, format, va, 0); + va_end(va); + return retval; +} + + PyObject * _PyObject_CallMethod_SizeT(PyObject *obj, const char *name, const char *format, ...) diff --git a/Objects/classobject.c b/Objects/classobject.c index 9d4fc99..d7ccf31 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -9,8 +9,6 @@ #define TP_DESCR_GET(t) ((t)->tp_descr_get) -_Py_IDENTIFIER(__name__); -_Py_IDENTIFIER(__qualname__); PyObject * PyMethod_Function(PyObject *im) @@ -123,14 +121,13 @@ method_reduce(PyMethodObject *im, PyObject *Py_UNUSED(ignored)) PyObject *self = PyMethod_GET_SELF(im); PyObject *func = PyMethod_GET_FUNCTION(im); PyObject *funcname; - _Py_IDENTIFIER(getattr); - funcname = _PyObject_GetAttrId(func, &PyId___name__); + funcname = PyObject_GetAttr(func, &_Py_ID(__name__)); if (funcname == NULL) { return NULL; } - return Py_BuildValue("N(ON)", _PyEval_GetBuiltinId(&PyId_getattr), - self, funcname); + return Py_BuildValue( + "N(ON)", _PyEval_GetBuiltin(&_Py_ID(getattr)), self, funcname); } static PyMethodDef method_methods[] = { @@ -280,9 +277,9 @@ method_repr(PyMethodObject *a) PyObject *funcname, *result; const char *defname = "?"; - if (_PyObject_LookupAttrId(func, &PyId___qualname__, &funcname) < 0 || + if (_PyObject_LookupAttr(func, &_Py_ID(__qualname__), &funcname) < 0 || (funcname == NULL && - _PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0)) + _PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0)) { return NULL; } @@ -515,7 +512,7 @@ instancemethod_repr(PyObject *self) return NULL; } - if (_PyObject_LookupAttrId(func, &PyId___name__, &funcname) < 0) { + if (_PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0) { return NULL; } if (funcname != NULL && !PyUnicode_Check(funcname)) { diff --git a/Objects/complexobject.c b/Objects/complexobject.c index e0766de..9bd68d5 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -281,9 +281,8 @@ static PyObject * try_complex_special_method(PyObject *op) { PyObject *f; - _Py_IDENTIFIER(__complex__); - f = _PyObject_LookupSpecial(op, &PyId___complex__); + f = _PyObject_LookupSpecial(op, &_Py_ID(__complex__)); if (f) { PyObject *res = _PyObject_CallNoArgs(f); Py_DECREF(f); diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 962136b..9516268 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -7,8 +7,6 @@ #include "pycore_tuple.h" // _PyTuple_ITEMS() #include "structmember.h" // PyMemberDef -_Py_IDENTIFIER(getattr); - /*[clinic input] class mappingproxy "mappingproxyobject *" "&PyDictProxy_Type" class property "propertyobject *" "&PyProperty_Type" @@ -571,7 +569,6 @@ static PyObject * calculate_qualname(PyDescrObject *descr) { PyObject *type_qualname, *res; - _Py_IDENTIFIER(__qualname__); if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) { PyErr_SetString(PyExc_TypeError, @@ -579,8 +576,8 @@ calculate_qualname(PyDescrObject *descr) return NULL; } - type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type, - &PyId___qualname__); + type_qualname = PyObject_GetAttr( + (PyObject *)descr->d_type, &_Py_ID(__qualname__)); if (type_qualname == NULL) return NULL; @@ -608,7 +605,7 @@ descr_get_qualname(PyDescrObject *descr, void *Py_UNUSED(ignored)) static PyObject * descr_reduce(PyDescrObject *descr, PyObject *Py_UNUSED(ignored)) { - return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr), + return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(getattr)), PyDescr_TYPE(descr), PyDescr_NAME(descr)); } @@ -1086,8 +1083,7 @@ mappingproxy_get(mappingproxyobject *pp, PyObject *const *args, Py_ssize_t nargs { return NULL; } - _Py_IDENTIFIER(get); - return _PyObject_VectorcallMethodId(&PyId_get, newargs, + return _PyObject_VectorcallMethod(&_Py_ID(get), newargs, 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL); } @@ -1095,36 +1091,31 @@ mappingproxy_get(mappingproxyobject *pp, PyObject *const *args, Py_ssize_t nargs static PyObject * mappingproxy_keys(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(keys); - return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId_keys); + return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(keys)); } static PyObject * mappingproxy_values(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(values); - return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId_values); + return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(values)); } static PyObject * mappingproxy_items(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(items); - return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId_items); + return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(items)); } static PyObject * mappingproxy_copy(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(copy); - return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId_copy); + return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(copy)); } static PyObject * mappingproxy_reversed(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(__reversed__); - return _PyObject_CallMethodIdNoArgs(pp->mapping, &PyId___reversed__); + return PyObject_CallMethodNoArgs(pp->mapping, &_Py_ID(__reversed__)); } /* WARNING: mappingproxy methods must not give access @@ -1321,7 +1312,7 @@ wrapper_repr(wrapperobject *wp) static PyObject * wrapper_reduce(wrapperobject *wp, PyObject *Py_UNUSED(ignored)) { - return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr), + return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(getattr)), wp->self, PyDescr_NAME(wp->descr)); } @@ -1756,9 +1747,8 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset, /* if no docstring given and the getter has one, use that one */ if ((doc == NULL || doc == Py_None) && fget != NULL) { - _Py_IDENTIFIER(__doc__); PyObject *get_doc; - int rc = _PyObject_LookupAttrId(fget, &PyId___doc__, &get_doc); + int rc = _PyObject_LookupAttr(fget, &_Py_ID(__doc__), &get_doc); if (rc <= 0) { return rc; } @@ -1770,7 +1760,8 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset, in dict of the subclass instance instead, otherwise it gets shadowed by __doc__ in the class's dict. */ - int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc); + int err = PyObject_SetAttr( + (PyObject *)self, &_Py_ID(__doc__), get_doc); Py_DECREF(get_doc); if (err < 0) return -1; diff --git a/Objects/dictobject.c b/Objects/dictobject.c index c41bdb8..18de42c 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -1485,6 +1485,17 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key) } PyObject * +_PyDict_GetItemWithError(PyObject *dp, PyObject *kv) +{ + assert(PyUnicode_CheckExact(kv)); + Py_hash_t hash = kv->ob_type->tp_hash(kv); + if (hash == -1) { + return NULL; + } + return _PyDict_GetItem_KnownHash(dp, kv, hash); +} + +PyObject * _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) { PyObject *kv; @@ -2166,8 +2177,8 @@ dict_subscript(PyDictObject *mp, PyObject *key) if (!PyDict_CheckExact(mp)) { /* Look up __missing__ method if we're a subclass. */ PyObject *missing, *res; - _Py_IDENTIFIER(__missing__); - missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__); + missing = _PyObject_LookupSpecial( + (PyObject *)mp, &_Py_ID(__missing__)); if (missing != NULL) { res = PyObject_CallOneArg(missing, key); Py_DECREF(missing); @@ -2369,9 +2380,8 @@ dict_update_arg(PyObject *self, PyObject *arg) if (PyDict_CheckExact(arg)) { return PyDict_Merge(self, arg, 1); } - _Py_IDENTIFIER(keys); PyObject *func; - if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) { + if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) { return -1; } if (func != NULL) { @@ -4128,7 +4138,6 @@ dict___reversed___impl(PyDictObject *self) static PyObject * dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); /* copy the iterator state */ dictiterobject tmp = *di; Py_XINCREF(tmp.di_dict); @@ -4138,7 +4147,7 @@ dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored)) if (list == NULL) { return NULL; } - return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list); + return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list); } PyTypeObject PyDictRevIterItem_Type = { @@ -4408,9 +4417,8 @@ dictviews_sub(PyObject *self, PyObject *other) return NULL; } - _Py_IDENTIFIER(difference_update); - PyObject *tmp = _PyObject_CallMethodIdOneArg( - result, &PyId_difference_update, other); + PyObject *tmp = PyObject_CallMethodOneArg( + result, &_Py_ID(difference_update), other); if (tmp == NULL) { Py_DECREF(result); return NULL; @@ -4446,8 +4454,8 @@ _PyDictView_Intersect(PyObject* self, PyObject *other) /* if other is a set and self is smaller than other, reuse set intersection logic */ if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) { - _Py_IDENTIFIER(intersection); - return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL); + return PyObject_CallMethodObjArgs( + other, &_Py_ID(intersection), self, NULL); } /* if other is another dict view, and it is bigger than self, @@ -4587,9 +4595,8 @@ dictitems_xor(PyObject *self, PyObject *other) } key = val1 = val2 = NULL; - _Py_IDENTIFIER(items); - PyObject *remaining_pairs = _PyObject_CallMethodIdNoArgs(temp_dict, - &PyId_items); + PyObject *remaining_pairs = PyObject_CallMethodNoArgs( + temp_dict, &_Py_ID(items)); if (remaining_pairs == NULL) { goto error; } @@ -4621,9 +4628,8 @@ dictviews_xor(PyObject* self, PyObject *other) return NULL; } - _Py_IDENTIFIER(symmetric_difference_update); - PyObject *tmp = _PyObject_CallMethodIdOneArg( - result, &PyId_symmetric_difference_update, other); + PyObject *tmp = PyObject_CallMethodOneArg( + result, &_Py_ID(symmetric_difference_update), other); if (tmp == NULL) { Py_DECREF(result); return NULL; diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 828f1f9..d84bac6 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -356,9 +356,8 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq) Py_ssize_t n; PyObject *reversed_meth; reversedobject *ro; - _Py_IDENTIFIER(__reversed__); - reversed_meth = _PyObject_LookupSpecial(seq, &PyId___reversed__); + reversed_meth = _PyObject_LookupSpecial(seq, &_Py_ID(__reversed__)); if (reversed_meth == Py_None) { Py_DECREF(reversed_meth); PyErr_Format(PyExc_TypeError, diff --git a/Objects/exceptions.c b/Objects/exceptions.c index ea8a310..9b1c9f1 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1499,16 +1499,14 @@ ImportError_getstate(PyImportErrorObject *self) { PyObject *dict = ((PyBaseExceptionObject *)self)->dict; if (self->name || self->path) { - _Py_IDENTIFIER(name); - _Py_IDENTIFIER(path); dict = dict ? PyDict_Copy(dict) : PyDict_New(); if (dict == NULL) return NULL; - if (self->name && _PyDict_SetItemId(dict, &PyId_name, self->name) < 0) { + if (self->name && PyDict_SetItem(dict, &_Py_ID(name), self->name) < 0) { Py_DECREF(dict); return NULL; } - if (self->path && _PyDict_SetItemId(dict, &PyId_path, self->path) < 0) { + if (self->path && PyDict_SetItem(dict, &_Py_ID(path), self->path) < 0) { Py_DECREF(dict); return NULL; } diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 8ca56a8..8dba5b9 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -26,8 +26,6 @@ extern "C" { #endif -_Py_IDENTIFIER(open); - /* External C interface */ PyObject * @@ -40,9 +38,9 @@ PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const c io = PyImport_ImportModule("_io"); if (io == NULL) return NULL; - stream = _PyObject_CallMethodId(io, &PyId_open, "isisssO", fd, mode, - buffering, encoding, errors, - newline, closefd ? Py_True : Py_False); + stream = _PyObject_CallMethod(io, &_Py_ID(open), "isisssO", fd, mode, + buffering, encoding, errors, + newline, closefd ? Py_True : Py_False); Py_DECREF(io); if (stream == NULL) return NULL; @@ -54,7 +52,6 @@ PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const c PyObject * PyFile_GetLine(PyObject *f, int n) { - _Py_IDENTIFIER(readline); PyObject *result; if (f == NULL) { @@ -63,10 +60,10 @@ PyFile_GetLine(PyObject *f, int n) } if (n <= 0) { - result = _PyObject_CallMethodIdNoArgs(f, &PyId_readline); + result = PyObject_CallMethodNoArgs(f, &_Py_ID(readline)); } else { - result = _PyObject_CallMethodId(f, &PyId_readline, "i", n); + result = _PyObject_CallMethod(f, &_Py_ID(readline), "i", n); } if (result != NULL && !PyBytes_Check(result) && !PyUnicode_Check(result)) { @@ -120,13 +117,12 @@ int PyFile_WriteObject(PyObject *v, PyObject *f, int flags) { PyObject *writer, *value, *result; - _Py_IDENTIFIER(write); if (f == NULL) { PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); return -1; } - writer = _PyObject_GetAttrId(f, &PyId_write); + writer = PyObject_GetAttr(f, &_Py_ID(write)); if (writer == NULL) return -1; if (flags & Py_PRINT_RAW) { @@ -182,12 +178,11 @@ PyObject_AsFileDescriptor(PyObject *o) { int fd; PyObject *meth; - _Py_IDENTIFIER(fileno); if (PyLong_Check(o)) { fd = _PyLong_AsInt(o); } - else if (_PyObject_LookupAttrId(o, &PyId_fileno, &meth) < 0) { + else if (_PyObject_LookupAttr(o, &_Py_ID(fileno), &meth) < 0) { return -1; } else if (meth != NULL) { @@ -509,8 +504,7 @@ PyFile_OpenCodeObject(PyObject *path) } else { iomod = PyImport_ImportModule("_io"); if (iomod) { - f = _PyObject_CallMethodId(iomod, &PyId_open, "Os", - path, "rb"); + f = _PyObject_CallMethod(iomod, &_Py_ID(open), "Os", path, "rb"); Py_DECREF(iomod); } } diff --git a/Objects/frameobject.c b/Objects/frameobject.c index dd29541..dc21003 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -770,8 +770,6 @@ PyTypeObject PyFrame_Type = { 0, /* tp_dict */ }; -_Py_IDENTIFIER(__builtins__); - static void init_frame(InterpreterFrame *frame, PyFunctionObject *func, PyObject *locals) { @@ -1074,7 +1072,7 @@ PyFrame_GetBack(PyFrameObject *frame) PyObject* _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals) { - PyObject *builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__); + PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__)); if (builtins) { if (PyModule_Check(builtins)) { builtins = _PyModule_GetDict(builtins); diff --git a/Objects/funcobject.c b/Objects/funcobject.c index a8f006d..deacfd5 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -79,8 +79,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname Py_INCREF(doc); // __module__: Use globals['__name__'] if it exists, or NULL. - _Py_IDENTIFIER(__name__); - PyObject *module = _PyDict_GetItemIdWithError(globals, &PyId___name__); + PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__)); PyObject *builtins = NULL; if (module == NULL && _PyErr_Occurred(tstate)) { goto error; @@ -808,12 +807,7 @@ functools_wraps(PyObject *wrapper, PyObject *wrapped) { #define COPY_ATTR(ATTR) \ do { \ - _Py_IDENTIFIER(ATTR); \ - PyObject *attr = _PyUnicode_FromId(&PyId_ ## ATTR); \ - if (attr == NULL) { \ - return -1; \ - } \ - if (functools_copy_attr(wrapper, wrapped, attr) < 0) { \ + if (functools_copy_attr(wrapper, wrapped, &_Py_ID(ATTR)) < 0) { \ return -1; \ } \ } while (0) \ diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index dbe5d89..b416449 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -41,10 +41,6 @@ ga_traverse(PyObject *self, visitproc visit, void *arg) static int ga_repr_item(_PyUnicodeWriter *writer, PyObject *p) { - _Py_IDENTIFIER(__module__); - _Py_IDENTIFIER(__qualname__); - _Py_IDENTIFIER(__origin__); - _Py_IDENTIFIER(__args__); PyObject *qualname = NULL; PyObject *module = NULL; PyObject *r = NULL; @@ -57,12 +53,12 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p) goto done; } - if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) { goto done; } if (tmp != NULL) { Py_DECREF(tmp); - if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) { goto done; } if (tmp != NULL) { @@ -72,13 +68,13 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p) } } - if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) { goto done; } if (qualname == NULL) { goto use_repr; } - if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) { goto done; } if (module == NULL || module == Py_None) { @@ -218,9 +214,9 @@ _Py_make_parameters(PyObject *args) iparam += tuple_add(parameters, iparam, t); } else { - _Py_IDENTIFIER(__parameters__); PyObject *subparams; - if (_PyObject_LookupAttrId(t, &PyId___parameters__, &subparams) < 0) { + if (_PyObject_LookupAttr(t, &_Py_ID(__parameters__), + &subparams) < 0) { Py_DECREF(parameters); return NULL; } @@ -260,9 +256,8 @@ _Py_make_parameters(PyObject *args) static PyObject * subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems) { - _Py_IDENTIFIER(__parameters__); PyObject *subparams; - if (_PyObject_LookupAttrId(obj, &PyId___parameters__, &subparams) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__parameters__), &subparams) < 0) { return NULL; } if (subparams && PyTuple_Check(subparams) && PyTuple_GET_SIZE(subparams)) { diff --git a/Objects/genobject.c b/Objects/genobject.c index b2d402e..b34931a 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -320,7 +320,6 @@ static int gen_close_iter(PyObject *yf) { PyObject *retval = NULL; - _Py_IDENTIFIER(close); if (PyGen_CheckExact(yf) || PyCoro_CheckExact(yf)) { retval = gen_close((PyGenObject *)yf, NULL); @@ -329,7 +328,7 @@ gen_close_iter(PyObject *yf) } else { PyObject *meth; - if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) { + if (_PyObject_LookupAttr(yf, &_Py_ID(close), &meth) < 0) { PyErr_WriteUnraisable(yf); } if (meth) { @@ -417,7 +416,6 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, PyObject *typ, PyObject *val, PyObject *tb) { PyObject *yf = _PyGen_yf(gen); - _Py_IDENTIFIER(throw); if (yf) { InterpreterFrame *frame = (InterpreterFrame *)gen->gi_iframe; @@ -462,7 +460,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, } else { /* `yf` is an iterator or a coroutine-like object. */ PyObject *meth; - if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) { + if (_PyObject_LookupAttr(yf, &_Py_ID(throw), &meth) < 0) { Py_DECREF(yf); return NULL; } diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 5db6bc1..1732a03 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -10,8 +10,6 @@ typedef struct { PyObject *it_seq; /* Set to NULL when iterator is exhausted */ } seqiterobject; -_Py_IDENTIFIER(iter); - PyObject * PySeqIter_New(PyObject *seq) { @@ -106,10 +104,10 @@ static PyObject * iter_reduce(seqiterobject *it, PyObject *Py_UNUSED(ignored)) { if (it->it_seq != NULL) - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); else - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); @@ -245,10 +243,10 @@ static PyObject * calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored)) { if (it->it_callable != NULL && it->it_sentinel != NULL) - return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(OO)", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_callable, it->it_sentinel); else - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } static PyMethodDef calliter_methods[] = { diff --git a/Objects/listobject.c b/Objects/listobject.c index 752d9e0..1ba1c1b 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3505,25 +3505,25 @@ listreviter_setstate(listreviterobject *it, PyObject *state) static PyObject * listiter_reduce_general(void *_it, int forward) { - _Py_IDENTIFIER(iter); - _Py_IDENTIFIER(reversed); PyObject *list; /* the objects are not the same, index is of different types! */ if (forward) { listiterobject *it = (listiterobject *)_it; - if (it->it_seq) - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + if (it->it_seq) { + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); + } } else { listreviterobject *it = (listreviterobject *)_it; - if (it->it_seq) - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_reversed), + if (it->it_seq) { + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(reversed)), it->it_seq, it->it_index); + } } /* empty iterator, create an empty list */ list = PyList_New(0); if (list == NULL) return NULL; - return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list); + return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list); } diff --git a/Objects/longobject.c b/Objects/longobject.c index e1f6a64..3438906 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -22,9 +22,6 @@ class int "PyObject *" "&PyLong_Type" [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/ -_Py_IDENTIFIER(little); -_Py_IDENTIFIER(big); - /* Is this PyLong of size 1, 0 or -1? */ #define IS_MEDIUM_VALUE(x) (((size_t)Py_SIZE(x)) + 1U < 3U) @@ -5775,9 +5772,9 @@ int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, if (byteorder == NULL) little_endian = 0; - else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) + else if (_PyUnicode_Equal(byteorder, &_Py_ID(little))) little_endian = 1; - else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) + else if (_PyUnicode_Equal(byteorder, &_Py_ID(big))) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, @@ -5837,9 +5834,9 @@ int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj, if (byteorder == NULL) little_endian = 0; - else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little)) + else if (_PyUnicode_Equal(byteorder, &_Py_ID(little))) little_endian = 1; - else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big)) + else if (_PyUnicode_Equal(byteorder, &_Py_ID(big))) little_endian = 0; else { PyErr_SetString(PyExc_ValueError, diff --git a/Objects/methodobject.c b/Objects/methodobject.c index 2df63cf..93fac22 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -179,12 +179,10 @@ meth_dealloc(PyCFunctionObject *m) static PyObject * meth_reduce(PyCFunctionObject *m, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(getattr); - if (m->m_self == NULL || PyModule_Check(m->m_self)) return PyUnicode_FromString(m->m_ml->ml_name); - return Py_BuildValue("N(Os)", _PyEval_GetBuiltinId(&PyId_getattr), + return Py_BuildValue("N(Os)", _PyEval_GetBuiltin(&_Py_ID(getattr)), m->m_self, m->m_ml->ml_name); } @@ -223,14 +221,13 @@ meth_get__qualname__(PyCFunctionObject *m, void *closure) Otherwise return type(m.__self__).__qualname__ + '.' + m.__name__ (e.g. [].append.__qualname__ == 'list.append') */ PyObject *type, *type_qualname, *res; - _Py_IDENTIFIER(__qualname__); if (m->m_self == NULL || PyModule_Check(m->m_self)) return PyUnicode_FromString(m->m_ml->ml_name); type = PyType_Check(m->m_self) ? m->m_self : (PyObject*)Py_TYPE(m->m_self); - type_qualname = _PyObject_GetAttrId(type, &PyId___qualname__); + type_qualname = PyObject_GetAttr(type, &_Py_ID(__qualname__)); if (type_qualname == NULL) return NULL; diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 1d649a7..bd5e561 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -10,13 +10,6 @@ static Py_ssize_t max_module_number; -_Py_IDENTIFIER(__doc__); -_Py_IDENTIFIER(__name__); -_Py_IDENTIFIER(__spec__); -_Py_IDENTIFIER(__dict__); -_Py_IDENTIFIER(__dir__); -_Py_IDENTIFIER(__annotations__); - static PyMemberDef module_members[] = { {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY}, {0} @@ -61,22 +54,19 @@ static int module_init_dict(PyModuleObject *mod, PyObject *md_dict, PyObject *name, PyObject *doc) { - _Py_IDENTIFIER(__package__); - _Py_IDENTIFIER(__loader__); - assert(md_dict != NULL); if (doc == NULL) doc = Py_None; - if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0) + if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0) return -1; - if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0) + if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0) return -1; - if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0) + if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0) return -1; - if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0) + if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0) return -1; - if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0) + if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0) return -1; if (PyUnicode_CheckExact(name)) { Py_INCREF(name); @@ -474,7 +464,7 @@ PyModule_SetDocString(PyObject *m, const char *doc) PyObject *v; v = PyUnicode_FromString(doc); - if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) { + if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) { Py_XDECREF(v); return -1; } @@ -503,7 +493,7 @@ PyModule_GetNameObject(PyObject *m) } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || !PyDict_Check(d) || - (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL || + (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL || !PyUnicode_Check(name)) { if (!PyErr_Occurred()) { @@ -528,7 +518,6 @@ PyModule_GetName(PyObject *m) PyObject* PyModule_GetFilenameObject(PyObject *m) { - _Py_IDENTIFIER(__file__); PyObject *d; PyObject *fileobj; if (!PyModule_Check(m)) { @@ -537,7 +526,7 @@ PyModule_GetFilenameObject(PyObject *m) } d = ((PyModuleObject *)m)->md_dict; if (d == NULL || - (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL || + (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL || !PyUnicode_Check(fileobj)) { if (!PyErr_Occurred()) { @@ -726,8 +715,7 @@ int _PyModuleSpec_IsInitializing(PyObject *spec) { if (spec != NULL) { - _Py_IDENTIFIER(_initializing); - PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing); + PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_initializing)); if (value != NULL) { int initializing = PyObject_IsTrue(value); Py_DECREF(value); @@ -750,8 +738,7 @@ _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name) return 0; } - _Py_IDENTIFIER(_uninitialized_submodules); - PyObject *value = _PyObject_GetAttrId(spec, &PyId__uninitialized_submodules); + PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules)); if (value == NULL) { return 0; } @@ -774,18 +761,17 @@ module_getattro(PyModuleObject *m, PyObject *name) } PyErr_Clear(); assert(m->md_dict != NULL); - _Py_IDENTIFIER(__getattr__); - getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__); + getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__)); if (getattr) { return PyObject_CallOneArg(getattr, name); } if (PyErr_Occurred()) { return NULL; } - mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__); + mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__)); if (mod_name && PyUnicode_Check(mod_name)) { Py_INCREF(mod_name); - PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__); + PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__)); if (spec == NULL && PyErr_Occurred()) { Py_DECREF(mod_name); return NULL; @@ -861,11 +847,11 @@ static PyObject * module_dir(PyObject *self, PyObject *args) { PyObject *result = NULL; - PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); + PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__)); if (dict != NULL) { if (PyDict_Check(dict)) { - PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__); + PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__)); if (dirfunc) { result = _PyObject_CallNoArgs(dirfunc); } @@ -891,7 +877,7 @@ static PyMethodDef module_methods[] = { static PyObject * module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored)) { - PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__); + PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__)); if ((dict == NULL) || !PyDict_Check(dict)) { PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary"); @@ -901,8 +887,8 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored)) PyObject *annotations; /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */ - if (_PyDict_ContainsId(dict, &PyId___annotations__)) { - annotations = _PyDict_GetItemIdWithError(dict, &PyId___annotations__); + if (PyDict_Contains(dict, &_Py_ID(__annotations__))) { + annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__)); /* ** _PyDict_GetItemIdWithError could still fail, ** for instance with a well-timed Ctrl-C or a MemoryError. @@ -914,7 +900,8 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored)) } else { annotations = PyDict_New(); if (annotations) { - int result = _PyDict_SetItemId(dict, &PyId___annotations__, annotations); + int result = PyDict_SetItem( + dict, &_Py_ID(__annotations__), annotations); if (result) { Py_CLEAR(annotations); } @@ -928,7 +915,7 @@ static int module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored)) { int ret = -1; - PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__); + PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__)); if ((dict == NULL) || !PyDict_Check(dict)) { PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary"); @@ -937,17 +924,17 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor if (value != NULL) { /* set */ - ret = _PyDict_SetItemId(dict, &PyId___annotations__, value); + ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value); goto exit; } /* delete */ - if (!_PyDict_ContainsId(dict, &PyId___annotations__)) { + if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) { PyErr_Format(PyExc_AttributeError, "__annotations__"); goto exit; } - ret = _PyDict_DelItemId(dict, &PyId___annotations__); + ret = PyDict_DelItem(dict, &_Py_ID(__annotations__)); exit: Py_XDECREF(dict); diff --git a/Objects/object.c b/Objects/object.c index 3082e70..3044c86 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -31,11 +31,6 @@ extern "C" { /* Defined in tracemalloc.c */ extern void _PyMem_DumpTraceback(int fd, const void *ptr); -_Py_IDENTIFIER(Py_Repr); -_Py_IDENTIFIER(__bytes__); -_Py_IDENTIFIER(__dir__); -_Py_IDENTIFIER(__isabstractmethod__); - int _PyObject_CheckConsistency(PyObject *op, int check_content) @@ -562,7 +557,7 @@ PyObject_Bytes(PyObject *v) return v; } - func = _PyObject_LookupSpecial(v, &PyId___bytes__); + func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__)); if (func != NULL) { result = _PyObject_CallNoArgs(func); Py_DECREF(func); @@ -600,12 +595,9 @@ def _PyObject_FunctionStr(x): PyObject * _PyObject_FunctionStr(PyObject *x) { - _Py_IDENTIFIER(__module__); - _Py_IDENTIFIER(__qualname__); - _Py_IDENTIFIER(builtins); assert(!PyErr_Occurred()); PyObject *qualname; - int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname); + int ret = _PyObject_LookupAttr(x, &_Py_ID(__qualname__), &qualname); if (qualname == NULL) { if (ret < 0) { return NULL; @@ -614,13 +606,9 @@ _PyObject_FunctionStr(PyObject *x) } PyObject *module; PyObject *result = NULL; - ret = _PyObject_LookupAttrId(x, &PyId___module__, &module); + ret = _PyObject_LookupAttr(x, &_Py_ID(__module__), &module); if (module != NULL && module != Py_None) { - PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins); - if (builtinsname == NULL) { - goto done; - } - ret = PyObject_RichCompareBool(module, builtinsname, Py_NE); + ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE); if (ret < 0) { // error goto done; @@ -858,7 +846,7 @@ _PyObject_IsAbstract(PyObject *obj) if (obj == NULL) return 0; - res = _PyObject_LookupAttrId(obj, &PyId___isabstractmethod__, &isabstract); + res = _PyObject_LookupAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract); if (res > 0) { res = PyObject_IsTrue(isabstract); Py_DECREF(isabstract); @@ -892,8 +880,6 @@ static inline int set_attribute_error_context(PyObject* v, PyObject* name) { assert(PyErr_Occurred()); - _Py_IDENTIFIER(name); - _Py_IDENTIFIER(obj); // Intercept AttributeError exceptions and augment them to offer // suggestions later. if (PyErr_ExceptionMatches(PyExc_AttributeError)){ @@ -901,8 +887,8 @@ set_attribute_error_context(PyObject* v, PyObject* name) PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); if (PyErr_GivenExceptionMatches(value, PyExc_AttributeError) && - (_PyObject_SetAttrId(value, &PyId_name, name) || - _PyObject_SetAttrId(value, &PyId_obj, v))) { + (PyObject_SetAttr(value, &_Py_ID(name), name) || + PyObject_SetAttr(value, &_Py_ID(obj), v))) { return 1; } PyErr_Restore(type, value, traceback); @@ -1569,7 +1555,7 @@ static PyObject * _dir_object(PyObject *obj) { PyObject *result, *sorted; - PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__); + PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__)); assert(obj != NULL); if (dirfunc == NULL) { @@ -2148,7 +2134,7 @@ Py_ReprEnter(PyObject *obj) early on startup. */ if (dict == NULL) return 0; - list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); + list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); if (list == NULL) { if (PyErr_Occurred()) { return -1; @@ -2156,7 +2142,7 @@ Py_ReprEnter(PyObject *obj) list = PyList_New(0); if (list == NULL) return -1; - if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0) + if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0) return -1; Py_DECREF(list); } @@ -2184,7 +2170,7 @@ Py_ReprLeave(PyObject *obj) if (dict == NULL) goto finally; - list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr); + list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr)); if (list == NULL || !PyList_Check(list)) goto finally; diff --git a/Objects/odictobject.c b/Objects/odictobject.c index e27bcec..c207593 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -525,8 +525,6 @@ struct _odictnode { #define _odict_FOREACH(od, node) \ for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node)) -_Py_IDENTIFIER(items); - /* Return the index into the hash table, regardless of a valid node. */ static Py_ssize_t _odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash) @@ -949,12 +947,11 @@ PyDoc_STRVAR(odict_reduce__doc__, "Return state information for pickling"); static PyObject * odict_reduce(register PyODictObject *od, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(__dict__); PyObject *dict = NULL, *result = NULL; PyObject *items_iter, *items, *args = NULL; /* capture any instance state */ - dict = _PyObject_GetAttrId((PyObject *)od, &PyId___dict__); + dict = PyObject_GetAttr((PyObject *)od, &_Py_ID(__dict__)); if (dict == NULL) goto Done; else { @@ -973,7 +970,7 @@ odict_reduce(register PyODictObject *od, PyObject *Py_UNUSED(ignored)) if (args == NULL) goto Done; - items = _PyObject_CallMethodIdNoArgs((PyObject *)od, &PyId_items); + items = PyObject_CallMethodNoArgs((PyObject *)od, &_Py_ID(items)); if (items == NULL) goto Done; @@ -1431,8 +1428,8 @@ odict_repr(PyODictObject *self) } } else { - PyObject *items = _PyObject_CallMethodIdNoArgs((PyObject *)self, - &PyId_items); + PyObject *items = PyObject_CallMethodNoArgs( + (PyObject *)self, &_Py_ID(items)); if (items == NULL) goto Done; pieces = PySequence_List(items); @@ -1808,7 +1805,6 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling"); static PyObject * odictiter_reduce(odictiterobject *di, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); /* copy the iterator state */ odictiterobject tmp = *di; Py_XINCREF(tmp.di_odict); @@ -1821,7 +1817,7 @@ odictiter_reduce(odictiterobject *di, PyObject *Py_UNUSED(ignored)) if (list == NULL) { return NULL; } - return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list); + return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list); } static PyMethodDef odictiter_methods[] = { @@ -2217,9 +2213,8 @@ mutablemapping_update_arg(PyObject *self, PyObject *arg) Py_DECREF(items); return res; } - _Py_IDENTIFIER(keys); PyObject *func; - if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) { + if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) { return -1; } if (func != NULL) { @@ -2251,7 +2246,7 @@ mutablemapping_update_arg(PyObject *self, PyObject *arg) } return 0; } - if (_PyObject_LookupAttrId(arg, &PyId_items, &func) < 0) { + if (_PyObject_LookupAttr(arg, &_Py_ID(items), &func) < 0) { return -1; } if (func != NULL) { diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index a848d67..5d583b2 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -21,8 +21,6 @@ typedef struct { PyObject *length; } rangeobject; -_Py_IDENTIFIER(iter); - /* Helper function for validating step. Always returns a new reference or NULL on error. */ @@ -813,8 +811,8 @@ rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored)) if (range == NULL) goto err; /* return the result */ - return Py_BuildValue("N(N)l", _PyEval_GetBuiltinId(&PyId_iter), - range, r->index); + return Py_BuildValue( + "N(N)l", _PyEval_GetBuiltin(&_Py_ID(iter)), range, r->index); err: Py_XDECREF(start); Py_XDECREF(stop); @@ -967,8 +965,8 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored)) } /* return the result */ - return Py_BuildValue("N(N)O", _PyEval_GetBuiltinId(&PyId_iter), - range, r->index); + return Py_BuildValue( + "N(N)O", _PyEval_GetBuiltin(&_Py_ID(iter)), range, r->index); } static PyObject * diff --git a/Objects/setobject.c b/Objects/setobject.c index ca3cfe8..fe12494 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -770,7 +770,6 @@ static PyObject *setiter_iternext(setiterobject *si); static PyObject * setiter_reduce(setiterobject *si, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); /* copy the iterator state */ setiterobject tmp = *si; Py_XINCREF(tmp.si_set); @@ -781,7 +780,7 @@ setiter_reduce(setiterobject *si, PyObject *Py_UNUSED(ignored)) if (list == NULL) { return NULL; } - return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list); + return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list); } PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); @@ -1906,7 +1905,6 @@ static PyObject * set_reduce(PySetObject *so, PyObject *Py_UNUSED(ignored)) { PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; - _Py_IDENTIFIER(__dict__); keys = PySequence_List((PyObject *)so); if (keys == NULL) @@ -1914,7 +1912,7 @@ set_reduce(PySetObject *so, PyObject *Py_UNUSED(ignored)) args = PyTuple_Pack(1, keys); if (args == NULL) goto done; - if (_PyObject_LookupAttrId((PyObject *)so, &PyId___dict__, &dict) < 0) { + if (_PyObject_LookupAttr((PyObject *)so, &_Py_ID(__dict__), &dict) < 0) { goto done; } if (dict == NULL) { diff --git a/Objects/structseq.c b/Objects/structseq.c index 092e163..229e3d8 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -23,17 +23,9 @@ static const char match_args_key[] = "__match_args__"; They are only allowed for indices < n_visible_fields. */ const char * const PyStructSequence_UnnamedField = "unnamed field"; -_Py_IDENTIFIER(n_sequence_fields); -_Py_IDENTIFIER(n_fields); -_Py_IDENTIFIER(n_unnamed_fields); - static Py_ssize_t -get_type_attr_as_size(PyTypeObject *tp, _Py_Identifier *id) +get_type_attr_as_size(PyTypeObject *tp, PyObject *name) { - PyObject *name = _PyUnicode_FromId(id); - if (name == NULL) { - return -1; - } PyObject *v = PyDict_GetItemWithError(tp->tp_dict, name); if (v == NULL && !PyErr_Occurred()) { PyErr_Format(PyExc_TypeError, @@ -44,11 +36,14 @@ get_type_attr_as_size(PyTypeObject *tp, _Py_Identifier *id) } #define VISIBLE_SIZE(op) Py_SIZE(op) -#define VISIBLE_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_sequence_fields) -#define REAL_SIZE_TP(tp) get_type_attr_as_size(tp, &PyId_n_fields) +#define VISIBLE_SIZE_TP(tp) \ + get_type_attr_as_size(tp, &_Py_ID(n_sequence_fields)) +#define REAL_SIZE_TP(tp) \ + get_type_attr_as_size(tp, &_Py_ID(n_fields)) #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op)) -#define UNNAMED_FIELDS_TP(tp) get_type_attr_as_size(tp, &PyId_n_unnamed_fields) +#define UNNAMED_FIELDS_TP(tp) \ + get_type_attr_as_size(tp, &_Py_ID(n_unnamed_fields)) #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op)) @@ -622,21 +617,3 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc) { return _PyStructSequence_NewType(desc, 0); } - - -/* runtime lifecycle */ - -PyStatus _PyStructSequence_InitState(PyInterpreterState *interp) -{ - if (!_Py_IsMainInterpreter(interp)) { - return _PyStatus_OK(); - } - - if (_PyUnicode_FromId(&PyId_n_sequence_fields) == NULL - || _PyUnicode_FromId(&PyId_n_fields) == NULL - || _PyUnicode_FromId(&PyId_n_unnamed_fields) == NULL) - { - return _PyStatus_ERR("can't initialize structseq state"); - } - return _PyStatus_OK(); -} diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 86f541a..ec9147c 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -1193,12 +1193,11 @@ PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list( static PyObject * tupleiter_reduce(tupleiterobject *it, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); if (it->it_seq) - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); else - return Py_BuildValue("N(())", _PyEval_GetBuiltinId(&PyId_iter)); + return Py_BuildValue("N(())", _PyEval_GetBuiltin(&_Py_ID(iter))); } static PyObject * diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 0c893ea..ef3549c 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -59,30 +59,6 @@ typedef struct PySlot_Offset { # define INTERN_NAME_STRINGS #endif -/* alphabetical order */ -_Py_IDENTIFIER(__abstractmethods__); -_Py_IDENTIFIER(__annotations__); -_Py_IDENTIFIER(__class__); -_Py_IDENTIFIER(__class_getitem__); -_Py_IDENTIFIER(__classcell__); -_Py_IDENTIFIER(__delitem__); -_Py_IDENTIFIER(__dict__); -_Py_IDENTIFIER(__doc__); -_Py_IDENTIFIER(__getattribute__); -_Py_IDENTIFIER(__getitem__); -_Py_IDENTIFIER(__hash__); -_Py_IDENTIFIER(__init_subclass__); -_Py_IDENTIFIER(__len__); -_Py_IDENTIFIER(__module__); -_Py_IDENTIFIER(__name__); -_Py_IDENTIFIER(__new__); -_Py_IDENTIFIER(__qualname__); -_Py_IDENTIFIER(__set_name__); -_Py_IDENTIFIER(__setitem__); -_Py_IDENTIFIER(__weakref__); -_Py_IDENTIFIER(builtins); -_Py_IDENTIFIER(mro); - static PyObject * slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); @@ -90,7 +66,7 @@ static void clear_slotdefs(void); static PyObject * -lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound); +lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound); static int slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value); @@ -175,7 +151,7 @@ _PyType_CheckConsistency(PyTypeObject *type) if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) { CHECK(type->tp_new == NULL); - CHECK(_PyDict_ContainsId(type->tp_dict, &PyId___new__) == 0); + CHECK(PyDict_Contains(type->tp_dict, &_Py_ID(__new__)) == 0); } return 1; @@ -374,11 +350,11 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) { if (custom) { mro_meth = lookup_maybe_method( - (PyObject *)type, &PyId_mro, &unbound); + (PyObject *)type, &_Py_ID(mro), &unbound); if (mro_meth == NULL) goto clear; type_mro_meth = lookup_maybe_method( - (PyObject *)&PyType_Type, &PyId_mro, &unbound); + (PyObject *)&PyType_Type, &_Py_ID(mro), &unbound); if (type_mro_meth == NULL) goto clear; if (mro_meth != type_mro_meth) @@ -572,7 +548,7 @@ type_module(PyTypeObject *type, void *context) PyObject *mod; if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { - mod = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___module__); + mod = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__module__)); if (mod == NULL) { if (!PyErr_Occurred()) { PyErr_Format(PyExc_AttributeError, "__module__"); @@ -590,8 +566,8 @@ type_module(PyTypeObject *type, void *context) PyUnicode_InternInPlace(&mod); } else { - mod = _PyUnicode_FromId(&PyId_builtins); - Py_XINCREF(mod); + mod = &_Py_ID(builtins); + Py_INCREF(mod); } } return mod; @@ -605,7 +581,7 @@ type_set_module(PyTypeObject *type, PyObject *value, void *context) PyType_Modified(type); - return _PyDict_SetItemId(type->tp_dict, &PyId___module__, value); + return PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), value); } static PyObject * @@ -615,12 +591,11 @@ type_abstractmethods(PyTypeObject *type, void *context) /* type itself has an __abstractmethods__ descriptor (this). Don't return that. */ if (type != &PyType_Type) - mod = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___abstractmethods__); + mod = PyDict_GetItemWithError(type->tp_dict, + &_Py_ID(__abstractmethods__)); if (!mod) { if (!PyErr_Occurred()) { - PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); - if (message) - PyErr_SetObject(PyExc_AttributeError, message); + PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__)); } return NULL; } @@ -640,15 +615,13 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) abstract = PyObject_IsTrue(value); if (abstract < 0) return -1; - res = _PyDict_SetItemId(type->tp_dict, &PyId___abstractmethods__, value); + res = PyDict_SetItem(type->tp_dict, &_Py_ID(__abstractmethods__), value); } else { abstract = 0; - res = _PyDict_DelItemId(type->tp_dict, &PyId___abstractmethods__); + res = PyDict_DelItem(type->tp_dict, &_Py_ID(__abstractmethods__)); if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { - PyObject *message = _PyUnicode_FromId(&PyId___abstractmethods__); - if (message) - PyErr_SetObject(PyExc_AttributeError, message); + PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__)); return -1; } } @@ -902,7 +875,7 @@ type_get_doc(PyTypeObject *type, void *context) if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) { return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc); } - result = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___doc__); + result = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__)); if (result == NULL) { if (!PyErr_Occurred()) { result = Py_None; @@ -931,7 +904,7 @@ type_set_doc(PyTypeObject *type, PyObject *value, void *context) if (!check_set_special_type_attr(type, value, "__doc__")) return -1; PyType_Modified(type); - return _PyDict_SetItemId(type->tp_dict, &PyId___doc__, value); + return PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), value); } static PyObject * @@ -944,17 +917,18 @@ type_get_annotations(PyTypeObject *type, void *context) PyObject *annotations; /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */ - if (_PyDict_ContainsId(type->tp_dict, &PyId___annotations__)) { - annotations = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___annotations__); + if (PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) { + annotations = PyDict_GetItemWithError( + type->tp_dict, &_Py_ID(__annotations__)); /* - ** _PyDict_GetItemIdWithError could still fail, + ** PyDict_GetItemWithError could still fail, ** for instance with a well-timed Ctrl-C or a MemoryError. ** so let's be totally safe. */ if (annotations) { if (Py_TYPE(annotations)->tp_descr_get) { - annotations = Py_TYPE(annotations)->tp_descr_get(annotations, NULL, - (PyObject *)type); + annotations = Py_TYPE(annotations)->tp_descr_get( + annotations, NULL, (PyObject *)type); } else { Py_INCREF(annotations); } @@ -962,7 +936,8 @@ type_get_annotations(PyTypeObject *type, void *context) } else { annotations = PyDict_New(); if (annotations) { - int result = _PyDict_SetItemId(type->tp_dict, &PyId___annotations__, annotations); + int result = PyDict_SetItem( + type->tp_dict, &_Py_ID(__annotations__), annotations); if (result) { Py_CLEAR(annotations); } else { @@ -986,14 +961,14 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context) int result; if (value != NULL) { /* set */ - result = _PyDict_SetItemId(type->tp_dict, &PyId___annotations__, value); + result = PyDict_SetItem(type->tp_dict, &_Py_ID(__annotations__), value); } else { /* delete */ - if (!_PyDict_ContainsId(type->tp_dict, &PyId___annotations__)) { + if (!PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) { PyErr_Format(PyExc_AttributeError, "__annotations__"); return -1; } - result = _PyDict_DelItemId(type->tp_dict, &PyId___annotations__); + result = PyDict_DelItem(type->tp_dict, &_Py_ID(__annotations__)); } if (result == 0) { @@ -1074,7 +1049,7 @@ type_repr(PyTypeObject *type) return NULL; } - if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) + if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins))) rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); else rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); @@ -1604,11 +1579,11 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) */ PyObject * -_PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) +_PyObject_LookupSpecial(PyObject *self, PyObject *attr) { PyObject *res; - res = _PyType_LookupId(Py_TYPE(self), attrid); + res = _PyType_Lookup(Py_TYPE(self), attr); if (res != NULL) { descrgetfunc f; if ((f = Py_TYPE(res)->tp_descr_get) == NULL) @@ -1619,10 +1594,19 @@ _PyObject_LookupSpecial(PyObject *self, _Py_Identifier *attrid) return res; } +PyObject * +_PyObject_LookupSpecialId(PyObject *self, _Py_Identifier *attrid) +{ + PyObject *attr = _PyUnicode_FromId(attrid); /* borrowed */ + if (attr == NULL) + return NULL; + return _PyObject_LookupSpecial(self, attr); +} + static PyObject * -lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound) +lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound) { - PyObject *res = _PyType_LookupId(Py_TYPE(self), attrid); + PyObject *res = _PyType_Lookup(Py_TYPE(self), attr); if (res == NULL) { return NULL; } @@ -1646,11 +1630,11 @@ lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound) } static PyObject * -lookup_method(PyObject *self, _Py_Identifier *attrid, int *unbound) +lookup_method(PyObject *self, PyObject *attr, int *unbound) { - PyObject *res = lookup_maybe_method(self, attrid, unbound); + PyObject *res = lookup_maybe_method(self, attr, unbound); if (res == NULL && !PyErr_Occurred()) { - PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(attrid)); + PyErr_SetObject(PyExc_AttributeError, attr); } return res; } @@ -1687,8 +1671,7 @@ call_unbound_noarg(int unbound, PyObject *func, PyObject *self) args is an argument vector of length nargs. The first element in this vector is the special object "self" which is used for the method lookup */ static PyObject * -vectorcall_method(_Py_Identifier *name, - PyObject *const *args, Py_ssize_t nargs) +vectorcall_method(PyObject *name, PyObject *const *args, Py_ssize_t nargs) { assert(nargs >= 1); @@ -1707,7 +1690,7 @@ vectorcall_method(_Py_Identifier *name, /* Clone of vectorcall_method() that returns NotImplemented * when the lookup fails. */ static PyObject * -vectorcall_maybe(PyThreadState *tstate, _Py_Identifier *name, +vectorcall_maybe(PyThreadState *tstate, PyObject *name, PyObject *const *args, Py_ssize_t nargs) { assert(nargs >= 1); @@ -1770,7 +1753,7 @@ static PyObject * class_name(PyObject *cls) { PyObject *name; - if (_PyObject_LookupAttrId(cls, &PyId___name__, &name) == 0) { + if (_PyObject_LookupAttr(cls, &_Py_ID(__name__), &name) == 0) { name = PyObject_Repr(cls); } return name; @@ -2093,8 +2076,8 @@ mro_invoke(PyTypeObject *type) if (custom) { int unbound; - PyObject *mro_meth = lookup_method((PyObject *)type, &PyId_mro, - &unbound); + PyObject *mro_meth = lookup_method( + (PyObject *)type, &_Py_ID(mro), &unbound); if (mro_meth == NULL) return NULL; mro_result = call_unbound_noarg(unbound, mro_meth, (PyObject *)type); @@ -2309,7 +2292,7 @@ get_dict_descriptor(PyTypeObject *type) { PyObject *descr; - descr = _PyType_LookupId(type, &PyId___dict__); + descr = _PyType_Lookup(type, &_Py_ID(__dict__)); if (descr == NULL || !PyDescr_IsData(descr)) return NULL; @@ -2558,7 +2541,7 @@ type_new_visit_slots(type_new_ctx *ctx) return -1; } assert(PyUnicode_Check(name)); - if (_PyUnicode_EqualToASCIIId(name, &PyId___dict__)) { + if (_PyUnicode_Equal(name, &_Py_ID(__dict__))) { if (!ctx->may_add_dict || ctx->add_dict != 0) { PyErr_SetString(PyExc_TypeError, "__dict__ slot disallowed: " @@ -2567,7 +2550,7 @@ type_new_visit_slots(type_new_ctx *ctx) } ctx->add_dict++; } - if (_PyUnicode_EqualToASCIIId(name, &PyId___weakref__)) { + if (_PyUnicode_Equal(name, &_Py_ID(__weakref__))) { if (!ctx->may_add_weak || ctx->add_weak != 0) { PyErr_SetString(PyExc_TypeError, "__weakref__ slot disallowed: " @@ -2601,10 +2584,8 @@ type_new_copy_slots(type_new_ctx *ctx, PyObject *dict) Py_ssize_t j = 0; for (Py_ssize_t i = 0; i < nslot; i++) { PyObject *slot = PyTuple_GET_ITEM(slots, i); - if ((ctx->add_dict && - _PyUnicode_EqualToASCIIId(slot, &PyId___dict__)) || - (ctx->add_weak && - _PyUnicode_EqualToASCIIString(slot, "__weakref__"))) + if ((ctx->add_dict && _PyUnicode_Equal(slot, &_Py_ID(__dict__))) || + (ctx->add_weak && _PyUnicode_Equal(slot, &_Py_ID(__weakref__)))) { continue; } @@ -2623,8 +2604,8 @@ type_new_copy_slots(type_new_ctx *ctx, PyObject *dict) /* CPython inserts __qualname__ and __classcell__ (when needed) into the namespace when creating a class. They will be deleted below so won't act as class variables. */ - if (!_PyUnicode_EqualToASCIIId(slot, &PyId___qualname__) && - !_PyUnicode_EqualToASCIIId(slot, &PyId___classcell__)) + if (!_PyUnicode_Equal(slot, &_Py_ID(__qualname__)) && + !_PyUnicode_Equal(slot, &_Py_ID(__classcell__))) { PyErr_Format(PyExc_ValueError, "%R in __slots__ conflicts with class variable", @@ -2819,7 +2800,7 @@ type_new_set_name(const type_new_ctx *ctx, PyTypeObject *type) static int type_new_set_module(PyTypeObject *type) { - int r = _PyDict_ContainsId(type->tp_dict, &PyId___module__); + int r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__)); if (r < 0) { return -1; } @@ -2832,7 +2813,7 @@ type_new_set_module(PyTypeObject *type) return 0; } - PyObject *module = _PyDict_GetItemIdWithError(globals, &PyId___name__); + PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__)); if (module == NULL) { if (PyErr_Occurred()) { return -1; @@ -2840,7 +2821,7 @@ type_new_set_module(PyTypeObject *type) return 0; } - if (_PyDict_SetItemId(type->tp_dict, &PyId___module__, module) < 0) { + if (PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), module) < 0) { return -1; } return 0; @@ -2853,8 +2834,8 @@ static int type_new_set_ht_name(PyTypeObject *type) { PyHeapTypeObject *et = (PyHeapTypeObject *)type; - PyObject *qualname = _PyDict_GetItemIdWithError(type->tp_dict, - &PyId___qualname__); + PyObject *qualname = PyDict_GetItemWithError( + type->tp_dict, &_Py_ID(__qualname__)); if (qualname != NULL) { if (!PyUnicode_Check(qualname)) { PyErr_Format(PyExc_TypeError, @@ -2863,7 +2844,7 @@ type_new_set_ht_name(PyTypeObject *type) return -1; } et->ht_qualname = Py_NewRef(qualname); - if (_PyDict_DelItemId(type->tp_dict, &PyId___qualname__) < 0) { + if (PyDict_DelItem(type->tp_dict, &_Py_ID(__qualname__)) < 0) { return -1; } } @@ -2883,7 +2864,7 @@ type_new_set_ht_name(PyTypeObject *type) static int type_new_set_doc(PyTypeObject *type) { - PyObject *doc = _PyDict_GetItemIdWithError(type->tp_dict, &PyId___doc__); + PyObject *doc = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__)); if (doc == NULL) { if (PyErr_Occurred()) { return -1; @@ -2916,9 +2897,9 @@ type_new_set_doc(PyTypeObject *type) static int -type_new_staticmethod(PyTypeObject *type, _Py_Identifier *attr_id) +type_new_staticmethod(PyTypeObject *type, PyObject *attr) { - PyObject *func = _PyDict_GetItemIdWithError(type->tp_dict, attr_id); + PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr); if (func == NULL) { if (PyErr_Occurred()) { return -1; @@ -2933,7 +2914,7 @@ type_new_staticmethod(PyTypeObject *type, _Py_Identifier *attr_id) if (static_func == NULL) { return -1; } - if (_PyDict_SetItemId(type->tp_dict, attr_id, static_func) < 0) { + if (PyDict_SetItem(type->tp_dict, attr, static_func) < 0) { Py_DECREF(static_func); return -1; } @@ -2943,9 +2924,9 @@ type_new_staticmethod(PyTypeObject *type, _Py_Identifier *attr_id) static int -type_new_classmethod(PyTypeObject *type, _Py_Identifier *attr_id) +type_new_classmethod(PyTypeObject *type, PyObject *attr) { - PyObject *func = _PyDict_GetItemIdWithError(type->tp_dict, attr_id); + PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr); if (func == NULL) { if (PyErr_Occurred()) { return -1; @@ -2961,7 +2942,7 @@ type_new_classmethod(PyTypeObject *type, _Py_Identifier *attr_id) return -1; } - if (_PyDict_SetItemId(type->tp_dict, attr_id, method) < 0) { + if (PyDict_SetItem(type->tp_dict, attr, method) < 0) { Py_DECREF(method); return -1; } @@ -3052,8 +3033,8 @@ type_new_set_slots(const type_new_ctx *ctx, PyTypeObject *type) static int type_new_set_classcell(PyTypeObject *type) { - PyObject *cell = _PyDict_GetItemIdWithError(type->tp_dict, - &PyId___classcell__); + PyObject *cell = PyDict_GetItemWithError( + type->tp_dict, &_Py_ID(__classcell__)); if (cell == NULL) { if (PyErr_Occurred()) { return -1; @@ -3070,7 +3051,7 @@ type_new_set_classcell(PyTypeObject *type) } (void)PyCell_Set(cell, (PyObject *) type); - if (_PyDict_DelItemId(type->tp_dict, &PyId___classcell__) < 0) { + if (PyDict_DelItem(type->tp_dict, &_Py_ID(__classcell__)) < 0) { return -1; } return 0; @@ -3098,16 +3079,16 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type) /* Special-case __new__: if it's a plain function, make it a static function */ - if (type_new_staticmethod(type, &PyId___new__) < 0) { + if (type_new_staticmethod(type, &_Py_ID(__new__)) < 0) { return -1; } /* Special-case __init_subclass__ and __class_getitem__: if they are plain functions, make them classmethods */ - if (type_new_classmethod(type, &PyId___init_subclass__) < 0) { + if (type_new_classmethod(type, &_Py_ID(__init_subclass__)) < 0) { return -1; } - if (type_new_classmethod(type, &PyId___class_getitem__) < 0) { + if (type_new_classmethod(type, &_Py_ID(__class_getitem__)) < 0) { return -1; } @@ -3127,8 +3108,7 @@ type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type) static int type_new_get_slots(type_new_ctx *ctx, PyObject *dict) { - _Py_IDENTIFIER(__slots__); - PyObject *slots = _PyDict_GetItemIdWithError(dict, &PyId___slots__); + PyObject *slots = PyDict_GetItemWithError(dict, &_Py_ID(__slots__)); if (slots == NULL) { if (PyErr_Occurred()) { return -1; @@ -3251,15 +3231,14 @@ type_new_get_bases(type_new_ctx *ctx, PyObject **type) return 0; } - _Py_IDENTIFIER(__mro_entries__); for (Py_ssize_t i = 0; i < nbases; i++) { PyObject *base = PyTuple_GET_ITEM(ctx->bases, i); if (PyType_Check(base)) { continue; } PyObject *mro_entries; - if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, - &mro_entries) < 0) { + if (_PyObject_LookupAttr(base, &_Py_ID(__mro_entries__), + &mro_entries) < 0) { return -1; } if (mro_entries != NULL) { @@ -3608,7 +3587,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) PyObject *__doc__ = PyUnicode_FromString(_PyType_DocWithoutSignature(type->tp_name, type->tp_doc)); if (!__doc__) goto fail; - r = _PyDict_SetItemId(type->tp_dict, &PyId___doc__, __doc__); + r = PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), __doc__); Py_DECREF(__doc__); if (r < 0) goto fail; @@ -3626,7 +3605,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) } /* Set type.__module__ */ - r = _PyDict_ContainsId(type->tp_dict, &PyId___module__); + r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__)); if (r < 0) { goto fail; } @@ -3638,7 +3617,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) if (modname == NULL) { goto fail; } - r = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); + r = PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), modname); Py_DECREF(modname); if (r != 0) goto fail; @@ -4217,13 +4196,12 @@ merge_class_dict(PyObject *dict, PyObject *aclass) { PyObject *classdict; PyObject *bases; - _Py_IDENTIFIER(__bases__); assert(PyDict_Check(dict)); assert(aclass); /* Merge in the type's dict (if any). */ - if (_PyObject_LookupAttrId(aclass, &PyId___dict__, &classdict) < 0) { + if (_PyObject_LookupAttr(aclass, &_Py_ID(__dict__), &classdict) < 0) { return -1; } if (classdict != NULL) { @@ -4234,7 +4212,7 @@ merge_class_dict(PyObject *dict, PyObject *aclass) } /* Recursively merge in the base types' (if any) dicts. */ - if (_PyObject_LookupAttrId(aclass, &PyId___bases__, &bases) < 0) { + if (_PyObject_LookupAttr(aclass, &_Py_ID(__bases__), &bases) < 0) { return -1; } if (bases != NULL) { @@ -4557,8 +4535,6 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyObject *abstract_methods; PyObject *sorted_methods; PyObject *joined; - PyObject *comma; - _Py_static_string(comma_id, ", "); Py_ssize_t method_count; /* Compute ", ".join(sorted(type.__abstractmethods__)) @@ -4574,12 +4550,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(sorted_methods); return NULL; } - comma = _PyUnicode_FromId(&comma_id); - if (comma == NULL) { - Py_DECREF(sorted_methods); - return NULL; - } - joined = PyUnicode_Join(comma, sorted_methods); + joined = PyUnicode_Join(&_Py_STR(comma_sep), sorted_methods); method_count = PyObject_Length(sorted_methods); Py_DECREF(sorted_methods); if (joined == NULL) @@ -4632,7 +4603,7 @@ object_repr(PyObject *self) Py_XDECREF(mod); return NULL; } - if (mod != NULL && !_PyUnicode_EqualToASCIIId(mod, &PyId_builtins)) + if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins))) rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); else rtn = PyUnicode_FromFormat("<%s object at %p>", @@ -4925,27 +4896,19 @@ static PyGetSetDef object_getsets[] = { static PyObject * import_copyreg(void) { - PyObject *copyreg_str; - PyObject *copyreg_module; - _Py_IDENTIFIER(copyreg); - - copyreg_str = _PyUnicode_FromId(&PyId_copyreg); - if (copyreg_str == NULL) { - return NULL; - } /* Try to fetch cached copy of copyreg from sys.modules first in an attempt to avoid the import overhead. Previously this was implemented by storing a reference to the cached module in a static variable, but this broke when multiple embedded interpreters were in use (see issue #17408 and #19088). */ - copyreg_module = PyImport_GetModule(copyreg_str); + PyObject *copyreg_module = PyImport_GetModule(&_Py_ID(copyreg)); if (copyreg_module != NULL) { return copyreg_module; } if (PyErr_Occurred()) { return NULL; } - return PyImport_Import(copyreg_str); + return PyImport_Import(&_Py_ID(copyreg)); } static PyObject * @@ -4953,13 +4916,11 @@ _PyType_GetSlotNames(PyTypeObject *cls) { PyObject *copyreg; PyObject *slotnames; - _Py_IDENTIFIER(__slotnames__); - _Py_IDENTIFIER(_slotnames); assert(PyType_Check(cls)); /* Get the slot names from the cache in the class if possible. */ - slotnames = _PyDict_GetItemIdWithError(cls->tp_dict, &PyId___slotnames__); + slotnames = PyDict_GetItemWithError(cls->tp_dict, &_Py_ID(__slotnames__)); if (slotnames != NULL) { if (slotnames != Py_None && !PyList_Check(slotnames)) { PyErr_Format(PyExc_TypeError, @@ -4985,8 +4946,8 @@ _PyType_GetSlotNames(PyTypeObject *cls) /* Use _slotnames function from the copyreg module to find the slots by this class and its bases. This function will cache the result in __slotnames__. */ - slotnames = _PyObject_CallMethodIdOneArg(copyreg, &PyId__slotnames, - (PyObject *)cls); + slotnames = PyObject_CallMethodOneArg( + copyreg, &_Py_ID(_slotnames), (PyObject *)cls); Py_DECREF(copyreg); if (slotnames == NULL) return NULL; @@ -5006,9 +4967,8 @@ _PyObject_GetState(PyObject *obj, int required) { PyObject *state; PyObject *getstate; - _Py_IDENTIFIER(__getstate__); - if (_PyObject_LookupAttrId(obj, &PyId___getstate__, &getstate) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__getstate__), &getstate) < 0) { return NULL; } if (getstate == NULL) { @@ -5144,8 +5104,6 @@ static int _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) { PyObject *getnewargs, *getnewargs_ex; - _Py_IDENTIFIER(__getnewargs_ex__); - _Py_IDENTIFIER(__getnewargs__); if (args == NULL || kwargs == NULL) { PyErr_BadInternalCall(); @@ -5154,7 +5112,7 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) /* We first attempt to fetch the arguments for __new__ by calling __getnewargs_ex__ on the object. */ - getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__); + getnewargs_ex = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs_ex__)); if (getnewargs_ex != NULL) { PyObject *newargs = _PyObject_CallNoArgs(getnewargs_ex); Py_DECREF(getnewargs_ex); @@ -5207,7 +5165,7 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs) /* The object does not have __getnewargs_ex__ so we fallback on using __getnewargs__ instead. */ - getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__); + getnewargs = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs__)); if (getnewargs != NULL) { *args = _PyObject_CallNoArgs(getnewargs); Py_DECREF(getnewargs); @@ -5260,10 +5218,7 @@ _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, Py_INCREF(*dictitems); } else { - PyObject *items; - _Py_IDENTIFIER(items); - - items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items); + PyObject *items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items)); if (items == NULL) { Py_CLEAR(*listitems); return -1; @@ -5307,12 +5262,11 @@ reduce_newobj(PyObject *obj) } hasargs = (args != NULL); if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) { - _Py_IDENTIFIER(__newobj__); PyObject *cls; Py_ssize_t i, n; Py_XDECREF(kwargs); - newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj__); + newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj__)); Py_DECREF(copyreg); if (newobj == NULL) { Py_XDECREF(args); @@ -5336,9 +5290,7 @@ reduce_newobj(PyObject *obj) Py_XDECREF(args); } else if (args != NULL) { - _Py_IDENTIFIER(__newobj_ex__); - - newobj = _PyObject_GetAttrId(copyreg, &PyId___newobj_ex__); + newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj_ex__)); Py_DECREF(copyreg); if (newobj == NULL) { Py_DECREF(args); @@ -5444,17 +5396,16 @@ object___reduce_ex___impl(PyObject *self, int protocol) { static PyObject *objreduce; PyObject *reduce, *res; - _Py_IDENTIFIER(__reduce__); if (objreduce == NULL) { - objreduce = _PyDict_GetItemIdWithError(PyBaseObject_Type.tp_dict, - &PyId___reduce__); + objreduce = PyDict_GetItemWithError( + PyBaseObject_Type.tp_dict, &_Py_ID(__reduce__)); if (objreduce == NULL && PyErr_Occurred()) { return NULL; } } - if (_PyObject_LookupAttrId(self, &PyId___reduce__, &reduce) < 0) { + if (_PyObject_LookupAttr(self, &_Py_ID(__reduce__), &reduce) < 0) { return NULL; } if (reduce != NULL) { @@ -5462,7 +5413,7 @@ object___reduce_ex___impl(PyObject *self, int protocol) int override; cls = (PyObject *) Py_TYPE(self); - clsreduce = _PyObject_GetAttrId(cls, &PyId___reduce__); + clsreduce = PyObject_GetAttr(cls, &_Py_ID(__reduce__)); if (clsreduce == NULL) { Py_DECREF(reduce); return NULL; @@ -5570,7 +5521,7 @@ object___dir___impl(PyObject *self) PyObject *itsclass = NULL; /* Get __dict__ (which may or may not be a real dict...) */ - if (_PyObject_LookupAttrId(self, &PyId___dict__, &dict) < 0) { + if (_PyObject_LookupAttr(self, &_Py_ID(__dict__), &dict) < 0) { return NULL; } if (dict == NULL) { @@ -5591,7 +5542,7 @@ object___dir___impl(PyObject *self) goto error; /* Merge in attrs reachable from its class. */ - if (_PyObject_LookupAttrId(self, &PyId___class__, &itsclass) < 0) { + if (_PyObject_LookupAttr(self, &_Py_ID(__class__), &itsclass) < 0) { goto error; } /* XXX(tomer): Perhaps fall back to Py_TYPE(obj) if no @@ -5857,12 +5808,11 @@ static int overrides_hash(PyTypeObject *type) { PyObject *dict = type->tp_dict; - _Py_IDENTIFIER(__eq__); assert(dict != NULL); - int r = _PyDict_ContainsId(dict, &PyId___eq__); + int r = PyDict_Contains(dict, &_Py_ID(__eq__)); if (r == 0) { - r = _PyDict_ContainsId(dict, &PyId___hash__); + r = PyDict_Contains(dict, &_Py_ID(__hash__)); } return r; } @@ -6177,7 +6127,7 @@ type_ready_set_dict(PyTypeObject *type) static int type_dict_set_doc(PyTypeObject *type) { - int r = _PyDict_ContainsId(type->tp_dict, &PyId___doc__); + int r = PyDict_Contains(type->tp_dict, &_Py_ID(__doc__)); if (r < 0) { return -1; } @@ -6193,14 +6143,14 @@ type_dict_set_doc(PyTypeObject *type) return -1; } - if (_PyDict_SetItemId(type->tp_dict, &PyId___doc__, doc) < 0) { + if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), doc) < 0) { Py_DECREF(doc); return -1; } Py_DECREF(doc); } else { - if (_PyDict_SetItemId(type->tp_dict, &PyId___doc__, Py_None) < 0) { + if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), Py_None) < 0) { return -1; } } @@ -6348,7 +6298,7 @@ type_ready_set_hash(PyTypeObject *type) return 0; } - int r = _PyDict_ContainsId(type->tp_dict, &PyId___hash__); + int r = PyDict_Contains(type->tp_dict, &_Py_ID(__hash__)); if (r < 0) { return -1; } @@ -6356,7 +6306,7 @@ type_ready_set_hash(PyTypeObject *type) return 0; } - if (_PyDict_SetItemId(type->tp_dict, &PyId___hash__, Py_None) < 0) { + if (PyDict_SetItem(type->tp_dict, &_Py_ID(__hash__), Py_None) < 0) { return -1; } type->tp_hash = PyObject_HashNotImplemented; @@ -7173,7 +7123,7 @@ static struct PyMethodDef tp_new_methoddef[] = { static int add_tp_new_wrapper(PyTypeObject *type) { - int r = _PyDict_ContainsId(type->tp_dict, &PyId___new__); + int r = PyDict_Contains(type->tp_dict, &_Py_ID(__new__)); if (r > 0) { return 0; } @@ -7185,7 +7135,7 @@ add_tp_new_wrapper(PyTypeObject *type) if (func == NULL) { return -1; } - r = _PyDict_SetItemId(type->tp_dict, &PyId___new__, func); + r = PyDict_SetItem(type->tp_dict, &_Py_ID(__new__), func); Py_DECREF(func); return r; } @@ -7193,33 +7143,31 @@ add_tp_new_wrapper(PyTypeObject *type) /* Slot wrappers that call the corresponding __foo__ slot. See comments below at override_slots() for more explanation. */ -#define SLOT0(FUNCNAME, OPSTR) \ +#define SLOT0(FUNCNAME, DUNDER) \ static PyObject * \ FUNCNAME(PyObject *self) \ { \ PyObject* stack[1] = {self}; \ - _Py_static_string(id, OPSTR); \ - return vectorcall_method(&id, stack, 1); \ + return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \ } -#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE) \ +#define SLOT1(FUNCNAME, DUNDER, ARG1TYPE) \ static PyObject * \ FUNCNAME(PyObject *self, ARG1TYPE arg1) \ { \ PyObject* stack[2] = {self, arg1}; \ - _Py_static_string(id, OPSTR); \ - return vectorcall_method(&id, stack, 2); \ + return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \ } /* Boolean helper for SLOT1BINFULL(). right.__class__ is a nontrivial subclass of left.__class__. */ static int -method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *name) +method_is_overloaded(PyObject *left, PyObject *right, PyObject *name) { PyObject *a, *b; int ok; - if (_PyObject_LookupAttrId((PyObject *)(Py_TYPE(right)), name, &b) < 0) { + if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(right)), name, &b) < 0) { return -1; } if (b == NULL) { @@ -7227,7 +7175,7 @@ method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *nam return 0; } - if (_PyObject_LookupAttrId((PyObject *)(Py_TYPE(left)), name, &a) < 0) { + if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(left)), name, &a) < 0) { Py_DECREF(b); return -1; } @@ -7244,14 +7192,12 @@ method_is_overloaded(PyObject *left, PyObject *right, struct _Py_Identifier *nam } -#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ +#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, DUNDER, RDUNDER) \ static PyObject * \ FUNCNAME(PyObject *self, PyObject *other) \ { \ PyObject* stack[2]; \ PyThreadState *tstate = _PyThreadState_GET(); \ - _Py_static_string(op_id, OPSTR); \ - _Py_static_string(rop_id, ROPSTR); \ int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \ Py_TYPE(other)->tp_as_number != NULL && \ Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ @@ -7259,14 +7205,14 @@ FUNCNAME(PyObject *self, PyObject *other) \ Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ PyObject *r; \ if (do_other && PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { \ - int ok = method_is_overloaded(self, other, &rop_id); \ + int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \ if (ok < 0) { \ return NULL; \ } \ if (ok) { \ stack[0] = other; \ stack[1] = self; \ - r = vectorcall_maybe(tstate, &rop_id, stack, 2); \ + r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ if (r != Py_NotImplemented) \ return r; \ Py_DECREF(r); \ @@ -7275,7 +7221,7 @@ FUNCNAME(PyObject *self, PyObject *other) \ } \ stack[0] = self; \ stack[1] = other; \ - r = vectorcall_maybe(tstate, &op_id, stack, 2); \ + r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \ if (r != Py_NotImplemented || \ Py_IS_TYPE(other, Py_TYPE(self))) \ return r; \ @@ -7284,19 +7230,19 @@ FUNCNAME(PyObject *self, PyObject *other) \ if (do_other) { \ stack[0] = other; \ stack[1] = self; \ - return vectorcall_maybe(tstate, &rop_id, stack, 2); \ + return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ } \ Py_RETURN_NOTIMPLEMENTED; \ } -#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ - SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) +#define SLOT1BIN(FUNCNAME, SLOTNAME, DUNDER, RDUNDER) \ + SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, DUNDER, RDUNDER) static Py_ssize_t slot_sq_length(PyObject *self) { PyObject* stack[1] = {self}; - PyObject *res = vectorcall_method(&PyId___len__, stack, 1); + PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1); Py_ssize_t len; if (res == NULL) @@ -7328,7 +7274,7 @@ slot_sq_item(PyObject *self, Py_ssize_t i) return NULL; } PyObject *stack[2] = {self, ival}; - PyObject *retval = vectorcall_method(&PyId___getitem__, stack, 2); + PyObject *retval = vectorcall_method(&_Py_ID(__getitem__), stack, 2); Py_DECREF(ival); return retval; } @@ -7348,11 +7294,11 @@ slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) stack[0] = self; stack[1] = index_obj; if (value == NULL) { - res = vectorcall_method(&PyId___delitem__, stack, 2); + res = vectorcall_method(&_Py_ID(__delitem__), stack, 2); } else { stack[2] = value; - res = vectorcall_method(&PyId___setitem__, stack, 3); + res = vectorcall_method(&_Py_ID(__setitem__), stack, 3); } Py_DECREF(index_obj); @@ -7369,9 +7315,8 @@ slot_sq_contains(PyObject *self, PyObject *value) PyThreadState *tstate = _PyThreadState_GET(); PyObject *func, *res; int result = -1, unbound; - _Py_IDENTIFIER(__contains__); - func = lookup_maybe_method(self, &PyId___contains__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__contains__), &unbound); if (func == Py_None) { Py_DECREF(func); PyErr_Format(PyExc_TypeError, @@ -7398,7 +7343,7 @@ slot_sq_contains(PyObject *self, PyObject *value) #define slot_mp_length slot_sq_length -SLOT1(slot_mp_subscript, "__getitem__", PyObject *) +SLOT1(slot_mp_subscript, __getitem__, PyObject *) static int slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) @@ -7409,11 +7354,11 @@ slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) stack[0] = self; stack[1] = key; if (value == NULL) { - res = vectorcall_method(&PyId___delitem__, stack, 2); + res = vectorcall_method(&_Py_ID(__delitem__), stack, 2); } else { stack[2] = value; - res = vectorcall_method(&PyId___setitem__, stack, 3); + res = vectorcall_method(&_Py_ID(__setitem__), stack, 3); } if (res == NULL) @@ -7422,23 +7367,20 @@ slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) return 0; } -SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") -SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") -SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") -SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, "__matmul__", "__rmatmul__") -SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") -SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") +SLOT1BIN(slot_nb_add, nb_add, __add__, __radd__) +SLOT1BIN(slot_nb_subtract, nb_subtract, __sub__, __rsub__) +SLOT1BIN(slot_nb_multiply, nb_multiply, __mul__, __rmul__) +SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, __matmul__, __rmatmul__) +SLOT1BIN(slot_nb_remainder, nb_remainder, __mod__, __rmod__) +SLOT1BIN(slot_nb_divmod, nb_divmod, __divmod__, __rdivmod__) static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); -SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, - nb_power, "__pow__", "__rpow__") +SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, nb_power, __pow__, __rpow__) static PyObject * slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) { - _Py_IDENTIFIER(__pow__); - if (modulus == Py_None) return slot_nb_power_binary(self, other); /* Three-arg power doesn't use __rpow__. But ternary_op @@ -7447,14 +7389,14 @@ slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) if (Py_TYPE(self)->tp_as_number != NULL && Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { PyObject* stack[3] = {self, other, modulus}; - return vectorcall_method(&PyId___pow__, stack, 3); + return vectorcall_method(&_Py_ID(__pow__), stack, 3); } Py_RETURN_NOTIMPLEMENTED; } -SLOT0(slot_nb_negative, "__neg__") -SLOT0(slot_nb_positive, "__pos__") -SLOT0(slot_nb_absolute, "__abs__") +SLOT0(slot_nb_negative, __neg__) +SLOT0(slot_nb_positive, __pos__) +SLOT0(slot_nb_absolute, __abs__) static int slot_nb_bool(PyObject *self) @@ -7462,15 +7404,14 @@ slot_nb_bool(PyObject *self) PyObject *func, *value; int result, unbound; int using_len = 0; - _Py_IDENTIFIER(__bool__); - func = lookup_maybe_method(self, &PyId___bool__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__bool__), &unbound); if (func == NULL) { if (PyErr_Occurred()) { return -1; } - func = lookup_maybe_method(self, &PyId___len__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__len__), &unbound); if (func == NULL) { if (PyErr_Occurred()) { return -1; @@ -7513,53 +7454,50 @@ error: static PyObject * slot_nb_index(PyObject *self) { - _Py_IDENTIFIER(__index__); PyObject *stack[1] = {self}; - return vectorcall_method(&PyId___index__, stack, 1); + return vectorcall_method(&_Py_ID(__index__), stack, 1); } -SLOT0(slot_nb_invert, "__invert__") -SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") -SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") -SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") -SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") -SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") +SLOT0(slot_nb_invert, __invert__) +SLOT1BIN(slot_nb_lshift, nb_lshift, __lshift__, __rlshift__) +SLOT1BIN(slot_nb_rshift, nb_rshift, __rshift__, __rrshift__) +SLOT1BIN(slot_nb_and, nb_and, __and__, __rand__) +SLOT1BIN(slot_nb_xor, nb_xor, __xor__, __rxor__) +SLOT1BIN(slot_nb_or, nb_or, __or__, __ror__) -SLOT0(slot_nb_int, "__int__") -SLOT0(slot_nb_float, "__float__") -SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *) -SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *) -SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *) -SLOT1(slot_nb_inplace_matrix_multiply, "__imatmul__", PyObject *) -SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *) +SLOT0(slot_nb_int, __int__) +SLOT0(slot_nb_float, __float__) +SLOT1(slot_nb_inplace_add, __iadd__, PyObject *) +SLOT1(slot_nb_inplace_subtract, __isub__, PyObject *) +SLOT1(slot_nb_inplace_multiply, __imul__, PyObject *) +SLOT1(slot_nb_inplace_matrix_multiply, __imatmul__, PyObject *) +SLOT1(slot_nb_inplace_remainder, __imod__, PyObject *) /* Can't use SLOT1 here, because nb_inplace_power is ternary */ static PyObject * slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) { PyObject *stack[2] = {self, arg1}; - _Py_IDENTIFIER(__ipow__); - return vectorcall_method(&PyId___ipow__, stack, 2); -} -SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *) -SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *) -SLOT1(slot_nb_inplace_and, "__iand__", PyObject *) -SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *) -SLOT1(slot_nb_inplace_or, "__ior__", PyObject *) + return vectorcall_method(&_Py_ID(__ipow__), stack, 2); +} +SLOT1(slot_nb_inplace_lshift, __ilshift__, PyObject *) +SLOT1(slot_nb_inplace_rshift, __irshift__, PyObject *) +SLOT1(slot_nb_inplace_and, __iand__, PyObject *) +SLOT1(slot_nb_inplace_xor, __ixor__, PyObject *) +SLOT1(slot_nb_inplace_or, __ior__, PyObject *) SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, - "__floordiv__", "__rfloordiv__") -SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") -SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *) -SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *) + __floordiv__, __rfloordiv__) +SLOT1BIN(slot_nb_true_divide, nb_true_divide, __truediv__, __rtruediv__) +SLOT1(slot_nb_inplace_floor_divide, __ifloordiv__, PyObject *) +SLOT1(slot_nb_inplace_true_divide, __itruediv__, PyObject *) static PyObject * slot_tp_repr(PyObject *self) { PyObject *func, *res; - _Py_IDENTIFIER(__repr__); int unbound; - func = lookup_maybe_method(self, &PyId___repr__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__repr__), &unbound); if (func != NULL) { res = call_unbound_noarg(unbound, func, self); Py_DECREF(func); @@ -7570,7 +7508,7 @@ slot_tp_repr(PyObject *self) Py_TYPE(self)->tp_name, self); } -SLOT0(slot_tp_str, "__str__") +SLOT0(slot_tp_str, __str__) static Py_hash_t slot_tp_hash(PyObject *self) @@ -7579,7 +7517,7 @@ slot_tp_hash(PyObject *self) Py_ssize_t h; int unbound; - func = lookup_maybe_method(self, &PyId___hash__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound); if (func == Py_None) { Py_DECREF(func); @@ -7624,10 +7562,9 @@ static PyObject * slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) { PyThreadState *tstate = _PyThreadState_GET(); - _Py_IDENTIFIER(__call__); int unbound; - PyObject *meth = lookup_method(self, &PyId___call__, &unbound); + PyObject *meth = lookup_method(self, &_Py_ID(__call__), &unbound); if (meth == NULL) { return NULL; } @@ -7659,7 +7596,7 @@ static PyObject * slot_tp_getattro(PyObject *self, PyObject *name) { PyObject *stack[2] = {self, name}; - return vectorcall_method(&PyId___getattribute__, stack, 2); + return vectorcall_method(&_Py_ID(__getattribute__), stack, 2); } static PyObject * @@ -7685,14 +7622,13 @@ slot_tp_getattr_hook(PyObject *self, PyObject *name) { PyTypeObject *tp = Py_TYPE(self); PyObject *getattr, *getattribute, *res; - _Py_IDENTIFIER(__getattr__); /* speed hack: we could use lookup_maybe, but that would resolve the method fully for each attribute lookup for classes with __getattr__, even when the attribute is present. So we use _PyType_Lookup and create the method only when needed, with call_attribute. */ - getattr = _PyType_LookupId(tp, &PyId___getattr__); + getattr = _PyType_Lookup(tp, &_Py_ID(__getattr__)); if (getattr == NULL) { /* No __getattr__ hook: use a simpler dispatcher */ tp->tp_getattro = slot_tp_getattro; @@ -7704,7 +7640,7 @@ slot_tp_getattr_hook(PyObject *self, PyObject *name) __getattr__, even when self has the default __getattribute__ method. So we use _PyType_Lookup and create the method only when needed, with call_attribute. */ - getattribute = _PyType_LookupId(tp, &PyId___getattribute__); + getattribute = _PyType_Lookup(tp, &_Py_ID(__getattribute__)); if (getattribute == NULL || (Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) && ((PyWrapperDescrObject *)getattribute)->d_wrapped == @@ -7728,17 +7664,15 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) { PyObject *stack[3]; PyObject *res; - _Py_IDENTIFIER(__delattr__); - _Py_IDENTIFIER(__setattr__); stack[0] = self; stack[1] = name; if (value == NULL) { - res = vectorcall_method(&PyId___delattr__, stack, 2); + res = vectorcall_method(&_Py_ID(__delattr__), stack, 2); } else { stack[2] = value; - res = vectorcall_method(&PyId___setattr__, stack, 3); + res = vectorcall_method(&_Py_ID(__setattr__), stack, 3); } if (res == NULL) return -1; @@ -7746,13 +7680,13 @@ slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) return 0; } -static _Py_Identifier name_op[] = { - _Py_static_string_init("__lt__"), - _Py_static_string_init("__le__"), - _Py_static_string_init("__eq__"), - _Py_static_string_init("__ne__"), - _Py_static_string_init("__gt__"), - _Py_static_string_init("__ge__"), +static PyObject *name_op[] = { + &_Py_ID(__lt__), + &_Py_ID(__le__), + &_Py_ID(__eq__), + &_Py_ID(__ne__), + &_Py_ID(__gt__), + &_Py_ID(__ge__), }; static PyObject * @@ -7761,7 +7695,7 @@ slot_tp_richcompare(PyObject *self, PyObject *other, int op) PyThreadState *tstate = _PyThreadState_GET(); int unbound; - PyObject *func = lookup_maybe_method(self, &name_op[op], &unbound); + PyObject *func = lookup_maybe_method(self, name_op[op], &unbound); if (func == NULL) { PyErr_Clear(); Py_RETURN_NOTIMPLEMENTED; @@ -7778,9 +7712,8 @@ slot_tp_iter(PyObject *self) { int unbound; PyObject *func, *res; - _Py_IDENTIFIER(__iter__); - func = lookup_maybe_method(self, &PyId___iter__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__iter__), &unbound); if (func == Py_None) { Py_DECREF(func); PyErr_Format(PyExc_TypeError, @@ -7796,7 +7729,7 @@ slot_tp_iter(PyObject *self) } PyErr_Clear(); - func = lookup_maybe_method(self, &PyId___getitem__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__getitem__), &unbound); if (func == NULL) { PyErr_Format(PyExc_TypeError, "'%.200s' object is not iterable", @@ -7810,9 +7743,8 @@ slot_tp_iter(PyObject *self) static PyObject * slot_tp_iternext(PyObject *self) { - _Py_IDENTIFIER(__next__); PyObject *stack[1] = {self}; - return vectorcall_method(&PyId___next__, stack, 1); + return vectorcall_method(&_Py_ID(__next__), stack, 1); } static PyObject * @@ -7820,9 +7752,8 @@ slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) { PyTypeObject *tp = Py_TYPE(self); PyObject *get; - _Py_IDENTIFIER(__get__); - get = _PyType_LookupId(tp, &PyId___get__); + get = _PyType_Lookup(tp, &_Py_ID(__get__)); if (get == NULL) { /* Avoid further slowdowns */ if (tp->tp_descr_get == slot_tp_descr_get) @@ -7842,17 +7773,15 @@ slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) { PyObject* stack[3]; PyObject *res; - _Py_IDENTIFIER(__delete__); - _Py_IDENTIFIER(__set__); stack[0] = self; stack[1] = target; if (value == NULL) { - res = vectorcall_method(&PyId___delete__, stack, 2); + res = vectorcall_method(&_Py_ID(__delete__), stack, 2); } else { stack[2] = value; - res = vectorcall_method(&PyId___set__, stack, 3); + res = vectorcall_method(&_Py_ID(__set__), stack, 3); } if (res == NULL) return -1; @@ -7865,9 +7794,8 @@ slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) { PyThreadState *tstate = _PyThreadState_GET(); - _Py_IDENTIFIER(__init__); int unbound; - PyObject *meth = lookup_method(self, &PyId___init__, &unbound); + PyObject *meth = lookup_method(self, &_Py_ID(__init__), &unbound); if (meth == NULL) { return -1; } @@ -7899,7 +7827,7 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) PyThreadState *tstate = _PyThreadState_GET(); PyObject *func, *result; - func = _PyObject_GetAttrId((PyObject *)type, &PyId___new__); + func = PyObject_GetAttr((PyObject *)type, &_Py_ID(__new__)); if (func == NULL) { return NULL; } @@ -7912,7 +7840,6 @@ slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static void slot_tp_finalize(PyObject *self) { - _Py_IDENTIFIER(__del__); int unbound; PyObject *del, *res; PyObject *error_type, *error_value, *error_traceback; @@ -7921,7 +7848,7 @@ slot_tp_finalize(PyObject *self) PyErr_Fetch(&error_type, &error_value, &error_traceback); /* Execute __del__ method, if any. */ - del = lookup_maybe_method(self, &PyId___del__, &unbound); + del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound); if (del != NULL) { res = call_unbound_noarg(unbound, del, self); if (res == NULL) @@ -7940,9 +7867,8 @@ slot_am_await(PyObject *self) { int unbound; PyObject *func, *res; - _Py_IDENTIFIER(__await__); - func = lookup_maybe_method(self, &PyId___await__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__await__), &unbound); if (func != NULL) { res = call_unbound_noarg(unbound, func, self); Py_DECREF(func); @@ -7959,9 +7885,8 @@ slot_am_aiter(PyObject *self) { int unbound; PyObject *func, *res; - _Py_IDENTIFIER(__aiter__); - func = lookup_maybe_method(self, &PyId___aiter__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__aiter__), &unbound); if (func != NULL) { res = call_unbound_noarg(unbound, func, self); Py_DECREF(func); @@ -7978,9 +7903,8 @@ slot_am_anext(PyObject *self) { int unbound; PyObject *func, *res; - _Py_IDENTIFIER(__anext__); - func = lookup_maybe_method(self, &PyId___anext__, &unbound); + func = lookup_maybe_method(self, &_Py_ID(__anext__), &unbound); if (func != NULL) { res = call_unbound_noarg(unbound, func, self); Py_DECREF(func); @@ -8612,7 +8536,8 @@ type_new_set_names(PyTypeObject *type) Py_ssize_t i = 0; PyObject *key, *value; while (PyDict_Next(names_to_set, &i, &key, &value)) { - PyObject *set_name = _PyObject_LookupSpecial(value, &PyId___set_name__); + PyObject *set_name = _PyObject_LookupSpecial(value, + &_Py_ID(__set_name__)); if (set_name == NULL) { if (PyErr_Occurred()) { goto error; @@ -8652,7 +8577,7 @@ type_new_init_subclass(PyTypeObject *type, PyObject *kwds) return -1; } - PyObject *func = _PyObject_GetAttrId(super, &PyId___init_subclass__); + PyObject *func = PyObject_GetAttr(super, &_Py_ID(__init_subclass__)); Py_DECREF(super); if (func == NULL) { return -1; @@ -8862,7 +8787,7 @@ super_getattro(PyObject *self, PyObject *name) (i.e. super, or a subclass), not the class of su->obj. */ if (PyUnicode_Check(name) && PyUnicode_GET_LENGTH(name) == 9 && - _PyUnicode_EqualToASCIIId(name, &PyId___class__)) + _PyUnicode_Equal(name, &_Py_ID(__class__))) goto skip; mro = starttype->tp_mro; @@ -8954,7 +8879,7 @@ supercheck(PyTypeObject *type, PyObject *obj) /* Try the slow way */ PyObject *class_attr; - if (_PyObject_LookupAttrId(obj, &PyId___class__, &class_attr) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &class_attr) < 0) { return NULL; } if (class_attr != NULL && @@ -9045,7 +8970,7 @@ super_init_without_args(InterpreterFrame *cframe, PyCodeObject *co, assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0); PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i); assert(PyUnicode_Check(name)); - if (_PyUnicode_EqualToASCIIId(name, &PyId___class__)) { + if (_PyUnicode_Equal(name, &_Py_ID(__class__))) { PyObject *cell = _PyFrame_GetLocalsArray(cframe)[i]; if (cell == NULL || !PyCell_Check(cell)) { PyErr_SetString(PyExc_RuntimeError, diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 888871a..908ad51 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -260,11 +260,7 @@ get_unicode_state(void) // Return a borrowed reference to the empty string singleton. static inline PyObject* unicode_get_empty(void) { - struct _Py_unicode_state *state = get_unicode_state(); - // unicode_get_empty() must not be called before _PyUnicode_Init() - // or after _PyUnicode_Fini() - assert(state->empty_string != NULL); - return state->empty_string; + return &_Py_STR(empty); } @@ -1388,25 +1384,6 @@ _PyUnicode_Dump(PyObject *op) } #endif -static int -unicode_create_empty_string_singleton(struct _Py_unicode_state *state) -{ - // Use size=1 rather than size=0, so PyUnicode_New(0, maxchar) can be - // optimized to always use state->empty_string without having to check if - // it is NULL or not. - PyObject *empty = PyUnicode_New(1, 0); - if (empty == NULL) { - return -1; - } - PyUnicode_1BYTE_DATA(empty)[0] = 0; - _PyUnicode_LENGTH(empty) = 0; - assert(_PyUnicode_CheckConsistency(empty, 1)); - - assert(state->empty_string == NULL); - state->empty_string = empty; - return 0; -} - PyObject * PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) @@ -2009,10 +1986,11 @@ unicode_dealloc(PyObject *unicode) static int unicode_is_singleton(PyObject *unicode) { - struct _Py_unicode_state *state = get_unicode_state(); - if (unicode == state->empty_string) { + if (unicode == &_Py_STR(empty)) { return 1; } + + struct _Py_unicode_state *state = get_unicode_state(); PyASCIIObject *ascii = (PyASCIIObject *)unicode; if (ascii->state.kind != PyUnicode_WCHAR_KIND && ascii->length == 1) { Py_UCS4 ch = PyUnicode_READ_CHAR(unicode, 0); @@ -15551,11 +15529,14 @@ _PyUnicode_InitState(PyInterpreterState *interp) PyStatus _PyUnicode_InitGlobalObjects(PyInterpreterState *interp) { - struct _Py_unicode_state *state = &interp->unicode; - if (unicode_create_empty_string_singleton(state) < 0) { - return _PyStatus_NO_MEMORY(); + if (!_Py_IsMainInterpreter(interp)) { + return _PyStatus_OK(); } +#ifdef Py_DEBUG + assert(_PyUnicode_CheckConsistency(&_Py_STR(empty), 1)); +#endif + return _PyStatus_OK(); } @@ -15798,15 +15779,14 @@ PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list( static PyObject * unicodeiter_reduce(unicodeiterobject *it, PyObject *Py_UNUSED(ignored)) { - _Py_IDENTIFIER(iter); if (it->it_seq != NULL) { - return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter), + return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), it->it_seq, it->it_index); } else { PyObject *u = (PyObject *)_PyUnicode_New(0); if (u == NULL) return NULL; - return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), u); + return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), u); } } @@ -16137,7 +16117,6 @@ _PyUnicode_Fini(PyInterpreterState *interp) for (Py_ssize_t i = 0; i < 256; i++) { Py_CLEAR(state->latin1[i]); } - Py_CLEAR(state->empty_string); } diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 80c7038..72a0a3f 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -255,10 +255,6 @@ _Py_union_type_or(PyObject* self, PyObject* other) static int union_repr_item(_PyUnicodeWriter *writer, PyObject *p) { - _Py_IDENTIFIER(__module__); - _Py_IDENTIFIER(__qualname__); - _Py_IDENTIFIER(__origin__); - _Py_IDENTIFIER(__args__); PyObject *qualname = NULL; PyObject *module = NULL; PyObject *tmp; @@ -269,13 +265,13 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p) return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); } - if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) { goto exit; } if (tmp) { Py_DECREF(tmp); - if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) { goto exit; } if (tmp) { @@ -285,13 +281,13 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p) } } - if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) { goto exit; } if (qualname == NULL) { goto use_repr; } - if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) { + if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) { goto exit; } if (module == NULL || module == Py_None) { diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 76121f9..71dfa64 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -163,7 +163,6 @@ static PyObject * weakref_repr(PyWeakReference *self) { PyObject *name, *repr; - _Py_IDENTIFIER(__name__); PyObject* obj = PyWeakref_GET_OBJECT(self); if (obj == Py_None) { @@ -171,7 +170,7 @@ weakref_repr(PyWeakReference *self) } Py_INCREF(obj); - if (_PyObject_LookupAttrId(obj, &PyId___name__, &name) < 0) { + if (_PyObject_LookupAttr(obj, &_Py_ID(__name__), &name) < 0) { Py_DECREF(obj); return NULL; } @@ -462,10 +461,9 @@ proxy_checkref(PyWeakReference *proxy) #define WRAP_METHOD(method, special) \ static PyObject * \ method(PyObject *proxy, PyObject *Py_UNUSED(ignored)) { \ - _Py_IDENTIFIER(special); \ UNWRAP(proxy); \ Py_INCREF(proxy); \ - PyObject* res = _PyObject_CallMethodIdNoArgs(proxy, &PyId_##special); \ + PyObject* res = PyObject_CallMethodNoArgs(proxy, &_Py_ID(special)); \ Py_DECREF(proxy); \ return res; \ } |