Skip to content

Commit

Permalink
Replace _Py_Identifier objects with interned strings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alex Malyshev authored and facebook-github-bot committed Jul 26, 2024
1 parent 7b5bcda commit f0cf0cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions Include/cpython/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit f0cf0cf

Please sign in to comment.