@@ -691,7 +691,7 @@ def visitModule(self, mod):
691
691
Py_ssize_t i, numfields = 0;
692
692
int res = -1;
693
693
PyObject *key, *value, *fields;
694
- astmodulestate *state = astmodulestate_global ;
694
+ astmodulestate *state = get_global_ast_state() ;
695
695
if (_PyObject_LookupAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
696
696
goto cleanup;
697
697
}
@@ -760,7 +760,7 @@ def visitModule(self, mod):
760
760
static PyObject *
761
761
ast_type_reduce(PyObject *self, PyObject *unused)
762
762
{
763
- astmodulestate *state = astmodulestate_global ;
763
+ astmodulestate *state = get_global_ast_state() ;
764
764
PyObject *dict;
765
765
if (_PyObject_LookupAttr(self, state->__dict__, &dict) < 0) {
766
766
return NULL;
@@ -971,19 +971,7 @@ def visitModule(self, mod):
971
971
972
972
self .emit ("static int init_types(void)" ,0 )
973
973
self .emit ("{" , 0 )
974
- self .emit ("PyObject *module = PyState_FindModule(&_astmodule);" , 1 )
975
- self .emit ("if (module == NULL) {" , 1 )
976
- self .emit ("module = PyModule_Create(&_astmodule);" , 2 )
977
- self .emit ("if (!module) {" , 2 )
978
- self .emit ("return 0;" , 3 )
979
- self .emit ("}" , 2 )
980
- self .emit ("if (PyState_AddModule(module, &_astmodule) < 0) {" , 2 )
981
- self .emit ("return 0;" , 3 )
982
- self .emit ("}" , 2 )
983
- self .emit ("}" , 1 )
984
- self .emit ("" , 0 )
985
-
986
- self .emit ("astmodulestate *state = get_ast_state(module);" , 1 )
974
+ self .emit ("astmodulestate *state = get_global_ast_state();" , 1 )
987
975
self .emit ("if (state->initialized) return 1;" , 1 )
988
976
self .emit ("if (init_identifiers(state) < 0) return 0;" , 1 )
989
977
self .emit ("state->AST_type = PyType_FromSpec(&AST_type_spec);" , 1 )
@@ -1061,13 +1049,16 @@ def visitModule(self, mod):
1061
1049
self .emit ("PyMODINIT_FUNC" , 0 )
1062
1050
self .emit ("PyInit__ast(void)" , 0 )
1063
1051
self .emit ("{" , 0 )
1064
- self .emit ("PyObject *m;" , 1 )
1065
- self .emit ("if (!init_types()) return NULL; " , 1 )
1066
- self .emit ('m = PyState_FindModule(&_astmodule);' , 1 )
1067
- self .emit ("if (!m) return NULL; " , 1 )
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 )
1068
1056
self .emit ('astmodulestate *state = get_ast_state(m);' , 1 )
1069
1057
self .emit ('' , 1 )
1070
1058
1059
+ self .emit ("if (!init_types()) {" , 1 )
1060
+ self .emit ("goto error;" , 2 )
1061
+ self .emit ("}" , 1 )
1071
1062
self .emit ('if (PyModule_AddObject(m, "AST", state->AST_type) < 0) {' , 1 )
1072
1063
self .emit ('goto error;' , 2 )
1073
1064
self .emit ('}' , 1 )
@@ -1084,6 +1075,7 @@ def visitModule(self, mod):
1084
1075
for dfn in mod .dfns :
1085
1076
self .visit (dfn )
1086
1077
self .emit ("return m;" , 1 )
1078
+ self .emit ("" , 0 )
1087
1079
self .emit ("error:" , 0 )
1088
1080
self .emit ("Py_DECREF(m);" , 1 )
1089
1081
self .emit ("return NULL;" , 1 )
@@ -1263,9 +1255,11 @@ class PartingShots(StaticVisitor):
1263
1255
CODE = """
1264
1256
PyObject* PyAST_mod2obj(mod_ty t)
1265
1257
{
1266
- if (!init_types())
1258
+ if (!init_types()) {
1267
1259
return NULL;
1268
- astmodulestate *state = astmodulestate_global;
1260
+ }
1261
+
1262
+ astmodulestate *state = get_global_ast_state();
1269
1263
return ast2obj_mod(state, t);
1270
1264
}
1271
1265
@@ -1279,16 +1273,17 @@ class PartingShots(StaticVisitor):
1279
1273
return NULL;
1280
1274
}
1281
1275
1282
- astmodulestate *state = astmodulestate_global ;
1276
+ astmodulestate *state = get_global_ast_state() ;
1283
1277
PyObject *req_type[3];
1284
1278
req_type[0] = state->Module_type;
1285
1279
req_type[1] = state->Expression_type;
1286
1280
req_type[2] = state->Interactive_type;
1287
1281
1288
1282
assert(0 <= mode && mode <= 2);
1289
1283
1290
- if (!init_types())
1284
+ if (!init_types()) {
1291
1285
return NULL;
1286
+ }
1292
1287
1293
1288
isinstance = PyObject_IsInstance(ast, req_type[mode]);
1294
1289
if (isinstance == -1)
@@ -1308,9 +1303,11 @@ class PartingShots(StaticVisitor):
1308
1303
1309
1304
int PyAST_Check(PyObject* obj)
1310
1305
{
1311
- if (!init_types())
1306
+ if (!init_types()) {
1312
1307
return -1;
1313
- astmodulestate *state = astmodulestate_global;
1308
+ }
1309
+
1310
+ astmodulestate *state = get_global_ast_state();
1314
1311
return PyObject_IsInstance(obj, state->AST_type);
1315
1312
}
1316
1313
"""
@@ -1361,13 +1358,12 @@ def generate_module_def(f, mod):
1361
1358
f .write (' PyObject *' + s + ';\n ' )
1362
1359
f .write ('} astmodulestate;\n \n ' )
1363
1360
f .write ("""
1361
+ static astmodulestate global_ast_state;
1362
+
1364
1363
static astmodulestate *
1365
- get_ast_state(PyObject *module)
1364
+ get_ast_state(PyObject *Py_UNUSED( module) )
1366
1365
{
1367
- assert(module != NULL);
1368
- void *state = PyModule_GetState(module);
1369
- assert(state != NULL);
1370
- return (astmodulestate *)state;
1366
+ return &global_ast_state;
1371
1367
}
1372
1368
1373
1369
static int astmodule_clear(PyObject *module)
@@ -1396,17 +1392,14 @@ def generate_module_def(f, mod):
1396
1392
1397
1393
static struct PyModuleDef _astmodule = {
1398
1394
PyModuleDef_HEAD_INIT,
1399
- "_ast",
1400
- NULL,
1401
- sizeof(astmodulestate),
1402
- NULL,
1403
- NULL,
1404
- astmodule_traverse,
1405
- astmodule_clear,
1406
- astmodule_free,
1395
+ .m_name = "_ast",
1396
+ .m_size = -1,
1397
+ .m_traverse = astmodule_traverse,
1398
+ .m_clear = astmodule_clear,
1399
+ .m_free = astmodule_free,
1407
1400
};
1408
1401
1409
- #define astmodulestate_global get_ast_state(PyState_FindModule(&_astmodule) )
1402
+ #define get_global_ast_state() (&global_ast_state )
1410
1403
1411
1404
""" )
1412
1405
f .write ('static int init_identifiers(astmodulestate *state)\n ' )
0 commit comments