gh-132781: Cleanup Code Related to NotShareableError (gh-132782)

The following are added to the internal C-API:

* _PyErr_FormatV()
* _PyErr_SetModuleNotFoundError()
* _PyXIData_GetNotShareableErrorType()
* _PyXIData_FormatNotShareableError()

We also drop _PyXIData_lookup_context_t and _PyXIData_GetLookupContext().
This commit is contained in:
Eric Snow 2025-04-25 14:43:38 -06:00 committed by GitHub
parent 4c20f46fa0
commit cd9536a087
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 322 additions and 177 deletions

View file

@ -1754,17 +1754,10 @@ static int
channel_send(_channels *channels, int64_t cid, PyObject *obj,
_waiting_t *waiting, int unboundop)
{
PyInterpreterState *interp = _get_current_interp();
if (interp == NULL) {
return -1;
}
PyThreadState *tstate = _PyThreadState_GET();
PyInterpreterState *interp = tstate->interp;
int64_t interpid = PyInterpreterState_GetID(interp);
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return -1;
}
// Look up the channel.
PyThread_type_lock mutex = NULL;
_channel_state *chan = NULL;
@ -1786,7 +1779,7 @@ channel_send(_channels *channels, int64_t cid, PyObject *obj,
PyThread_release_lock(mutex);
return -1;
}
if (_PyObject_GetXIData(&ctx, obj, data) != 0) {
if (_PyObject_GetXIData(tstate, obj, data) != 0) {
PyThread_release_lock(mutex);
GLOBAL_FREE(data);
return -1;

View file

@ -1127,11 +1127,7 @@ queue_destroy(_queues *queues, int64_t qid)
static int
queue_put(_queues *queues, int64_t qid, PyObject *obj, int fmt, int unboundop)
{
PyInterpreterState *interp = PyInterpreterState_Get();
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return -1;
}
PyThreadState *tstate = PyThreadState_Get();
// Look up the queue.
_queue *queue = NULL;
@ -1147,12 +1143,13 @@ queue_put(_queues *queues, int64_t qid, PyObject *obj, int fmt, int unboundop)
_queue_unmark_waiter(queue, queues->mutex);
return -1;
}
if (_PyObject_GetXIData(&ctx, obj, data) != 0) {
if (_PyObject_GetXIData(tstate, obj, data) != 0) {
_queue_unmark_waiter(queue, queues->mutex);
GLOBAL_FREE(data);
return -1;
}
assert(_PyXIData_INTERPID(data) == PyInterpreterState_GetID(interp));
assert(_PyXIData_INTERPID(data) ==
PyInterpreterState_GetID(tstate->interp));
// Add the data to the queue.
int64_t interpid = -1; // _queueitem_init() will set it.

View file

@ -8,24 +8,16 @@
static int
ensure_xid_class(PyTypeObject *cls, xidatafunc getdata)
{
PyInterpreterState *interp = PyInterpreterState_Get();
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return -1;
}
return _PyXIData_RegisterClass(&ctx, cls, getdata);
PyThreadState *tstate = PyThreadState_Get();
return _PyXIData_RegisterClass(tstate, cls, getdata);
}
#ifdef REGISTERS_HEAP_TYPES
static int
clear_xid_class(PyTypeObject *cls)
{
PyInterpreterState *interp = PyInterpreterState_Get();
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return -1;
}
return _PyXIData_UnregisterClass(&ctx, cls);
PyThreadState *tstate = PyThreadState_Get();
return _PyXIData_UnregisterClass(tstate, cls);
}
#endif

View file

@ -1278,13 +1278,8 @@ object_is_shareable(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}
PyInterpreterState *interp = PyInterpreterState_Get();
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return NULL;
}
if (_PyObject_CheckXIData(&ctx, obj) == 0) {
PyThreadState *tstate = _PyThreadState_GET();
if (_PyObject_CheckXIData(tstate, obj) == 0) {
Py_RETURN_TRUE;
}
PyErr_Clear();
@ -1577,14 +1572,9 @@ The 'interpreters' module provides a more convenient interface.");
static int
module_exec(PyObject *mod)
{
PyInterpreterState *interp = PyInterpreterState_Get();
PyThreadState *tstate = _PyThreadState_GET();
module_state *state = get_module_state(mod);
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return -1;
}
#define ADD_WHENCE(NAME) \
if (PyModule_AddIntConstant(mod, "WHENCE_" #NAME, \
_PyInterpreterState_WHENCE_##NAME) < 0) \
@ -1606,7 +1596,8 @@ module_exec(PyObject *mod)
if (PyModule_AddType(mod, (PyTypeObject *)PyExc_InterpreterNotFoundError) < 0) {
goto error;
}
if (PyModule_AddType(mod, (PyTypeObject *)ctx.PyExc_NotShareableError) < 0) {
PyObject *exctype = _PyXIData_GetNotShareableErrorType(tstate);
if (PyModule_AddType(mod, (PyTypeObject *)exctype) < 0) {
goto error;
}

View file

@ -1696,11 +1696,7 @@ _xid_capsule_destructor(PyObject *capsule)
static PyObject *
get_crossinterp_data(PyObject *self, PyObject *args)
{
PyInterpreterState *interp = PyInterpreterState_Get();
_PyXIData_lookup_context_t ctx;
if (_PyXIData_GetLookupContext(interp, &ctx) < 0) {
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
PyObject *obj = NULL;
if (!PyArg_ParseTuple(args, "O:get_crossinterp_data", &obj)) {
@ -1711,7 +1707,7 @@ get_crossinterp_data(PyObject *self, PyObject *args)
if (data == NULL) {
return NULL;
}
if (_PyObject_GetXIData(&ctx, obj, data) != 0) {
if (_PyObject_GetXIData(tstate, obj, data) != 0) {
_PyXIData_Free(data);
return NULL;
}