forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_xxsubinterpretersmodule.c
More file actions
3576 lines (3130 loc) · 90.5 KB
/
Copy path_xxsubinterpretersmodule.c
File metadata and controls
3576 lines (3130 loc) · 90.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* _interpreters module */
/* low-level access to interpreter primitives */
#include "Python.h"
#include "frameobject.h"
#include "interpreteridobject.h"
// XXX Emit a warning?
#define IGNORE_FAILURE(msg) \
fprintf(stderr, " -----\nRunFailedError: %s\n", msg); \
PyErr_PrintEx(0); \
fprintf(stderr, " -----\n"); \
PyErr_Clear();
typedef void (*_deallocfunc)(void *);
static PyInterpreterState *
_get_current(void)
{
// _PyInterpreterState_Get() aborts if lookup fails, so don't need
// to check the result for NULL.
return _PyInterpreterState_Get();
}
/* string utils *************************************************************/
// PyMem_Free() must be used to dealocate the resulting string.
static char *
_strdup_and_size(const char *data, Py_ssize_t *psize, _deallocfunc *dealloc)
{
if (data == NULL) {
if (psize != NULL) {
*psize = 0;
}
if (dealloc != NULL) {
*dealloc = NULL;
}
return "";
}
Py_ssize_t size;
if (psize == NULL) {
size = strlen(data);
} else {
size = *psize;
if (size == 0) {
size = strlen(data);
*psize = size; // The size "return" value.
}
}
char *copied = PyMem_Malloc(size+1);
if (copied == NULL) {
PyErr_NoMemory();
return NULL;
}
if (dealloc != NULL) {
*dealloc = PyMem_Free;
}
memcpy(copied, data, size+1);
return copied;
}
static const char *
_pyobj_get_str_and_size(PyObject *obj, Py_ssize_t *psize)
{
if (PyUnicode_Check(obj)) {
return PyUnicode_AsUTF8AndSize(obj, psize);
} else {
const char *data = NULL;
PyBytes_AsStringAndSize(obj, (char **)&data, psize);
return data;
}
}
/* "raw" strings */
typedef struct _rawstring {
Py_ssize_t size;
const char *data;
_deallocfunc dealloc;
} _rawstring;
static void
_rawstring_init(_rawstring *raw)
{
raw->size = 0;
raw->data = NULL;
raw->dealloc = NULL;
}
static _rawstring *
_rawstring_new(void)
{
_rawstring *raw = PyMem_NEW(_rawstring, 1);
if (raw == NULL) {
PyErr_NoMemory();
return NULL;
}
_rawstring_init(raw);
return raw;
}
static void
_rawstring_clear(_rawstring *raw)
{
if (raw->data != NULL && raw->dealloc != NULL) {
(*raw->dealloc)((void *)raw->data);
}
_rawstring_init(raw);
}
static void
_rawstring_free(_rawstring *raw)
{
_rawstring_clear(raw);
PyMem_Free(raw);
}
static int
_rawstring_is_clear(_rawstring *raw)
{
return raw->size == 0 && raw->data == NULL && raw->dealloc == NULL;
}
//static void
//_rawstring_move(_rawstring *raw, _rawstring *src)
//{
// raw->size = src->size;
// raw->data = src->data;
// raw->dealloc = src->dealloc;
// _rawstring_init(src);
//}
static void
_rawstring_proxy(_rawstring *raw, const char *str)
{
if (str == NULL) {
str = "";
}
raw->size = strlen(str);
raw->data = str;
raw->dealloc = NULL;
}
static int
_rawstring_buffer(_rawstring *raw, Py_ssize_t size)
{
raw->data = PyMem_Malloc(size+1);
if (raw->data == NULL) {
PyErr_NoMemory();
return -1;
}
raw->size = size;
raw->dealloc = PyMem_Free;
return 0;
}
static int
_rawstring_strcpy(_rawstring *raw, const char *str, Py_ssize_t size)
{
_deallocfunc dealloc = NULL;
const char *copied = _strdup_and_size(str, &size, &dealloc);
if (copied == NULL) {
return -1;
}
raw->size = size;
raw->dealloc = dealloc;
raw->data = copied;
return 0;
}
static int
_rawstring_from_pyobj(_rawstring *raw, PyObject *obj)
{
Py_ssize_t size = 0;
const char *data = _pyobj_get_str_and_size(obj, &size);
if (PyErr_Occurred()) {
return -1;
}
if (_rawstring_strcpy(raw, data, size) != 0) {
return -1;
}
return 0;
}
static int
_rawstring_from_pyobj_attr(_rawstring *raw, PyObject *obj, const char *attr)
{
int res = -1;
PyObject *valueobj = PyObject_GetAttrString(obj, attr);
if (valueobj == NULL) {
goto done;
}
if (!PyUnicode_Check(valueobj)) {
// XXX PyObject_Str()? Repr()?
goto done;
}
const char *valuestr = PyUnicode_AsUTF8(valueobj);
if (valuestr == NULL) {
if (PyErr_Occurred()) {
goto done;
}
} else if (_rawstring_strcpy(raw, valuestr, 0) != 0) {
_rawstring_clear(raw);
goto done;
}
res = 0;
done:
Py_XDECREF(valueobj);
return res;
}
static PyObject *
_rawstring_as_pybytes(_rawstring *raw)
{
return PyBytes_FromStringAndSize(raw->data, raw->size);
}
/* object utils *************************************************************/
static void
_pyobj_identify_type(PyObject *obj, _rawstring *modname, _rawstring *clsname)
{
PyObject *objtype = (PyObject *)Py_TYPE(obj);
// Try __module__ and __name__.
if (_rawstring_from_pyobj_attr(modname, objtype, "__module__") != 0) {
// Fall back to the previous values in "modname".
IGNORE_FAILURE("bad __module__");
}
if (_rawstring_from_pyobj_attr(clsname, objtype, "__name__") != 0) {
// Fall back to the previous values in "clsname".
IGNORE_FAILURE("bad __name__");
}
// XXX Fall back to __qualname__?
// XXX Fall back to tp_name?
}
static PyObject *
_pyobj_get_class(const char *modname, const char *clsname)
{
assert(clsname != NULL);
if (modname == NULL) {
modname = "builtins";
}
PyObject *module = PyImport_ImportModule(modname);
if (module == NULL) {
return NULL;
}
PyObject *cls = PyObject_GetAttrString(module, clsname);
Py_DECREF(module);
return cls;
}
static PyObject *
_pyobj_create(const char *modname, const char *clsname, PyObject *arg)
{
PyObject *cls = _pyobj_get_class(modname, clsname);
if (cls == NULL) {
return NULL;
}
PyObject *obj = NULL;
if (arg == NULL) {
obj = _PyObject_CallNoArg(cls);
} else {
obj = PyObject_CallFunction(cls, "O", arg);
}
Py_DECREF(cls);
return obj;
}
/* object snapshots */
typedef struct _objsnapshot {
// If modname is NULL then try "builtins" and "__main__".
_rawstring modname;
// clsname is required.
_rawstring clsname;
// The rest are optional.
// The serialized exception.
_rawstring *serialized;
} _objsnapshot;
static void
_objsnapshot_init(_objsnapshot *osn)
{
_rawstring_init(&osn->modname);
_rawstring_init(&osn->clsname);
osn->serialized = NULL;
}
//static _objsnapshot *
//_objsnapshot_new(void)
//{
// _objsnapshot *osn = PyMem_NEW(_objsnapshot, 1);
// if (osn == NULL) {
// PyErr_NoMemory();
// return NULL;
// }
// _objsnapshot_init(osn);
// return osn;
//}
static void
_objsnapshot_clear(_objsnapshot *osn)
{
_rawstring_clear(&osn->modname);
_rawstring_clear(&osn->clsname);
if (osn->serialized != NULL) {
_rawstring_free(osn->serialized);
osn->serialized = NULL;
}
}
//static void
//_objsnapshot_free(_objsnapshot *osn)
//{
// _objsnapshot_clear(osn);
// PyMem_Free(osn);
//}
#ifndef NDEBUG
static int
_objsnapshot_is_clear(_objsnapshot *osn)
{
return osn->serialized == NULL
&& _rawstring_is_clear(&osn->modname)
&& _rawstring_is_clear(&osn->clsname);
}
#endif
static void
_objsnapshot_summarize(_objsnapshot *osn, _rawstring *rawbuf, const char *msg)
{
if (msg == NULL || *msg == '\0') {
// XXX Keep it NULL?
// XXX Keep it an empty string?
// XXX Use something more informative?
msg = "<no message>";
}
const char *clsname = osn->clsname.data;
const char *modname = osn->modname.data;
if (modname && *modname == '\0') {
modname = NULL;
}
// Prep the buffer.
Py_ssize_t size = strlen(clsname);
if (modname != NULL) {
if (strcmp(modname, "builtins") == 0) {
modname = NULL;
} else if (strcmp(modname, "__main__") == 0) {
modname = NULL;
} else {
size += strlen(modname) + 1;
}
}
if (msg != NULL) {
size += strlen(": ") + strlen(msg);
}
if (modname != NULL || msg != NULL) {
if (_rawstring_buffer(rawbuf, size) != 0) {
IGNORE_FAILURE("could not summarize object snapshot");
return;
}
}
// ...else we'll proxy clsname as-is, so no need to allocate a buffer.
// XXX Use __qualname__ somehow?
char *buf = (char *)rawbuf->data;
if (modname != NULL) {
if (msg != NULL) {
snprintf(buf, size+1, "%s.%s: %s", modname, clsname, msg);
} else {
snprintf(buf, size+1, "%s.%s", modname, clsname);
}
} else if (msg != NULL) {
snprintf(buf, size+1, "%s: %s", clsname, msg);
} else {
_rawstring_proxy(rawbuf, clsname);
}
}
static _rawstring *
_objsnapshot_get_minimal_summary(_objsnapshot *osn, PyObject *obj)
{
const char *str = NULL;
PyObject *objstr = PyObject_Str(obj);
if (objstr == NULL) {
PyErr_Clear();
} else {
str = PyUnicode_AsUTF8(objstr);
if (str == NULL) {
PyErr_Clear();
}
}
_rawstring *summary = _rawstring_new();
if (summary == NULL) {
return NULL;
}
_objsnapshot_summarize(osn, summary, str);
return summary;
}
static void
_objsnapshot_extract(_objsnapshot *osn, PyObject *obj)
{
assert(_objsnapshot_is_clear(osn));
// Get the "qualname".
_rawstring_proxy(&osn->modname, "<unknown>");
_rawstring_proxy(&osn->clsname, "<unknown>");
_pyobj_identify_type(obj, &osn->modname, &osn->clsname);
// Serialize the object.
// XXX Use marshal?
PyObject *pickle = PyImport_ImportModule("pickle");
if (pickle == NULL) {
IGNORE_FAILURE("could not serialize object: pickle import failed");
return;
}
PyObject *objdata = PyObject_CallMethod(pickle, "dumps", "(O)", obj);
Py_DECREF(pickle);
if (objdata == NULL) {
IGNORE_FAILURE("could not serialize object: pickle.dumps failed");
} else {
_rawstring *serialized = _rawstring_new();
int res = _rawstring_from_pyobj(serialized, objdata);
Py_DECREF(objdata);
if (res != 0) {
IGNORE_FAILURE("could not serialize object: raw str failed");
_rawstring_free(serialized);
} else if (serialized->size == 0) {
_rawstring_free(serialized);
} else {
osn->serialized = serialized;
}
}
}
static PyObject *
_objsnapshot_resolve_serialized(_objsnapshot *osn)
{
assert(osn->serialized != NULL);
// XXX Use marshal?
PyObject *pickle = PyImport_ImportModule("pickle");
if (pickle == NULL) {
return NULL;
}
PyObject *objdata = _rawstring_as_pybytes(osn->serialized);
if (objdata == NULL) {
return NULL;
} else {
PyObject *obj = PyObject_CallMethod(pickle, "loads", "O", objdata);
Py_DECREF(objdata);
return obj;
}
}
static PyObject *
_objsnapshot_resolve_naive(_objsnapshot *osn, PyObject *arg)
{
if (_rawstring_is_clear(&osn->clsname)) {
// We can't proceed without at least the class name.
PyErr_SetString(PyExc_ValueError, "missing class name");
return NULL;
}
if (osn->modname.data != NULL) {
return _pyobj_create(osn->modname.data, osn->clsname.data, arg);
} else {
PyObject *obj = _pyobj_create("builtins", osn->clsname.data, arg);
if (obj == NULL) {
PyErr_Clear();
obj = _pyobj_create("__main__", osn->clsname.data, arg);
}
return obj;
}
}
static PyObject *
_objsnapshot_resolve(_objsnapshot *osn)
{
if (osn->serialized != NULL) {
PyObject *obj = _objsnapshot_resolve_serialized(osn);
if (obj != NULL) {
return obj;
}
IGNORE_FAILURE("could not de-serialize object");
}
// Fall back to naive resolution.
return _objsnapshot_resolve_naive(osn, NULL);
}
/* exception utils **********************************************************/
// _pyexc_create is inspired by _PyErr_SetObject().
static PyObject *
_pyexc_create(PyObject *exctype, const char *msg, PyObject *tb)
{
assert(exctype != NULL && PyExceptionClass_Check(exctype));
PyObject *curtype = NULL, *curexc = NULL, *curtb = NULL;
PyErr_Fetch(&curtype, &curexc, &curtb);
// Create the object.
PyObject *exc = NULL;
if (msg != NULL) {
PyObject *msgobj = PyUnicode_FromString(msg);
if (msgobj == NULL) {
IGNORE_FAILURE("could not deserialize propagated error message");
}
exc = _PyObject_CallOneArg(exctype, msgobj);
Py_XDECREF(msgobj);
} else {
exc = _PyObject_CallNoArg(exctype);
}
if (exc == NULL) {
return NULL;
}
// Set the traceback, if any.
if (tb == NULL) {
tb = curtb;
}
if (tb != NULL) {
// This does *not* steal a reference!
PyException_SetTraceback(exc, tb);
}
PyErr_Restore(curtype, curexc, curtb);
return exc;
}
/* traceback snapshots */
typedef struct _tbsnapshot {
_rawstring tbs_funcname;
_rawstring tbs_filename;
int tbs_lineno;
struct _tbsnapshot *tbs_next;
} _tbsnapshot;
static void
_tbsnapshot_init(_tbsnapshot *tbs)
{
_rawstring_init(&tbs->tbs_funcname);
_rawstring_init(&tbs->tbs_filename);
tbs->tbs_lineno = -1;
tbs->tbs_next = NULL;
}
static _tbsnapshot *
_tbsnapshot_new(void)
{
_tbsnapshot *tbs = PyMem_NEW(_tbsnapshot, 1);
if (tbs == NULL) {
PyErr_NoMemory();
return NULL;
}
_tbsnapshot_init(tbs);
return tbs;
}
static void _tbsnapshot_free(_tbsnapshot *); // forward
static void
_tbsnapshot_clear(_tbsnapshot *tbs)
{
_rawstring_clear(&tbs->tbs_funcname);
_rawstring_clear(&tbs->tbs_filename);
tbs->tbs_lineno = -1;
if (tbs->tbs_next != NULL) {
_tbsnapshot_free(tbs->tbs_next);
tbs->tbs_next = NULL;
}
}
static void
_tbsnapshot_free(_tbsnapshot *tbs)
{
_tbsnapshot_clear(tbs);
PyMem_Free(tbs);
}
#ifndef NDEBUG
static int
_tbsnapshot_is_clear(_tbsnapshot *tbs)
{
return tbs->tbs_lineno == -1 && tbs->tbs_next == NULL
&& _rawstring_is_clear(&tbs->tbs_funcname)
&& _rawstring_is_clear(&tbs->tbs_filename);
}
#endif
static int
_tbsnapshot_from_pytb(_tbsnapshot *tbs, PyTracebackObject *pytb)
{
assert(_tbsnapshot_is_clear(tbs));
assert(pytb != NULL);
PyCodeObject *pycode = pytb->tb_frame->f_code;
const char *funcname = PyUnicode_AsUTF8(pycode->co_name);
if (_rawstring_strcpy(&tbs->tbs_funcname, funcname, 0) != 0) {
goto error;
}
const char *filename = PyUnicode_AsUTF8(pycode->co_filename);
if (_rawstring_strcpy(&tbs->tbs_filename, filename, 0) != 0) {
goto error;
}
tbs->tbs_lineno = pytb->tb_lineno;
return 0;
error:
_tbsnapshot_clear(tbs);
return -1;
}
static int
_tbsnapshot_extract(_tbsnapshot *tbs, PyTracebackObject *pytb)
{
assert(_tbsnapshot_is_clear(tbs));
assert(pytb != NULL);
_tbsnapshot *next = NULL;
while (pytb->tb_next != NULL) {
_tbsnapshot *_next = _tbsnapshot_new();
if (_next == NULL) {
goto error;
}
if (_tbsnapshot_from_pytb(_next, pytb) != 0) {
goto error;
}
if (next != NULL) {
_next->tbs_next = next;
}
next = _next;
pytb = pytb->tb_next;
}
if (_tbsnapshot_from_pytb(tbs, pytb) != 0) {
goto error;
}
tbs->tbs_next = next;
return 0;
error:
_tbsnapshot_clear(tbs);
return -1;
}
static PyObject *
_tbsnapshot_resolve(_tbsnapshot *tbs)
{
assert(!PyErr_Occurred());
// At this point there should be no traceback set yet.
while (tbs != NULL) {
const char *funcname = tbs->tbs_funcname.data;
const char *filename = tbs->tbs_filename.data;
_PyTraceback_Add(funcname ? funcname : "",
filename ? filename : "",
tbs->tbs_lineno);
tbs = tbs->tbs_next;
}
PyObject *exctype = NULL, *excval = NULL, *tb = NULL;
PyErr_Fetch(&exctype, &excval, &tb);
// Leave it cleared.
return tb;
}
/* exception snapshots */
typedef struct _excsnapshot {
_objsnapshot es_object;
_rawstring *es_msg;
struct _excsnapshot *es_cause;
struct _excsnapshot *es_context;
char es_suppress_context;
struct _tbsnapshot *es_traceback;
} _excsnapshot;
static void
_excsnapshot_init(_excsnapshot *es)
{
_objsnapshot_init(&es->es_object);
es->es_msg = NULL;
es->es_cause = NULL;
es->es_context = NULL;
es->es_suppress_context = 0;
es->es_traceback = NULL;
}
static _excsnapshot *
_excsnapshot_new(void) {
_excsnapshot *es = PyMem_NEW(_excsnapshot, 1);
if (es == NULL) {
PyErr_NoMemory();
return NULL;
}
_excsnapshot_init(es);
return es;
}
static void _excsnapshot_free(_excsnapshot *); // forward
static void
_excsnapshot_clear(_excsnapshot *es)
{
_objsnapshot_clear(&es->es_object);
if (es->es_msg != NULL) {
_rawstring_free(es->es_msg);
es->es_msg = NULL;
}
if (es->es_cause != NULL) {
_excsnapshot_free(es->es_cause);
es->es_cause = NULL;
}
if (es->es_context != NULL) {
_excsnapshot_free(es->es_context);
es->es_context = NULL;
}
es->es_suppress_context = 0;
if (es->es_traceback != NULL) {
_tbsnapshot_free(es->es_traceback);
es->es_traceback = NULL;
}
}
static void
_excsnapshot_free(_excsnapshot *es)
{
_excsnapshot_clear(es);
PyMem_Free(es);
}
#ifndef NDEBUG
static int
_excsnapshot_is_clear(_excsnapshot *es)
{
return es->es_suppress_context == 0
&& es->es_cause == NULL
&& es->es_context == NULL
&& es->es_traceback == NULL
&& es->es_msg == NULL
&& _objsnapshot_is_clear(&es->es_object);
}
#endif
static PyObject *
_excsnapshot_get_exc_naive(_excsnapshot *es)
{
_rawstring buf;
const char *msg = NULL;
if (es->es_msg != NULL) {
msg = es->es_msg->data;
} else {
_objsnapshot_summarize(&es->es_object, &buf, NULL);
if (buf.size > 0) {
msg = buf.data;
}
}
PyObject *exc = NULL;
// XXX Use _objsnapshot_resolve_naive()?
const char *modname = es->es_object.modname.size > 0
? es->es_object.modname.data
: NULL;
PyObject *exctype = _pyobj_get_class(modname, es->es_object.clsname.data);
if (exctype != NULL) {
exc = _pyexc_create(exctype, msg, NULL);
Py_DECREF(exctype);
if (exc != NULL) {
return exc;
}
PyErr_Clear();
} else {
PyErr_Clear();
}
exctype = PyExc_Exception;
return _pyexc_create(exctype, msg, NULL);
}
static PyObject *
_excsnapshot_get_exc(_excsnapshot *es)
{
assert(!_objsnapshot_is_clear(&es->es_object));
PyObject *exc = _objsnapshot_resolve(&es->es_object);
if (exc == NULL) {
// Fall back to resolving the object.
PyObject *curtype = NULL, *curexc = NULL, *curtb = NULL;
PyErr_Fetch(&curtype, &curexc, &curtb);
exc = _excsnapshot_get_exc_naive(es);
if (exc == NULL) {
PyErr_Restore(curtype, curexc, curtb);
return NULL;
}
}
// People can do some weird stuff...
if (!PyExceptionInstance_Check(exc)) {
// We got a bogus "exception".
Py_DECREF(exc);
PyErr_SetString(PyExc_TypeError, "expected exception");
return NULL;
}
return exc;
}
static void _excsnapshot_extract(_excsnapshot *, PyObject *);
static void
_excsnapshot_extract(_excsnapshot *es, PyObject *excobj)
{
assert(_excsnapshot_is_clear(es));
assert(PyExceptionInstance_Check(excobj));
_objsnapshot_extract(&es->es_object, excobj);
es->es_msg = _objsnapshot_get_minimal_summary(&es->es_object, excobj);
if (es->es_msg == NULL) {
PyErr_Clear();
}
PyBaseExceptionObject *exc = (PyBaseExceptionObject *)excobj;
if (exc->cause != NULL && exc->cause != Py_None) {
es->es_cause = _excsnapshot_new();
_excsnapshot_extract(es->es_cause, exc->cause);
}
if (exc->context != NULL && exc->context != Py_None) {
es->es_context = _excsnapshot_new();
_excsnapshot_extract(es->es_context, exc->context);
}
es->es_suppress_context = exc->suppress_context;
PyObject *tb = PyException_GetTraceback(excobj);
if (PyErr_Occurred()) {
IGNORE_FAILURE("could not get traceback");
} else if (tb == Py_None) {
Py_DECREF(tb);
tb = NULL;
}
if (tb != NULL) {
es->es_traceback = _tbsnapshot_new();
if (_tbsnapshot_extract(es->es_traceback,
(PyTracebackObject *)tb) != 0) {
IGNORE_FAILURE("could not extract __traceback__");
}
}
}
static PyObject *
_excsnapshot_resolve(_excsnapshot *es)
{
PyObject *exc = _excsnapshot_get_exc(es);
if (exc == NULL) {
return NULL;
}
if (es->es_traceback != NULL) {
PyObject *tb = _tbsnapshot_resolve(es->es_traceback);
if (tb == NULL) {
// The snapshot is still somewhat useful without this.
IGNORE_FAILURE("could not deserialize traceback");
} else {
// This does not steal references.
PyException_SetTraceback(exc, tb);
Py_DECREF(tb);
}
}
// NULL means "not set".
if (es->es_context != NULL) {
PyObject *context = _excsnapshot_resolve(es->es_context);
if (context == NULL) {
// The snapshot is still useful without this.
IGNORE_FAILURE("could not deserialize __context__");
} else {
// This steals references but we have one to give.
PyException_SetContext(exc, context);
}
}
// NULL means "not set".
if (es->es_cause != NULL) {
PyObject *cause = _excsnapshot_resolve(es->es_cause);
if (cause == NULL) {
// The snapshot is still useful without this.
IGNORE_FAILURE("could not deserialize __cause__");
} else {
// This steals references, but we have one to give.
PyException_SetCause(exc, cause);
}
}
// NULL means "not set".
((PyBaseExceptionObject *)exc)->suppress_context = es->es_suppress_context;
return exc;
}
/* data-sharing-specific code ***********************************************/
/* shared "object" */
struct _sharednsitem {
_rawstring name;
_PyCrossInterpreterData data;
};
static void _sharednsitem_clear(struct _sharednsitem *); // forward
static int
_sharednsitem_init(struct _sharednsitem *item, PyObject *key, PyObject *value)
{
if (_rawstring_from_pyobj(&item->name, key) != 0) {
return -1;
}
if (_PyObject_GetCrossInterpreterData(value, &item->data) != 0) {
_sharednsitem_clear(item);
return -1;
}
return 0;
}
static void
_sharednsitem_clear(struct _sharednsitem *item)
{
_rawstring_clear(&item->name);
_PyCrossInterpreterData_Release(&item->data);
}
static int
_sharednsitem_apply(struct _sharednsitem *item, PyObject *ns)
{
PyObject *name = PyUnicode_FromString(item->name.data);
if (name == NULL) {
return -1;
}
PyObject *value = _PyCrossInterpreterData_NewObject(&item->data);
if (value == NULL) {
Py_DECREF(name);
return -1;
}
int res = PyDict_SetItem(ns, name, value);
Py_DECREF(name);
Py_DECREF(value);
return res;
}
typedef struct _sharedns {
Py_ssize_t len;
struct _sharednsitem* items;
} _sharedns;
static _sharedns *
_sharedns_new(Py_ssize_t len)
{
_sharedns *shared = PyMem_NEW(_sharedns, 1);
if (shared == NULL) {
PyErr_NoMemory();
return NULL;
}
shared->len = len;
shared->items = PyMem_NEW(struct _sharednsitem, len);
if (shared->items == NULL) {
PyErr_NoMemory();
PyMem_Free(shared);
return NULL;
}
return shared;
}
static void
_sharedns_free(_sharedns *shared)
{
for (Py_ssize_t i=0; i < shared->len; i++) {
_sharednsitem_clear(&shared->items[i]);