@@ -692,6 +692,9 @@ def visitModule(self, mod):
692
692
int res = -1;
693
693
PyObject *key, *value, *fields;
694
694
astmodulestate *state = get_global_ast_state();
695
+ if (state == NULL) {
696
+ goto cleanup;
697
+ }
695
698
if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
696
699
goto cleanup;
697
700
}
@@ -761,6 +764,10 @@ def visitModule(self, mod):
761
764
ast_type_reduce(PyObject *self, PyObject *unused)
762
765
{
763
766
astmodulestate *state = get_global_ast_state();
767
+ if (state == NULL) {
768
+ return NULL;
769
+ }
770
+
764
771
PyObject *dict;
765
772
if (_PyObject_LookupAttr(self, state->__dict__, &dict) < 0) {
766
773
return NULL;
@@ -969,9 +976,8 @@ def visitModule(self, mod):
969
976
970
977
""" , 0 , reflow = False )
971
978
972
- self .emit ("static int init_types(void )" ,0 )
979
+ self .emit ("static int init_types(astmodulestate *state )" ,0 )
973
980
self .emit ("{" , 0 )
974
- self .emit ("astmodulestate *state = get_global_ast_state();" , 1 )
975
981
self .emit ("if (state->initialized) return 1;" , 1 )
976
982
self .emit ("if (init_identifiers(state) < 0) return 0;" , 1 )
977
983
self .emit ("state->AST_type = PyType_FromSpec(&AST_type_spec);" , 1 )
@@ -1046,40 +1052,55 @@ def emit_defaults(self, name, fields, depth):
1046
1052
class ASTModuleVisitor (PickleVisitor ):
1047
1053
1048
1054
def visitModule (self , mod ):
1049
- self .emit ("PyMODINIT_FUNC " , 0 )
1050
- self .emit ("PyInit__ast(void )" , 0 )
1055
+ self .emit ("static int " , 0 )
1056
+ self .emit ("astmodule_exec(PyObject *m )" , 0 )
1051
1057
self .emit ("{" , 0 )
1052
- self .emit ("PyObject *m = PyModule_Create(&_astmodule);" , 1 )
1053
- self .emit ("if (!m) {" , 1 )
1054
- self .emit ("return NULL;" , 2 )
1055
- self .emit ("}" , 1 )
1056
1058
self .emit ('astmodulestate *state = get_ast_state(m);' , 1 )
1057
- self .emit ('' , 1 )
1059
+ self .emit ("" , 0 )
1058
1060
1059
- self .emit ("if (!init_types()) {" , 1 )
1060
- self .emit ("goto error ;" , 2 )
1061
+ self .emit ("if (!init_types(state )) {" , 1 )
1062
+ self .emit ("return -1 ;" , 2 )
1061
1063
self .emit ("}" , 1 )
1062
1064
self .emit ('if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {' , 1 )
1063
- self .emit ('goto error ;' , 2 )
1065
+ self .emit ('return -1 ;' , 2 )
1064
1066
self .emit ('}' , 1 )
1065
1067
self .emit ('Py_INCREF(state->AST_type);' , 1 )
1066
1068
self .emit ('if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) {' , 1 )
1067
- self .emit ("goto error ;" , 2 )
1069
+ self .emit ("return -1 ;" , 2 )
1068
1070
self .emit ('}' , 1 )
1069
1071
self .emit ('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) {' , 1 )
1070
- self .emit ("goto error ;" , 2 )
1072
+ self .emit ("return -1 ;" , 2 )
1071
1073
self .emit ('}' , 1 )
1072
1074
self .emit ('if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) {' , 1 )
1073
- self .emit ("goto error ;" , 2 )
1075
+ self .emit ("return -1 ;" , 2 )
1074
1076
self .emit ('}' , 1 )
1075
1077
for dfn in mod .dfns :
1076
1078
self .visit (dfn )
1077
- self .emit ("return m;" , 1 )
1078
- self .emit ("" , 0 )
1079
- self .emit ("error:" , 0 )
1080
- self .emit ("Py_DECREF(m);" , 1 )
1081
- self .emit ("return NULL;" , 1 )
1079
+ self .emit ("return 0;" , 1 )
1082
1080
self .emit ("}" , 0 )
1081
+ self .emit ("" , 0 )
1082
+ self .emit ("""
1083
+ static PyModuleDef_Slot astmodule_slots[] = {
1084
+ {Py_mod_exec, astmodule_exec},
1085
+ {0, NULL}
1086
+ };
1087
+
1088
+ static struct PyModuleDef _astmodule = {
1089
+ PyModuleDef_HEAD_INIT,
1090
+ .m_name = "_ast",
1091
+ .m_size = sizeof(astmodulestate),
1092
+ .m_slots = astmodule_slots,
1093
+ .m_traverse = astmodule_traverse,
1094
+ .m_clear = astmodule_clear,
1095
+ .m_free = astmodule_free,
1096
+ };
1097
+
1098
+ PyMODINIT_FUNC
1099
+ PyInit__ast(void)
1100
+ {
1101
+ return PyModuleDef_Init(&_astmodule);
1102
+ }
1103
+ """ .strip (), 0 , reflow = False )
1083
1104
1084
1105
def visitProduct (self , prod , name ):
1085
1106
self .addObj (name )
@@ -1095,7 +1116,7 @@ def visitConstructor(self, cons, name):
1095
1116
def addObj (self , name ):
1096
1117
self .emit ("if (PyModule_AddObject(m, \" %s\" , "
1097
1118
"state->%s_type) < 0) {" % (name , name ), 1 )
1098
- self .emit ("goto error ;" , 2 )
1119
+ self .emit ("return -1 ;" , 2 )
1099
1120
self .emit ('}' , 1 )
1100
1121
self .emit ("Py_INCREF(state->%s_type);" % name , 1 )
1101
1122
@@ -1255,11 +1276,10 @@ class PartingShots(StaticVisitor):
1255
1276
CODE = """
1256
1277
PyObject* PyAST_mod2obj(mod_ty t)
1257
1278
{
1258
- if (!init_types()) {
1279
+ astmodulestate *state = get_global_ast_state();
1280
+ if (state == NULL) {
1259
1281
return NULL;
1260
1282
}
1261
-
1262
- astmodulestate *state = get_global_ast_state();
1263
1283
return ast2obj_mod(state, t);
1264
1284
}
1265
1285
@@ -1281,10 +1301,6 @@ class PartingShots(StaticVisitor):
1281
1301
1282
1302
assert(0 <= mode && mode <= 2);
1283
1303
1284
- if (!init_types()) {
1285
- return NULL;
1286
- }
1287
-
1288
1304
isinstance = PyObject_IsInstance(ast, req_type[mode]);
1289
1305
if (isinstance == -1)
1290
1306
return NULL;
@@ -1303,11 +1319,10 @@ class PartingShots(StaticVisitor):
1303
1319
1304
1320
int PyAST_Check(PyObject* obj)
1305
1321
{
1306
- if (!init_types()) {
1322
+ astmodulestate *state = get_global_ast_state();
1323
+ if (state == NULL) {
1307
1324
return -1;
1308
1325
}
1309
-
1310
- astmodulestate *state = get_global_ast_state();
1311
1326
return PyObject_IsInstance(obj, state->AST_type);
1312
1327
}
1313
1328
"""
@@ -1358,12 +1373,35 @@ def generate_module_def(f, mod):
1358
1373
f .write (' PyObject *' + s + ';\n ' )
1359
1374
f .write ('} astmodulestate;\n \n ' )
1360
1375
f .write ("""
1361
- static astmodulestate global_ast_state;
1376
+ static astmodulestate*
1377
+ get_ast_state(PyObject *module)
1378
+ {
1379
+ void *state = PyModule_GetState(module);
1380
+ assert(state != NULL);
1381
+ return (astmodulestate*)state;
1382
+ }
1362
1383
1363
- static astmodulestate *
1364
- get_ast_state(PyObject *Py_UNUSED(module) )
1384
+ static astmodulestate*
1385
+ get_global_ast_state(void )
1365
1386
{
1366
- return &global_ast_state;
1387
+ _Py_IDENTIFIER(_ast);
1388
+ PyObject *name = _PyUnicode_FromId(&PyId__ast); // borrowed reference
1389
+ if (name == NULL) {
1390
+ return NULL;
1391
+ }
1392
+ PyObject *module = PyImport_GetModule(name);
1393
+ if (module == NULL) {
1394
+ if (PyErr_Occurred()) {
1395
+ return NULL;
1396
+ }
1397
+ module = PyImport_Import(name);
1398
+ if (module == NULL) {
1399
+ return NULL;
1400
+ }
1401
+ }
1402
+ astmodulestate *state = get_ast_state(module);
1403
+ Py_DECREF(module);
1404
+ return state;
1367
1405
}
1368
1406
1369
1407
static int astmodule_clear(PyObject *module)
@@ -1390,17 +1428,6 @@ def generate_module_def(f, mod):
1390
1428
astmodule_clear((PyObject*)module);
1391
1429
}
1392
1430
1393
- static struct PyModuleDef _astmodule = {
1394
- PyModuleDef_HEAD_INIT,
1395
- .m_name = "_ast",
1396
- .m_size = -1,
1397
- .m_traverse = astmodule_traverse,
1398
- .m_clear = astmodule_clear,
1399
- .m_free = astmodule_free,
1400
- };
1401
-
1402
- #define get_global_ast_state() (&global_ast_state)
1403
-
1404
1431
""" )
1405
1432
f .write ('static int init_identifiers(astmodulestate *state)\n ' )
1406
1433
f .write ('{\n ' )
0 commit comments