Debian Codegraph @

rpython/tool/cparser/test/test_cts.py

raw symbols SCIP
1 import pytest
2 from rpython.flowspace.model import const
3 from rpython.flowspace.objspace import build_flow
4 from rpython.translator.simplify import simplify_graph
5 from rpython.rtyper.lltypesystem import rffi, lltype
6 from rpython.tool.cparser import parse_source, CTypeSpace
7
8 def test_configure():
9 decl = """
10 typedef ssize_t Py_ssize_t;
11
12 typedef struct {
13 Py_ssize_t ob_refcnt;
14 Py_ssize_t ob_pypy_link;
15 double ob_fval;
16 } TestFloatObject;
17 """
18 cts = parse_source(decl)
19 TestFloatObject = cts.definitions['TestFloatObject']
20 assert isinstance(TestFloatObject, lltype.Struct)
21 assert TestFloatObject.c_ob_refcnt == rffi.SSIZE_T
22 assert TestFloatObject.c_ob_pypy_link == rffi.SSIZE_T
23 assert TestFloatObject.c_ob_fval == rffi.DOUBLE
24
25 def test_simple():
26 decl = "typedef ssize_t Py_ssize_t;"
27 cts = parse_source(decl)
28 assert cts.definitions == {'Py_ssize_t': rffi.SSIZE_T}
29
30 def test_win64():
31 decl = """
32 #ifdef _WIN64
33 typedef long long Py_ssize_t;
34 #else
35 typedef long Py_ssize_t;
36 #endif
37 """
38 cts = parse_source(decl)
39 assert cts.definitions == {'Py_ssize_t': rffi.SIGNED}
40
41 def test_macro():
42 decl = """
43 typedef ssize_t Py_ssize_t;
44
45 #define PyObject_HEAD \
46 Py_ssize_t ob_refcnt; \
47 Py_ssize_t ob_pypy_link; \
48
49 typedef struct {
50 PyObject_HEAD
51 double ob_fval;
52 } PyFloatObject;
53 """
54 cts = parse_source(decl)
55 assert 'PyFloatObject' in cts.definitions
56 assert 'PyObject_HEAD' in cts.macros
57
58 def test_include():
59 cdef1 = """
60 typedef ssize_t Py_ssize_t;
61
62 #define PyObject_HEAD \
63 Py_ssize_t ob_refcnt; \
64 Py_ssize_t ob_pypy_link; \
65
66 typedef struct {
67 char *name;
68 } Type;
69 """
70 cdef2 = """
71 typedef struct {
72 PyObject_HEAD
73 Py_ssize_t ob_foo;
74 Type *type;
75 } Object;
76 """
77 cts1 = parse_source(cdef1)
78 Type = cts1.definitions['Type']
79 assert isinstance(Type, lltype.Struct)
80 cts2 = parse_source(cdef2, includes=[cts1])
81 assert 'Type' not in cts2.definitions
82 Object = cts2.definitions['Object']
83 assert Object.c_type.TO is Type
84
85 def test_multiple_sources():
86 cdef1 = """
87 typedef ssize_t Py_ssize_t;
88
89 #define PyObject_HEAD \
90 Py_ssize_t ob_refcnt; \
91 Py_ssize_t ob_pypy_link; \
92
93 typedef struct {
94 char *name;
95 } Type;
96 """
97 cdef2 = """
98 typedef struct {
99 PyObject_HEAD
100 Py_ssize_t ob_foo;
101 Type *type;
102 } Object;
103 """
104 cts = CTypeSpace()
105 cts.parse_source(cdef1)
106 Type = cts.definitions['Type']
107 assert isinstance(Type, lltype.Struct)
108 assert 'Object' not in cts.definitions
109 cts.parse_source(cdef2)
110 Object = cts.definitions['Object']
111 assert Object.c_type.TO is Type
112
113 def test_incomplete():
114 cdef = """
115 typedef ssize_t Py_ssize_t;
116
117 typedef struct {
118 Py_ssize_t ob_refcnt;
119 Py_ssize_t ob_pypy_link;
120 struct _typeobject *ob_type;
121 } Object;
122
123 typedef struct {
124 void *buf;
125 Object *obj;
126 } Buffer;
127
128 """
129 cts = CTypeSpace()
130 cts.parse_source(cdef, configure=False)
131 with pytest.raises(ValueError):
132 cts.configure_types()
133
134 def test_incomplete_struct():
135 cdef = """
136 typedef struct s *ptr;
137 struct s {void* x;};
138 """
139 cts = parse_source(cdef)
140 PTR = cts.gettype("ptr")
141 assert isinstance(PTR.TO, lltype.Struct)
142 hash(PTR)
143
144 def test_recursive():
145 cdef = """
146 typedef ssize_t Py_ssize_t;
147
148 typedef struct {
149 Py_ssize_t ob_refcnt;
150 Py_ssize_t ob_pypy_link;
151 struct _typeobject *ob_type;
152 } Object;
153
154 typedef struct {
155 void *buf;
156 Object *obj;
157 } Buffer;
158
159 typedef struct _typeobject {
160 Object *obj;
161 } Type;
162 """
163 cts = parse_source(cdef)
164 Object = cts.definitions['Object']
165 assert isinstance(Object, lltype.Struct)
166 hash(Object)
167
168 def test_nested_struct():
169 cdef = """
170 typedef struct {
171 int x;
172 } foo;
173 typedef struct {
174 foo y;
175 } bar;
176 """
177 cts = parse_source(cdef)
178 bar = cts.gettype('bar')
179 assert isinstance(bar, lltype.Struct)
180 hash(bar) # bar is hashable
181
182 def test_nested_struct_2():
183 cdef = """
184 typedef struct _object {
185 int ob_refcnt;
186 struct _typeobject *ob_type;
187 } PyObject;
188
189 typedef struct _varobject {
190 PyObject ob_base;
191 int ob_size;
192 } PyVarObject;
193
194 typedef struct _typeobject {
195 PyVarObject ob_base;
196 } PyTypeObject;
197 """
198 cts = parse_source(cdef)
199 bar = cts.gettype('PyTypeObject')
200 assert isinstance(bar, lltype.Struct)
201 hash(bar) # bar is hashable
202
203 def test_named_struct():
204 cdef = """
205 struct foo {
206 int x;
207 };
208 """
209 cts = parse_source(cdef)
210 foo = cts.gettype('struct foo')
211 assert isinstance(foo, lltype.Struct)
212 hash(foo)
213
214 def test_const():
215 cdef = """
216 typedef struct {
217 const char * const foo;
218 } bar;
219 """
220 cts = parse_source(cdef)
221 assert cts.definitions['bar'].c_foo == rffi.CONST_CCHARP != rffi.CCHARP
222
223 def test_enum():
224 cdef = """
225 typedef enum {
226 mp_ass_subscript = 3,
227 mp_length = 4,
228 mp_subscript = 5,
229 } Slot;
230 """
231 cts = parse_source(cdef)
232 assert cts.gettype('Slot').mp_length == 4
233
234 def test_translate_enum():
235 cdef = """
236 typedef enum {
237 mp_ass_subscript = 3,
238 mp_length = 4,
239 mp_subscript = 5,
240 } Slot;
241 """
242 cts = parse_source(cdef)
243 def f():
244 return cts.gettype('Slot').mp_length
245 graph = build_flow(f)
246 simplify_graph(graph)
247 # Check that the result is constant-folded
248 assert graph.startblock.operations == []
249 [link] = graph.startblock.exits
250 assert link.target is graph.returnblock
251 assert link.args[0] == const(4)
252
253 def test_gettype():
254 decl = """
255 typedef ssize_t Py_ssize_t;
256
257 #define PyObject_HEAD \
258 Py_ssize_t ob_refcnt; \
259 Py_ssize_t ob_pypy_link; \
260
261 typedef struct {
262 PyObject_HEAD
263 double ob_fval;
264 } TestFloatObject;
265 """
266 cts = parse_source(decl)
267 assert cts.gettype('Py_ssize_t') == rffi.SSIZE_T
268 assert cts.gettype('TestFloatObject *').TO.c_ob_refcnt == rffi.SSIZE_T
269 assert cts.cast('Py_ssize_t', 42) == rffi.cast(rffi.SSIZE_T, 42)
270
271 def test_parse_funcdecl():
272 decl = """
273 typedef ssize_t Py_ssize_t;
274
275 #define PyObject_HEAD \
276 Py_ssize_t ob_refcnt; \
277 Py_ssize_t ob_pypy_link; \
278
279 typedef struct {
280 PyObject_HEAD
281 double ob_fval;
282 } TestFloatObject;
283
284 typedef TestFloatObject* (*func_t)(int, int);
285 """
286 cts = parse_source(decl)
287 func_decl = cts.parse_func("func_t * some_func(TestFloatObject*)")
288 assert func_decl.name == 'some_func'
289 assert func_decl.get_llresult(cts) == cts.gettype('func_t*')
290 assert func_decl.get_llargs(cts) == [cts.gettype('TestFloatObject *')]
291
292 def test_struct_in_func_args():
293 decl = """
294 typedef struct {int x;} obj;
295 typedef int (*func)(obj x);
296 """
297 cts = parse_source(decl)
298 OBJ = cts.gettype('obj')
299 FUNCPTR = cts.gettype('func')
300 assert FUNCPTR.TO.ARGS == (OBJ,)
301
302 def test_wchar_t():
303 cdef = """
304 typedef struct { wchar_t* x; } test;
305 """
306 cts = parse_source(cdef, headers=['stddef.h'])
307 obj = lltype.malloc(cts.gettype('test'), flavor='raw')
308 obj.c_x = cts.cast('wchar_t*', 0)
309 obj.c_x = lltype.nullptr(rffi.CWCHARP.TO)
310 lltype.free(obj, flavor='raw')
311
312 def test_translate_cast():
313 cdef = "typedef ssize_t Py_ssize_t;"
314 cts = parse_source(cdef)
315
316 def f():
317 return cts.cast('Py_ssize_t*', 0)
318 graph = build_flow(f)
319 simplify_graph(graph)
320 assert len(graph.startblock.operations) == 1
321 op = graph.startblock.operations[0]
322 assert op.args[0] == const(rffi.cast)
323 assert op.args[1].value is cts.gettype('Py_ssize_t*')
324
325 def test_translate_gettype():
326 cdef = "typedef ssize_t Py_ssize_t;"
327 cts = parse_source(cdef)
328
329 def f():
330 return cts.gettype('Py_ssize_t*')
331 graph = build_flow(f)
332 simplify_graph(graph)
333 # Check that the result is constant-folded
334 assert graph.startblock.operations == []
335 [link] = graph.startblock.exits
336 assert link.target is graph.returnblock
337 assert link.args[0] == const(rffi.CArrayPtr(rffi.SSIZE_T))