Skip to content

Commit 094bf2c

Browse files
committed
Fix geninterop script and regenerate interop39.cs
- Only record structs when they are defined, not when they are declared - If a struct was only declared when a typedef was created, it won't contain its member declarations. Those have to be drawn from the recorded structs instead. - Rename internal members of AstParser to make it easier to debug
1 parent fd4f9bc commit 094bf2c

File tree

2 files changed

+98
-42
lines changed

2 files changed

+98
-42
lines changed

src/runtime/interop39.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,57 @@ namespace Python.Runtime
1818
internal static partial class TypeOffset
1919
{
2020
// Auto-generated from PyHeapTypeObject in Python.h
21+
public static int ob_refcnt = 0;
22+
public static int ob_type = 0;
23+
public static int ob_size = 0;
24+
public static int tp_name = 0;
25+
public static int tp_basicsize = 0;
26+
public static int tp_itemsize = 0;
27+
public static int tp_dealloc = 0;
28+
public static int tp_vectorcall_offset = 0;
29+
public static int tp_getattr = 0;
30+
public static int tp_setattr = 0;
31+
public static int tp_as_async = 0;
32+
public static int tp_repr = 0;
33+
public static int tp_as_number = 0;
34+
public static int tp_as_sequence = 0;
35+
public static int tp_as_mapping = 0;
36+
public static int tp_hash = 0;
37+
public static int tp_call = 0;
38+
public static int tp_str = 0;
39+
public static int tp_getattro = 0;
40+
public static int tp_setattro = 0;
41+
public static int tp_as_buffer = 0;
42+
public static int tp_flags = 0;
43+
public static int tp_doc = 0;
44+
public static int tp_traverse = 0;
45+
public static int tp_clear = 0;
46+
public static int tp_richcompare = 0;
47+
public static int tp_weaklistoffset = 0;
48+
public static int tp_iter = 0;
49+
public static int tp_iternext = 0;
50+
public static int tp_methods = 0;
51+
public static int tp_members = 0;
52+
public static int tp_getset = 0;
53+
public static int tp_base = 0;
54+
public static int tp_dict = 0;
55+
public static int tp_descr_get = 0;
56+
public static int tp_descr_set = 0;
57+
public static int tp_dictoffset = 0;
58+
public static int tp_init = 0;
59+
public static int tp_alloc = 0;
60+
public static int tp_new = 0;
61+
public static int tp_free = 0;
62+
public static int tp_is_gc = 0;
63+
public static int tp_bases = 0;
64+
public static int tp_mro = 0;
65+
public static int tp_cache = 0;
66+
public static int tp_subclasses = 0;
67+
public static int tp_weaklist = 0;
68+
public static int tp_del = 0;
69+
public static int tp_version_tag = 0;
70+
public static int tp_finalize = 0;
71+
public static int tp_vectorcall = 0;
2172
public static int am_await = 0;
2273
public static int am_aiter = 0;
2374
public static int am_anext = 0;
@@ -174,4 +225,3 @@ internal static partial class SlotTypes
174225

175226
}
176227
#endif
177-

tools/geninterop/geninterop.py

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ class AstParser(object):
5353
"""Walk an AST and determine the members of all structs"""
5454

5555
def __init__(self):
56-
self.__typedefs = {}
57-
self.__typedecls = {}
58-
self.__structs = {}
59-
self.__struct_stack = []
60-
self.__struct_members_stack = []
61-
self.__ptr_decl_depth = 0
62-
self.__struct_members = {}
63-
self.__decl_names = {}
56+
self._typedefs = {}
57+
self._typedecls = {}
58+
self._structs = {}
59+
self._struct_stack = []
60+
self._struct_members_stack = []
61+
self._ptr_decl_depth = 0
62+
self._struct_members = {}
63+
self._decl_names = {}
6464

6565
def get_struct_members(self, name):
6666
"""return a list of (name, type) of struct members"""
67-
defs = self.__typedefs.get(name)
67+
defs = self._typedefs.get(name)
6868
if defs is None:
6969
return None
70-
node = self.__get_leaf_node(defs)
70+
node = self._get_leaf_node(defs)
7171
name = node.name
7272
if name is None:
7373
name = defs.declname
74-
return self.__struct_members.get(name)
74+
return self._struct_members.get(name)
7575

7676
def visit(self, node):
7777
if isinstance(node, c_ast.FileAST):
@@ -96,27 +96,27 @@ def visit_ast(self, ast):
9696
self.visit(node)
9797

9898
def visit_typedef(self, typedef):
99-
self.__typedefs[typedef.name] = typedef.type
99+
self._typedefs[typedef.name] = typedef.type
100100
self.visit(typedef.type)
101101

102102
def visit_typedecl(self, typedecl):
103-
self.__decl_names[typedecl.type] = typedecl.declname
103+
self._decl_names[typedecl.type] = typedecl.declname
104104
self.visit(typedecl.type)
105105

106106
def visit_struct(self, struct):
107-
self.__structs[self.__get_struct_name(struct)] = struct
108107
if struct.decls:
108+
self._structs[self._get_struct_name(struct)] = struct
109109
# recurse into the struct
110-
self.__struct_stack.insert(0, struct)
110+
self._struct_stack.insert(0, struct)
111111
for decl in struct.decls:
112-
self.__struct_members_stack.insert(0, decl.name)
112+
self._struct_members_stack.insert(0, decl.name)
113113
self.visit(decl)
114-
self.__struct_members_stack.pop(0)
115-
self.__struct_stack.pop(0)
116-
elif self.__ptr_decl_depth:
114+
self._struct_members_stack.pop(0)
115+
self._struct_stack.pop(0)
116+
elif self._ptr_decl_depth:
117117
# the struct is empty, but add it as a member to the current
118118
# struct as the current member maybe a pointer to it.
119-
self.__add_struct_member(struct.name)
119+
self._add_struct_member(struct.name)
120120

121121
def visit_decl(self, decl):
122122
self.visit(decl.type)
@@ -125,51 +125,57 @@ def visit_funcdecl(self, funcdecl):
125125
self.visit(funcdecl.type)
126126

127127
def visit_ptrdecl(self, ptrdecl):
128-
self.__ptr_decl_depth += 1
128+
self._ptr_decl_depth += 1
129129
self.visit(ptrdecl.type)
130-
self.__ptr_decl_depth -= 1
130+
self._ptr_decl_depth -= 1
131131

132132
def visit_identifier(self, identifier):
133133
type_name = " ".join(identifier.names)
134-
self.__add_struct_member(type_name)
134+
self._add_struct_member(type_name)
135135

136-
def __add_struct_member(self, type_name):
137-
if not (self.__struct_stack and self.__struct_members_stack):
136+
def _add_struct_member(self, type_name):
137+
if not (self._struct_stack and self._struct_members_stack):
138138
return
139139

140140
# add member to current struct
141-
current_struct = self.__struct_stack[0]
142-
member_name = self.__struct_members_stack[0]
143-
struct_members = self.__struct_members.setdefault(
144-
self.__get_struct_name(current_struct), [])
141+
current_struct = self._struct_stack[0]
142+
member_name = self._struct_members_stack[0]
143+
struct_members = self._struct_members.setdefault(
144+
self._get_struct_name(current_struct), [])
145145

146146
# get the node associated with this type
147147
node = None
148-
if type_name in self.__typedefs:
149-
node = self.__get_leaf_node(self.__typedefs[type_name])
150-
elif type_name in self.__structs:
151-
node = self.__structs[type_name]
148+
if type_name in self._typedefs:
149+
node = self._get_leaf_node(self._typedefs[type_name])
150+
# If the struct was only declared when the typedef was created, its member
151+
# information will not have been recorded and we have to look it up in the
152+
# structs
153+
if isinstance(node, c_ast.Struct) and node.decls is None:
154+
if node.name in self._structs:
155+
node = self._structs[node.name]
156+
elif type_name in self._structs:
157+
node = self._structs[type_name]
152158

153159
# If it's a struct (and not a pointer to a struct) expand
154160
# it into the current struct definition
155-
if not self.__ptr_decl_depth and isinstance(node, c_ast.Struct):
161+
if not self._ptr_decl_depth and isinstance(node, c_ast.Struct):
156162
for decl in node.decls or []:
157-
self.__struct_members_stack.insert(0, decl.name)
163+
self._struct_members_stack.insert(0, decl.name)
158164
self.visit(decl)
159-
self.__struct_members_stack.pop(0)
165+
self._struct_members_stack.pop(0)
160166
else:
161167
# otherwise add it as a single member
162168
struct_members.append((member_name, type_name))
163169

164-
def __get_leaf_node(self, node):
170+
def _get_leaf_node(self, node):
165171
if isinstance(node, c_ast.Typedef):
166-
return self.__get_leaf_node(node.type)
172+
return self._get_leaf_node(node.type)
167173
if isinstance(node, c_ast.TypeDecl):
168-
return self.__get_leaf_node(node.type)
174+
return self._get_leaf_node(node.type)
169175
return node
170176

171-
def __get_struct_name(self, node):
172-
return node.name or self.__decl_names.get(node) or "_struct_%d" % id(node)
177+
def _get_struct_name(self, node):
178+
return node.name or self._decl_names.get(node) or "_struct_%d" % id(node)
173179

174180

175181
class Writer(object):

0 commit comments

Comments
 (0)