Skip to content

gh-117482: Cleaning up tp_dict for static builtin types in subinterpreters #117660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2345,5 +2345,41 @@ def ex(a, /, b, *, c):
)


class SubinterpreterTests(unittest.TestCase):

@classmethod
def setUpClass(cls):
global interpreters
try:
from test.support import interpreters
except ModuleNotFoundError:
raise unittest.SkipTest('subinterpreters required')
import test.support.interpreters.channels

@cpython_only
def test_slot_wrappers(self):
rch, sch = interpreters.channels.create()

# For now it's sufficient to check int.__str__.
# See https://github.com/python/cpython/issues/117482
# and https://github.com/python/cpython/pull/117660.
script = '''if True:
text = repr(int.__str__)
sch.send_nowait(text)
'''

exec(script)
expected = rch.recv()

interp = interpreters.create()
interp.exec('from test.support import interpreters')
interp.exec('import test.support.interpreters.channels')
interp.prepare_main(sch=sch)
interp.exec(script)
results = rch.recv()

self.assertEqual(results, expected)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix "tp" slot wrappers for builtin types on subinterpreters.
In particular, this fixes a regression in methods like ``int.__str__()``.
55 changes: 55 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,48 @@ lookup_tp_dict(PyTypeObject *self)
return self->tp_dict;
}

static int
fix_builtin_slot_wrappers(PyTypeObject *self, PyInterpreterState *interp)
{
assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
assert(!_Py_IsMainInterpreter(interp));

managed_static_type_state *state = _PyStaticType_GetState(interp, self);
assert(state != NULL);
PyInterpreterState *main_interp = _PyInterpreterState_Main();
managed_static_type_state *main_state = _PyStaticType_GetState(main_interp, self);
assert(main_state != NULL);

int res = -1;
PyObject* keys_to_remove = PyList_New(0);
if (keys_to_remove == NULL) {
goto finally;
}
Py_ssize_t i = 0;
PyObject *key, *value;
while (PyDict_Next(state->tp_dict, &i, &key, &value)) {
if (!PyDict_Contains(main_state->tp_dict, key)) {
if (PyList_Append(keys_to_remove, key) < 0) {
goto finally;
}
}
}

Py_ssize_t list_size = PyList_Size(keys_to_remove);
for (Py_ssize_t i = 0; i < list_size; i++) {
PyObject* key = PyList_GetItem(keys_to_remove, i);
if (PyDict_DelItem(state->tp_dict, key) < 0) {
goto finally;
}
}

res = 0;

finally:
Py_XDECREF(keys_to_remove);
return res;
}

PyObject *
_PyType_GetDict(PyTypeObject *self)
{
Expand Down Expand Up @@ -11101,6 +11143,19 @@ add_operators(PyTypeObject *type)
Py_DECREF(descr);
}
}

/* For now, we work around issues with slot wrappers
for builtin types in subinterpreters.
See https://github.com/python/cpython/issues/117482. */
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
PyInterpreterState *interp = _PyInterpreterState_GET();
if (!_Py_IsMainInterpreter(interp)) {
if (fix_builtin_slot_wrappers(type, interp) < 0) {
return -1;
}
}
}

return 0;
}

Expand Down
Loading