[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/cpython/blob/main/Modules/_interpretersmodule.c
[Back]
[Original]
cpython/Modules/_interpretersmodule.c at main python/cpython GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.6k
(2.6k)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
cpython
/
Modules
/
_interpretersmodule.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
1684 lines (1445 loc) 45.4 KB
main
Breadcrumbs
cpython
/
Modules
/
_interpretersmodule.c
Copy path
Top
File metadata and controls
Code
Blame
1684 lines (1445 loc) 45.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 */
#ifndef
Py_BUILD_CORE_BUILTIN
# define
Py_BUILD_CORE_MODULE
1
#endif
#include
"Python.h"
#include
"pycore_code.h"
// _PyCode_HAS_EXECUTORS()
#include
"pycore_crossinterp.h"
// _PyXIData_t
#include
"pycore_pyerrors.h"
// _PyErr_GetRaisedException()
#include
"pycore_interp.h"
// _PyInterpreterState_IDIncref()
#include
"pycore_modsupport.h"
// _PyArg_BadArgument()
#include
"pycore_namespace.h"
// _PyNamespace_New()
#include
"pycore_pybuffer.h"
// _PyBuffer_ReleaseInInterpreterAndRawFree()
#include
"pycore_pylifecycle.h"
// _PyInterpreterConfig_AsDict()
#include
"pycore_pystate.h"
// _PyInterpreterState_IsRunningMain()
#include
"pycore_tuple.h"
// _PyTuple_FromPairSteal
#include
"marshal.h"
// PyMarshal_ReadObjectFromString()
#include
"_interpreters_common.h"
#include
"clinic/_interpretersmodule.c.h"
#define
MODULE_NAME
_interpreters
#define
MODULE_NAME_STR
Py_STRINGIFY(MODULE_NAME)
#define
MODINIT_FUNC_NAME
RESOLVE_MODINIT_FUNC_NAME(MODULE_NAME)
/*[clinic input]
module _interpreters
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=bfd967980a0de892]*/
static
PyInterpreterState
*
_get_current_interp
(
void
)
{
// PyInterpreterState_Get() aborts if lookup fails, so don't need
// to check the result for NULL.
return
PyInterpreterState_Get
();
}
#define
look_up_interp
_PyInterpreterState_LookUpIDObject
static
PyObject
*
_get_current_module
(
void
)
{
PyObject
*
name
=
PyUnicode_FromString
(
MODULE_NAME_STR
);
if
(
name
==
NULL
) {
return
NULL
;
}
PyObject
*
mod
=
PyImport_GetModule
(
name
);
Py_DECREF
(
name
);
if
(
mod
==
NULL
) {
return
NULL
;
}
assert
(
mod
!=
Py_None
);
return
mod
;
}
static
int
is_running_main
(
PyInterpreterState
*
interp
)
{
if
(
_PyInterpreterState_IsRunningMain
(
interp
)) {
return
1
;
}
// Unlike with the general C-API, we can be confident that someone
// using this module for the main interpreter is doing so through
// the main program. Thus we can make this extra check. This benefits
// applications that embed Python but haven't been updated yet
// to call _PyInterpreterState_SetRunningMain().
if
(
_Py_IsMainInterpreter
(
interp
)) {
return
1
;
}
return
0
;
}
static
inline
int
is_notshareable_raised
(
PyThreadState
*
tstate
)
{
PyObject
*
exctype
=
_PyXIData_GetNotShareableErrorType
(
tstate
);
return
_PyErr_ExceptionMatches
(
tstate
,
exctype
);
}
static
void
unwrap_not_shareable
(
PyThreadState
*
tstate
,
_PyXI_failure
*
failure
)
{
if
(
_PyXI_UnwrapNotShareableError
(
tstate
,
failure
)
<
0
) {
_PyErr_Clear
(
tstate
);
}
}
/* Cross-interpreter Buffer Views *******************************************/
/* When a memoryview object is "shared" between interpreters,
* its underlying "buffer" memory is actually shared, rather than just
* copied. This facilitates efficient use of that data where otherwise
* interpreters are strictly isolated. However, this also means that
* the underlying data is subject to the complexities of thread-safety,
* which the user must manage carefully.
*
* When the memoryview is "shared", it is essentially copied in the same
* way as PyMemory_FromObject() does, but in another interpreter.
* The Py_buffer value is copied like normal, including the "buf" pointer,
* with one key exception.
*
* When a Py_buffer is released and it holds a reference to an object,
* that object gets a chance to call its bf_releasebuffer() (if any)
* before the object is decref'ed. The same is true with the memoryview
* tp_dealloc, which essentially calls PyBuffer_Release().
*
* The problem for a Py_buffer shared between two interpreters is that
* the naive approach breaks interpreter isolation. Operations on an
* object must only happen while that object's interpreter is active.
* If the copied mv->view.obj pointed to the original memoryview then
* the PyBuffer_Release() would happen under the wrong interpreter.
*
* To work around this, we set mv->view.obj on the copied memoryview
* to a wrapper object with the only job of releasing the original
* buffer in a cross-interpreter-safe way.
*/
// XXX Note that there is still an issue to sort out, where the original
// interpreter is destroyed but code in another interpreter is still
// using dependent buffers. Using such buffers segfaults. This will
// require a careful fix. In the meantime, users will have to be
// diligent about avoiding the problematic situation.
typedef
struct
{
PyObject
base
;
Py_buffer
*
view
;
int64_t
interpid
;
}
xibufferview
;
static
PyObject
*
xibufferview_from_buffer
(
PyTypeObject
*
cls
,
Py_buffer
*
view
,
int64_t
interpid
)
{
assert
(
interpid
>=
0
);
Py_buffer
*
copied
=
PyMem_RawMalloc
(
sizeof
(
Py_buffer
));
if
(
copied
==
NULL
) {
return
PyErr_NoMemory
();
}
/* This steals the view->obj reference */
*
copied
=
*
view
;
xibufferview
*
self
=
PyObject_Malloc
(
sizeof
(
xibufferview
));
if
(
self
==
NULL
) {
PyMem_RawFree
(
copied
);
return
PyErr_NoMemory
();
}
PyObject_Init
(
&
self
->
base
,
cls
);
*
self
=
(
xibufferview
){
.
base
=
self
->
base
,
.
view
=
copied
,
.
interpid
=
interpid
,
};
return
(
PyObject
*
)
self
;
}
static
void
xibufferview_dealloc
(
PyObject
*
op
)
{
xibufferview
*
self
=
(
xibufferview
*
)
op
;
if
(
self
->
view
!=
NULL
) {
PyInterpreterState
*
interp
=
_PyInterpreterState_LookUpID
(
self
->
interpid
);
if
(
interp
==
NULL
) {
/* The interpreter is no longer alive. */
PyErr_Clear
();
PyMem_RawFree
(
self
->
view
);
}
else
{
if
(
_PyBuffer_ReleaseInInterpreterAndRawFree
(
interp
,
self
->
view
)
<
0
)
{
// XXX Emit a warning?
PyErr_Clear
();
}
}
}
PyTypeObject
*
tp
=
Py_TYPE
(
self
);
tp
->
tp_free
(
self
);
/* "Instances of heap-allocated types hold a reference to their type."
* See: https://docs.python.org/3.11/howto/isolating-extensions.html#garbage-collection-protocol
* See: https://docs.python.org/3.11/c-api/typeobj.html#c.PyTypeObject.tp_traverse
*/
// XXX Why don't we implement Py_TPFLAGS_HAVE_GC, e.g. Py_tp_traverse,
// like we do for _abc._abc_data?
Py_DECREF
(
tp
);
}
static
int
xibufferview_getbuf
(
PyObject
*
op
,
Py_buffer
*
view
,
int
flags
)
{
/* Only PyMemoryView_FromObject() should ever call this,
via _memoryview_from_xid() below. */
xibufferview
*
self
=
(
xibufferview
*
)
op
;
*
view
=
*
self
->
view
;
/* This is the workaround mentioned earlier. */
view
->
obj
=
op
;
// XXX Should we leave it alone?
view
->
internal
=
NULL
;
return
0
;
}
static
PyType_Slot
XIBufferViewType_slots
[]
=
{
{
Py_tp_dealloc
,
xibufferview_dealloc
},
{
Py_bf_getbuffer
,
xibufferview_getbuf
},
// We don't bother with Py_bf_releasebuffer since we don't need it.
{
0
,
NULL
},
};
static
PyType_Spec
XIBufferViewType_spec
=
{
.
name
=
MODULE_NAME_STR
".CrossInterpreterBufferView"
,
.
basicsize
=
sizeof
(
xibufferview
),
.
flags
=
(
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_BASETYPE
|
Py_TPFLAGS_DISALLOW_INSTANTIATION
|
Py_TPFLAGS_IMMUTABLETYPE
),
.
slots
=
XIBufferViewType_slots
,
};
static
PyTypeObject
*
_get_current_xibufferview_type
(
void
);
struct
xibuffer
{
Py_buffer
view
;
int
used
;
};
static
PyObject
*
_memoryview_from_xid
(
_PyXIData_t
*
data
)
{
assert
(
_PyXIData_DATA
(
data
)
!=
NULL
);
assert
(
_PyXIData_OBJ
(
data
)
==
NULL
);
assert
(
_PyXIData_INTERPID
(
data
) >=
0
);
struct
xibuffer
*
view
=
(
struct
xibuffer
*
)
_PyXIData_DATA
(
data
);
assert
(!
view
->
used
);
PyTypeObject
*
cls
=
_get_current_xibufferview_type
();
if
(
cls
==
NULL
) {
return
NULL
;
}
PyObject
*
obj
=
xibufferview_from_buffer
(
cls
,
&
view
->
view
,
_PyXIData_INTERPID
(
data
));
if
(
obj
==
NULL
) {
return
NULL
;
}
PyObject
*
res
=
PyMemoryView_FromObject
(
obj
);
if
(
res
==
NULL
) {
Py_DECREF
(
obj
);
return
NULL
;
}
view
->
used
=
1
;
return
res
;
}
static
void
_pybuffer_shared_free
(
void
*
data
)
{
struct
xibuffer
*
view
=
(
struct
xibuffer
*
)
data
;
if
(!
view
->
used
) {
PyBuffer_Release
(
&
view
->
view
);
}
PyMem_RawFree
(
data
);
}
static
int
_pybuffer_shared
(
PyThreadState
*
tstate
,
PyObject
*
obj
,
_PyXIData_t
*
data
)
{
struct
xibuffer
*
view
=
PyMem_RawMalloc
(
sizeof
(
struct
xibuffer
));
if
(
view
==
NULL
) {
PyErr_NoMemory
();
return
-1
;
}
view
->
used
=
0
;
/* This will increment the memoryview's export count, which won't get
* decremented until the view sent to other interpreters is released. */
if
(
PyObject_GetBuffer
(
obj
,
&
view
->
view
,
PyBUF_FULL_RO
)
<
0
) {
PyMem_RawFree
(
view
);
return
-1
;
}
/* The view holds a reference to the object, so we don't worry
* about also tracking it on the cross-interpreter data. */
_PyXIData_Init
(
data
,
tstate
->
interp
,
view
,
NULL
,
_memoryview_from_xid
);
data
->
free
=
_pybuffer_shared_free
;
return
0
;
}
static
int
register_memoryview_xid
(
PyObject
*
mod
,
PyTypeObject
*
*
p_state
)
{
// XIBufferView
assert
(
*
p_state
==
NULL
);
PyTypeObject
*
cls
=
(
PyTypeObject
*
)
PyType_FromModuleAndSpec
(
mod
,
&
XIBufferViewType_spec
,
NULL
);
if
(
cls
==
NULL
) {
return
-1
;
}
if
(
PyModule_AddType
(
mod
,
cls
)
<
0
) {
Py_DECREF
(
cls
);
return
-1
;
}
*
p_state
=
cls
;
// Register XID for the builtin memoryview type.
if
(
ensure_xid_class
(
&
PyMemoryView_Type
,
GETDATA
(
_pybuffer_shared
))
<
0
) {
return
-1
;
}
// We don't ever bother un-registering memoryview.
return
0
;
}
/* module state *************************************************************/
typedef
struct
{
int
_notused
;
/* heap types */
PyTypeObject
*
XIBufferViewType
;
}
module_state
;
static
inline
module_state
*
get_module_state
(
PyObject
*
mod
)
{
assert
(
mod
!=
NULL
);
module_state
*
state
=
PyModule_GetState
(
mod
);
assert
(
state
!=
NULL
);
return
state
;
}
static
module_state
*
_get_current_module_state
(
void
)
{
PyObject
*
mod
=
_get_current_module
();
if
(
mod
==
NULL
) {
mod
=
PyImport_ImportModule
(
MODULE_NAME_STR
);
if
(
mod
==
NULL
) {
return
NULL
;
}
}
module_state
*
state
=
get_module_state
(
mod
);
Py_DECREF
(
mod
);
return
state
;
}
static
int
traverse_module_state
(
module_state
*
state
,
visitproc
visit
,
void
*
arg
)
{
/* heap types */
Py_VISIT
(
state
->
XIBufferViewType
);
return
0
;
}
static
int
clear_module_state
(
module_state
*
state
)
{
/* heap types */
Py_CLEAR
(
state
->
XIBufferViewType
);
return
0
;
}
static
PyTypeObject
*
_get_current_xibufferview_type
(
void
)
{
module_state
*
state
=
_get_current_module_state
();
if
(
state
==
NULL
) {
return
NULL
;
}
return
state
->
XIBufferViewType
;
}
/* interpreter-specific code ************************************************/
static
int
init_named_config
(
PyInterpreterConfig
*
config
,
const
char
*
name
)
{
if
(
name
==
NULL
||
strcmp
(
name
,
""
)
==
0
||
strcmp
(
name
,
"default"
)
==
0
)
{
name
=
"isolated"
;
}
if
(
strcmp
(
name
,
"isolated"
)
==
0
) {
*
config
=
(
PyInterpreterConfig
)
_PyInterpreterConfig_INIT
;
}
else
if
(
strcmp
(
name
,
"legacy"
)
==
0
) {
*
config
=
(
PyInterpreterConfig
)
_PyInterpreterConfig_LEGACY_INIT
;
}
else
if
(
strcmp
(
name
,
"empty"
)
==
0
) {
*
config
=
(
PyInterpreterConfig
){
0
};
}
else
{
PyErr_Format
(
PyExc_ValueError
,
"unsupported config name '%s'"
,
name
);
return
-1
;
}
return
0
;
}
static
int
config_from_object
(
PyObject
*
configobj
,
PyInterpreterConfig
*
config
)
{
if
(
configobj
==
NULL
||
configobj
==
Py_None
) {
if
(
init_named_config
(
config
,
NULL
)
<
0
) {
return
-1
;
}
}
else
if
(
PyUnicode_Check
(
configobj
)) {
const
char
*
utf8name
=
PyUnicode_AsUTF8
(
configobj
);
if
(
utf8name
==
NULL
) {
return
-1
;
}
if
(
init_named_config
(
config
,
utf8name
)
<
0
) {
return
-1
;
}
}
else
{
PyObject
*
dict
=
PyObject_GetAttrString
(
configobj
,
"__dict__"
);
if
(
dict
==
NULL
) {
PyErr_Format
(
PyExc_TypeError
,
"bad config %R"
,
configobj
);
return
-1
;
}
int
res
=
_PyInterpreterConfig_InitFromDict
(
config
,
dict
);
Py_DECREF
(
dict
);
if
(
res
<
0
) {
return
-1
;
}
}
return
0
;
}
struct
interp_call
{
_PyXIData_t
*
func
;
_PyXIData_t
*
args
;
_PyXIData_t
*
kwargs
;
struct
{
_PyXIData_t
func
;
_PyXIData_t
args
;
_PyXIData_t
kwargs
;
}
_preallocated
;
};
static
void
_interp_call_clear
(
struct
interp_call
*
call
)
{
if
(
call
->
func
!=
NULL
) {
_PyXIData_Clear
(
NULL
,
call
->
func
);
}
if
(
call
->
args
!=
NULL
) {
_PyXIData_Clear
(
NULL
,
call
->
args
);
}
if
(
call
->
kwargs
!=
NULL
) {
_PyXIData_Clear
(
NULL
,
call
->
kwargs
);
}
*
call
=
(
struct
interp_call
){
0
};
}
static
int
_interp_call_pack
(
PyThreadState
*
tstate
,
struct
interp_call
*
call
,
PyObject
*
func
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
xidata_fallback_t
fallback
=
_PyXIDATA_FULL_FALLBACK
;
assert
(
call
->
func
==
NULL
);
assert
(
call
->
args
==
NULL
);
assert
(
call
->
kwargs
==
NULL
);
// Handle the func.
if
(!
PyCallable_Check
(
func
)) {
_PyErr_Format
(
tstate
,
PyExc_TypeError
,
"expected a callable, got %R"
,
func
);
return
-1
;
}
if
(
_PyFunction_GetXIData
(
tstate
,
func
,
&
call
->
_preallocated
.
func
)
<
0
) {
PyObject
*
exc
=
_PyErr_GetRaisedException
(
tstate
);
if
(
_PyPickle_GetXIData
(
tstate
,
func
,
&
call
->
_preallocated
.
func
)
<
0
) {
_PyErr_SetRaisedException
(
tstate
,
exc
);
return
-1
;
}
Py_DECREF
(
exc
);
}
call
->
func
=
&
call
->
_preallocated
.
func
;
// Handle the args.
if
(
args
==
NULL
||
args
==
Py_None
) {
// Leave it empty.
}
else
{
assert
(
PyTuple_Check
(
args
));
if
(
PyTuple_GET_SIZE
(
args
)
>
0
) {
if
(
_PyObject_GetXIData
(
tstate
,
args
,
fallback
,
&
call
->
_preallocated
.
args
)
<
0
)
{
_interp_call_clear
(
call
);
return
-1
;
}
call
->
args
=
&
call
->
_preallocated
.
args
;
}
}
// Handle the kwargs.
if
(
kwargs
==
NULL
||
kwargs
==
Py_None
) {
// Leave it empty.
}
else
{
assert
(
PyDict_Check
(
kwargs
));
if
(
PyDict_GET_SIZE
(
kwargs
)
>
0
) {
if
(
_PyObject_GetXIData
(
tstate
,
kwargs
,
fallback
,
&
call
->
_preallocated
.
kwargs
)
<
0
)
{
_interp_call_clear
(
call
);
return
-1
;
}
call
->
kwargs
=
&
call
->
_preallocated
.
kwargs
;
}
}
return
0
;
}
static
void
wrap_notshareable
(
PyThreadState
*
tstate
,
const
char
*
label
)
{
if
(!
is_notshareable_raised
(
tstate
)) {
return
;
}
assert
(
label
!=
NULL
&&
strlen
(
label
)
>
0
);
PyObject
*
cause
=
_PyErr_GetRaisedException
(
tstate
);
_PyXIData_FormatNotShareableError
(
tstate
,
"%s not shareable"
,
label
);
PyObject
*
exc
=
_PyErr_GetRaisedException
(
tstate
);
PyException_SetCause
(
exc
,
cause
);
_PyErr_SetRaisedException
(
tstate
,
exc
);
}
static
int
_interp_call_unpack
(
struct
interp_call
*
call
,
PyObject
*
*
p_func
,
PyObject
*
*
p_args
,
PyObject
*
*
p_kwargs
)
{
PyThreadState
*
tstate
=
PyThreadState_Get
();
// Unpack the func.
PyObject
*
func
=
_PyXIData_NewObject
(
call
->
func
);
if
(
func
==
NULL
) {
wrap_notshareable
(
tstate
,
"func"
);
return
-1
;
}
// Unpack the args.
PyObject
*
args
;
if
(
call
->
args
==
NULL
) {
args
=
PyTuple_New
(
0
);
if
(
args
==
NULL
) {
Py_DECREF
(
func
);
return
-1
;
}
}
else
{
args
=
_PyXIData_NewObject
(
call
->
args
);
if
(
args
==
NULL
) {
wrap_notshareable
(
tstate
,
"args"
);
Py_DECREF
(
func
);
return
-1
;
}
assert
(
PyTuple_Check
(
args
));
}
// Unpack the kwargs.
PyObject
*
kwargs
=
NULL
;
if
(
call
->
kwargs
!=
NULL
) {
kwargs
=
_PyXIData_NewObject
(
call
->
kwargs
);
if
(
kwargs
==
NULL
) {
wrap_notshareable
(
tstate
,
"kwargs"
);
Py_DECREF
(
func
);
Py_DECREF
(
args
);
return
-1
;
}
assert
(
PyDict_Check
(
kwargs
));
}
*
p_func
=
func
;
*
p_args
=
args
;
*
p_kwargs
=
kwargs
;
return
0
;
}
static
int
_make_call
(
struct
interp_call
*
call
,
PyObject
*
*
p_result
,
_PyXI_failure
*
failure
)
{
assert
(
call
!=
NULL
&&
call
->
func
!=
NULL
);
PyThreadState
*
tstate
=
_PyThreadState_GET
();
// Get the func and args.
PyObject
*
func
=
NULL
,
*
args
=
NULL
,
*
kwargs
=
NULL
;
if
(
_interp_call_unpack
(
call
,
&
func
,
&
args
,
&
kwargs
)
<
0
) {
assert
(
func
==
NULL
);
assert
(
args
==
NULL
);
assert
(
kwargs
==
NULL
);
_PyXI_InitFailure
(
failure
,
_PyXI_ERR_OTHER
,
NULL
);
unwrap_not_shareable
(
tstate
,
failure
);
return
-1
;
}
assert
(!
_PyErr_Occurred
(
tstate
));
// Make the call.
PyObject
*
resobj
=
PyObject_Call
(
func
,
args
,
kwargs
);
Py_DECREF
(
func
);
Py_XDECREF
(
args
);
Py_XDECREF
(
kwargs
);
if
(
resobj
==
NULL
) {
return
-1
;
}
*
p_result
=
resobj
;
return
0
;
}
static
int
_run_script
(
_PyXIData_t
*
script
,
PyObject
*
ns
,
_PyXI_failure
*
failure
)
{
PyObject
*
code
=
_PyXIData_NewObject
(
script
);
if
(
code
==
NULL
) {
_PyXI_InitFailure
(
failure
,
_PyXI_ERR_NOT_SHAREABLE
,
NULL
);
return
-1
;
}
PyObject
*
result
=
PyEval_EvalCode
(
code
,
ns
,
ns
);
Py_DECREF
(
code
);
if
(
result
==
NULL
) {
_PyXI_InitFailure
(
failure
,
_PyXI_ERR_UNCAUGHT_EXCEPTION
,
NULL
);
return
-1
;
}
assert
(
result
==
Py_None
);
Py_DECREF
(
result
);
// We throw away the result.
return
0
;
}
struct
run_result
{
PyObject
*
result
;
PyObject
*
excinfo
;
};
static
void
_run_result_clear
(
struct
run_result
*
runres
)
{
Py_CLEAR
(
runres
->
result
);
Py_CLEAR
(
runres
->
excinfo
);
}
static
int
_run_in_interpreter
(
PyThreadState
*
tstate
,
PyInterpreterState
*
interp
,
_PyXIData_t
*
script
,
struct
interp_call
*
call
,
PyObject
*
shareables
,
struct
run_result
*
runres
)
{
assert
(!
_PyErr_Occurred
(
tstate
));
int
res
=
-1
;
_PyXI_failure
*
failure
=
_PyXI_NewFailure
();
if
(
failure
==
NULL
) {
return
-1
;
}
_PyXI_session
*
session
=
_PyXI_NewSession
();
if
(
session
==
NULL
) {
_PyXI_FreeFailure
(
failure
);
return
-1
;
}
_PyXI_session_result
result
=
{
0
};
// Prep and switch interpreters.
if
(
_PyXI_Enter
(
session
,
interp
,
shareables
,
&
result
)
<
0
) {
// If an error occurred at this step, it means that interp
// was not prepared and switched.
_PyXI_FreeSession
(
session
);
_PyXI_FreeFailure
(
failure
);
assert
(
result
.
excinfo
==
NULL
);
return
-1
;
}
// Run in the interpreter.
if
(
script
!=
NULL
) {
assert
(
call
==
NULL
);
PyObject
*
mainns
=
_PyXI_GetMainNamespace
(
session
,
failure
);
if
(
mainns
==
NULL
) {
goto
finally
;
}
res
=
_run_script
(
script
,
mainns
,
failure
);
}
else
{
assert
(
call
!=
NULL
);
PyObject
*
resobj
;
res
=
_make_call
(
call
,
&
resobj
,
failure
);
if
(
res
==
0
) {
res
=
_PyXI_Preserve
(
session
,
"resobj"
,
resobj
,
failure
);
Py_DECREF
(
resobj
);
if
(
res
<
0
) {
goto
finally
;
}
}
}
finally
:
// Clean up and switch back.
(
void
)
res
;
int
exitres
=
_PyXI_Exit
(
session
,
failure
,
&
result
);
assert
(
res
==
0
||
exitres
!=
0
);
_PyXI_FreeSession
(
session
);
_PyXI_FreeFailure
(
failure
);
res
=
exitres
;
if
(
_PyErr_Occurred
(
tstate
)) {
// It's a directly propagated exception.
assert
(
res
<
0
);
}
else
if
(
res
<
0
) {
assert
(
result
.
excinfo
!=
NULL
);
runres
->
excinfo
=
Py_NewRef
(
result
.
excinfo
);
res
=
-1
;
}
else
{
assert
(
result
.
excinfo
==
NULL
);
runres
->
result
=
_PyXI_GetPreserved
(
&
result
,
"resobj"
);
if
(
_PyErr_Occurred
(
tstate
)) {
res
=
-1
;
}
}
_PyXI_ClearResult
(
&
result
);
return
res
;
}
/* module level code ********************************************************/
static
long
get_whence
(
PyInterpreterState
*
interp
)
{
return
_PyInterpreterState_GetWhence
(
interp
);
}
static
PyInterpreterState
*
resolve_interp
(
PyObject
*
idobj
,
int
restricted
,
int
reqready
,
const
char
*
op
)
{
PyInterpreterState
*
interp
;
if
(
idobj
==
NULL
) {
interp
=
PyInterpreterState_Get
();
}
else
{
interp
=
look_up_interp
(
idobj
);
if
(
interp
==
NULL
) {
return
NULL
;
}
}
if
(
reqready
&&
!
_PyInterpreterState_IsReady
(
interp
)) {
if
(
idobj
==
NULL
) {
PyErr_Format
(
PyExc_InterpreterError
,
"cannot %s current interpreter (not ready)"
,
op
);
}
else
{
PyErr_Format
(
PyExc_InterpreterError
,
"cannot %s interpreter %R (not ready)"
,
op
,
idobj
);
}
return
NULL
;
}
if
(
restricted
&&
get_whence
(
interp
)
!=
_PyInterpreterState_WHENCE_STDLIB
) {
if
(
idobj
==
NULL
) {
PyErr_Format
(
PyExc_InterpreterError
,
"cannot %s unrecognized current interpreter"
,
op
);
}
else
{
PyErr_Format
(
PyExc_InterpreterError
,
"cannot %s unrecognized interpreter %R"
,
op
,
idobj
);
}
return
NULL
;
}
return
interp
;
}
static
PyObject
*
get_summary
(
PyInterpreterState
*
interp
)
{
PyObject
*
idobj
=
_PyInterpreterState_GetIDObject
(
interp
);
if
(
idobj
==
NULL
) {
return
NULL
;
}
PyObject
*
whenceobj
=
PyLong_FromLong
(
get_whence
(
interp
));
if
(
whenceobj
==
NULL
) {
Py_DECREF
(
idobj
);
return
NULL
;
}
return
_PyTuple_FromPairSteal
(
idobj
,
whenceobj
);
}
/*[clinic input]
_interpreters.new_config
name: str(c_default="NULL") = 'isolated'
/
**overrides: dict
Return a representation of a new PyInterpreterConfig.
The name determines the initial values of the config. Supported named
configs are: default, isolated, legacy, and empty.
Any keyword arguments are set on the corresponding config fields,
overriding the initial values.
[clinic start generated code]*/
static
PyObject
*
_interpreters_new_config_impl
(
PyObject
*
module
,
const
char
*
name
,
PyObject
*
overrides
)
/*[clinic end generated code: output=90ca53e5c8f66e31 input=67a5a2832894a72b]*/
{
PyInterpreterConfig
config
;
if
(
init_named_config
(
&
config
,
name
)
<
0
) {
return
NULL
;
}
if
(
PyDict_GET_SIZE
(
overrides
)
>
0
) {
if
(
_PyInterpreterConfig_UpdateFromDict
(
&
config
,
overrides
)
<
0
) {
return
NULL
;
}
}
PyObject
*
dict
=
_PyInterpreterConfig_AsDict
(
&
config
);
if
(
dict
==
NULL
) {
return
NULL
;
}
PyObject
*
configobj
=
_PyNamespace_New
(
dict
);
Py_DECREF
(
dict
);
return
configobj
;
}
/*[clinic input]
_interpreters.create
config as configobj: object(py_default="'isolated'") = NULL
*
reqrefs: bool = False
Create a new interpreter and return a unique generated ID.
The caller is responsible for destroying the interpreter before exiting,
typically by using _interpreters.destroy(). This can be managed
automatically by passing "reqrefs=True" and then using _incref() and
_decref() appropriately.
"config" must be a valid interpreter config or the name of a
predefined config ('isolated' or 'legacy'). The default
is 'isolated'.
[clinic start generated code]*/
static
PyObject
*
_interpreters_create_impl
(
PyObject
*
module
,
PyObject
*
configobj
,
int
reqrefs
)
/*[clinic end generated code: output=c1cc6835b1277c16 input=235ce396a23624d5]*/
{
PyInterpreterConfig
config
;
if
(
config_from_object
(
configobj
,
&
config
)
<
0
) {
return
NULL
;
}
long
whence
=
_PyInterpreterState_WHENCE_STDLIB
;
PyInterpreterState
*
interp
=
\
_PyXI_NewInterpreter
(
&
config
,
&
whence
,
NULL
,
NULL
);
if
(
interp
==
NULL
) {
// XXX Move the chained exception to interpreters.create()?
PyObject
*
exc
=
PyErr_GetRaisedException
();
assert
(
exc
!=
NULL
);
PyErr_SetString
(
PyExc_InterpreterError
,
"interpreter creation failed"
);
_PyErr_ChainExceptions1
(
exc
);
return
NULL
;
}
assert
(
_PyInterpreterState_IsReady
(
interp
));
PyObject
*
idobj
=
_PyInterpreterState_GetIDObject
(
interp
);
if
(
idobj
==
NULL
) {
_PyXI_EndInterpreter
(
interp
,
NULL
,
NULL
);
return
NULL
;
}
if
(
reqrefs
) {
// Decref to 0 will destroy the interpreter.
_PyInterpreterState_RequireIDRef
(
interp
,
1
);
}
return
idobj
;
}
/*[clinic input]
_interpreters.destroy
id: object
*
restrict as restricted: bool = False
Destroy the identified interpreter.
Attempting to destroy the current interpreter raises InterpreterError.
So does an unrecognized ID.
[clinic start generated code]*/
static
PyObject
*
_interpreters_destroy_impl
(
PyObject
*
module
,
PyObject
*
id
,
int
restricted
)
/*[clinic end generated code: output=0bc20da8700ab4dd input=561bdd6537639d40]*/
{
// Look up the interpreter.
int
reqready
=
0
;
PyInterpreterState
*
interp
=
\
resolve_interp
(
id
,
restricted
,
reqready
,
"destroy"
);
if
(
interp
==
NULL
) {
return
NULL
;
}
// Ensure we don't try to destroy the current interpreter.
PyInterpreterState
*
current
=
_get_current_interp
();
if
(
current
==
NULL
) {
return
NULL
;
}
if
(
interp
==
current
) {
PyErr_SetString
(
PyExc_InterpreterError
,
"cannot destroy the current interpreter"
);
return
NULL
;
}
// Ensure the interpreter isn't running.
/* XXX We *could* support destroying a running interpreter but
aren't going to worry about it for now. */
if
(
is_running_main
(
interp
)) {
PyErr_Format
(
PyExc_InterpreterError
,
"interpreter running"
);
return
NULL
;
}
// Destroy the interpreter.
_PyXI_EndInterpreter
(
interp
,
NULL
,
NULL
);
Py_RETURN_NONE
;
}
/*[clinic input]
_interpreters.list_all
*
require_ready as reqready: bool = False
Return a list containing the ID of every existing interpreter.
[clinic start generated code]*/
static
PyObject
*
_interpreters_list_all_impl
(
PyObject
*
module
,
int
reqready
)
/*[clinic end generated code: output=3f21c1a7c78043c0 input=35bae91c381a2cf9]*/
{
PyObject
*
ids
=
PyList_New
(
0
);
if
(
ids
==
NULL
) {
return
NULL
;
}
PyInterpreterState
*
interp
=
PyInterpreterState_Head
();
while
(
interp
!=
NULL
) {
if
(!
reqready
||
_PyInterpreterState_IsReady
(
interp
)) {
PyObject
*
item
=
get_summary
(
interp
);
if
(
item
==
NULL
) {
Py_DECREF
(
ids
);
return
NULL
;
}
// insert at front of list
int
res
=
PyList_Insert
(
ids
,
0
,
item
);
Py_DECREF
(
item
);
if
(
res
<
0
) {
Py_DECREF
(
ids
);
return
NULL
;
}
}
interp
=
PyInterpreterState_Next
(
interp
);
}
return
ids
;
}
/*[clinic input]
_interpreters.get_current
Return (ID, whence) of the current interpreter.
[clinic start generated code]*/
static
PyObject
*
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page