@@ -298,7 +298,7 @@ def emit(s, depth=depth):
298298 # rudimentary attribute handling
299299 type = str (field .type )
300300 assert type in asdl .builtin_types , type
301- emit ("%s %s;" % (type , field .name ), depth + 1 );
301+ emit ("%s %s;" % (type , field .name ), depth + 1 )
302302 emit ("};" )
303303 emit ("" )
304304
@@ -332,7 +332,7 @@ def visitProduct(self, product, name, depth):
332332 # rudimentary attribute handling
333333 type = str (field .type )
334334 assert type in asdl .builtin_types , type
335- self .emit ("%s %s;" % (type , field .name ), depth + 1 );
335+ self .emit ("%s %s;" % (type , field .name ), depth + 1 )
336336 self .emit ("};" , depth )
337337 self .emit ("" , depth )
338338
@@ -434,7 +434,7 @@ def emit(s, depth=0, reflow=True):
434434 emit ('return NULL;' , 2 )
435435 emit ('}' , 1 )
436436
437- emit ("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype , 1 );
437+ emit ("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype , 1 )
438438 emit ("if (!p)" , 1 )
439439 emit ("return NULL;" , 2 )
440440 if union :
@@ -1734,6 +1734,17 @@ def visitModule(self, mod):
17341734 state->__module__,
17351735 state->ast,
17361736 state->__doc__, doc);
1737+ if (result) {
1738+ PyObject *empty_dict = PyDict_New();
1739+ if (!empty_dict ||
1740+ PyObject_SetAttr(result, state->_field_types, empty_dict) < 0) {
1741+ Py_XDECREF(empty_dict);
1742+ Py_DECREF(result);
1743+ Py_DECREF(fnames);
1744+ return NULL;
1745+ }
1746+ Py_DECREF(empty_dict);
1747+ }
17371748 Py_DECREF(fnames);
17381749 return result;
17391750}
@@ -1860,16 +1871,20 @@ def visitModule(self, mod):
18601871
18611872static int add_ast_fields(struct ast_state *state)
18621873{
1863- PyObject *empty_tuple;
1874+ PyObject *empty_tuple, *empty_dict ;
18641875 empty_tuple = PyTuple_New(0);
1865- if (!empty_tuple ||
1876+ empty_dict = PyDict_New();
1877+ if (!empty_tuple || !empty_dict ||
18661878 PyObject_SetAttrString(state->AST_type, "_fields", empty_tuple) < 0 ||
1879+ PyObject_SetAttrString(state->AST_type, "_field_types", empty_dict) < 0 ||
18671880 PyObject_SetAttrString(state->AST_type, "__match_args__", empty_tuple) < 0 ||
18681881 PyObject_SetAttrString(state->AST_type, "_attributes", empty_tuple) < 0) {
18691882 Py_XDECREF(empty_tuple);
1883+ Py_XDECREF(empty_dict);
18701884 return -1;
18711885 }
18721886 Py_DECREF(empty_tuple);
1887+ Py_DECREF(empty_dict);
18731888 return 0;
18741889}
18751890
@@ -2089,13 +2104,13 @@ def simpleSum(self, sum, name):
20892104 self .emit ("case %s:" % t .name , 2 )
20902105 self .emit ("return Py_NewRef(state->%s_singleton);" % t .name , 3 )
20912106 self .emit ("}" , 1 )
2092- self .emit ("Py_UNREACHABLE();" , 1 );
2107+ self .emit ("Py_UNREACHABLE();" , 1 )
20932108 self .emit ("}" , 0 )
20942109
20952110 def visitProduct (self , prod , name ):
20962111 self .func_begin (name )
20972112 self .emit ("tp = (PyTypeObject *)state->%s_type;" % name , 1 )
2098- self .emit ("result = PyType_GenericNew(tp, NULL, NULL);" , 1 );
2113+ self .emit ("result = PyType_GenericNew(tp, NULL, NULL);" , 1 )
20992114 self .emit ("if (!result) return NULL;" , 1 )
21002115 for field in prod .fields :
21012116 self .visitField (field , name , 1 , True )
@@ -2110,7 +2125,7 @@ def visitProduct(self, prod, name):
21102125 def visitConstructor (self , cons , enum , name ):
21112126 self .emit ("case %s_kind:" % cons .name , 1 )
21122127 self .emit ("tp = (PyTypeObject *)state->%s_type;" % cons .name , 2 )
2113- self .emit ("result = PyType_GenericNew(tp, NULL, NULL);" , 2 );
2128+ self .emit ("result = PyType_GenericNew(tp, NULL, NULL);" , 2 )
21142129 self .emit ("if (!result) goto failed;" , 2 )
21152130 for f in cons .fields :
21162131 self .visitField (f , cons .name , 2 , False )
@@ -2272,6 +2287,7 @@ def generate_module_def(mod, metadata, f, internal_h):
22722287 state_strings = {
22732288 "ast" ,
22742289 "_fields" ,
2290+ "_field_types" ,
22752291 "__match_args__" ,
22762292 "__doc__" ,
22772293 "__dict__" ,
0 commit comments