File indexing completed on 2025-11-19 09:50:45
0001 #ifndef Py_INTERNAL_CROSSINTERP_H
0002 #define Py_INTERNAL_CROSSINTERP_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006
0007 #ifndef Py_BUILD_CORE
0008 # error "this header requires Py_BUILD_CORE define"
0009 #endif
0010
0011 #include "pycore_lock.h" // PyMutex
0012 #include "pycore_pyerrors.h"
0013
0014
0015
0016
0017
0018 PyAPI_DATA(PyObject *) PyExc_InterpreterError;
0019 PyAPI_DATA(PyObject *) PyExc_InterpreterNotFoundError;
0020
0021
0022
0023
0024
0025
0026 typedef int (*_Py_simple_func)(void *);
0027 extern int _Py_CallInInterpreter(
0028 PyInterpreterState *interp,
0029 _Py_simple_func func,
0030 void *arg);
0031 extern int _Py_CallInInterpreterAndRawFree(
0032 PyInterpreterState *interp,
0033 _Py_simple_func func,
0034 void *arg);
0035
0036
0037
0038
0039
0040
0041 typedef struct _xid _PyCrossInterpreterData;
0042 typedef PyObject *(*xid_newobjectfunc)(_PyCrossInterpreterData *);
0043 typedef void (*xid_freefunc)(void *);
0044
0045
0046
0047
0048 struct _xid {
0049
0050
0051
0052 void *data;
0053
0054
0055
0056
0057
0058
0059 PyObject *obj;
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 int64_t interpid;
0070
0071
0072
0073
0074 xid_newobjectfunc new_object;
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084 xid_freefunc free;
0085 };
0086
0087 PyAPI_FUNC(_PyCrossInterpreterData *) _PyCrossInterpreterData_New(void);
0088 PyAPI_FUNC(void) _PyCrossInterpreterData_Free(_PyCrossInterpreterData *data);
0089
0090 #define _PyCrossInterpreterData_DATA(DATA) ((DATA)->data)
0091 #define _PyCrossInterpreterData_OBJ(DATA) ((DATA)->obj)
0092 #define _PyCrossInterpreterData_INTERPID(DATA) ((DATA)->interpid)
0093
0094
0095
0096
0097
0098 PyAPI_FUNC(void) _PyCrossInterpreterData_Init(
0099 _PyCrossInterpreterData *data,
0100 PyInterpreterState *interp, void *shared, PyObject *obj,
0101 xid_newobjectfunc new_object);
0102 PyAPI_FUNC(int) _PyCrossInterpreterData_InitWithSize(
0103 _PyCrossInterpreterData *,
0104 PyInterpreterState *interp, const size_t, PyObject *,
0105 xid_newobjectfunc);
0106 PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
0107 PyInterpreterState *, _PyCrossInterpreterData *);
0108
0109
0110
0111
0112 #define _PyCrossInterpreterData_SET_FREE(DATA, FUNC) \
0113 do { \
0114 (DATA)->free = (FUNC); \
0115 } while (0)
0116
0117
0118
0119
0120
0121
0122
0123 #define _PyCrossInterpreterData_SET_NEW_OBJECT(DATA, FUNC) \
0124 do { \
0125 (DATA)->new_object = (FUNC); \
0126 } while (0)
0127
0128
0129
0130
0131 PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
0132 PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
0133 PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
0134 PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
0135 PyAPI_FUNC(int) _PyCrossInterpreterData_ReleaseAndRawFree(_PyCrossInterpreterData *);
0136
0137
0138
0139
0140
0141
0142
0143
0144 typedef int (*crossinterpdatafunc)(PyThreadState *tstate, PyObject *,
0145 _PyCrossInterpreterData *);
0146
0147 struct _xidregitem;
0148
0149 struct _xidregitem {
0150 struct _xidregitem *prev;
0151 struct _xidregitem *next;
0152
0153 PyTypeObject *cls;
0154
0155 PyObject *weakref;
0156 size_t refcount;
0157 crossinterpdatafunc getdata;
0158 };
0159
0160 struct _xidregistry {
0161 int global;
0162 int initialized;
0163 PyMutex mutex;
0164 struct _xidregitem *head;
0165 };
0166
0167 PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
0168 PyAPI_FUNC(int) _PyCrossInterpreterData_UnregisterClass(PyTypeObject *);
0169 PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
0170
0171
0172
0173
0174
0175
0176 struct _xi_runtime_state {
0177
0178
0179 struct _xidregistry registry;
0180 };
0181
0182 struct _xi_state {
0183
0184
0185 struct _xidregistry registry;
0186
0187
0188 PyObject *PyExc_NotShareableError;
0189 };
0190
0191 extern PyStatus _PyXI_Init(PyInterpreterState *interp);
0192 extern void _PyXI_Fini(PyInterpreterState *interp);
0193
0194 extern PyStatus _PyXI_InitTypes(PyInterpreterState *interp);
0195 extern void _PyXI_FiniTypes(PyInterpreterState *interp);
0196
0197 #define _PyInterpreterState_GetXIState(interp) (&(interp)->xi)
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209 typedef struct _excinfo {
0210 struct _excinfo_type {
0211 PyTypeObject *builtin;
0212 const char *name;
0213 const char *qualname;
0214 const char *module;
0215 } type;
0216 const char *msg;
0217 const char *errdisplay;
0218 } _PyXI_excinfo;
0219
0220 PyAPI_FUNC(int) _PyXI_InitExcInfo(_PyXI_excinfo *info, PyObject *exc);
0221 PyAPI_FUNC(PyObject *) _PyXI_FormatExcInfo(_PyXI_excinfo *info);
0222 PyAPI_FUNC(PyObject *) _PyXI_ExcInfoAsObject(_PyXI_excinfo *info);
0223 PyAPI_FUNC(void) _PyXI_ClearExcInfo(_PyXI_excinfo *info);
0224
0225
0226 typedef enum error_code {
0227 _PyXI_ERR_NO_ERROR = 0,
0228 _PyXI_ERR_UNCAUGHT_EXCEPTION = -1,
0229 _PyXI_ERR_OTHER = -2,
0230 _PyXI_ERR_NO_MEMORY = -3,
0231 _PyXI_ERR_ALREADY_RUNNING = -4,
0232 _PyXI_ERR_MAIN_NS_FAILURE = -5,
0233 _PyXI_ERR_APPLY_NS_FAILURE = -6,
0234 _PyXI_ERR_NOT_SHAREABLE = -7,
0235 } _PyXI_errcode;
0236
0237
0238 typedef struct _sharedexception {
0239
0240 PyInterpreterState *interp;
0241
0242 _PyXI_errcode code;
0243
0244
0245
0246 _PyXI_excinfo uncaught;
0247 } _PyXI_error;
0248
0249 PyAPI_FUNC(PyObject *) _PyXI_ApplyError(_PyXI_error *err);
0250
0251
0252 typedef struct xi_session _PyXI_session;
0253 typedef struct _sharedns _PyXI_namespace;
0254
0255 PyAPI_FUNC(void) _PyXI_FreeNamespace(_PyXI_namespace *ns);
0256 PyAPI_FUNC(_PyXI_namespace *) _PyXI_NamespaceFromNames(PyObject *names);
0257 PyAPI_FUNC(int) _PyXI_FillNamespaceFromDict(
0258 _PyXI_namespace *ns,
0259 PyObject *nsobj,
0260 _PyXI_session *session);
0261 PyAPI_FUNC(int) _PyXI_ApplyNamespace(
0262 _PyXI_namespace *ns,
0263 PyObject *nsobj,
0264 PyObject *dflt);
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277 struct xi_session {
0278
0279
0280
0281
0282 PyThreadState *prev_tstate;
0283
0284
0285 PyThreadState *init_tstate;
0286
0287 int own_init_tstate;
0288
0289
0290
0291
0292
0293
0294 int running;
0295
0296
0297
0298 PyObject *main_ns;
0299
0300
0301
0302 _PyXI_errcode *error_override;
0303
0304 _PyXI_error *error;
0305
0306
0307 _PyXI_error _error;
0308 _PyXI_errcode _error_override;
0309 };
0310
0311 PyAPI_FUNC(int) _PyXI_Enter(
0312 _PyXI_session *session,
0313 PyInterpreterState *interp,
0314 PyObject *nsupdates);
0315 PyAPI_FUNC(void) _PyXI_Exit(_PyXI_session *session);
0316
0317 PyAPI_FUNC(PyObject *) _PyXI_ApplyCapturedException(_PyXI_session *session);
0318 PyAPI_FUNC(int) _PyXI_HasCapturedException(_PyXI_session *session);
0319
0320
0321
0322
0323
0324
0325
0326 PyAPI_FUNC(PyInterpreterState *) _PyXI_NewInterpreter(
0327 PyInterpreterConfig *config,
0328 long *maybe_whence,
0329 PyThreadState **p_tstate,
0330 PyThreadState **p_save_tstate);
0331 PyAPI_FUNC(void) _PyXI_EndInterpreter(
0332 PyInterpreterState *interp,
0333 PyThreadState *tstate,
0334 PyThreadState **p_save_tstate);
0335
0336
0337 #ifdef __cplusplus
0338 }
0339 #endif
0340 #endif