aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/crossinterp_data_lookup.h
diff options
context:
space:
mode:
Diffstat (limited to 'Python/crossinterp_data_lookup.h')
-rw-r--r--Python/crossinterp_data_lookup.h97
1 files changed, 66 insertions, 31 deletions
diff --git a/Python/crossinterp_data_lookup.h b/Python/crossinterp_data_lookup.h
index 48e5d9762cd..8358ba65073 100644
--- a/Python/crossinterp_data_lookup.h
+++ b/Python/crossinterp_data_lookup.h
@@ -1,7 +1,10 @@
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
-typedef _PyXIData_lookup_context_t dlcontext_t;
+typedef struct _dlcontext {
+ _PyXIData_lookup_t *global;
+ _PyXIData_lookup_t *local;
+} dlcontext_t;
typedef _PyXIData_registry_t dlregistry_t;
typedef _PyXIData_regitem_t dlregitem_t;
@@ -26,28 +29,15 @@ xid_lookup_fini(_PyXIData_lookup_t *state)
_xidregistry_fini(&state->registry);
}
-static xidatafunc
-lookup_getdata(dlcontext_t *ctx, PyObject *obj)
-{
- /* Cross-interpreter objects are looked up by exact match on the class.
- We can reassess this policy when we move from a global registry to a
- tp_* slot. */
- return _lookup_getdata_from_registry(ctx, obj);
-}
-
-
-/* exported API */
-
-int
-_PyXIData_GetLookupContext(PyInterpreterState *interp,
- _PyXIData_lookup_context_t *res)
+static int
+get_lookup_context(PyThreadState *tstate, dlcontext_t *res)
{
- _PyXI_global_state_t *global = _PyXI_GET_GLOBAL_STATE(interp);
+ _PyXI_global_state_t *global = _PyXI_GET_GLOBAL_STATE(tstate->interp);
if (global == NULL) {
assert(PyErr_Occurred());
return -1;
}
- _PyXI_state_t *local = _PyXI_GET_STATE(interp);
+ _PyXI_state_t *local = _PyXI_GET_STATE(tstate->interp);
if (local == NULL) {
assert(PyErr_Occurred());
return -1;
@@ -55,15 +45,57 @@ _PyXIData_GetLookupContext(PyInterpreterState *interp,
*res = (dlcontext_t){
.global = &global->data_lookup,
.local = &local->data_lookup,
- .PyExc_NotShareableError = local->exceptions.PyExc_NotShareableError,
};
return 0;
}
+static xidatafunc
+lookup_getdata(dlcontext_t *ctx, PyObject *obj)
+{
+ /* Cross-interpreter objects are looked up by exact match on the class.
+ We can reassess this policy when we move from a global registry to a
+ tp_* slot. */
+ return _lookup_getdata_from_registry(ctx, obj);
+}
+
+
+/* exported API */
+
+PyObject *
+_PyXIData_GetNotShareableErrorType(PyThreadState *tstate)
+{
+ PyObject *exctype = get_notshareableerror_type(tstate);
+ assert(exctype != NULL);
+ return exctype;
+}
+
+void
+_PyXIData_SetNotShareableError(PyThreadState *tstate, const char *msg)
+{
+ PyObject *cause = NULL;
+ set_notshareableerror(tstate, cause, 1, msg);
+}
+
+void
+_PyXIData_FormatNotShareableError(PyThreadState *tstate,
+ const char *format, ...)
+{
+ PyObject *cause = NULL;
+ va_list vargs;
+ va_start(vargs, format);
+ format_notshareableerror_v(tstate, cause, 1, format, vargs);
+ va_end(vargs);
+}
+
+
xidatafunc
-_PyXIData_Lookup(_PyXIData_lookup_context_t *ctx, PyObject *obj)
+_PyXIData_Lookup(PyThreadState *tstate, PyObject *obj)
{
- return lookup_getdata(ctx, obj);
+ dlcontext_t ctx;
+ if (get_lookup_context(tstate, &ctx) < 0) {
+ return NULL;
+ }
+ return lookup_getdata(&ctx, obj);
}
@@ -250,7 +282,7 @@ _xidregistry_clear(dlregistry_t *xidregistry)
}
int
-_PyXIData_RegisterClass(_PyXIData_lookup_context_t *ctx,
+_PyXIData_RegisterClass(PyThreadState *tstate,
PyTypeObject *cls, xidatafunc getdata)
{
if (!PyType_Check(cls)) {
@@ -263,7 +295,11 @@ _PyXIData_RegisterClass(_PyXIData_lookup_context_t *ctx,
}
int res = 0;
- dlregistry_t *xidregistry = _get_xidregistry_for_type(ctx, cls);
+ dlcontext_t ctx;
+ if (get_lookup_context(tstate, &ctx) < 0) {
+ return -1;
+ }
+ dlregistry_t *xidregistry = _get_xidregistry_for_type(&ctx, cls);
_xidregistry_lock(xidregistry);
dlregitem_t *matched = _xidregistry_find_type(xidregistry, cls);
@@ -281,10 +317,14 @@ finally:
}
int
-_PyXIData_UnregisterClass(_PyXIData_lookup_context_t *ctx, PyTypeObject *cls)
+_PyXIData_UnregisterClass(PyThreadState *tstate, PyTypeObject *cls)
{
int res = 0;
- dlregistry_t *xidregistry = _get_xidregistry_for_type(ctx, cls);
+ dlcontext_t ctx;
+ if (get_lookup_context(tstate, &ctx) < 0) {
+ return -1;
+ }
+ dlregistry_t *xidregistry = _get_xidregistry_for_type(&ctx, cls);
_xidregistry_lock(xidregistry);
dlregitem_t *matched = _xidregistry_find_type(xidregistry, cls);
@@ -508,11 +548,6 @@ _tuple_shared_free(void* data)
static int
_tuple_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
{
- dlcontext_t ctx;
- if (_PyXIData_GetLookupContext(tstate->interp, &ctx) < 0) {
- return -1;
- }
-
Py_ssize_t len = PyTuple_GET_SIZE(obj);
if (len < 0) {
return -1;
@@ -539,7 +574,7 @@ _tuple_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *data)
int res = -1;
if (!_Py_EnterRecursiveCallTstate(tstate, " while sharing a tuple")) {
- res = _PyObject_GetXIData(&ctx, item, data);
+ res = _PyObject_GetXIData(tstate, item, data);
_Py_LeaveRecursiveCallTstate(tstate);
}
if (res < 0) {