Coverage Report

Created: 2026-04-12 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/crossinterp.c
Line
Count
Source
1
2
/* API for managing interactions between isolated interpreters */
3
4
#include "Python.h"
5
#include "marshal.h"              // PyMarshal_WriteObjectToString()
6
#include "osdefs.h"               // MAXPATHLEN
7
#include "pycore_ceval.h"         // _Py_simple_func
8
#include "pycore_crossinterp.h"   // _PyXIData_t
9
#include "pycore_function.h"      // _PyFunction_VerifyStateless()
10
#include "pycore_global_strings.h"  // _Py_ID()
11
#include "pycore_import.h"        // _PyImport_SetModule()
12
#include "pycore_initconfig.h"    // _PyStatus_OK()
13
#include "pycore_namespace.h"     // _PyNamespace_New()
14
#include "pycore_pythonrun.h"     // _Py_SourceAsString()
15
#include "pycore_runtime.h"       // _PyRuntime
16
#include "pycore_setobject.h"     // _PySet_NextEntry()
17
#include "pycore_typeobject.h"    // _PyStaticType_InitBuiltin()
18
19
20
static Py_ssize_t
21
_Py_GetMainfile(char *buffer, size_t maxlen)
22
0
{
23
    // We don't expect subinterpreters to have the __main__ module's
24
    // __name__ set, but proceed just in case.
25
0
    PyThreadState *tstate = _PyThreadState_GET();
26
0
    PyObject *module = _Py_GetMainModule(tstate);
27
0
    if (_Py_CheckMainModule(module) < 0) {
28
0
        Py_XDECREF(module);
29
0
        return -1;
30
0
    }
31
0
    Py_ssize_t size = _PyModule_GetFilenameUTF8(module, buffer, maxlen);
32
0
    Py_DECREF(module);
33
0
    return size;
34
0
}
35
36
37
static PyObject *
38
runpy_run_path(const char *filename, const char *modname)
39
0
{
40
0
    PyObject *run_path = PyImport_ImportModuleAttrString("runpy", "run_path");
41
0
    if (run_path == NULL) {
42
0
        return NULL;
43
0
    }
44
0
    PyObject *args = Py_BuildValue("(sOs)", filename, Py_None, modname);
45
0
    if (args == NULL) {
46
0
        Py_DECREF(run_path);
47
0
        return NULL;
48
0
    }
49
0
    PyObject *ns = PyObject_Call(run_path, args, NULL);
50
0
    Py_DECREF(run_path);
51
0
    Py_DECREF(args);
52
0
    return ns;
53
0
}
54
55
56
static void
57
set_exc_with_cause(PyObject *exctype, const char *msg)
58
0
{
59
0
    PyObject *cause = PyErr_GetRaisedException();
60
0
    PyErr_SetString(exctype, msg);
61
0
    PyObject *exc = PyErr_GetRaisedException();
62
0
    PyException_SetCause(exc, cause);
63
0
    PyErr_SetRaisedException(exc);
64
0
}
65
66
67
/****************************/
68
/* module duplication utils */
69
/****************************/
70
71
struct sync_module_result {
72
    PyObject *module;
73
    PyObject *loaded;
74
    PyObject *failed;
75
};
76
77
struct sync_module {
78
    const char *filename;
79
    char _filename[MAXPATHLEN+1];
80
    struct sync_module_result cached;
81
};
82
83
static void
84
sync_module_clear(struct sync_module *data)
85
0
{
86
0
    data->filename = NULL;
87
0
    Py_CLEAR(data->cached.module);
88
0
    Py_CLEAR(data->cached.loaded);
89
0
    Py_CLEAR(data->cached.failed);
90
0
}
91
92
static void
93
sync_module_capture_exc(PyThreadState *tstate, struct sync_module *data)
94
0
{
95
0
    assert(_PyErr_Occurred(tstate));
96
0
    PyObject *context = data->cached.failed;
97
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
98
0
    _PyErr_SetRaisedException(tstate, Py_NewRef(exc));
99
0
    if (context != NULL) {
100
0
        PyException_SetContext(exc, context);
101
0
    }
102
0
    data->cached.failed = exc;
103
0
}
104
105
106
static int
107
ensure_isolated_main(PyThreadState *tstate, struct sync_module *main)
108
0
{
109
    // Load the module from the original file (or from a cache).
110
111
    // First try the local cache.
112
0
    if (main->cached.failed != NULL) {
113
        // We'll deal with this in apply_isolated_main().
114
0
        assert(main->cached.module == NULL);
115
0
        assert(main->cached.loaded == NULL);
116
0
        return 0;
117
0
    }
118
0
    else if (main->cached.loaded != NULL) {
119
0
        assert(main->cached.module != NULL);
120
0
        return 0;
121
0
    }
122
0
    assert(main->cached.module == NULL);
123
124
0
    if (main->filename == NULL) {
125
0
        _PyErr_SetString(tstate, PyExc_NotImplementedError, "");
126
0
        return -1;
127
0
    }
128
129
    // It wasn't in the local cache so we'll need to populate it.
130
0
    PyObject *mod = _Py_GetMainModule(tstate);
131
0
    if (_Py_CheckMainModule(mod) < 0) {
132
        // This is probably unrecoverable, so don't bother caching the error.
133
0
        assert(_PyErr_Occurred(tstate));
134
0
        Py_XDECREF(mod);
135
0
        return -1;
136
0
    }
137
0
    PyObject *loaded = NULL;
138
139
    // Try the per-interpreter cache for the loaded module.
140
    // XXX Store it in sys.modules?
141
0
    PyObject *interpns = PyInterpreterState_GetDict(tstate->interp);
142
0
    assert(interpns != NULL);
143
0
    PyObject *key = PyUnicode_FromString("CACHED_MODULE_NS___main__");
144
0
    if (key == NULL) {
145
        // It's probably unrecoverable, so don't bother caching the error.
146
0
        Py_DECREF(mod);
147
0
        return -1;
148
0
    }
149
0
    else if (PyDict_GetItemRef(interpns, key, &loaded) < 0) {
150
        // It's probably unrecoverable, so don't bother caching the error.
151
0
        Py_DECREF(mod);
152
0
        Py_DECREF(key);
153
0
        return -1;
154
0
    }
155
0
    else if (loaded == NULL) {
156
        // It wasn't already loaded from file.
157
0
        loaded = PyModule_NewObject(&_Py_ID(__main__));
158
0
        if (loaded == NULL) {
159
0
            goto error;
160
0
        }
161
0
        PyObject *ns = _PyModule_GetDict(loaded);
162
163
        // We don't want to trigger "if __name__ == '__main__':",
164
        // so we use a bogus module name.
165
0
        PyObject *loaded_ns =
166
0
                    runpy_run_path(main->filename, "<fake __main__>");
167
0
        if (loaded_ns == NULL) {
168
0
            goto error;
169
0
        }
170
0
        int res = PyDict_Update(ns, loaded_ns);
171
0
        Py_DECREF(loaded_ns);
172
0
        if (res < 0) {
173
0
            goto error;
174
0
        }
175
176
        // Set the per-interpreter cache entry.
177
0
        if (PyDict_SetItem(interpns, key, loaded) < 0) {
178
0
            goto error;
179
0
        }
180
0
    }
181
182
0
    Py_DECREF(key);
183
0
    main->cached = (struct sync_module_result){
184
0
       .module = mod,
185
0
       .loaded = loaded,
186
0
    };
187
0
    return 0;
188
189
0
error:
190
0
    sync_module_capture_exc(tstate, main);
191
0
    Py_XDECREF(loaded);
192
0
    Py_DECREF(mod);
193
0
    Py_XDECREF(key);
194
0
    return -1;
195
0
}
196
197
#ifndef NDEBUG
198
static int
199
main_mod_matches(PyObject *expected)
200
{
201
    PyObject *mod = PyImport_GetModule(&_Py_ID(__main__));
202
    Py_XDECREF(mod);
203
    return mod == expected;
204
}
205
#endif
206
207
static int
208
apply_isolated_main(PyThreadState *tstate, struct sync_module *main)
209
0
{
210
0
    assert((main->cached.loaded == NULL) == (main->cached.loaded == NULL));
211
0
    if (main->cached.failed != NULL) {
212
        // It must have failed previously.
213
0
        assert(main->cached.loaded == NULL);
214
0
        _PyErr_SetRaisedException(tstate, main->cached.failed);
215
0
        return -1;
216
0
    }
217
0
    assert(main->cached.loaded != NULL);
218
219
0
    assert(main_mod_matches(main->cached.module));
220
0
    if (_PyImport_SetModule(&_Py_ID(__main__), main->cached.loaded) < 0) {
221
0
        sync_module_capture_exc(tstate, main);
222
0
        return -1;
223
0
    }
224
0
    return 0;
225
0
}
226
227
static void
228
restore_main(PyThreadState *tstate, struct sync_module *main)
229
0
{
230
0
    assert(main->cached.failed == NULL);
231
0
    assert(main->cached.module != NULL);
232
0
    assert(main->cached.loaded != NULL);
233
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
234
0
    assert(main_mod_matches(main->cached.loaded));
235
0
    int res = _PyImport_SetModule(&_Py_ID(__main__), main->cached.module);
236
0
    assert(res == 0);
237
0
    if (res < 0) {
238
0
        PyErr_FormatUnraisable("Exception ignored while restoring __main__");
239
0
    }
240
0
    _PyErr_SetRaisedException(tstate, exc);
241
0
}
242
243
244
/**************/
245
/* exceptions */
246
/**************/
247
248
typedef struct xi_exceptions exceptions_t;
249
static int init_static_exctypes(exceptions_t *, PyInterpreterState *);
250
static void fini_static_exctypes(exceptions_t *, PyInterpreterState *);
251
static int init_heap_exctypes(exceptions_t *);
252
static void fini_heap_exctypes(exceptions_t *);
253
#include "crossinterp_exceptions.h"
254
255
256
/***************************/
257
/* cross-interpreter calls */
258
/***************************/
259
260
int
261
_Py_CallInInterpreter(PyInterpreterState *interp,
262
                      _Py_simple_func func, void *arg)
263
0
{
264
0
    if (interp == PyInterpreterState_Get()) {
265
0
        return func(arg);
266
0
    }
267
    // XXX Emit a warning if this fails?
268
0
    _PyEval_AddPendingCall(interp, (_Py_pending_call_func)func, arg, 0);
269
0
    return 0;
270
0
}
271
272
int
273
_Py_CallInInterpreterAndRawFree(PyInterpreterState *interp,
274
                                _Py_simple_func func, void *arg)
275
0
{
276
0
    if (interp == PyInterpreterState_Get()) {
277
0
        int res = func(arg);
278
0
        PyMem_RawFree(arg);
279
0
        return res;
280
0
    }
281
    // XXX Emit a warning if this fails?
282
0
    _PyEval_AddPendingCall(interp, func, arg, _Py_PENDING_RAWFREE);
283
0
    return 0;
284
0
}
285
286
287
/**************************/
288
/* cross-interpreter data */
289
/**************************/
290
291
/* registry of {type -> _PyXIData_getdata_t} */
292
293
/* For now we use a global registry of shareable classes.
294
   An alternative would be to add a tp_* slot for a class's
295
   _PyXIData_getdata_t.  It would be simpler and more efficient. */
296
297
static void xid_lookup_init(_PyXIData_lookup_t *);
298
static void xid_lookup_fini(_PyXIData_lookup_t *);
299
struct _dlcontext;
300
static _PyXIData_getdata_t lookup_getdata(struct _dlcontext *, PyObject *);
301
#include "crossinterp_data_lookup.h"
302
303
304
/* lifecycle */
305
306
_PyXIData_t *
307
_PyXIData_New(void)
308
0
{
309
0
    _PyXIData_t *xid = PyMem_RawCalloc(1, sizeof(_PyXIData_t));
310
0
    if (xid == NULL) {
311
0
        PyErr_NoMemory();
312
0
    }
313
0
    return xid;
314
0
}
315
316
void
317
_PyXIData_Free(_PyXIData_t *xid)
318
0
{
319
0
    PyInterpreterState *interp = PyInterpreterState_Get();
320
0
    _PyXIData_Clear(interp, xid);
321
0
    PyMem_RawFree(xid);
322
0
}
323
324
325
/* defining cross-interpreter data */
326
327
static inline void
328
_xidata_init(_PyXIData_t *xidata)
329
0
{
330
    // If the value is being reused
331
    // then _xidata_clear() should have been called already.
332
0
    assert(xidata->data == NULL);
333
0
    assert(xidata->obj == NULL);
334
0
    *xidata = (_PyXIData_t){0};
335
0
    _PyXIData_INTERPID(xidata) = -1;
336
0
}
337
338
static inline void
339
_xidata_clear(_PyXIData_t *xidata)
340
0
{
341
    // _PyXIData_t only has two members that need to be
342
    // cleaned up, if set: "xidata" must be freed and "obj" must be decref'ed.
343
    // In both cases the original (owning) interpreter must be used,
344
    // which is the caller's responsibility to ensure.
345
0
    if (xidata->data != NULL) {
346
0
        if (xidata->free != NULL) {
347
0
            xidata->free(xidata->data);
348
0
        }
349
0
        xidata->data = NULL;
350
0
    }
351
0
    Py_CLEAR(xidata->obj);
352
0
}
353
354
void
355
_PyXIData_Init(_PyXIData_t *xidata,
356
               PyInterpreterState *interp,
357
               void *shared, PyObject *obj,
358
               xid_newobjfunc new_object)
359
0
{
360
0
    assert(xidata != NULL);
361
0
    assert(new_object != NULL);
362
0
    _xidata_init(xidata);
363
0
    xidata->data = shared;
364
0
    if (obj != NULL) {
365
0
        assert(interp != NULL);
366
        // released in _PyXIData_Clear()
367
0
        xidata->obj = Py_NewRef(obj);
368
0
    }
369
    // Ideally every object would know its owning interpreter.
370
    // Until then, we have to rely on the caller to identify it
371
    // (but we don't need it in all cases).
372
0
    _PyXIData_INTERPID(xidata) = (interp != NULL)
373
0
        ? PyInterpreterState_GetID(interp)
374
0
        : -1;
375
0
    xidata->new_object = new_object;
376
0
}
377
378
int
379
_PyXIData_InitWithSize(_PyXIData_t *xidata,
380
                       PyInterpreterState *interp,
381
                       const size_t size, PyObject *obj,
382
                       xid_newobjfunc new_object)
383
0
{
384
0
    assert(size > 0);
385
    // For now we always free the shared data in the same interpreter
386
    // where it was allocated, so the interpreter is required.
387
0
    assert(interp != NULL);
388
0
    _PyXIData_Init(xidata, interp, NULL, obj, new_object);
389
0
    xidata->data = PyMem_RawCalloc(1, size);
390
0
    if (xidata->data == NULL) {
391
0
        return -1;
392
0
    }
393
0
    xidata->free = PyMem_RawFree;
394
0
    return 0;
395
0
}
396
397
void
398
_PyXIData_Clear(PyInterpreterState *interp, _PyXIData_t *xidata)
399
0
{
400
0
    assert(xidata != NULL);
401
    // This must be called in the owning interpreter.
402
0
    assert(interp == NULL
403
0
           || _PyXIData_INTERPID(xidata) == -1
404
0
           || _PyXIData_INTERPID(xidata) == PyInterpreterState_GetID(interp));
405
0
    _xidata_clear(xidata);
406
0
}
407
408
409
/* getting cross-interpreter data */
410
411
static inline void
412
_set_xid_lookup_failure(PyThreadState *tstate, PyObject *obj, const char *msg,
413
                        PyObject *cause)
414
0
{
415
0
    if (msg != NULL) {
416
0
        assert(obj == NULL);
417
0
        set_notshareableerror(tstate, cause, 0, msg);
418
0
    }
419
0
    else if (obj == NULL) {
420
0
        msg = "object does not support cross-interpreter data";
421
0
        set_notshareableerror(tstate, cause, 0, msg);
422
0
    }
423
0
    else {
424
0
        msg = "%R does not support cross-interpreter data";
425
0
        format_notshareableerror(tstate, cause, 0, msg, obj);
426
0
    }
427
0
}
428
429
430
int
431
_PyObject_CheckXIData(PyThreadState *tstate, PyObject *obj)
432
0
{
433
0
    dlcontext_t ctx;
434
0
    if (get_lookup_context(tstate, &ctx) < 0) {
435
0
        return -1;
436
0
    }
437
0
    _PyXIData_getdata_t getdata = lookup_getdata(&ctx, obj);
438
0
    if (getdata.basic == NULL && getdata.fallback == NULL) {
439
0
        if (!_PyErr_Occurred(tstate)) {
440
0
            _set_xid_lookup_failure(tstate, obj, NULL, NULL);
441
0
        }
442
0
        return -1;
443
0
    }
444
0
    return 0;
445
0
}
446
447
static int
448
_check_xidata(PyThreadState *tstate, _PyXIData_t *xidata)
449
0
{
450
    // xidata->data can be anything, including NULL, so we don't check it.
451
452
    // xidata->obj may be NULL, so we don't check it.
453
454
0
    if (_PyXIData_INTERPID(xidata) < 0) {
455
0
        PyErr_SetString(PyExc_SystemError, "missing interp");
456
0
        return -1;
457
0
    }
458
459
0
    if (xidata->new_object == NULL) {
460
0
        PyErr_SetString(PyExc_SystemError, "missing new_object func");
461
0
        return -1;
462
0
    }
463
464
    // xidata->free may be NULL, so we don't check it.
465
466
0
    return 0;
467
0
}
468
469
static int
470
_get_xidata(PyThreadState *tstate,
471
            PyObject *obj, xidata_fallback_t fallback, _PyXIData_t *xidata)
472
0
{
473
0
    PyInterpreterState *interp = tstate->interp;
474
475
0
    assert(xidata->data == NULL);
476
0
    assert(xidata->obj == NULL);
477
0
    if (xidata->data != NULL || xidata->obj != NULL) {
478
0
        _PyErr_SetString(tstate, PyExc_ValueError, "xidata not cleared");
479
0
        return -1;
480
0
    }
481
482
    // Call the "getdata" func for the object.
483
0
    dlcontext_t ctx;
484
0
    if (get_lookup_context(tstate, &ctx) < 0) {
485
0
        return -1;
486
0
    }
487
0
    Py_INCREF(obj);
488
0
    _PyXIData_getdata_t getdata = lookup_getdata(&ctx, obj);
489
0
    if (getdata.basic == NULL && getdata.fallback == NULL) {
490
0
        if (PyErr_Occurred()) {
491
0
            Py_DECREF(obj);
492
0
            return -1;
493
0
        }
494
        // Fall back to obj
495
0
        Py_DECREF(obj);
496
0
        if (!_PyErr_Occurred(tstate)) {
497
0
            _set_xid_lookup_failure(tstate, obj, NULL, NULL);
498
0
        }
499
0
        return -1;
500
0
    }
501
0
    int res = getdata.basic != NULL
502
0
        ? getdata.basic(tstate, obj, xidata)
503
0
        : getdata.fallback(tstate, obj, fallback, xidata);
504
0
    Py_DECREF(obj);
505
0
    if (res != 0) {
506
0
        PyObject *cause = _PyErr_GetRaisedException(tstate);
507
0
        assert(cause != NULL);
508
0
        _set_xid_lookup_failure(tstate, obj, NULL, cause);
509
0
        Py_XDECREF(cause);
510
0
        return -1;
511
0
    }
512
513
    // Fill in the blanks and validate the result.
514
0
    _PyXIData_INTERPID(xidata) = PyInterpreterState_GetID(interp);
515
0
    if (_check_xidata(tstate, xidata) != 0) {
516
0
        (void)_PyXIData_Release(xidata);
517
0
        return -1;
518
0
    }
519
520
0
    return 0;
521
0
}
522
523
int
524
_PyObject_GetXIDataNoFallback(PyThreadState *tstate,
525
                              PyObject *obj, _PyXIData_t *xidata)
526
0
{
527
0
    return _get_xidata(tstate, obj, _PyXIDATA_XIDATA_ONLY, xidata);
528
0
}
529
530
int
531
_PyObject_GetXIData(PyThreadState *tstate,
532
                    PyObject *obj, xidata_fallback_t fallback,
533
                    _PyXIData_t *xidata)
534
0
{
535
0
    switch (fallback) {
536
0
        case _PyXIDATA_XIDATA_ONLY:
537
0
            return _get_xidata(tstate, obj, fallback, xidata);
538
0
        case _PyXIDATA_FULL_FALLBACK:
539
0
            if (_get_xidata(tstate, obj, fallback, xidata) == 0) {
540
0
                return 0;
541
0
            }
542
0
            PyObject *exc = _PyErr_GetRaisedException(tstate);
543
0
            if (PyFunction_Check(obj)) {
544
0
                if (_PyFunction_GetXIData(tstate, obj, xidata) == 0) {
545
0
                    Py_DECREF(exc);
546
0
                    return 0;
547
0
                }
548
0
                _PyErr_Clear(tstate);
549
0
            }
550
            // We could try _PyMarshal_GetXIData() but we won't for now.
551
0
            if (_PyPickle_GetXIData(tstate, obj, xidata) == 0) {
552
0
                Py_DECREF(exc);
553
0
                return 0;
554
0
            }
555
            // Raise the original exception.
556
0
            _PyErr_SetRaisedException(tstate, exc);
557
0
            return -1;
558
0
        default:
559
#ifdef Py_DEBUG
560
            Py_FatalError("unsupported xidata fallback option");
561
#endif
562
0
            _PyErr_SetString(tstate, PyExc_SystemError,
563
0
                             "unsupported xidata fallback option");
564
0
            return -1;
565
0
    }
566
0
}
567
568
569
/* pickle C-API */
570
571
/* Per-interpreter cache for pickle.dumps and pickle.loads.
572
 *
573
 * Each interpreter has its own cache in _PyXI_state_t.pickle, preserving
574
 * interpreter isolation.  The cache is populated lazily on first use and
575
 * cleared during interpreter finalization in _Py_xi_state_fini().
576
 *
577
 * Note: the cached references are captured at first use and not invalidated
578
 * on module reload.  This matches the caching pattern used elsewhere in
579
 * CPython (e.g. arraymodule.c, _decimal.c). */
580
581
static PyObject *
582
_get_pickle_dumps(PyThreadState *tstate)
583
0
{
584
0
    _PyXI_state_t *state = _PyXI_GET_STATE(tstate->interp);
585
0
    PyObject *dumps = state->pickle.dumps;
586
0
    if (dumps != NULL) {
587
0
        return dumps;
588
0
    }
589
0
    dumps = PyImport_ImportModuleAttrString("pickle", "dumps");
590
0
    if (dumps == NULL) {
591
0
        return NULL;
592
0
    }
593
0
    state->pickle.dumps = dumps;  // owns the reference
594
0
    return dumps;
595
0
}
596
597
static PyObject *
598
_get_pickle_loads(PyThreadState *tstate)
599
0
{
600
0
    _PyXI_state_t *state = _PyXI_GET_STATE(tstate->interp);
601
0
    PyObject *loads = state->pickle.loads;
602
0
    if (loads != NULL) {
603
0
        return loads;
604
0
    }
605
0
    loads = PyImport_ImportModuleAttrString("pickle", "loads");
606
0
    if (loads == NULL) {
607
0
        return NULL;
608
0
    }
609
0
    state->pickle.loads = loads;  // owns the reference
610
0
    return loads;
611
0
}
612
613
struct _pickle_context {
614
    PyThreadState *tstate;
615
};
616
617
static PyObject *
618
_PyPickle_Dumps(struct _pickle_context *ctx, PyObject *obj)
619
0
{
620
0
    PyObject *dumps = _get_pickle_dumps(ctx->tstate);
621
0
    if (dumps == NULL) {
622
0
        return NULL;
623
0
    }
624
    // dumps is a borrowed reference from the cache.
625
0
    return PyObject_CallOneArg(dumps, obj);
626
0
}
627
628
629
struct _unpickle_context {
630
    PyThreadState *tstate;
631
    // We only special-case the __main__ module,
632
    // since other modules behave consistently.
633
    struct sync_module main;
634
};
635
636
static void
637
_unpickle_context_clear(struct _unpickle_context *ctx)
638
0
{
639
0
    sync_module_clear(&ctx->main);
640
0
}
641
642
static int
643
check_missing___main___attr(PyObject *exc)
644
0
{
645
0
    assert(!PyErr_Occurred());
646
0
    if (!PyErr_GivenExceptionMatches(exc, PyExc_AttributeError)) {
647
0
        return 0;
648
0
    }
649
650
    // Get the error message.
651
0
    PyObject *args = PyException_GetArgs(exc);
652
0
    if (args == NULL || args == Py_None || PyObject_Size(args) < 1) {
653
0
        Py_XDECREF(args);
654
0
        assert(!PyErr_Occurred());
655
0
        return 0;
656
0
    }
657
0
    PyObject *msgobj = args;
658
0
    if (!PyUnicode_Check(msgobj)) {
659
0
        msgobj = PySequence_GetItem(args, 0);
660
0
        Py_DECREF(args);
661
0
        if (msgobj == NULL) {
662
0
            PyErr_Clear();
663
0
            return 0;
664
0
        }
665
0
    }
666
0
    const char *err = PyUnicode_AsUTF8(msgobj);
667
668
    // Check if it's a missing __main__ attr.
669
0
    int cmp = strncmp(err, "module '__main__' has no attribute '", 36);
670
0
    Py_DECREF(msgobj);
671
0
    return cmp == 0;
672
0
}
673
674
static PyObject *
675
_PyPickle_Loads(struct _unpickle_context *ctx, PyObject *pickled)
676
0
{
677
0
    PyThreadState *tstate = ctx->tstate;
678
679
0
    PyObject *exc = NULL;
680
    // loads is a borrowed reference from the per-interpreter cache.
681
0
    PyObject *loads = _get_pickle_loads(tstate);
682
0
    if (loads == NULL) {
683
0
        return NULL;
684
0
    }
685
686
    // Make an initial attempt to unpickle.
687
0
    PyObject *obj = PyObject_CallOneArg(loads, pickled);
688
0
    if (obj != NULL) {
689
0
        goto finally;
690
0
    }
691
0
    assert(_PyErr_Occurred(tstate));
692
0
    if (ctx == NULL) {
693
0
        goto finally;
694
0
    }
695
0
    exc = _PyErr_GetRaisedException(tstate);
696
0
    if (!check_missing___main___attr(exc)) {
697
0
        goto finally;
698
0
    }
699
700
    // Temporarily swap in a fake __main__ loaded from the original
701
    // file and cached.  Note that functions will use the cached ns
702
    // for __globals__, // not the actual module.
703
0
    if (ensure_isolated_main(tstate, &ctx->main) < 0) {
704
0
        goto finally;
705
0
    }
706
0
    if (apply_isolated_main(tstate, &ctx->main) < 0) {
707
0
        goto finally;
708
0
    }
709
710
    // Try to unpickle once more.
711
0
    obj = PyObject_CallOneArg(loads, pickled);
712
0
    restore_main(tstate, &ctx->main);
713
0
    if (obj == NULL) {
714
0
        goto finally;
715
0
    }
716
0
    Py_CLEAR(exc);
717
718
0
finally:
719
0
    if (exc != NULL) {
720
0
        if (_PyErr_Occurred(tstate)) {
721
0
            sync_module_capture_exc(tstate, &ctx->main);
722
0
        }
723
        // We restore the original exception.
724
        // It might make sense to chain it (__context__).
725
0
        _PyErr_SetRaisedException(tstate, exc);
726
0
    }
727
0
    return obj;
728
0
}
729
730
731
/* pickle wrapper */
732
733
struct _pickle_xid_context {
734
    // __main__.__file__
735
    struct {
736
        const char *utf8;
737
        size_t len;
738
        char _utf8[MAXPATHLEN+1];
739
    } mainfile;
740
};
741
742
static int
743
_set_pickle_xid_context(PyThreadState *tstate, struct _pickle_xid_context *ctx)
744
0
{
745
    // Set mainfile if possible.
746
0
    Py_ssize_t len = _Py_GetMainfile(ctx->mainfile._utf8, MAXPATHLEN);
747
0
    if (len < 0) {
748
        // For now we ignore any exceptions.
749
0
        PyErr_Clear();
750
0
    }
751
0
    else if (len > 0) {
752
0
        ctx->mainfile.utf8 = ctx->mainfile._utf8;
753
0
        ctx->mainfile.len = (size_t)len;
754
0
    }
755
756
0
    return 0;
757
0
}
758
759
760
struct _shared_pickle_data {
761
    _PyBytes_data_t pickled;  // Must be first if we use _PyBytes_FromXIData().
762
    struct _pickle_xid_context ctx;
763
};
764
765
PyObject *
766
_PyPickle_LoadFromXIData(_PyXIData_t *xidata)
767
0
{
768
0
    PyThreadState *tstate = _PyThreadState_GET();
769
0
    struct _shared_pickle_data *shared =
770
0
                            (struct _shared_pickle_data *)xidata->data;
771
    // We avoid copying the pickled data by wrapping it in a memoryview.
772
    // The alternative is to get a bytes object using _PyBytes_FromXIData().
773
0
    PyObject *pickled = PyMemoryView_FromMemory(
774
0
            (char *)shared->pickled.bytes, shared->pickled.len, PyBUF_READ);
775
0
    if (pickled == NULL) {
776
0
        return NULL;
777
0
    }
778
779
    // Unpickle the object.
780
0
    struct _unpickle_context ctx = {
781
0
        .tstate = tstate,
782
0
        .main = {
783
0
            .filename = shared->ctx.mainfile.utf8,
784
0
        },
785
0
    };
786
0
    PyObject *obj = _PyPickle_Loads(&ctx, pickled);
787
0
    Py_DECREF(pickled);
788
0
    _unpickle_context_clear(&ctx);
789
0
    if (obj == NULL) {
790
0
        PyObject *cause = _PyErr_GetRaisedException(tstate);
791
0
        assert(cause != NULL);
792
0
        _set_xid_lookup_failure(
793
0
                    tstate, NULL, "object could not be unpickled", cause);
794
0
        Py_DECREF(cause);
795
0
    }
796
0
    return obj;
797
0
}
798
799
800
int
801
_PyPickle_GetXIData(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
802
0
{
803
    // Pickle the object.
804
0
    struct _pickle_context ctx = {
805
0
        .tstate = tstate,
806
0
    };
807
0
    PyObject *bytes = _PyPickle_Dumps(&ctx, obj);
808
0
    if (bytes == NULL) {
809
0
        PyObject *cause = _PyErr_GetRaisedException(tstate);
810
0
        assert(cause != NULL);
811
0
        _set_xid_lookup_failure(
812
0
                    tstate, NULL, "object could not be pickled", cause);
813
0
        Py_DECREF(cause);
814
0
        return -1;
815
0
    }
816
817
    // If we had an "unwrapper" mechnanism, we could call
818
    // _PyObject_GetXIData() on the bytes object directly and add
819
    // a simple unwrapper to call pickle.loads() on the bytes.
820
0
    size_t size = sizeof(struct _shared_pickle_data);
821
0
    struct _shared_pickle_data *shared =
822
0
            (struct _shared_pickle_data *)_PyBytes_GetXIDataWrapped(
823
0
                    tstate, bytes, size, _PyPickle_LoadFromXIData, xidata);
824
0
    Py_DECREF(bytes);
825
0
    if (shared == NULL) {
826
0
        return -1;
827
0
    }
828
829
    // If it mattered, we could skip getting __main__.__file__
830
    // when "__main__" doesn't show up in the pickle bytes.
831
0
    if (_set_pickle_xid_context(tstate, &shared->ctx) < 0) {
832
0
        _xidata_clear(xidata);
833
0
        return -1;
834
0
    }
835
836
0
    return 0;
837
0
}
838
839
840
/* marshal wrapper */
841
842
PyObject *
843
_PyMarshal_ReadObjectFromXIData(_PyXIData_t *xidata)
844
0
{
845
0
    PyThreadState *tstate = _PyThreadState_GET();
846
0
    _PyBytes_data_t *shared = (_PyBytes_data_t *)xidata->data;
847
0
    PyObject *obj = PyMarshal_ReadObjectFromString(shared->bytes, shared->len);
848
0
    if (obj == NULL) {
849
0
        PyObject *cause = _PyErr_GetRaisedException(tstate);
850
0
        assert(cause != NULL);
851
0
        _set_xid_lookup_failure(
852
0
                    tstate, NULL, "object could not be unmarshalled", cause);
853
0
        Py_DECREF(cause);
854
0
        return NULL;
855
0
    }
856
0
    return obj;
857
0
}
858
859
int
860
_PyMarshal_GetXIData(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
861
0
{
862
0
    PyObject *bytes = PyMarshal_WriteObjectToString(obj, Py_MARSHAL_VERSION);
863
0
    if (bytes == NULL) {
864
0
        PyObject *cause = _PyErr_GetRaisedException(tstate);
865
0
        assert(cause != NULL);
866
0
        _set_xid_lookup_failure(
867
0
                    tstate, NULL, "object could not be marshalled", cause);
868
0
        Py_DECREF(cause);
869
0
        return -1;
870
0
    }
871
0
    size_t size = sizeof(_PyBytes_data_t);
872
0
    _PyBytes_data_t *shared = _PyBytes_GetXIDataWrapped(
873
0
            tstate, bytes, size, _PyMarshal_ReadObjectFromXIData, xidata);
874
0
    Py_DECREF(bytes);
875
0
    if (shared == NULL) {
876
0
        return -1;
877
0
    }
878
0
    return 0;
879
0
}
880
881
882
/* script wrapper */
883
884
static int
885
verify_script(PyThreadState *tstate, PyCodeObject *co, int checked, int pure)
886
0
{
887
    // Make sure it isn't a closure and (optionally) doesn't use globals.
888
0
    PyObject *builtins = NULL;
889
0
    if (pure) {
890
0
        builtins = _PyEval_GetBuiltins(tstate);
891
0
        assert(builtins != NULL);
892
0
    }
893
0
    if (checked) {
894
0
        assert(_PyCode_VerifyStateless(tstate, co, NULL, NULL, builtins) == 0);
895
0
    }
896
0
    else if (_PyCode_VerifyStateless(tstate, co, NULL, NULL, builtins) < 0) {
897
0
        return -1;
898
0
    }
899
    // Make sure it doesn't have args.
900
0
    if (co->co_argcount > 0
901
0
        || co->co_posonlyargcount > 0
902
0
        || co->co_kwonlyargcount > 0
903
0
        || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS))
904
0
    {
905
0
        _PyErr_SetString(tstate, PyExc_ValueError,
906
0
                         "code with args not supported");
907
0
        return -1;
908
0
    }
909
    // Make sure it doesn't return anything.
910
0
    if (!_PyCode_ReturnsOnlyNone(co)) {
911
0
        _PyErr_SetString(tstate, PyExc_ValueError,
912
0
                         "code that returns a value is not a script");
913
0
        return -1;
914
0
    }
915
0
    return 0;
916
0
}
917
918
static int
919
get_script_xidata(PyThreadState *tstate, PyObject *obj, int pure,
920
                  _PyXIData_t *xidata)
921
0
{
922
    // Get the corresponding code object.
923
0
    PyObject *code = NULL;
924
0
    int checked = 0;
925
0
    if (PyCode_Check(obj)) {
926
0
        code = obj;
927
0
        Py_INCREF(code);
928
0
    }
929
0
    else if (PyFunction_Check(obj)) {
930
0
        code = PyFunction_GET_CODE(obj);
931
0
        assert(code != NULL);
932
0
        Py_INCREF(code);
933
0
        if (pure) {
934
0
            if (_PyFunction_VerifyStateless(tstate, obj) < 0) {
935
0
                goto error;
936
0
            }
937
0
            checked = 1;
938
0
        }
939
0
    }
940
0
    else {
941
0
        const char *filename = "<script>";
942
0
        int optimize = 0;
943
0
        PyCompilerFlags cf = _PyCompilerFlags_INIT;
944
0
        cf.cf_flags = PyCF_SOURCE_IS_UTF8;
945
0
        PyObject *ref = NULL;
946
0
        const char *script = _Py_SourceAsString(obj, "???", "???", &cf, &ref);
947
0
        if (script == NULL) {
948
0
            if (!_PyObject_SupportedAsScript(obj)) {
949
                // We discard the raised exception.
950
0
                _PyErr_Format(tstate, PyExc_TypeError,
951
0
                              "unsupported script %R", obj);
952
0
            }
953
0
            goto error;
954
0
        }
955
#ifdef Py_GIL_DISABLED
956
        // Don't immortalize code constants to avoid memory leaks.
957
        ((_PyThreadStateImpl *)tstate)->suppress_co_const_immortalization++;
958
#endif
959
0
        code = Py_CompileStringExFlags(
960
0
                    script, filename, Py_file_input, &cf, optimize);
961
#ifdef Py_GIL_DISABLED
962
        ((_PyThreadStateImpl *)tstate)->suppress_co_const_immortalization--;
963
#endif
964
0
        Py_XDECREF(ref);
965
0
        if (code == NULL) {
966
0
            goto error;
967
0
        }
968
        // Compiled text can't have args or any return statements,
969
        // nor be a closure.  It can use globals though.
970
0
        if (!pure) {
971
            // We don't need to check for globals either.
972
0
            checked = 1;
973
0
        }
974
0
    }
975
976
    // Make sure it's actually a script.
977
0
    if (verify_script(tstate, (PyCodeObject *)code, checked, pure) < 0) {
978
0
        goto error;
979
0
    }
980
981
    // Convert the code object.
982
0
    int res = _PyCode_GetXIData(tstate, code, xidata);
983
0
    Py_DECREF(code);
984
0
    if (res < 0) {
985
0
        return -1;
986
0
    }
987
0
    return 0;
988
989
0
error:
990
0
    Py_XDECREF(code);
991
0
    PyObject *cause = _PyErr_GetRaisedException(tstate);
992
0
    assert(cause != NULL);
993
0
    _set_xid_lookup_failure(
994
0
                tstate, NULL, "object not a valid script", cause);
995
0
    Py_DECREF(cause);
996
0
    return -1;
997
0
}
998
999
int
1000
_PyCode_GetScriptXIData(PyThreadState *tstate,
1001
                        PyObject *obj, _PyXIData_t *xidata)
1002
0
{
1003
0
    return get_script_xidata(tstate, obj, 0, xidata);
1004
0
}
1005
1006
int
1007
_PyCode_GetPureScriptXIData(PyThreadState *tstate,
1008
                            PyObject *obj, _PyXIData_t *xidata)
1009
0
{
1010
0
    return get_script_xidata(tstate, obj, 1, xidata);
1011
0
}
1012
1013
1014
/* using cross-interpreter data */
1015
1016
PyObject *
1017
_PyXIData_NewObject(_PyXIData_t *xidata)
1018
0
{
1019
0
    return xidata->new_object(xidata);
1020
0
}
1021
1022
static int
1023
_call_clear_xidata(void *data)
1024
0
{
1025
0
    _xidata_clear((_PyXIData_t *)data);
1026
0
    return 0;
1027
0
}
1028
1029
static int
1030
_xidata_release(_PyXIData_t *xidata, int rawfree)
1031
0
{
1032
0
    if ((xidata->data == NULL || xidata->free == NULL) && xidata->obj == NULL) {
1033
        // Nothing to release!
1034
0
        if (rawfree) {
1035
0
            PyMem_RawFree(xidata);
1036
0
        }
1037
0
        else {
1038
0
            xidata->data = NULL;
1039
0
        }
1040
0
        return 0;
1041
0
    }
1042
1043
    // Switch to the original interpreter.
1044
0
    PyInterpreterState *interp = _PyInterpreterState_LookUpID(
1045
0
                                        _PyXIData_INTERPID(xidata));
1046
0
    if (interp == NULL) {
1047
        // The interpreter was already destroyed.
1048
        // This function shouldn't have been called.
1049
        // XXX Someone leaked some memory...
1050
0
        assert(PyErr_Occurred());
1051
0
        if (rawfree) {
1052
0
            PyMem_RawFree(xidata);
1053
0
        }
1054
0
        return -1;
1055
0
    }
1056
1057
    // "Release" the data and/or the object.
1058
0
    if (rawfree) {
1059
0
        return _Py_CallInInterpreterAndRawFree(interp, _call_clear_xidata, xidata);
1060
0
    }
1061
0
    else {
1062
0
        return _Py_CallInInterpreter(interp, _call_clear_xidata, xidata);
1063
0
    }
1064
0
}
1065
1066
int
1067
_PyXIData_Release(_PyXIData_t *xidata)
1068
0
{
1069
0
    return _xidata_release(xidata, 0);
1070
0
}
1071
1072
int
1073
_PyXIData_ReleaseAndRawFree(_PyXIData_t *xidata)
1074
0
{
1075
0
    return _xidata_release(xidata, 1);
1076
0
}
1077
1078
1079
/*************************/
1080
/* convenience utilities */
1081
/*************************/
1082
1083
static char *
1084
_copy_string_obj_raw(PyObject *strobj, Py_ssize_t *p_size)
1085
0
{
1086
0
    Py_ssize_t size = -1;
1087
0
    const char *str = PyUnicode_AsUTF8AndSize(strobj, &size);
1088
0
    if (str == NULL) {
1089
0
        return NULL;
1090
0
    }
1091
1092
0
    if (size != (Py_ssize_t)strlen(str)) {
1093
0
        PyErr_SetString(PyExc_ValueError, "found embedded NULL character");
1094
0
        return NULL;
1095
0
    }
1096
1097
0
    char *copied = PyMem_RawMalloc(size+1);
1098
0
    if (copied == NULL) {
1099
0
        PyErr_NoMemory();
1100
0
        return NULL;
1101
0
    }
1102
0
    strcpy(copied, str);
1103
0
    if (p_size != NULL) {
1104
0
        *p_size = size;
1105
0
    }
1106
0
    return copied;
1107
0
}
1108
1109
1110
static int
1111
_convert_exc_to_TracebackException(PyObject *exc, PyObject **p_tbexc)
1112
0
{
1113
0
    PyObject *args = NULL;
1114
0
    PyObject *kwargs = NULL;
1115
0
    PyObject *create = NULL;
1116
1117
    // This is inspired by _PyErr_Display().
1118
0
    PyObject *tbexc_type = PyImport_ImportModuleAttrString(
1119
0
        "traceback",
1120
0
        "TracebackException");
1121
0
    if (tbexc_type == NULL) {
1122
0
        return -1;
1123
0
    }
1124
0
    create = PyObject_GetAttrString(tbexc_type, "from_exception");
1125
0
    Py_DECREF(tbexc_type);
1126
0
    if (create == NULL) {
1127
0
        return -1;
1128
0
    }
1129
1130
0
    args = PyTuple_Pack(1, exc);
1131
0
    if (args == NULL) {
1132
0
        goto error;
1133
0
    }
1134
1135
0
    kwargs = PyDict_New();
1136
0
    if (kwargs == NULL) {
1137
0
        goto error;
1138
0
    }
1139
0
    if (PyDict_SetItemString(kwargs, "save_exc_type", Py_False) < 0) {
1140
0
        goto error;
1141
0
    }
1142
0
    if (PyDict_SetItemString(kwargs, "lookup_lines", Py_False) < 0) {
1143
0
        goto error;
1144
0
    }
1145
1146
0
    PyObject *tbexc = PyObject_Call(create, args, kwargs);
1147
0
    if (tbexc == NULL) {
1148
0
        goto error;
1149
0
    }
1150
0
    Py_DECREF(args);
1151
0
    Py_DECREF(kwargs);
1152
0
    Py_DECREF(create);
1153
1154
0
    *p_tbexc = tbexc;
1155
0
    return 0;
1156
1157
0
error:
1158
0
    Py_XDECREF(args);
1159
0
    Py_XDECREF(kwargs);
1160
0
    Py_XDECREF(create);
1161
0
    return -1;
1162
0
}
1163
1164
// We accommodate backports here.
1165
#ifndef _Py_EMPTY_STR
1166
0
# define _Py_EMPTY_STR &_Py_STR(empty)
1167
#endif
1168
1169
static const char *
1170
_format_TracebackException(PyObject *tbexc)
1171
0
{
1172
0
    PyObject *lines = PyObject_CallMethod(tbexc, "format", NULL);
1173
0
    if (lines == NULL) {
1174
0
        return NULL;
1175
0
    }
1176
0
    assert(_Py_EMPTY_STR != NULL);
1177
0
    PyObject *formatted_obj = PyUnicode_Join(_Py_EMPTY_STR, lines);
1178
0
    Py_DECREF(lines);
1179
0
    if (formatted_obj == NULL) {
1180
0
        return NULL;
1181
0
    }
1182
1183
0
    Py_ssize_t size = -1;
1184
0
    char *formatted = _copy_string_obj_raw(formatted_obj, &size);
1185
0
    Py_DECREF(formatted_obj);
1186
0
    if (formatted == NULL || size == 0) {
1187
0
        return formatted;
1188
0
    }
1189
0
    assert(formatted[size] == '\0');
1190
    // Remove a trailing newline if needed.
1191
0
    if (formatted[size-1] == '\n') {
1192
0
        formatted[size-1] = '\0';
1193
0
    }
1194
0
    return formatted;
1195
0
}
1196
1197
1198
static int
1199
_release_xid_data(_PyXIData_t *xidata, int rawfree)
1200
0
{
1201
0
    PyObject *exc = PyErr_GetRaisedException();
1202
0
    int res = rawfree
1203
0
        ? _PyXIData_ReleaseAndRawFree(xidata)
1204
0
        : _PyXIData_Release(xidata);
1205
0
    if (res < 0) {
1206
        /* The owning interpreter is already destroyed. */
1207
0
        _PyXIData_Clear(NULL, xidata);
1208
        // XXX Emit a warning?
1209
0
        PyErr_Clear();
1210
0
    }
1211
0
    PyErr_SetRaisedException(exc);
1212
0
    return res;
1213
0
}
1214
1215
1216
/***********************/
1217
/* exception snapshots */
1218
/***********************/
1219
1220
static int
1221
_excinfo_init_type_from_exception(struct _excinfo_type *info, PyObject *exc)
1222
0
{
1223
    /* Note that this copies directly rather than into an intermediate
1224
       struct and does not clear on error.  If we need that then we
1225
       should have a separate function to wrap this one
1226
       and do all that there. */
1227
0
    PyObject *strobj = NULL;
1228
1229
0
    PyTypeObject *type = Py_TYPE(exc);
1230
0
    if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
1231
0
        assert(_Py_IsImmortal((PyObject *)type));
1232
0
        info->builtin = type;
1233
0
    }
1234
0
    else {
1235
        // Only builtin types are preserved.
1236
0
        info->builtin = NULL;
1237
0
    }
1238
1239
    // __name__
1240
0
    strobj = PyType_GetName(type);
1241
0
    if (strobj == NULL) {
1242
0
        return -1;
1243
0
    }
1244
0
    info->name = _copy_string_obj_raw(strobj, NULL);
1245
0
    Py_DECREF(strobj);
1246
0
    if (info->name == NULL) {
1247
0
        return -1;
1248
0
    }
1249
1250
    // __qualname__
1251
0
    strobj = PyType_GetQualName(type);
1252
0
    if (strobj == NULL) {
1253
0
        return -1;
1254
0
    }
1255
0
    info->qualname = _copy_string_obj_raw(strobj, NULL);
1256
0
    Py_DECREF(strobj);
1257
0
    if (info->qualname == NULL) {
1258
0
        return -1;
1259
0
    }
1260
1261
    // __module__
1262
0
    strobj = PyType_GetModuleName(type);
1263
0
    if (strobj == NULL) {
1264
0
        return -1;
1265
0
    }
1266
0
    info->module = _copy_string_obj_raw(strobj, NULL);
1267
0
    Py_DECREF(strobj);
1268
0
    if (info->module == NULL) {
1269
0
        return -1;
1270
0
    }
1271
1272
0
    return 0;
1273
0
}
1274
1275
static int
1276
_excinfo_init_type_from_object(struct _excinfo_type *info, PyObject *exctype)
1277
0
{
1278
0
    PyObject *strobj = NULL;
1279
1280
    // __name__
1281
0
    strobj = PyObject_GetAttrString(exctype, "__name__");
1282
0
    if (strobj == NULL) {
1283
0
        return -1;
1284
0
    }
1285
0
    info->name = _copy_string_obj_raw(strobj, NULL);
1286
0
    Py_DECREF(strobj);
1287
0
    if (info->name == NULL) {
1288
0
        return -1;
1289
0
    }
1290
1291
    // __qualname__
1292
0
    strobj = PyObject_GetAttrString(exctype, "__qualname__");
1293
0
    if (strobj == NULL) {
1294
0
        return -1;
1295
0
    }
1296
0
    info->qualname = _copy_string_obj_raw(strobj, NULL);
1297
0
    Py_DECREF(strobj);
1298
0
    if (info->qualname == NULL) {
1299
0
        return -1;
1300
0
    }
1301
1302
    // __module__
1303
0
    strobj = PyObject_GetAttrString(exctype, "__module__");
1304
0
    if (strobj == NULL) {
1305
0
        return -1;
1306
0
    }
1307
0
    info->module = _copy_string_obj_raw(strobj, NULL);
1308
0
    Py_DECREF(strobj);
1309
0
    if (info->module == NULL) {
1310
0
        return -1;
1311
0
    }
1312
1313
0
    return 0;
1314
0
}
1315
1316
static void
1317
_excinfo_clear_type(struct _excinfo_type *info)
1318
0
{
1319
0
    if (info->builtin != NULL) {
1320
0
        assert(info->builtin->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
1321
0
        assert(_Py_IsImmortal((PyObject *)info->builtin));
1322
0
    }
1323
0
    if (info->name != NULL) {
1324
0
        PyMem_RawFree((void *)info->name);
1325
0
    }
1326
0
    if (info->qualname != NULL) {
1327
0
        PyMem_RawFree((void *)info->qualname);
1328
0
    }
1329
0
    if (info->module != NULL) {
1330
0
        PyMem_RawFree((void *)info->module);
1331
0
    }
1332
0
    *info = (struct _excinfo_type){NULL};
1333
0
}
1334
1335
static void
1336
_excinfo_normalize_type(struct _excinfo_type *info,
1337
                        const char **p_module, const char **p_qualname)
1338
0
{
1339
0
    if (info->name == NULL) {
1340
0
        assert(info->builtin == NULL);
1341
0
        assert(info->qualname == NULL);
1342
0
        assert(info->module == NULL);
1343
        // This is inspired by TracebackException.format_exception_only().
1344
0
        *p_module = NULL;
1345
0
        *p_qualname = NULL;
1346
0
        return;
1347
0
    }
1348
1349
0
    const char *module = info->module;
1350
0
    const char *qualname = info->qualname;
1351
0
    if (qualname == NULL) {
1352
0
        qualname = info->name;
1353
0
    }
1354
0
    assert(module != NULL);
1355
0
    if (strcmp(module, "builtins") == 0) {
1356
0
        module = NULL;
1357
0
    }
1358
0
    else if (strcmp(module, "__main__") == 0) {
1359
0
        module = NULL;
1360
0
    }
1361
0
    *p_qualname = qualname;
1362
0
    *p_module = module;
1363
0
}
1364
1365
static int
1366
excinfo_is_set(_PyXI_excinfo *info)
1367
0
{
1368
0
    return info->type.name != NULL || info->msg != NULL;
1369
0
}
1370
1371
static void
1372
_PyXI_excinfo_clear(_PyXI_excinfo *info)
1373
0
{
1374
0
    _excinfo_clear_type(&info->type);
1375
0
    if (info->msg != NULL) {
1376
0
        PyMem_RawFree((void *)info->msg);
1377
0
    }
1378
0
    if (info->errdisplay != NULL) {
1379
0
        PyMem_RawFree((void *)info->errdisplay);
1380
0
    }
1381
0
    *info = (_PyXI_excinfo){{NULL}};
1382
0
}
1383
1384
PyObject *
1385
_PyXI_excinfo_format(_PyXI_excinfo *info)
1386
0
{
1387
0
    const char *module, *qualname;
1388
0
    _excinfo_normalize_type(&info->type, &module, &qualname);
1389
0
    if (qualname != NULL) {
1390
0
        if (module != NULL) {
1391
0
            if (info->msg != NULL) {
1392
0
                return PyUnicode_FromFormat("%s.%s: %s",
1393
0
                                            module, qualname, info->msg);
1394
0
            }
1395
0
            else {
1396
0
                return PyUnicode_FromFormat("%s.%s", module, qualname);
1397
0
            }
1398
0
        }
1399
0
        else {
1400
0
            if (info->msg != NULL) {
1401
0
                return PyUnicode_FromFormat("%s: %s", qualname, info->msg);
1402
0
            }
1403
0
            else {
1404
0
                return PyUnicode_FromString(qualname);
1405
0
            }
1406
0
        }
1407
0
    }
1408
0
    else if (info->msg != NULL) {
1409
0
        return PyUnicode_FromString(info->msg);
1410
0
    }
1411
0
    else {
1412
0
        Py_RETURN_NONE;
1413
0
    }
1414
0
}
1415
1416
static const char *
1417
_PyXI_excinfo_InitFromException(_PyXI_excinfo *info, PyObject *exc)
1418
0
{
1419
0
    assert(exc != NULL);
1420
1421
0
    if (PyErr_GivenExceptionMatches(exc, PyExc_MemoryError)) {
1422
0
        _PyXI_excinfo_clear(info);
1423
0
        return NULL;
1424
0
    }
1425
0
    const char *failure = NULL;
1426
1427
0
    if (_excinfo_init_type_from_exception(&info->type, exc) < 0) {
1428
0
        failure = "error while initializing exception type snapshot";
1429
0
        goto error;
1430
0
    }
1431
1432
    // Extract the exception message.
1433
0
    PyObject *msgobj = PyObject_Str(exc);
1434
0
    if (msgobj == NULL) {
1435
0
        failure = "error while formatting exception";
1436
0
        goto error;
1437
0
    }
1438
0
    info->msg = _copy_string_obj_raw(msgobj, NULL);
1439
0
    Py_DECREF(msgobj);
1440
0
    if (info->msg == NULL) {
1441
0
        failure = "error while copying exception message";
1442
0
        goto error;
1443
0
    }
1444
1445
    // Pickle a traceback.TracebackException.
1446
0
    PyObject *tbexc = NULL;
1447
0
    if (_convert_exc_to_TracebackException(exc, &tbexc) < 0) {
1448
#ifdef Py_DEBUG
1449
        PyErr_FormatUnraisable("Exception ignored while creating TracebackException");
1450
#endif
1451
0
        PyErr_Clear();
1452
0
    }
1453
0
    else {
1454
0
        info->errdisplay = _format_TracebackException(tbexc);
1455
0
        Py_DECREF(tbexc);
1456
0
        if (info->errdisplay == NULL) {
1457
#ifdef Py_DEBUG
1458
            PyErr_FormatUnraisable("Exception ignored while formatting TracebackException");
1459
#endif
1460
0
            PyErr_Clear();
1461
0
        }
1462
0
    }
1463
1464
0
    return NULL;
1465
1466
0
error:
1467
0
    assert(failure != NULL);
1468
0
    _PyXI_excinfo_clear(info);
1469
0
    return failure;
1470
0
}
1471
1472
static const char *
1473
_PyXI_excinfo_InitFromObject(_PyXI_excinfo *info, PyObject *obj)
1474
0
{
1475
0
    const char *failure = NULL;
1476
1477
0
    PyObject *exctype = PyObject_GetAttrString(obj, "type");
1478
0
    if (exctype == NULL) {
1479
0
        failure = "exception snapshot missing 'type' attribute";
1480
0
        goto error;
1481
0
    }
1482
0
    int res = _excinfo_init_type_from_object(&info->type, exctype);
1483
0
    Py_DECREF(exctype);
1484
0
    if (res < 0) {
1485
0
        failure = "error while initializing exception type snapshot";
1486
0
        goto error;
1487
0
    }
1488
1489
    // Extract the exception message.
1490
0
    PyObject *msgobj = PyObject_GetAttrString(obj, "msg");
1491
0
    if (msgobj == NULL) {
1492
0
        failure = "exception snapshot missing 'msg' attribute";
1493
0
        goto error;
1494
0
    }
1495
0
    info->msg = _copy_string_obj_raw(msgobj, NULL);
1496
0
    Py_DECREF(msgobj);
1497
0
    if (info->msg == NULL) {
1498
0
        failure = "error while copying exception message";
1499
0
        goto error;
1500
0
    }
1501
1502
    // Pickle a traceback.TracebackException.
1503
0
    PyObject *errdisplay = PyObject_GetAttrString(obj, "errdisplay");
1504
0
    if (errdisplay == NULL) {
1505
0
        failure = "exception snapshot missing 'errdisplay' attribute";
1506
0
        goto error;
1507
0
    }
1508
0
    info->errdisplay = _copy_string_obj_raw(errdisplay, NULL);
1509
0
    Py_DECREF(errdisplay);
1510
0
    if (info->errdisplay == NULL) {
1511
0
        failure = "error while copying exception error display";
1512
0
        goto error;
1513
0
    }
1514
1515
0
    return NULL;
1516
1517
0
error:
1518
0
    assert(failure != NULL);
1519
0
    _PyXI_excinfo_clear(info);
1520
0
    return failure;
1521
0
}
1522
1523
static void
1524
_PyXI_excinfo_Apply(_PyXI_excinfo *info, PyObject *exctype)
1525
0
{
1526
0
    PyObject *tbexc = NULL;
1527
0
    if (info->errdisplay != NULL) {
1528
0
        tbexc = PyUnicode_FromString(info->errdisplay);
1529
0
        if (tbexc == NULL) {
1530
0
            PyErr_Clear();
1531
0
        }
1532
0
        else {
1533
0
            PyErr_SetObject(exctype, tbexc);
1534
0
            Py_DECREF(tbexc);
1535
0
            return;
1536
0
        }
1537
0
    }
1538
1539
0
    PyObject *formatted = _PyXI_excinfo_format(info);
1540
0
    PyErr_SetObject(exctype, formatted);
1541
0
    Py_XDECREF(formatted);
1542
1543
0
    if (tbexc != NULL) {
1544
0
        PyObject *exc = PyErr_GetRaisedException();
1545
0
        if (PyObject_SetAttrString(exc, "_errdisplay", tbexc) < 0) {
1546
#ifdef Py_DEBUG
1547
            PyErr_FormatUnraisable("Exception ignored while "
1548
                                   "setting _errdisplay");
1549
#endif
1550
0
            PyErr_Clear();
1551
0
        }
1552
0
        Py_DECREF(tbexc);
1553
0
        PyErr_SetRaisedException(exc);
1554
0
    }
1555
0
}
1556
1557
static PyObject *
1558
_PyXI_excinfo_TypeAsObject(_PyXI_excinfo *info)
1559
0
{
1560
0
    PyObject *ns = _PyNamespace_New(NULL);
1561
0
    if (ns == NULL) {
1562
0
        return NULL;
1563
0
    }
1564
0
    int empty = 1;
1565
1566
0
    if (info->type.name != NULL) {
1567
0
        PyObject *name = PyUnicode_FromString(info->type.name);
1568
0
        if (name == NULL) {
1569
0
            goto error;
1570
0
        }
1571
0
        int res = PyObject_SetAttrString(ns, "__name__", name);
1572
0
        Py_DECREF(name);
1573
0
        if (res < 0) {
1574
0
            goto error;
1575
0
        }
1576
0
        empty = 0;
1577
0
    }
1578
1579
0
    if (info->type.qualname != NULL) {
1580
0
        PyObject *qualname = PyUnicode_FromString(info->type.qualname);
1581
0
        if (qualname == NULL) {
1582
0
            goto error;
1583
0
        }
1584
0
        int res = PyObject_SetAttrString(ns, "__qualname__", qualname);
1585
0
        Py_DECREF(qualname);
1586
0
        if (res < 0) {
1587
0
            goto error;
1588
0
        }
1589
0
        empty = 0;
1590
0
    }
1591
1592
0
    if (info->type.module != NULL) {
1593
0
        PyObject *module = PyUnicode_FromString(info->type.module);
1594
0
        if (module == NULL) {
1595
0
            goto error;
1596
0
        }
1597
0
        int res = PyObject_SetAttrString(ns, "__module__", module);
1598
0
        Py_DECREF(module);
1599
0
        if (res < 0) {
1600
0
            goto error;
1601
0
        }
1602
0
        empty = 0;
1603
0
    }
1604
1605
0
    if (empty) {
1606
0
        Py_CLEAR(ns);
1607
0
    }
1608
1609
0
    return ns;
1610
1611
0
error:
1612
0
    Py_DECREF(ns);
1613
0
    return NULL;
1614
0
}
1615
1616
static PyObject *
1617
_PyXI_excinfo_AsObject(_PyXI_excinfo *info)
1618
0
{
1619
0
    PyObject *ns = _PyNamespace_New(NULL);
1620
0
    if (ns == NULL) {
1621
0
        return NULL;
1622
0
    }
1623
0
    int res;
1624
1625
0
    PyObject *type = _PyXI_excinfo_TypeAsObject(info);
1626
0
    if (type == NULL) {
1627
0
        if (PyErr_Occurred()) {
1628
0
            goto error;
1629
0
        }
1630
0
        type = Py_NewRef(Py_None);
1631
0
    }
1632
0
    res = PyObject_SetAttrString(ns, "type", type);
1633
0
    Py_DECREF(type);
1634
0
    if (res < 0) {
1635
0
        goto error;
1636
0
    }
1637
1638
0
    PyObject *msg = info->msg != NULL
1639
0
        ? PyUnicode_FromString(info->msg)
1640
0
        : Py_NewRef(Py_None);
1641
0
    if (msg == NULL) {
1642
0
        goto error;
1643
0
    }
1644
0
    res = PyObject_SetAttrString(ns, "msg", msg);
1645
0
    Py_DECREF(msg);
1646
0
    if (res < 0) {
1647
0
        goto error;
1648
0
    }
1649
1650
0
    PyObject *formatted = _PyXI_excinfo_format(info);
1651
0
    if (formatted == NULL) {
1652
0
        goto error;
1653
0
    }
1654
0
    res = PyObject_SetAttrString(ns, "formatted", formatted);
1655
0
    Py_DECREF(formatted);
1656
0
    if (res < 0) {
1657
0
        goto error;
1658
0
    }
1659
1660
0
    if (info->errdisplay != NULL) {
1661
0
        PyObject *tbexc = PyUnicode_FromString(info->errdisplay);
1662
0
        if (tbexc == NULL) {
1663
0
            PyErr_Clear();
1664
0
        }
1665
0
        else {
1666
0
            res = PyObject_SetAttrString(ns, "errdisplay", tbexc);
1667
0
            Py_DECREF(tbexc);
1668
0
            if (res < 0) {
1669
0
                goto error;
1670
0
            }
1671
0
        }
1672
0
    }
1673
1674
0
    return ns;
1675
1676
0
error:
1677
0
    Py_DECREF(ns);
1678
0
    return NULL;
1679
0
}
1680
1681
1682
_PyXI_excinfo *
1683
_PyXI_NewExcInfo(PyObject *exc)
1684
0
{
1685
0
    assert(!PyErr_Occurred());
1686
0
    if (exc == NULL || exc == Py_None) {
1687
0
        PyErr_SetString(PyExc_ValueError, "missing exc");
1688
0
        return NULL;
1689
0
    }
1690
0
    _PyXI_excinfo *info = PyMem_RawCalloc(1, sizeof(_PyXI_excinfo));
1691
0
    if (info == NULL) {
1692
0
        return NULL;
1693
0
    }
1694
0
    const char *failure;
1695
0
    if (PyExceptionInstance_Check(exc) || PyExceptionClass_Check(exc)) {
1696
0
        failure = _PyXI_excinfo_InitFromException(info, exc);
1697
0
    }
1698
0
    else {
1699
0
        failure = _PyXI_excinfo_InitFromObject(info, exc);
1700
0
    }
1701
0
    if (failure != NULL) {
1702
0
        PyMem_RawFree(info);
1703
0
        set_exc_with_cause(PyExc_Exception, failure);
1704
0
        return NULL;
1705
0
    }
1706
0
    return info;
1707
0
}
1708
1709
void
1710
_PyXI_FreeExcInfo(_PyXI_excinfo *info)
1711
0
{
1712
0
    _PyXI_excinfo_clear(info);
1713
0
    PyMem_RawFree(info);
1714
0
}
1715
1716
PyObject *
1717
_PyXI_FormatExcInfo(_PyXI_excinfo *info)
1718
0
{
1719
0
    return _PyXI_excinfo_format(info);
1720
0
}
1721
1722
PyObject *
1723
_PyXI_ExcInfoAsObject(_PyXI_excinfo *info)
1724
0
{
1725
0
    return _PyXI_excinfo_AsObject(info);
1726
0
}
1727
1728
1729
/***************************/
1730
/* short-term data sharing */
1731
/***************************/
1732
1733
/* error codes */
1734
1735
static int
1736
_PyXI_ApplyErrorCode(_PyXI_errcode code, PyInterpreterState *interp)
1737
0
{
1738
0
    PyThreadState *tstate = _PyThreadState_GET();
1739
1740
0
    assert(!PyErr_Occurred());
1741
0
    assert(code != _PyXI_ERR_NO_ERROR);
1742
0
    assert(code != _PyXI_ERR_UNCAUGHT_EXCEPTION);
1743
0
    switch (code) {
1744
0
    case _PyXI_ERR_OTHER:
1745
        // XXX msg?
1746
0
        PyErr_SetNone(PyExc_InterpreterError);
1747
0
        break;
1748
0
    case _PyXI_ERR_NO_MEMORY:
1749
0
        PyErr_NoMemory();
1750
0
        break;
1751
0
    case _PyXI_ERR_ALREADY_RUNNING:
1752
0
        assert(interp != NULL);
1753
0
        _PyErr_SetInterpreterAlreadyRunning();
1754
0
        break;
1755
0
    case _PyXI_ERR_MAIN_NS_FAILURE:
1756
0
        PyErr_SetString(PyExc_InterpreterError,
1757
0
                        "failed to get __main__ namespace");
1758
0
        break;
1759
0
    case _PyXI_ERR_APPLY_NS_FAILURE:
1760
0
        PyErr_SetString(PyExc_InterpreterError,
1761
0
                        "failed to apply namespace to __main__");
1762
0
        break;
1763
0
    case _PyXI_ERR_PRESERVE_FAILURE:
1764
0
        PyErr_SetString(PyExc_InterpreterError,
1765
0
                        "failed to preserve objects across session");
1766
0
        break;
1767
0
    case _PyXI_ERR_EXC_PROPAGATION_FAILURE:
1768
0
        PyErr_SetString(PyExc_InterpreterError,
1769
0
                        "failed to transfer exception between interpreters");
1770
0
        break;
1771
0
    case _PyXI_ERR_NOT_SHAREABLE:
1772
0
        _set_xid_lookup_failure(tstate, NULL, NULL, NULL);
1773
0
        break;
1774
0
    default:
1775
#ifdef Py_DEBUG
1776
        Py_FatalError("unsupported error code");
1777
#else
1778
0
        PyErr_Format(PyExc_RuntimeError, "unsupported error code %d", code);
1779
0
#endif
1780
0
    }
1781
0
    assert(PyErr_Occurred());
1782
0
    return -1;
1783
0
}
1784
1785
/* basic failure info */
1786
1787
struct xi_failure {
1788
    // The kind of error to propagate.
1789
    _PyXI_errcode code;
1790
    // The propagated message.
1791
    const char *msg;
1792
    int msg_owned;
1793
};  // _PyXI_failure
1794
1795
0
#define XI_FAILURE_INIT (_PyXI_failure){ .code = _PyXI_ERR_NO_ERROR }
1796
1797
static void
1798
clear_xi_failure(_PyXI_failure *failure)
1799
0
{
1800
0
    if (failure->msg != NULL && failure->msg_owned) {
1801
0
        PyMem_RawFree((void*)failure->msg);
1802
0
    }
1803
0
    *failure = XI_FAILURE_INIT;
1804
0
}
1805
1806
static void
1807
copy_xi_failure(_PyXI_failure *dest, _PyXI_failure *src)
1808
0
{
1809
0
    *dest = *src;
1810
0
    dest->msg_owned = 0;
1811
0
}
1812
1813
_PyXI_failure *
1814
_PyXI_NewFailure(void)
1815
0
{
1816
0
    _PyXI_failure *failure = PyMem_RawMalloc(sizeof(_PyXI_failure));
1817
0
    if (failure == NULL) {
1818
0
        PyErr_NoMemory();
1819
0
        return NULL;
1820
0
    }
1821
0
    *failure = XI_FAILURE_INIT;
1822
0
    return failure;
1823
0
}
1824
1825
void
1826
_PyXI_FreeFailure(_PyXI_failure *failure)
1827
0
{
1828
0
    clear_xi_failure(failure);
1829
0
    PyMem_RawFree(failure);
1830
0
}
1831
1832
_PyXI_errcode
1833
_PyXI_GetFailureCode(_PyXI_failure *failure)
1834
0
{
1835
0
    if (failure == NULL) {
1836
0
        return _PyXI_ERR_NO_ERROR;
1837
0
    }
1838
0
    return failure->code;
1839
0
}
1840
1841
void
1842
_PyXI_InitFailureUTF8(_PyXI_failure *failure,
1843
                      _PyXI_errcode code, const char *msg)
1844
0
{
1845
0
    *failure = (_PyXI_failure){
1846
0
        .code = code,
1847
0
        .msg = msg,
1848
0
        .msg_owned = 0,
1849
0
    };
1850
0
}
1851
1852
int
1853
_PyXI_InitFailure(_PyXI_failure *failure, _PyXI_errcode code, PyObject *obj)
1854
0
{
1855
0
    *failure = (_PyXI_failure){
1856
0
        .code = code,
1857
0
        .msg = NULL,
1858
0
        .msg_owned = 0,
1859
0
    };
1860
0
    if (obj == NULL) {
1861
0
        return 0;
1862
0
    }
1863
1864
0
    PyObject *msgobj = PyObject_Str(obj);
1865
0
    if (msgobj == NULL) {
1866
0
        return -1;
1867
0
    }
1868
    // This will leak if not paired with clear_xi_failure().
1869
    // That happens automatically in _capture_current_exception().
1870
0
    const char *msg = _copy_string_obj_raw(msgobj, NULL);
1871
0
    Py_DECREF(msgobj);
1872
0
    if (msg == NULL) {
1873
0
        return -1;
1874
0
    }
1875
0
    *failure = (_PyXI_failure){
1876
0
        .code = code,
1877
0
        .msg = msg,
1878
0
        .msg_owned = 1,
1879
0
    };
1880
0
    return 0;
1881
0
}
1882
1883
/* shared exceptions */
1884
1885
typedef struct {
1886
    // The originating interpreter.
1887
    PyInterpreterState *interp;
1888
    // The error to propagate, if different from the uncaught exception.
1889
    _PyXI_failure *override;
1890
    _PyXI_failure _override;
1891
    // The exception information to propagate, if applicable.
1892
    // This is populated only for some error codes,
1893
    // but always for _PyXI_ERR_UNCAUGHT_EXCEPTION.
1894
    _PyXI_excinfo uncaught;
1895
} _PyXI_error;
1896
1897
static void
1898
xi_error_clear(_PyXI_error *err)
1899
0
{
1900
0
    err->interp = NULL;
1901
0
    if (err->override != NULL) {
1902
0
        clear_xi_failure(err->override);
1903
0
    }
1904
0
    _PyXI_excinfo_clear(&err->uncaught);
1905
0
}
1906
1907
static int
1908
xi_error_is_set(_PyXI_error *error)
1909
0
{
1910
0
    if (error->override != NULL) {
1911
0
        assert(error->override->code != _PyXI_ERR_NO_ERROR);
1912
0
        assert(error->override->code != _PyXI_ERR_UNCAUGHT_EXCEPTION
1913
0
               || excinfo_is_set(&error->uncaught));
1914
0
        return 1;
1915
0
    }
1916
0
    return excinfo_is_set(&error->uncaught);
1917
0
}
1918
1919
static int
1920
xi_error_has_override(_PyXI_error *err)
1921
0
{
1922
0
    if (err->override == NULL) {
1923
0
        return 0;
1924
0
    }
1925
0
    return (err->override->code != _PyXI_ERR_NO_ERROR
1926
0
            && err->override->code != _PyXI_ERR_UNCAUGHT_EXCEPTION);
1927
0
}
1928
1929
static PyObject *
1930
xi_error_resolve_current_exc(PyThreadState *tstate,
1931
                             _PyXI_failure *override)
1932
0
{
1933
0
    assert(override == NULL || override->code != _PyXI_ERR_NO_ERROR);
1934
1935
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
1936
0
    if (exc == NULL) {
1937
0
        assert(override == NULL
1938
0
               || override->code != _PyXI_ERR_UNCAUGHT_EXCEPTION);
1939
0
    }
1940
0
    else if (override == NULL) {
1941
        // This is equivalent to _PyXI_ERR_UNCAUGHT_EXCEPTION.
1942
0
    }
1943
0
    else if (override->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
1944
        // We want to actually capture the current exception.
1945
0
    }
1946
0
    else if (exc != NULL) {
1947
        // It might make sense to do similarly for other codes.
1948
0
        if (override->code == _PyXI_ERR_ALREADY_RUNNING) {
1949
            // We don't need the exception info.
1950
0
            Py_CLEAR(exc);
1951
0
        }
1952
        // ...else we want to actually capture the current exception.
1953
0
    }
1954
0
    return exc;
1955
0
}
1956
1957
static void
1958
xi_error_set_override(PyThreadState *tstate, _PyXI_error *err,
1959
                      _PyXI_failure *override)
1960
0
{
1961
0
    assert(err->override == NULL);
1962
0
    assert(override != NULL);
1963
0
    assert(override->code != _PyXI_ERR_NO_ERROR);
1964
    // Use xi_error_set_exc() instead of setting _PyXI_ERR_UNCAUGHT_EXCEPTION..
1965
0
    assert(override->code != _PyXI_ERR_UNCAUGHT_EXCEPTION);
1966
0
    err->override = &err->_override;
1967
    // The caller still owns override->msg.
1968
0
    copy_xi_failure(&err->_override, override);
1969
0
    err->interp = tstate->interp;
1970
0
}
1971
1972
static void
1973
xi_error_set_override_code(PyThreadState *tstate, _PyXI_error *err,
1974
                           _PyXI_errcode code)
1975
0
{
1976
0
    _PyXI_failure override = XI_FAILURE_INIT;
1977
0
    override.code = code;
1978
0
    xi_error_set_override(tstate, err, &override);
1979
0
}
1980
1981
static const char *
1982
xi_error_set_exc(PyThreadState *tstate, _PyXI_error *err, PyObject *exc)
1983
0
{
1984
0
    assert(!_PyErr_Occurred(tstate));
1985
0
    assert(!xi_error_is_set(err));
1986
0
    assert(err->override == NULL);
1987
0
    assert(err->interp == NULL);
1988
0
    assert(exc != NULL);
1989
0
    const char *failure =
1990
0
                _PyXI_excinfo_InitFromException(&err->uncaught, exc);
1991
0
    if (failure != NULL) {
1992
        // We failed to initialize err->uncaught.
1993
        // XXX Print the excobj/traceback?  Emit a warning?
1994
        // XXX Print the current exception/traceback?
1995
0
        if (_PyErr_ExceptionMatches(tstate, PyExc_MemoryError)) {
1996
0
            xi_error_set_override_code(tstate, err, _PyXI_ERR_NO_MEMORY);
1997
0
        }
1998
0
        else {
1999
0
            xi_error_set_override_code(tstate, err, _PyXI_ERR_OTHER);
2000
0
        }
2001
0
        PyErr_Clear();
2002
0
    }
2003
0
    return failure;
2004
0
}
2005
2006
static PyObject *
2007
_PyXI_ApplyError(_PyXI_error *error, const char *failure)
2008
0
{
2009
0
    PyThreadState *tstate = PyThreadState_Get();
2010
2011
0
    if (failure != NULL) {
2012
0
        xi_error_clear(error);
2013
0
        return NULL;
2014
0
    }
2015
2016
0
    _PyXI_errcode code = _PyXI_ERR_UNCAUGHT_EXCEPTION;
2017
0
    if (error->override != NULL) {
2018
0
        code = error->override->code;
2019
0
        assert(code != _PyXI_ERR_NO_ERROR);
2020
0
    }
2021
2022
0
    if (code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
2023
        // We will raise an exception that proxies the propagated exception.
2024
0
       return _PyXI_excinfo_AsObject(&error->uncaught);
2025
0
    }
2026
0
    else if (code == _PyXI_ERR_NOT_SHAREABLE) {
2027
        // Propagate the exception directly.
2028
0
        assert(!_PyErr_Occurred(tstate));
2029
0
        PyObject *cause = NULL;
2030
0
        if (excinfo_is_set(&error->uncaught)) {
2031
            // Maybe instead set a PyExc_ExceptionSnapshot as __cause__?
2032
            // That type doesn't exist currently
2033
            // but would look like interpreters.ExecutionFailed.
2034
0
            _PyXI_excinfo_Apply(&error->uncaught, PyExc_Exception);
2035
0
            cause = _PyErr_GetRaisedException(tstate);
2036
0
        }
2037
0
        const char *msg = error->override != NULL
2038
0
            ? error->override->msg
2039
0
            : error->uncaught.msg;
2040
0
        _set_xid_lookup_failure(tstate, NULL, msg, cause);
2041
0
        Py_XDECREF(cause);
2042
0
    }
2043
0
    else {
2044
        // Raise an exception corresponding to the code.
2045
0
        (void)_PyXI_ApplyErrorCode(code, error->interp);
2046
0
        assert(error->override == NULL || error->override->msg == NULL);
2047
0
        if (excinfo_is_set(&error->uncaught)) {
2048
            // __context__ will be set to a proxy of the propagated exception.
2049
            // (or use PyExc_ExceptionSnapshot like _PyXI_ERR_NOT_SHAREABLE?)
2050
0
            PyObject *exc = _PyErr_GetRaisedException(tstate);
2051
0
            _PyXI_excinfo_Apply(&error->uncaught, PyExc_InterpreterError);
2052
0
            PyObject *exc2 = _PyErr_GetRaisedException(tstate);
2053
0
            PyException_SetContext(exc, exc2);
2054
0
            _PyErr_SetRaisedException(tstate, exc);
2055
0
        }
2056
0
    }
2057
0
    assert(PyErr_Occurred());
2058
0
    return NULL;
2059
0
}
2060
2061
/* shared namespaces */
2062
2063
/* Shared namespaces are expected to have relatively short lifetimes.
2064
   This means dealloc of a shared namespace will normally happen "soon".
2065
   Namespace items hold cross-interpreter data, which must get released.
2066
   If the namespace/items are cleared in a different interpreter than
2067
   where the items' cross-interpreter data was set then that will cause
2068
   pending calls to be used to release the cross-interpreter data.
2069
   The tricky bit is that the pending calls can happen sufficiently
2070
   later that the namespace/items might already be deallocated.  This is
2071
   a problem if the cross-interpreter data is allocated as part of a
2072
   namespace item.  If that's the case then we must ensure the shared
2073
   namespace is only cleared/freed *after* that data has been released. */
2074
2075
typedef struct _sharednsitem {
2076
    const char *name;
2077
    _PyXIData_t *xidata;
2078
    // We could have a "PyXIData _data" field, so it would
2079
    // be allocated as part of the item and avoid an extra allocation.
2080
    // However, doing so adds a bunch of complexity because we must
2081
    // ensure the item isn't freed before a pending call might happen
2082
    // in a different interpreter to release the XI data.
2083
} _PyXI_namespace_item;
2084
2085
#ifndef NDEBUG
2086
static int
2087
_sharednsitem_is_initialized(_PyXI_namespace_item *item)
2088
{
2089
    if (item->name != NULL) {
2090
        return 1;
2091
    }
2092
    return 0;
2093
}
2094
#endif
2095
2096
static int
2097
_sharednsitem_init(_PyXI_namespace_item *item, PyObject *key)
2098
0
{
2099
0
    item->name = _copy_string_obj_raw(key, NULL);
2100
0
    if (item->name == NULL) {
2101
0
        assert(!_sharednsitem_is_initialized(item));
2102
0
        return -1;
2103
0
    }
2104
0
    item->xidata = NULL;
2105
0
    assert(_sharednsitem_is_initialized(item));
2106
0
    return 0;
2107
0
}
2108
2109
static int
2110
_sharednsitem_has_value(_PyXI_namespace_item *item, int64_t *p_interpid)
2111
0
{
2112
0
    if (item->xidata == NULL) {
2113
0
        return 0;
2114
0
    }
2115
0
    if (p_interpid != NULL) {
2116
0
        *p_interpid = _PyXIData_INTERPID(item->xidata);
2117
0
    }
2118
0
    return 1;
2119
0
}
2120
2121
static int
2122
_sharednsitem_set_value(_PyXI_namespace_item *item, PyObject *value,
2123
                        xidata_fallback_t fallback)
2124
0
{
2125
0
    assert(_sharednsitem_is_initialized(item));
2126
0
    assert(item->xidata == NULL);
2127
0
    item->xidata = _PyXIData_New();
2128
0
    if (item->xidata == NULL) {
2129
0
        return -1;
2130
0
    }
2131
0
    PyThreadState *tstate = PyThreadState_Get();
2132
0
    if (_PyObject_GetXIData(tstate, value, fallback, item->xidata) < 0) {
2133
0
        PyMem_RawFree(item->xidata);
2134
0
        item->xidata = NULL;
2135
        // The caller may want to propagate PyExc_NotShareableError
2136
        // if currently switched between interpreters.
2137
0
        return -1;
2138
0
    }
2139
0
    return 0;
2140
0
}
2141
2142
static void
2143
_sharednsitem_clear_value(_PyXI_namespace_item *item)
2144
0
{
2145
0
    _PyXIData_t *xidata = item->xidata;
2146
0
    if (xidata != NULL) {
2147
0
        item->xidata = NULL;
2148
0
        int rawfree = 1;
2149
0
        (void)_release_xid_data(xidata, rawfree);
2150
0
    }
2151
0
}
2152
2153
static void
2154
_sharednsitem_clear(_PyXI_namespace_item *item)
2155
0
{
2156
0
    if (item->name != NULL) {
2157
0
        PyMem_RawFree((void *)item->name);
2158
0
        item->name = NULL;
2159
0
    }
2160
0
    _sharednsitem_clear_value(item);
2161
0
}
2162
2163
static int
2164
_sharednsitem_copy_from_ns(struct _sharednsitem *item, PyObject *ns,
2165
                           xidata_fallback_t fallback)
2166
0
{
2167
0
    assert(item->name != NULL);
2168
0
    assert(item->xidata == NULL);
2169
0
    PyObject *value = PyDict_GetItemString(ns, item->name);  // borrowed
2170
0
    if (value == NULL) {
2171
0
        if (PyErr_Occurred()) {
2172
0
            return -1;
2173
0
        }
2174
        // When applied, this item will be set to the default (or fail).
2175
0
        return 0;
2176
0
    }
2177
0
    if (_sharednsitem_set_value(item, value, fallback) < 0) {
2178
0
        return -1;
2179
0
    }
2180
0
    return 0;
2181
0
}
2182
2183
static int
2184
_sharednsitem_apply(_PyXI_namespace_item *item, PyObject *ns, PyObject *dflt)
2185
0
{
2186
0
    PyObject *name = PyUnicode_FromString(item->name);
2187
0
    if (name == NULL) {
2188
0
        return -1;
2189
0
    }
2190
0
    PyObject *value;
2191
0
    if (item->xidata != NULL) {
2192
0
        value = _PyXIData_NewObject(item->xidata);
2193
0
        if (value == NULL) {
2194
0
            Py_DECREF(name);
2195
0
            return -1;
2196
0
        }
2197
0
    }
2198
0
    else {
2199
0
        value = Py_NewRef(dflt);
2200
0
    }
2201
0
    int res = PyDict_SetItem(ns, name, value);
2202
0
    Py_DECREF(name);
2203
0
    Py_DECREF(value);
2204
0
    return res;
2205
0
}
2206
2207
2208
typedef struct {
2209
    Py_ssize_t maxitems;
2210
    Py_ssize_t numnames;
2211
    Py_ssize_t numvalues;
2212
    _PyXI_namespace_item items[1];
2213
} _PyXI_namespace;
2214
2215
#ifndef NDEBUG
2216
static int
2217
_sharedns_check_counts(_PyXI_namespace *ns)
2218
{
2219
    if (ns->maxitems <= 0) {
2220
        return 0;
2221
    }
2222
    if (ns->numnames < 0) {
2223
        return 0;
2224
    }
2225
    if (ns->numnames > ns->maxitems) {
2226
        return 0;
2227
    }
2228
    if (ns->numvalues < 0) {
2229
        return 0;
2230
    }
2231
    if (ns->numvalues > ns->numnames) {
2232
        return 0;
2233
    }
2234
    return 1;
2235
}
2236
2237
static int
2238
_sharedns_check_consistency(_PyXI_namespace *ns)
2239
{
2240
    if (!_sharedns_check_counts(ns)) {
2241
        return 0;
2242
    }
2243
2244
    Py_ssize_t i = 0;
2245
    _PyXI_namespace_item *item;
2246
    if (ns->numvalues > 0) {
2247
        item = &ns->items[0];
2248
        if (!_sharednsitem_is_initialized(item)) {
2249
            return 0;
2250
        }
2251
        int64_t interpid0 = -1;
2252
        if (!_sharednsitem_has_value(item, &interpid0)) {
2253
            return 0;
2254
        }
2255
        i += 1;
2256
        for (; i < ns->numvalues; i++) {
2257
            item = &ns->items[i];
2258
            if (!_sharednsitem_is_initialized(item)) {
2259
                return 0;
2260
            }
2261
            int64_t interpid = -1;
2262
            if (!_sharednsitem_has_value(item, &interpid)) {
2263
                return 0;
2264
            }
2265
            if (interpid != interpid0) {
2266
                return 0;
2267
            }
2268
        }
2269
    }
2270
    for (; i < ns->numnames; i++) {
2271
        item = &ns->items[i];
2272
        if (!_sharednsitem_is_initialized(item)) {
2273
            return 0;
2274
        }
2275
        if (_sharednsitem_has_value(item, NULL)) {
2276
            return 0;
2277
        }
2278
    }
2279
    for (; i < ns->maxitems; i++) {
2280
        item = &ns->items[i];
2281
        if (_sharednsitem_is_initialized(item)) {
2282
            return 0;
2283
        }
2284
        if (_sharednsitem_has_value(item, NULL)) {
2285
            return 0;
2286
        }
2287
    }
2288
    return 1;
2289
}
2290
#endif
2291
2292
static _PyXI_namespace *
2293
_sharedns_alloc(Py_ssize_t maxitems)
2294
0
{
2295
0
    if (maxitems < 0) {
2296
0
        if (!PyErr_Occurred()) {
2297
0
            PyErr_BadInternalCall();
2298
0
        }
2299
0
        return NULL;
2300
0
    }
2301
0
    else if (maxitems == 0) {
2302
0
        PyErr_SetString(PyExc_ValueError, "empty namespaces not allowed");
2303
0
        return NULL;
2304
0
    }
2305
2306
    // Check for overflow.
2307
0
    size_t fixedsize = sizeof(_PyXI_namespace) - sizeof(_PyXI_namespace_item);
2308
0
    if ((size_t)maxitems >
2309
0
        ((size_t)PY_SSIZE_T_MAX - fixedsize) / sizeof(_PyXI_namespace_item))
2310
0
    {
2311
0
        PyErr_NoMemory();
2312
0
        return NULL;
2313
0
    }
2314
2315
    // Allocate the value, including items.
2316
0
    size_t size = fixedsize + sizeof(_PyXI_namespace_item) * maxitems;
2317
2318
0
    _PyXI_namespace *ns = PyMem_RawCalloc(size, 1);
2319
0
    if (ns == NULL) {
2320
0
        PyErr_NoMemory();
2321
0
        return NULL;
2322
0
    }
2323
0
    ns->maxitems = maxitems;
2324
0
    assert(_sharedns_check_consistency(ns));
2325
0
    return ns;
2326
0
}
2327
2328
static void
2329
_sharedns_free(_PyXI_namespace *ns)
2330
0
{
2331
    // If we weren't always dynamically allocating the cross-interpreter
2332
    // data in each item then we would need to use a pending call
2333
    // to call _sharedns_free(), to avoid the race between freeing
2334
    // the shared namespace and releasing the XI data.
2335
0
    assert(_sharedns_check_counts(ns));
2336
0
    Py_ssize_t i = 0;
2337
0
    _PyXI_namespace_item *item;
2338
0
    if (ns->numvalues > 0) {
2339
        // One or more items may have interpreter-specific data.
2340
#ifndef NDEBUG
2341
        int64_t interpid = PyInterpreterState_GetID(PyInterpreterState_Get());
2342
        int64_t interpid_i;
2343
#endif
2344
0
        for (; i < ns->numvalues; i++) {
2345
0
            item = &ns->items[i];
2346
0
            assert(_sharednsitem_is_initialized(item));
2347
            // While we do want to ensure consistency across items,
2348
            // technically they don't need to match the current
2349
            // interpreter.  However, we keep the constraint for
2350
            // simplicity, by giving _PyXI_FreeNamespace() the exclusive
2351
            // responsibility of dealing with the owning interpreter.
2352
0
            assert(_sharednsitem_has_value(item, &interpid_i));
2353
0
            assert(interpid_i == interpid);
2354
0
            _sharednsitem_clear(item);
2355
0
        }
2356
0
    }
2357
0
    for (; i < ns->numnames; i++) {
2358
0
        item = &ns->items[i];
2359
0
        assert(_sharednsitem_is_initialized(item));
2360
0
        assert(!_sharednsitem_has_value(item, NULL));
2361
0
        _sharednsitem_clear(item);
2362
0
    }
2363
#ifndef NDEBUG
2364
    for (; i < ns->maxitems; i++) {
2365
        item = &ns->items[i];
2366
        assert(!_sharednsitem_is_initialized(item));
2367
        assert(!_sharednsitem_has_value(item, NULL));
2368
    }
2369
#endif
2370
2371
0
    PyMem_RawFree(ns);
2372
0
}
2373
2374
static _PyXI_namespace *
2375
_create_sharedns(PyObject *names)
2376
0
{
2377
0
    assert(names != NULL);
2378
0
    Py_ssize_t numnames = PyDict_CheckExact(names)
2379
0
        ? PyDict_Size(names)
2380
0
        : PySequence_Size(names);
2381
2382
0
    _PyXI_namespace *ns = _sharedns_alloc(numnames);
2383
0
    if (ns == NULL) {
2384
0
        return NULL;
2385
0
    }
2386
0
    _PyXI_namespace_item *items = ns->items;
2387
2388
    // Fill in the names.
2389
0
    if (PyDict_CheckExact(names)) {
2390
0
        Py_ssize_t i = 0;
2391
0
        Py_ssize_t pos = 0;
2392
0
        PyObject *name;
2393
0
        while(PyDict_Next(names, &pos, &name, NULL)) {
2394
0
            if (_sharednsitem_init(&items[i], name) < 0) {
2395
0
                goto error;
2396
0
            }
2397
0
            ns->numnames += 1;
2398
0
            i += 1;
2399
0
        }
2400
0
    }
2401
0
    else if (PySequence_Check(names)) {
2402
0
        for (Py_ssize_t i = 0; i < numnames; i++) {
2403
0
            PyObject *name = PySequence_GetItem(names, i);
2404
0
            if (name == NULL) {
2405
0
                goto error;
2406
0
            }
2407
0
            int res = _sharednsitem_init(&items[i], name);
2408
0
            Py_DECREF(name);
2409
0
            if (res < 0) {
2410
0
                goto error;
2411
0
            }
2412
0
            ns->numnames += 1;
2413
0
        }
2414
0
    }
2415
0
    else {
2416
0
        PyErr_SetString(PyExc_NotImplementedError,
2417
0
                        "non-sequence namespace not supported");
2418
0
        goto error;
2419
0
    }
2420
0
    assert(ns->numnames == ns->maxitems);
2421
0
    return ns;
2422
2423
0
error:
2424
0
    _sharedns_free(ns);
2425
0
    return NULL;
2426
0
}
2427
2428
static void _propagate_not_shareable_error(PyThreadState *,
2429
                                           _PyXI_failure *);
2430
2431
static int
2432
_fill_sharedns(_PyXI_namespace *ns, PyObject *nsobj,
2433
               xidata_fallback_t fallback, _PyXI_failure *p_err)
2434
0
{
2435
    // All items are expected to be shareable.
2436
0
    assert(_sharedns_check_counts(ns));
2437
0
    assert(ns->numnames == ns->maxitems);
2438
0
    assert(ns->numvalues == 0);
2439
0
    PyThreadState *tstate = PyThreadState_Get();
2440
0
    for (Py_ssize_t i=0; i < ns->maxitems; i++) {
2441
0
        if (_sharednsitem_copy_from_ns(&ns->items[i], nsobj, fallback) < 0) {
2442
0
            if (p_err != NULL) {
2443
0
                _propagate_not_shareable_error(tstate, p_err);
2444
0
            }
2445
            // Clear out the ones we set so far.
2446
0
            for (Py_ssize_t j=0; j < i; j++) {
2447
0
                _sharednsitem_clear_value(&ns->items[j]);
2448
0
                ns->numvalues -= 1;
2449
0
            }
2450
0
            return -1;
2451
0
        }
2452
0
        ns->numvalues += 1;
2453
0
    }
2454
0
    return 0;
2455
0
}
2456
2457
static int
2458
_sharedns_free_pending(void *data)
2459
0
{
2460
0
    _sharedns_free((_PyXI_namespace *)data);
2461
0
    return 0;
2462
0
}
2463
2464
static void
2465
_destroy_sharedns(_PyXI_namespace *ns)
2466
0
{
2467
0
    assert(_sharedns_check_counts(ns));
2468
0
    assert(ns->numnames == ns->maxitems);
2469
0
    if (ns->numvalues == 0) {
2470
0
        _sharedns_free(ns);
2471
0
        return;
2472
0
    }
2473
2474
0
    int64_t interpid0;
2475
0
    if (!_sharednsitem_has_value(&ns->items[0], &interpid0)) {
2476
        // This shouldn't have been possible.
2477
        // We can deal with it in _sharedns_free().
2478
0
        _sharedns_free(ns);
2479
0
        return;
2480
0
    }
2481
0
    PyInterpreterState *interp = _PyInterpreterState_LookUpID(interpid0);
2482
0
    if (interp == PyInterpreterState_Get()) {
2483
0
        _sharedns_free(ns);
2484
0
        return;
2485
0
    }
2486
2487
    // One or more items may have interpreter-specific data.
2488
    // Currently the xidata for each value is dynamically allocated,
2489
    // so technically we don't need to worry about that.
2490
    // However, explicitly adding a pending call here is simpler.
2491
0
    (void)_Py_CallInInterpreter(interp, _sharedns_free_pending, ns);
2492
0
}
2493
2494
static int
2495
_apply_sharedns(_PyXI_namespace *ns, PyObject *nsobj, PyObject *dflt)
2496
0
{
2497
0
    for (Py_ssize_t i=0; i < ns->maxitems; i++) {
2498
0
        if (_sharednsitem_apply(&ns->items[i], nsobj, dflt) != 0) {
2499
0
            return -1;
2500
0
        }
2501
0
    }
2502
0
    return 0;
2503
0
}
2504
2505
2506
/*********************************/
2507
/* switched-interpreter sessions */
2508
/*********************************/
2509
2510
struct xi_session {
2511
#define SESSION_UNUSED 0
2512
0
#define SESSION_ACTIVE 1
2513
    int status;
2514
    int switched;
2515
2516
    // Once a session has been entered, this is the tstate that was
2517
    // current before the session.  If it is different from cur_tstate
2518
    // then we must have switched interpreters.  Either way, this will
2519
    // be the current tstate once we exit the session.
2520
    PyThreadState *prev_tstate;
2521
    // Once a session has been entered, this is the current tstate.
2522
    // It must be current when the session exits.
2523
    PyThreadState *init_tstate;
2524
    // This is true if init_tstate needs cleanup during exit.
2525
    int own_init_tstate;
2526
2527
    // This is true if, while entering the session, init_thread took
2528
    // "ownership" of the interpreter's __main__ module.  This means
2529
    // it is the only thread that is allowed to run code there.
2530
    // (Caveat: for now, users may still run exec() against the
2531
    // __main__ module's dict, though that isn't advisable.)
2532
    int running;
2533
    // This is a cached reference to the __dict__ of the entered
2534
    // interpreter's __main__ module.  It is looked up when at the
2535
    // beginning of the session as a convenience.
2536
    PyObject *main_ns;
2537
2538
    // This is a dict of objects that will be available (via sharing)
2539
    // once the session exits.  Do not access this directly; use
2540
    // _PyXI_Preserve() and _PyXI_GetPreserved() instead;
2541
    PyObject *_preserved;
2542
};
2543
2544
_PyXI_session *
2545
_PyXI_NewSession(void)
2546
0
{
2547
0
    _PyXI_session *session = PyMem_RawCalloc(1, sizeof(_PyXI_session));
2548
0
    if (session == NULL) {
2549
0
        PyErr_NoMemory();
2550
0
        return NULL;
2551
0
    }
2552
0
    return session;
2553
0
}
2554
2555
void
2556
_PyXI_FreeSession(_PyXI_session *session)
2557
0
{
2558
0
    assert(session->status == SESSION_UNUSED);
2559
0
    PyMem_RawFree(session);
2560
0
}
2561
2562
2563
static inline int
2564
_session_is_active(_PyXI_session *session)
2565
0
{
2566
0
    return session->status == SESSION_ACTIVE;
2567
0
}
2568
2569
2570
/* enter/exit a cross-interpreter session */
2571
2572
static void
2573
_enter_session(_PyXI_session *session, PyInterpreterState *interp)
2574
0
{
2575
    // Set here and cleared in _exit_session().
2576
0
    assert(session->status == SESSION_UNUSED);
2577
0
    assert(!session->own_init_tstate);
2578
0
    assert(session->init_tstate == NULL);
2579
0
    assert(session->prev_tstate == NULL);
2580
    // Set elsewhere and cleared in _exit_session().
2581
0
    assert(!session->running);
2582
0
    assert(session->main_ns == NULL);
2583
2584
    // Switch to interpreter.
2585
0
    PyThreadState *tstate = PyThreadState_Get();
2586
0
    PyThreadState *prev = tstate;
2587
0
    int same_interp = (interp == tstate->interp);
2588
0
    if (!same_interp) {
2589
0
        tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_EXEC);
2590
        // XXX Possible GILState issues?
2591
0
        PyThreadState *swapped = PyThreadState_Swap(tstate);
2592
0
        assert(swapped == prev);
2593
0
        (void)swapped;
2594
0
    }
2595
2596
0
    *session = (_PyXI_session){
2597
0
        .status = SESSION_ACTIVE,
2598
0
        .switched = !same_interp,
2599
0
        .init_tstate = tstate,
2600
0
        .prev_tstate = prev,
2601
0
        .own_init_tstate = !same_interp,
2602
0
    };
2603
0
}
2604
2605
static void
2606
_exit_session(_PyXI_session *session)
2607
0
{
2608
0
    PyThreadState *tstate = session->init_tstate;
2609
0
    assert(tstate != NULL);
2610
0
    assert(PyThreadState_Get() == tstate);
2611
0
    assert(!_PyErr_Occurred(tstate));
2612
2613
    // Release any of the entered interpreters resources.
2614
0
    Py_CLEAR(session->main_ns);
2615
0
    Py_CLEAR(session->_preserved);
2616
2617
    // Ensure this thread no longer owns __main__.
2618
0
    if (session->running) {
2619
0
        _PyInterpreterState_SetNotRunningMain(tstate->interp);
2620
0
        assert(!_PyErr_Occurred(tstate));
2621
0
        session->running = 0;
2622
0
    }
2623
2624
    // Switch back.
2625
0
    assert(session->prev_tstate != NULL);
2626
0
    if (session->prev_tstate != session->init_tstate) {
2627
0
        assert(session->own_init_tstate);
2628
0
        session->own_init_tstate = 0;
2629
0
        PyThreadState_Clear(tstate);
2630
0
        PyThreadState_Swap(session->prev_tstate);
2631
0
        PyThreadState_Delete(tstate);
2632
0
    }
2633
0
    else {
2634
0
        assert(!session->own_init_tstate);
2635
0
    }
2636
2637
0
    *session = (_PyXI_session){0};
2638
0
}
2639
2640
static void
2641
_propagate_not_shareable_error(PyThreadState *tstate,
2642
                               _PyXI_failure *override)
2643
0
{
2644
0
    assert(override != NULL);
2645
0
    PyObject *exctype = get_notshareableerror_type(tstate);
2646
0
    if (exctype == NULL) {
2647
0
        PyErr_FormatUnraisable(
2648
0
                "Exception ignored while propagating not shareable error");
2649
0
        return;
2650
0
    }
2651
0
    if (PyErr_ExceptionMatches(exctype)) {
2652
        // We want to propagate the exception directly.
2653
0
        *override = (_PyXI_failure){
2654
0
            .code = _PyXI_ERR_NOT_SHAREABLE,
2655
0
        };
2656
0
    }
2657
0
}
2658
2659
2660
static int _ensure_main_ns(_PyXI_session *, _PyXI_failure *);
2661
static const char * capture_session_error(_PyXI_session *, _PyXI_error *,
2662
                                          _PyXI_failure *);
2663
2664
int
2665
_PyXI_Enter(_PyXI_session *session,
2666
            PyInterpreterState *interp, PyObject *nsupdates,
2667
            _PyXI_session_result *result)
2668
0
{
2669
#ifndef NDEBUG
2670
    PyThreadState *tstate = _PyThreadState_GET();  // Only used for asserts
2671
#endif
2672
2673
    // Convert the attrs for cross-interpreter use.
2674
0
    _PyXI_namespace *sharedns = NULL;
2675
0
    if (nsupdates != NULL) {
2676
0
        assert(PyDict_Check(nsupdates));
2677
0
        Py_ssize_t len = PyDict_Size(nsupdates);
2678
0
        if (len < 0) {
2679
0
            if (result != NULL) {
2680
0
                result->errcode = _PyXI_ERR_APPLY_NS_FAILURE;
2681
0
            }
2682
0
            return -1;
2683
0
        }
2684
0
        if (len > 0) {
2685
0
            sharedns = _create_sharedns(nsupdates);
2686
0
            if (sharedns == NULL) {
2687
0
                if (result != NULL) {
2688
0
                    result->errcode = _PyXI_ERR_APPLY_NS_FAILURE;
2689
0
                }
2690
0
                return -1;
2691
0
            }
2692
            // For now we limit it to shareable objects.
2693
0
            xidata_fallback_t fallback = _PyXIDATA_XIDATA_ONLY;
2694
0
            _PyXI_failure _err = XI_FAILURE_INIT;
2695
0
            if (_fill_sharedns(sharedns, nsupdates, fallback, &_err) < 0) {
2696
0
                assert(_PyErr_Occurred(tstate));
2697
0
                if (_err.code == _PyXI_ERR_NO_ERROR) {
2698
0
                    _err.code = _PyXI_ERR_UNCAUGHT_EXCEPTION;
2699
0
                }
2700
0
                _destroy_sharedns(sharedns);
2701
0
                if (result != NULL) {
2702
0
                    assert(_err.msg == NULL);
2703
0
                    result->errcode = _err.code;
2704
0
                }
2705
0
                return -1;
2706
0
            }
2707
0
        }
2708
0
    }
2709
2710
    // Switch to the requested interpreter (if necessary).
2711
0
    _enter_session(session, interp);
2712
0
    _PyXI_failure override = XI_FAILURE_INIT;
2713
0
    override.code = _PyXI_ERR_UNCAUGHT_EXCEPTION;
2714
#ifndef NDEBUG
2715
    tstate = _PyThreadState_GET();
2716
#endif
2717
2718
    // Ensure this thread owns __main__.
2719
0
    if (_PyInterpreterState_SetRunningMain(interp) < 0) {
2720
        // In the case where we didn't switch interpreters, it would
2721
        // be more efficient to leave the exception in place and return
2722
        // immediately.  However, life is simpler if we don't.
2723
0
        override.code = _PyXI_ERR_ALREADY_RUNNING;
2724
0
        goto error;
2725
0
    }
2726
0
    session->running = 1;
2727
2728
    // Apply the cross-interpreter data.
2729
0
    if (sharedns != NULL) {
2730
0
        if (_ensure_main_ns(session, &override) < 0) {
2731
0
            goto error;
2732
0
        }
2733
0
        if (_apply_sharedns(sharedns, session->main_ns, NULL) < 0) {
2734
0
            override.code = _PyXI_ERR_APPLY_NS_FAILURE;
2735
0
            goto error;
2736
0
        }
2737
0
        _destroy_sharedns(sharedns);
2738
0
    }
2739
2740
0
    override.code = _PyXI_ERR_NO_ERROR;
2741
0
    assert(!_PyErr_Occurred(tstate));
2742
0
    return 0;
2743
2744
0
error:
2745
    // We want to propagate all exceptions here directly (best effort).
2746
0
    assert(override.code != _PyXI_ERR_NO_ERROR);
2747
0
    _PyXI_error err = {0};
2748
0
    const char *failure = capture_session_error(session, &err, &override);
2749
2750
    // Exit the session.
2751
0
    _exit_session(session);
2752
#ifndef NDEBUG
2753
    tstate = _PyThreadState_GET();
2754
#endif
2755
2756
0
    if (sharedns != NULL) {
2757
0
        _destroy_sharedns(sharedns);
2758
0
    }
2759
2760
    // Apply the error from the other interpreter.
2761
0
    PyObject *excinfo = _PyXI_ApplyError(&err, failure);
2762
0
    xi_error_clear(&err);
2763
0
    if (excinfo != NULL) {
2764
0
        if (result != NULL) {
2765
0
            result->excinfo = excinfo;
2766
0
        }
2767
0
        else {
2768
#ifdef Py_DEBUG
2769
            fprintf(stderr, "_PyXI_Enter(): uncaught exception discarded");
2770
#endif
2771
0
            Py_DECREF(excinfo);
2772
0
        }
2773
0
    }
2774
0
    assert(_PyErr_Occurred(tstate));
2775
2776
0
    return -1;
2777
0
}
2778
2779
static int _pop_preserved(_PyXI_session *, _PyXI_namespace **, PyObject **,
2780
                          _PyXI_failure *);
2781
static int _finish_preserved(_PyXI_namespace *, PyObject **);
2782
2783
int
2784
_PyXI_Exit(_PyXI_session *session, _PyXI_failure *override,
2785
           _PyXI_session_result *result)
2786
0
{
2787
0
    PyThreadState *tstate = _PyThreadState_GET();
2788
0
    int res = 0;
2789
2790
    // Capture the raised exception, if any.
2791
0
    _PyXI_error err = {0};
2792
0
    const char *failure = NULL;
2793
0
    if (override != NULL && override->code == _PyXI_ERR_NO_ERROR) {
2794
0
        assert(override->msg == NULL);
2795
0
        override = NULL;
2796
0
    }
2797
0
    if (_PyErr_Occurred(tstate)) {
2798
0
        failure = capture_session_error(session, &err, override);
2799
0
    }
2800
0
    else {
2801
0
        assert(override == NULL);
2802
0
    }
2803
2804
    // Capture the preserved namespace.
2805
0
    _PyXI_namespace *preserved = NULL;
2806
0
    PyObject *preservedobj = NULL;
2807
0
    if (result != NULL) {
2808
0
        assert(!_PyErr_Occurred(tstate));
2809
0
        _PyXI_failure _override = XI_FAILURE_INIT;
2810
0
        if (_pop_preserved(
2811
0
                    session, &preserved, &preservedobj, &_override) < 0)
2812
0
        {
2813
0
            assert(preserved == NULL);
2814
0
            assert(preservedobj == NULL);
2815
0
            if (xi_error_is_set(&err)) {
2816
                // XXX Chain the exception (i.e. set __context__)?
2817
0
                PyErr_FormatUnraisable(
2818
0
                    "Exception ignored while capturing preserved objects");
2819
0
                clear_xi_failure(&_override);
2820
0
            }
2821
0
            else {
2822
0
                if (_override.code == _PyXI_ERR_NO_ERROR) {
2823
0
                    _override.code = _PyXI_ERR_UNCAUGHT_EXCEPTION;
2824
0
                }
2825
0
                failure = capture_session_error(session, &err, &_override);
2826
0
            }
2827
0
        }
2828
0
    }
2829
2830
    // Exit the session.
2831
0
    assert(!_PyErr_Occurred(tstate));
2832
0
    _exit_session(session);
2833
0
    tstate = _PyThreadState_GET();
2834
2835
    // Restore the preserved namespace.
2836
0
    assert(preserved == NULL || preservedobj == NULL);
2837
0
    if (_finish_preserved(preserved, &preservedobj) < 0) {
2838
0
        assert(preservedobj == NULL);
2839
0
        if (xi_error_is_set(&err)) {
2840
            // XXX Chain the exception (i.e. set __context__)?
2841
0
            PyErr_FormatUnraisable(
2842
0
                "Exception ignored while capturing preserved objects");
2843
0
        }
2844
0
        else {
2845
0
            xi_error_set_override_code(
2846
0
                            tstate, &err, _PyXI_ERR_PRESERVE_FAILURE);
2847
0
            _propagate_not_shareable_error(tstate, err.override);
2848
0
        }
2849
0
    }
2850
0
    if (result != NULL) {
2851
0
        result->preserved = preservedobj;
2852
0
        result->errcode = err.override != NULL
2853
0
            ? err.override->code
2854
0
            : _PyXI_ERR_NO_ERROR;
2855
0
    }
2856
2857
    // Apply the error from the other interpreter, if any.
2858
0
    if (xi_error_is_set(&err)) {
2859
0
        res = -1;
2860
0
        assert(!_PyErr_Occurred(tstate));
2861
0
        PyObject *excinfo = _PyXI_ApplyError(&err, failure);
2862
0
        if (excinfo == NULL) {
2863
0
            assert(_PyErr_Occurred(tstate));
2864
0
            if (result != NULL && !xi_error_has_override(&err)) {
2865
0
                _PyXI_ClearResult(result);
2866
0
                *result = (_PyXI_session_result){
2867
0
                    .errcode = _PyXI_ERR_EXC_PROPAGATION_FAILURE,
2868
0
                };
2869
0
            }
2870
0
        }
2871
0
        else if (result != NULL) {
2872
0
            result->excinfo = excinfo;
2873
0
        }
2874
0
        else {
2875
#ifdef Py_DEBUG
2876
            fprintf(stderr, "_PyXI_Exit(): uncaught exception discarded");
2877
#endif
2878
0
            Py_DECREF(excinfo);
2879
0
        }
2880
0
        xi_error_clear(&err);
2881
0
    }
2882
0
    return res;
2883
0
}
2884
2885
2886
/* in an active cross-interpreter session */
2887
2888
static const char *
2889
capture_session_error(_PyXI_session *session, _PyXI_error *err,
2890
                      _PyXI_failure *override)
2891
0
{
2892
0
    assert(_session_is_active(session));
2893
0
    assert(!xi_error_is_set(err));
2894
0
    PyThreadState *tstate = session->init_tstate;
2895
2896
    // Normalize the exception override.
2897
0
    if (override != NULL) {
2898
0
        if (override->code == _PyXI_ERR_UNCAUGHT_EXCEPTION) {
2899
0
            assert(override->msg == NULL);
2900
0
            override = NULL;
2901
0
        }
2902
0
        else {
2903
0
            assert(override->code != _PyXI_ERR_NO_ERROR);
2904
0
        }
2905
0
    }
2906
2907
    // Handle the exception, if any.
2908
0
    const char *failure = NULL;
2909
0
    PyObject *exc = xi_error_resolve_current_exc(tstate, override);
2910
0
    if (exc != NULL) {
2911
        // There is an unhandled exception we need to preserve.
2912
0
        failure = xi_error_set_exc(tstate, err, exc);
2913
0
        Py_DECREF(exc);
2914
0
        if (_PyErr_Occurred(tstate)) {
2915
0
            PyErr_FormatUnraisable(failure);
2916
0
        }
2917
0
    }
2918
2919
    // Handle the override.
2920
0
    if (override != NULL && failure == NULL) {
2921
0
        xi_error_set_override(tstate, err, override);
2922
0
    }
2923
2924
    // Finished!
2925
0
    assert(!_PyErr_Occurred(tstate));
2926
0
    return failure;
2927
0
}
2928
2929
static int
2930
_ensure_main_ns(_PyXI_session *session, _PyXI_failure *failure)
2931
0
{
2932
0
    assert(_session_is_active(session));
2933
0
    PyThreadState *tstate = session->init_tstate;
2934
0
    if (session->main_ns != NULL) {
2935
0
        return 0;
2936
0
    }
2937
    // Cache __main__.__dict__.
2938
0
    PyObject *main_mod = _Py_GetMainModule(tstate);
2939
0
    if (_Py_CheckMainModule(main_mod) < 0) {
2940
0
        Py_XDECREF(main_mod);
2941
0
        if (failure != NULL) {
2942
0
            *failure = (_PyXI_failure){
2943
0
                .code = _PyXI_ERR_MAIN_NS_FAILURE,
2944
0
            };
2945
0
        }
2946
0
        return -1;
2947
0
    }
2948
0
    PyObject *ns = PyModule_GetDict(main_mod);  // borrowed
2949
0
    Py_DECREF(main_mod);
2950
0
    if (ns == NULL) {
2951
0
        if (failure != NULL) {
2952
0
            *failure = (_PyXI_failure){
2953
0
                .code = _PyXI_ERR_MAIN_NS_FAILURE,
2954
0
            };
2955
0
        }
2956
0
        return -1;
2957
0
    }
2958
0
    session->main_ns = Py_NewRef(ns);
2959
0
    return 0;
2960
0
}
2961
2962
PyObject *
2963
_PyXI_GetMainNamespace(_PyXI_session *session, _PyXI_failure *failure)
2964
0
{
2965
0
    if (!_session_is_active(session)) {
2966
0
        PyErr_SetString(PyExc_RuntimeError, "session not active");
2967
0
        return NULL;
2968
0
    }
2969
0
    if (_ensure_main_ns(session, failure) < 0) {
2970
0
        return NULL;
2971
0
    }
2972
0
    return session->main_ns;
2973
0
}
2974
2975
2976
static int
2977
_pop_preserved(_PyXI_session *session,
2978
               _PyXI_namespace **p_xidata, PyObject **p_obj,
2979
               _PyXI_failure *p_failure)
2980
0
{
2981
0
    _PyXI_failure failure = XI_FAILURE_INIT;
2982
0
    _PyXI_namespace *xidata = NULL;
2983
0
    assert(_PyThreadState_GET() == session->init_tstate);  // active session
2984
2985
0
    if (session->_preserved == NULL) {
2986
0
        *p_xidata = NULL;
2987
0
        *p_obj = NULL;
2988
0
        return 0;
2989
0
    }
2990
0
    if (session->init_tstate == session->prev_tstate) {
2991
        // We did not switch interpreters.
2992
0
        *p_xidata = NULL;
2993
0
        *p_obj = session->_preserved;
2994
0
        session->_preserved = NULL;
2995
0
        return 0;
2996
0
    }
2997
0
    *p_obj = NULL;
2998
2999
    // We did switch interpreters.
3000
0
    Py_ssize_t len = PyDict_Size(session->_preserved);
3001
0
    if (len < 0) {
3002
0
        failure.code = _PyXI_ERR_PRESERVE_FAILURE;
3003
0
        goto error;
3004
0
    }
3005
0
    else if (len == 0) {
3006
0
        *p_xidata = NULL;
3007
0
    }
3008
0
    else {
3009
0
        _PyXI_namespace *xidata = _create_sharedns(session->_preserved);
3010
0
        if (xidata == NULL) {
3011
0
            failure.code = _PyXI_ERR_PRESERVE_FAILURE;
3012
0
            goto error;
3013
0
        }
3014
0
        if (_fill_sharedns(xidata, session->_preserved,
3015
0
                           _PyXIDATA_FULL_FALLBACK, &failure) < 0)
3016
0
        {
3017
0
            if (failure.code != _PyXI_ERR_NOT_SHAREABLE) {
3018
0
                assert(failure.msg != NULL);
3019
0
                failure.code = _PyXI_ERR_PRESERVE_FAILURE;
3020
0
            }
3021
0
            goto error;
3022
0
        }
3023
0
        *p_xidata = xidata;
3024
0
    }
3025
0
    Py_CLEAR(session->_preserved);
3026
0
    return 0;
3027
3028
0
error:
3029
0
    if (p_failure != NULL) {
3030
0
        *p_failure = failure;
3031
0
    }
3032
0
    if (xidata != NULL) {
3033
0
        _destroy_sharedns(xidata);
3034
0
    }
3035
0
    return -1;
3036
0
}
3037
3038
static int
3039
_finish_preserved(_PyXI_namespace *xidata, PyObject **p_preserved)
3040
0
{
3041
0
    if (xidata == NULL) {
3042
0
        return 0;
3043
0
    }
3044
0
    int res = -1;
3045
0
    if (p_preserved != NULL) {
3046
0
        PyObject *ns = PyDict_New();
3047
0
        if (ns == NULL) {
3048
0
            goto finally;
3049
0
        }
3050
0
        if (_apply_sharedns(xidata, ns, NULL) < 0) {
3051
0
            Py_CLEAR(ns);
3052
0
            goto finally;
3053
0
        }
3054
0
        *p_preserved = ns;
3055
0
    }
3056
0
    res = 0;
3057
3058
0
finally:
3059
0
    _destroy_sharedns(xidata);
3060
0
    return res;
3061
0
}
3062
3063
int
3064
_PyXI_Preserve(_PyXI_session *session, const char *name, PyObject *value,
3065
               _PyXI_failure *p_failure)
3066
0
{
3067
0
    _PyXI_failure failure = XI_FAILURE_INIT;
3068
0
    if (!_session_is_active(session)) {
3069
0
        PyErr_SetString(PyExc_RuntimeError, "session not active");
3070
0
        return -1;
3071
0
    }
3072
0
    if (session->_preserved == NULL) {
3073
0
        session->_preserved = PyDict_New();
3074
0
        if (session->_preserved == NULL) {
3075
0
            set_exc_with_cause(PyExc_RuntimeError,
3076
0
                               "failed to initialize preserved objects");
3077
0
            failure.code = _PyXI_ERR_PRESERVE_FAILURE;
3078
0
            goto error;
3079
0
        }
3080
0
    }
3081
0
    if (PyDict_SetItemString(session->_preserved, name, value) < 0) {
3082
0
        set_exc_with_cause(PyExc_RuntimeError, "failed to preserve object");
3083
0
        failure.code = _PyXI_ERR_PRESERVE_FAILURE;
3084
0
        goto error;
3085
0
    }
3086
0
    return 0;
3087
3088
0
error:
3089
0
    if (p_failure != NULL) {
3090
0
        *p_failure = failure;
3091
0
    }
3092
0
    return -1;
3093
0
}
3094
3095
PyObject *
3096
_PyXI_GetPreserved(_PyXI_session_result *result, const char *name)
3097
0
{
3098
0
    PyObject *value = NULL;
3099
0
    if (result->preserved != NULL) {
3100
0
        (void)PyDict_GetItemStringRef(result->preserved, name, &value);
3101
0
    }
3102
0
    return value;
3103
0
}
3104
3105
void
3106
_PyXI_ClearResult(_PyXI_session_result *result)
3107
0
{
3108
0
    Py_CLEAR(result->preserved);
3109
0
    Py_CLEAR(result->excinfo);
3110
0
}
3111
3112
3113
/*********************/
3114
/* runtime lifecycle */
3115
/*********************/
3116
3117
int
3118
_Py_xi_global_state_init(_PyXI_global_state_t *state)
3119
36
{
3120
36
    assert(state != NULL);
3121
36
    xid_lookup_init(&state->data_lookup);
3122
36
    return 0;
3123
36
}
3124
3125
void
3126
_Py_xi_global_state_fini(_PyXI_global_state_t *state)
3127
0
{
3128
0
    assert(state != NULL);
3129
0
    xid_lookup_fini(&state->data_lookup);
3130
0
}
3131
3132
int
3133
_Py_xi_state_init(_PyXI_state_t *state, PyInterpreterState *interp)
3134
36
{
3135
36
    assert(state != NULL);
3136
36
    assert(interp == NULL || state == _PyXI_GET_STATE(interp));
3137
3138
    // Initialize pickle function cache (before any fallible ops).
3139
36
    state->pickle.dumps = NULL;
3140
36
    state->pickle.loads = NULL;
3141
3142
36
    xid_lookup_init(&state->data_lookup);
3143
3144
    // Initialize exceptions.
3145
36
    if (interp != NULL) {
3146
0
        if (init_static_exctypes(&state->exceptions, interp) < 0) {
3147
0
            fini_heap_exctypes(&state->exceptions);
3148
0
            return -1;
3149
0
        }
3150
0
    }
3151
36
    if (init_heap_exctypes(&state->exceptions) < 0) {
3152
0
        return -1;
3153
0
    }
3154
3155
36
    return 0;
3156
36
}
3157
3158
void
3159
_Py_xi_state_fini(_PyXI_state_t *state, PyInterpreterState *interp)
3160
0
{
3161
0
    assert(state != NULL);
3162
0
    assert(interp == NULL || state == _PyXI_GET_STATE(interp));
3163
3164
    // Clear pickle function cache first: the cached functions may hold
3165
    // references to modules cleaned up by later finalization steps.
3166
0
    Py_CLEAR(state->pickle.dumps);
3167
0
    Py_CLEAR(state->pickle.loads);
3168
3169
0
    fini_heap_exctypes(&state->exceptions);
3170
0
    if (interp != NULL) {
3171
0
        fini_static_exctypes(&state->exceptions, interp);
3172
0
    }
3173
3174
0
    xid_lookup_fini(&state->data_lookup);
3175
0
}
3176
3177
3178
PyStatus
3179
_PyXI_Init(PyInterpreterState *interp)
3180
36
{
3181
36
    if (_Py_IsMainInterpreter(interp)) {
3182
36
        _PyXI_global_state_t *global_state = _PyXI_GET_GLOBAL_STATE(interp);
3183
36
        if (global_state == NULL) {
3184
0
            PyErr_PrintEx(0);
3185
0
            return _PyStatus_ERR(
3186
0
                    "failed to get global cross-interpreter state");
3187
0
        }
3188
36
        if (_Py_xi_global_state_init(global_state) < 0) {
3189
0
            PyErr_PrintEx(0);
3190
0
            return _PyStatus_ERR(
3191
0
                    "failed to initialize  global cross-interpreter state");
3192
0
        }
3193
36
    }
3194
3195
36
    _PyXI_state_t *state = _PyXI_GET_STATE(interp);
3196
36
    if (state == NULL) {
3197
0
        PyErr_PrintEx(0);
3198
0
        return _PyStatus_ERR(
3199
0
                "failed to get interpreter's cross-interpreter state");
3200
0
    }
3201
    // The static types were already initialized in _PyXI_InitTypes(),
3202
    // so we pass in NULL here to avoid initializing them again.
3203
36
    if (_Py_xi_state_init(state, NULL) < 0) {
3204
0
        PyErr_PrintEx(0);
3205
0
        return _PyStatus_ERR(
3206
0
                "failed to initialize interpreter's cross-interpreter state");
3207
0
    }
3208
3209
36
    return _PyStatus_OK();
3210
36
}
3211
3212
// _PyXI_Fini() must be called before the interpreter is cleared,
3213
// since we must clear some heap objects.
3214
3215
void
3216
_PyXI_Fini(PyInterpreterState *interp)
3217
0
{
3218
0
    _PyXI_state_t *state = _PyXI_GET_STATE(interp);
3219
#ifndef NDEBUG
3220
    if (state == NULL) {
3221
        PyErr_PrintEx(0);
3222
        return;
3223
    }
3224
#endif
3225
    // The static types will be finalized soon in _PyXI_FiniTypes(),
3226
    // so we pass in NULL here to avoid finalizing them right now.
3227
0
    _Py_xi_state_fini(state, NULL);
3228
3229
0
    if (_Py_IsMainInterpreter(interp)) {
3230
0
        _PyXI_global_state_t *global_state = _PyXI_GET_GLOBAL_STATE(interp);
3231
0
        _Py_xi_global_state_fini(global_state);
3232
0
    }
3233
0
}
3234
3235
PyStatus
3236
_PyXI_InitTypes(PyInterpreterState *interp)
3237
36
{
3238
36
    if (init_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp) < 0) {
3239
0
        PyErr_PrintEx(0);
3240
0
        return _PyStatus_ERR(
3241
0
                "failed to initialize the cross-interpreter exception types");
3242
0
    }
3243
    // We would initialize heap types here too but that leads to ref leaks.
3244
    // Instead, we initialize them in _PyXI_Init().
3245
36
    return _PyStatus_OK();
3246
36
}
3247
3248
void
3249
_PyXI_FiniTypes(PyInterpreterState *interp)
3250
0
{
3251
    // We would finalize heap types here too but that leads to ref leaks.
3252
    // Instead, we finalize them in _PyXI_Fini().
3253
0
    fini_static_exctypes(&_PyXI_GET_STATE(interp)->exceptions, interp);
3254
0
}
3255
3256
3257
/*************/
3258
/* other API */
3259
/*************/
3260
3261
PyInterpreterState *
3262
_PyXI_NewInterpreter(PyInterpreterConfig *config, long *maybe_whence,
3263
                     PyThreadState **p_tstate, PyThreadState **p_save_tstate)
3264
0
{
3265
0
    PyThreadState *save_tstate = PyThreadState_Swap(NULL);
3266
0
    assert(save_tstate != NULL);
3267
3268
0
    PyThreadState *tstate;
3269
0
    PyStatus status = Py_NewInterpreterFromConfig(&tstate, config);
3270
0
    if (PyStatus_Exception(status)) {
3271
        // Since no new thread state was created, there is no exception
3272
        // to propagate; raise a fresh one after swapping back in the
3273
        // old thread state.
3274
0
        PyThreadState_Swap(save_tstate);
3275
0
        _PyErr_SetFromPyStatus(status);
3276
0
        PyObject *exc = PyErr_GetRaisedException();
3277
0
        PyErr_SetString(PyExc_InterpreterError,
3278
0
                        "sub-interpreter creation failed");
3279
0
        _PyErr_ChainExceptions1(exc);
3280
0
        return NULL;
3281
0
    }
3282
0
    assert(tstate != NULL);
3283
0
    PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
3284
3285
0
    long whence = _PyInterpreterState_WHENCE_XI;
3286
0
    if (maybe_whence != NULL) {
3287
0
        whence = *maybe_whence;
3288
0
    }
3289
0
    _PyInterpreterState_SetWhence(interp, whence);
3290
3291
0
    if (p_tstate != NULL) {
3292
        // We leave the new thread state as the current one.
3293
0
        *p_tstate = tstate;
3294
0
    }
3295
0
    else {
3296
        // Throw away the initial tstate.
3297
0
        PyThreadState_Clear(tstate);
3298
0
        PyThreadState_Swap(save_tstate);
3299
0
        PyThreadState_Delete(tstate);
3300
0
        save_tstate = NULL;
3301
0
    }
3302
0
    if (p_save_tstate != NULL) {
3303
0
        *p_save_tstate = save_tstate;
3304
0
    }
3305
0
    return interp;
3306
0
}
3307
3308
void
3309
_PyXI_EndInterpreter(PyInterpreterState *interp,
3310
                     PyThreadState *tstate, PyThreadState **p_save_tstate)
3311
0
{
3312
#ifndef NDEBUG
3313
    long whence = _PyInterpreterState_GetWhence(interp);
3314
#endif
3315
0
    assert(whence != _PyInterpreterState_WHENCE_RUNTIME);
3316
3317
0
    if (!_PyInterpreterState_IsReady(interp)) {
3318
0
        assert(whence == _PyInterpreterState_WHENCE_UNKNOWN);
3319
        // PyInterpreterState_Clear() requires the GIL,
3320
        // which a not-ready does not have, so we don't clear it.
3321
        // That means there may be leaks here until clearing the
3322
        // interpreter is fixed.
3323
0
        PyInterpreterState_Delete(interp);
3324
0
        return;
3325
0
    }
3326
0
    assert(whence != _PyInterpreterState_WHENCE_UNKNOWN);
3327
3328
0
    PyThreadState *save_tstate = NULL;
3329
0
    PyThreadState *cur_tstate = PyThreadState_GET();
3330
0
    if (tstate == NULL) {
3331
0
        if (PyThreadState_GetInterpreter(cur_tstate) == interp) {
3332
0
            tstate = cur_tstate;
3333
0
        }
3334
0
        else {
3335
0
            tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
3336
0
            assert(tstate != NULL);
3337
0
            save_tstate = PyThreadState_Swap(tstate);
3338
0
        }
3339
0
    }
3340
0
    else {
3341
0
        assert(PyThreadState_GetInterpreter(tstate) == interp);
3342
0
        if (tstate != cur_tstate) {
3343
0
            assert(PyThreadState_GetInterpreter(cur_tstate) != interp);
3344
0
            save_tstate = PyThreadState_Swap(tstate);
3345
0
        }
3346
0
    }
3347
3348
0
    Py_EndInterpreter(tstate);
3349
3350
0
    if (p_save_tstate != NULL) {
3351
0
        save_tstate = *p_save_tstate;
3352
0
    }
3353
0
    PyThreadState_Swap(save_tstate);
3354
0
}