From f0cf0cf06ba16cd460be6438ab3edf80eda950df Mon Sep 17 00:00:00 2001 From: Alex Malyshev Date: Fri, 26 Jul 2024 10:16:09 -0700 Subject: [PATCH] Replace _Py_Identifier objects with interned strings Summary: _Py_Identifier is internal to CPython and it's been getting harder and harder to access, see https://bugs.python.org/issue46541. For most uses in CinderX we can initialize static local variables that will lazily create interned Python strings. It's not as efficient as _Py_Identifier, but it's using the stable API so it'll be easier to maintain over time. The places where we still have _Py_Identifier relate to using `_PyObject_LookupSpecial` (in 3.12 it's renamed to `_PyObject_LookupSpecialId`). There's no equivalent of that function that uses `PyObject*` values, we'll likely have to implement that ourselves. Reviewed By: DinoV Differential Revision: D59936685 fbshipit-source-id: 2393fa5f5fff8205ff5203ce7e814b8e8f340971 --- Include/cpython/ceval.h | 1 + Python/ceval.c | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Include/cpython/ceval.h b/Include/cpython/ceval.h index 06338928f67..b7a9966e0ac 100644 --- a/Include/cpython/ceval.h +++ b/Include/cpython/ceval.h @@ -13,6 +13,7 @@ PyAPI_FUNC(int) _PyEval_SetAsyncGenFinalizer(PyObject *); PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void); /* Helper to look up a builtin object */ +PyAPI_FUNC(PyObject *) _PyEval_GetBuiltin(PyObject *); PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *); /* Look at the current frame's (if any) code's co_flags, and turn on the corresponding compiler flags in cf->cf_flags. Return 1 if any diff --git a/Python/ceval.c b/Python/ceval.c index dc21b7b4a80..13b776d5a34 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -5675,19 +5675,22 @@ PyEval_GetBuiltins(void) /* Convenience function to get a builtin from its name */ PyObject * -_PyEval_GetBuiltinId(_Py_Identifier *name) +_PyEval_GetBuiltin(PyObject *name) { PyThreadState *tstate = _PyThreadState_GET(); - PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name); - if (attr) { - Py_INCREF(attr); - } - else if (!_PyErr_Occurred(tstate)) { - _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name)); + PyObject *attr = PyObject_GetItem(PyEval_GetBuiltins(), name); + if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + _PyErr_SetObject(tstate, PyExc_AttributeError, name); } return attr; } +PyObject * +_PyEval_GetBuiltinId(_Py_Identifier *name) +{ + return _PyEval_GetBuiltin(_PyUnicode_FromId(name)); +} + PyObject * PyEval_GetLocals(void) {