@@ -499,7 +499,7 @@ get_whence(PyInterpreterState *interp)
499
499
500
500
501
501
static PyInterpreterState *
502
- resolve_interp (PyObject * idobj , int restricted , const char * op )
502
+ resolve_interp (PyObject * idobj , int restricted , int reqready , const char * op )
503
503
{
504
504
PyInterpreterState * interp ;
505
505
if (idobj == NULL ) {
@@ -512,6 +512,18 @@ resolve_interp(PyObject *idobj, int restricted, const char *op)
512
512
}
513
513
}
514
514
515
+ if (reqready && !_PyInterpreterState_IsReady (interp )) {
516
+ if (idobj == NULL ) {
517
+ PyErr_Format (PyExc_InterpreterError ,
518
+ "cannot %s current interpreter (not ready)" , op );
519
+ }
520
+ else {
521
+ PyErr_Format (PyExc_InterpreterError ,
522
+ "cannot %s interpreter %R (not ready)" , op , idobj );
523
+ }
524
+ return NULL ;
525
+ }
526
+
515
527
if (restricted && get_whence (interp ) != _PyInterpreterState_WHENCE_STDLIB ) {
516
528
if (idobj == NULL ) {
517
529
PyErr_Format (PyExc_InterpreterError ,
@@ -619,6 +631,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
619
631
_PyErr_ChainExceptions1 (exc );
620
632
return NULL ;
621
633
}
634
+ assert (_PyInterpreterState_IsReady (interp ));
622
635
623
636
PyObject * idobj = _PyInterpreterState_GetIDObject (interp );
624
637
if (idobj == NULL ) {
@@ -664,7 +677,9 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
664
677
}
665
678
666
679
// Look up the interpreter.
667
- PyInterpreterState * interp = resolve_interp (id , restricted , "destroy" );
680
+ int reqready = 0 ;
681
+ PyInterpreterState * interp = \
682
+ resolve_interp (id , restricted , reqready , "destroy" );
668
683
if (interp == NULL ) {
669
684
return NULL ;
670
685
}
@@ -746,6 +761,7 @@ interp_get_current(PyObject *self, PyObject *Py_UNUSED(ignored))
746
761
if (interp == NULL ) {
747
762
return NULL ;
748
763
}
764
+ assert (_PyInterpreterState_IsReady (interp ));
749
765
return get_summary (interp );
750
766
}
751
767
@@ -759,6 +775,7 @@ static PyObject *
759
775
interp_get_main (PyObject * self , PyObject * Py_UNUSED (ignored ))
760
776
{
761
777
PyInterpreterState * interp = _PyInterpreterState_Main ();
778
+ assert (_PyInterpreterState_IsReady (interp ));
762
779
return get_summary (interp );
763
780
}
764
781
@@ -782,8 +799,9 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
782
799
}
783
800
784
801
// Look up the interpreter.
785
- PyInterpreterState * interp = resolve_interp (id , restricted ,
786
- "update __main__ for" );
802
+ int reqready = 1 ;
803
+ PyInterpreterState * interp = \
804
+ resolve_interp (id , restricted , reqready , "update __main__ for" );
787
805
if (interp == NULL ) {
788
806
return NULL ;
789
807
}
@@ -942,7 +960,9 @@ interp_exec(PyObject *self, PyObject *args, PyObject *kwds)
942
960
return NULL ;
943
961
}
944
962
945
- PyInterpreterState * interp = resolve_interp (id , restricted , "exec code for" );
963
+ int reqready = 1 ;
964
+ PyInterpreterState * interp = \
965
+ resolve_interp (id , restricted , reqready , "exec code for" );
946
966
if (interp == NULL ) {
947
967
return NULL ;
948
968
}
@@ -1004,7 +1024,9 @@ interp_call(PyObject *self, PyObject *args, PyObject *kwds)
1004
1024
return NULL ;
1005
1025
}
1006
1026
1007
- PyInterpreterState * interp = resolve_interp (id , restricted , "make a call in" );
1027
+ int reqready = 1 ;
1028
+ PyInterpreterState * interp = \
1029
+ resolve_interp (id , restricted , reqready , "make a call in" );
1008
1030
if (interp == NULL ) {
1009
1031
return NULL ;
1010
1032
}
@@ -1060,7 +1082,9 @@ interp_run_string(PyObject *self, PyObject *args, PyObject *kwds)
1060
1082
return NULL ;
1061
1083
}
1062
1084
1063
- PyInterpreterState * interp = resolve_interp (id , restricted , "run a string in" );
1085
+ int reqready = 1 ;
1086
+ PyInterpreterState * interp = \
1087
+ resolve_interp (id , restricted , reqready , "run a string in" );
1064
1088
if (interp == NULL ) {
1065
1089
return NULL ;
1066
1090
}
@@ -1102,8 +1126,9 @@ interp_run_func(PyObject *self, PyObject *args, PyObject *kwds)
1102
1126
return NULL ;
1103
1127
}
1104
1128
1105
- PyInterpreterState * interp = resolve_interp (id , restricted ,
1106
- "run a function in" );
1129
+ int reqready = 1 ;
1130
+ PyInterpreterState * interp = \
1131
+ resolve_interp (id , restricted , reqready , "run a function in" );
1107
1132
if (interp == NULL ) {
1108
1133
return NULL ;
1109
1134
}
@@ -1172,8 +1197,9 @@ interp_is_running(PyObject *self, PyObject *args, PyObject *kwds)
1172
1197
return NULL ;
1173
1198
}
1174
1199
1175
- PyInterpreterState * interp = resolve_interp (id , restricted ,
1176
- "check if running for" );
1200
+ int reqready = 1 ;
1201
+ PyInterpreterState * interp = \
1202
+ resolve_interp (id , restricted , reqready , "check if running for" );
1177
1203
if (interp == NULL ) {
1178
1204
return NULL ;
1179
1205
}
@@ -1206,8 +1232,9 @@ interp_get_config(PyObject *self, PyObject *args, PyObject *kwds)
1206
1232
idobj = NULL ;
1207
1233
}
1208
1234
1209
- PyInterpreterState * interp = resolve_interp (idobj , restricted ,
1210
- "get the config of" );
1235
+ int reqready = 0 ;
1236
+ PyInterpreterState * interp = \
1237
+ resolve_interp (idobj , restricted , reqready , "get the config of" );
1211
1238
if (interp == NULL ) {
1212
1239
return NULL ;
1213
1240
}
@@ -1272,7 +1299,9 @@ interp_incref(PyObject *self, PyObject *args, PyObject *kwds)
1272
1299
return NULL ;
1273
1300
}
1274
1301
1275
- PyInterpreterState * interp = resolve_interp (id , restricted , "incref" );
1302
+ int reqready = 1 ;
1303
+ PyInterpreterState * interp = \
1304
+ resolve_interp (id , restricted , reqready , "incref" );
1276
1305
if (interp == NULL ) {
1277
1306
return NULL ;
1278
1307
}
@@ -1299,7 +1328,9 @@ interp_decref(PyObject *self, PyObject *args, PyObject *kwds)
1299
1328
return NULL ;
1300
1329
}
1301
1330
1302
- PyInterpreterState * interp = resolve_interp (id , restricted , "decref" );
1331
+ int reqready = 1 ;
1332
+ PyInterpreterState * interp = \
1333
+ resolve_interp (id , restricted , reqready , "decref" );
1303
1334
if (interp == NULL ) {
1304
1335
return NULL ;
1305
1336
}
0 commit comments