Debian Codegraph @

extra_tests/cffi_tests/cffi1/test_dlopen.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import pytest
3 from cffi import FFI, VerificationError, CDefError
4 from cffi.recompiler import make_py_source
5 from extra_tests.cffi_tests.udir import udir
6
7
8 def test_simple():
9 ffi = FFI()
10 ffi.cdef("int close(int); static const int BB = 42; extern int somevar;")
11 target = udir.join('test_simple.py')
12 make_py_source(ffi, 'test_simple', str(target))
13 assert target.read() == r"""# auto-generated file
14 import _cffi_backend
15
16 ffi = _cffi_backend.FFI('test_simple',
17 _version = 0x2601,
18 _types = b'\x00\x00\x01\x0D\x00\x00\x07\x01\x00\x00\x00\x0F',
19 _globals = (b'\xFF\xFF\xFF\x1FBB',42,b'\x00\x00\x00\x23close',0,b'\x00\x00\x01\x21somevar',0),
20 )
21 """
22
23 def test_global_constant():
24 ffi = FFI()
25 ffi.cdef("static const long BB; static const float BF = 12;")
26 target = udir.join('test_valid_global_constant.py')
27 make_py_source(ffi, 'test_valid_global_constant', str(target))
28 assert target.read() == r"""# auto-generated file
29 import _cffi_backend
30
31 ffi = _cffi_backend.FFI('test_valid_global_constant',
32 _version = 0x2601,
33 _types = b'\x00\x00\x0D\x01\x00\x00\x09\x01',
34 _globals = (b'\x00\x00\x01\x25BB',0,b'\x00\x00\x00\x25BF',0),
35 )
36 """
37
38 def test_invalid_global_constant_3():
39 ffi = FFI()
40 e = pytest.raises(CDefError, ffi.cdef, "#define BB 12.34")
41 assert str(e.value).startswith(
42 "only supports one of the following syntax:")
43
44 def test_invalid_dotdotdot_in_macro():
45 ffi = FFI()
46 ffi.cdef("#define FOO ...")
47 target = udir.join('test_invalid_dotdotdot_in_macro.py')
48 e = pytest.raises(VerificationError, make_py_source, ffi,
49 'test_invalid_dotdotdot_in_macro', str(target))
50 assert str(e.value) == ("macro FOO: cannot use the syntax '...' in "
51 "'#define FOO ...' when using the ABI mode")
52
53 def test_typename():
54 ffi = FFI()
55 ffi.cdef("typedef int foobar_t;")
56 target = udir.join('test_typename.py')
57 make_py_source(ffi, 'test_typename', str(target))
58 assert target.read() == r"""# auto-generated file
59 import _cffi_backend
60
61 ffi = _cffi_backend.FFI('test_typename',
62 _version = 0x2601,
63 _types = b'\x00\x00\x07\x01',
64 _typenames = (b'\x00\x00\x00\x00foobar_t',),
65 )
66 """
67
68 def test_enum():
69 ffi = FFI()
70 ffi.cdef("enum myenum_e { AA, BB, CC=-42 };")
71 target = udir.join('test_enum.py')
72 make_py_source(ffi, 'test_enum', str(target))
73 assert target.read() == r"""# auto-generated file
74 import _cffi_backend
75
76 ffi = _cffi_backend.FFI('test_enum',
77 _version = 0x2601,
78 _types = b'\x00\x00\x00\x0B',
79 _globals = (b'\xFF\xFF\xFF\x0BAA',0,b'\xFF\xFF\xFF\x0BBB',1,b'\xFF\xFF\xFF\x0BCC',-42),
80 _enums = (b'\x00\x00\x00\x00\x00\x00\x00\x15myenum_e\x00AA,BB,CC',),
81 )
82 """
83
84 def test_struct():
85 ffi = FFI()
86 ffi.cdef("struct foo_s { int a; signed char b[]; }; struct bar_s;")
87 target = udir.join('test_struct.py')
88 make_py_source(ffi, 'test_struct', str(target))
89 assert target.read() == r"""# auto-generated file
90 import _cffi_backend
91
92 ffi = _cffi_backend.FFI('test_struct',
93 _version = 0x2601,
94 _types = b'\x00\x00\x07\x01\x00\x00\x03\x01\x00\x00\x01\x07\x00\x00\x00\x09\x00\x00\x01\x09',
95 _struct_unions = ((b'\x00\x00\x00\x03\x00\x00\x00\x10bar_s',),(b'\x00\x00\x00\x04\x00\x00\x00\x02foo_s',b'\x00\x00\x00\x11a',b'\x00\x00\x02\x11b')),
96 )
97 """
98
99 def test_include():
100 ffi = FFI()
101 ffi.cdef("#define ABC 123")
102 ffi.set_source('test_include', None)
103 target = udir.join('test_include.py')
104 make_py_source(ffi, 'test_include', str(target))
105 assert target.read() == r"""# auto-generated file
106 import _cffi_backend
107
108 ffi = _cffi_backend.FFI('test_include',
109 _version = 0x2601,
110 _types = b'',
111 _globals = (b'\xFF\xFF\xFF\x1FABC',123,),
112 )
113 """
114 #
115 ffi2 = FFI()
116 ffi2.include(ffi)
117 target2 = udir.join('test2_include.py')
118 make_py_source(ffi2, 'test2_include', str(target2))
119 assert target2.read() == r"""# auto-generated file
120 import _cffi_backend
121 from test_include import ffi as _ffi0
122
123 ffi = _cffi_backend.FFI('test2_include',
124 _version = 0x2601,
125 _types = b'',
126 _includes = (_ffi0,),
127 )
128 """
129
130 def test_negative_constant():
131 ffi = FFI()
132 ffi.cdef("static const int BB = -42;")
133 target = udir.join('test_negative_constant.py')
134 make_py_source(ffi, 'test_negative_constant', str(target))
135 assert target.read() == r"""# auto-generated file
136 import _cffi_backend
137
138 ffi = _cffi_backend.FFI('test_negative_constant',
139 _version = 0x2601,
140 _types = b'',
141 _globals = (b'\xFF\xFF\xFF\x1FBB',-42,),
142 )
143 """
144
145 def test_struct_included():
146 baseffi = FFI()
147 baseffi.cdef("struct foo_s { int x; };")
148 baseffi.set_source('test_struct_included_base', None)
149 #
150 ffi = FFI()
151 ffi.include(baseffi)
152 target = udir.join('test_struct_included.py')
153 make_py_source(ffi, 'test_struct_included', str(target))
154 assert target.read() == r"""# auto-generated file
155 import _cffi_backend
156 from test_struct_included_base import ffi as _ffi0
157
158 ffi = _cffi_backend.FFI('test_struct_included',
159 _version = 0x2601,
160 _types = b'\x00\x00\x00\x09',
161 _struct_unions = ((b'\x00\x00\x00\x00\x00\x00\x00\x08foo_s',),),
162 _includes = (_ffi0,),
163 )
164 """
165
166 def test_no_cross_include():
167 baseffi = FFI()
168 baseffi.set_source('test_no_cross_include_base', "..source..")
169 #
170 ffi = FFI()
171 ffi.include(baseffi)
172 target = udir.join('test_no_cross_include.py')
173 pytest.raises(VerificationError, make_py_source,
174 ffi, 'test_no_cross_include', str(target))
175
176 def test_array():
177 ffi = FFI()
178 ffi.cdef("typedef int32_t my_array_t[42];")
179 target = udir.join('test_array.py')
180 make_py_source(ffi, 'test_array', str(target))
181 assert target.read() == r"""# auto-generated file
182 import _cffi_backend
183
184 ffi = _cffi_backend.FFI('test_array',
185 _version = 0x2601,
186 _types = b'\x00\x00\x15\x01\x00\x00\x00\x05\x00\x00\x00\x2A',
187 _typenames = (b'\x00\x00\x00\x01my_array_t',),
188 )
189 """
190
191 def test_array_overflow():
192 ffi = FFI()
193 ffi.cdef("typedef int32_t my_array_t[3000000000];")
194 target = udir.join('test_array_overflow.py')
195 pytest.raises(OverflowError, make_py_source,
196 ffi, 'test_array_overflow', str(target))
197
198 def test_global_var():
199 ffi = FFI()
200 ffi.cdef("extern int myglob;")
201 target = udir.join('test_global_var.py')
202 make_py_source(ffi, 'test_global_var', str(target))
203 assert target.read() == r"""# auto-generated file
204 import _cffi_backend
205
206 ffi = _cffi_backend.FFI('test_global_var',
207 _version = 0x2601,
208 _types = b'\x00\x00\x07\x01',
209 _globals = (b'\x00\x00\x00\x21myglob',0,),
210 )
211 """
212
213 def test_bitfield():
214 ffi = FFI()
215 ffi.cdef("struct foo_s { int y:10; short x:5; };")
216 target = udir.join('test_bitfield.py')
217 make_py_source(ffi, 'test_bitfield', str(target))
218 assert target.read() == r"""# auto-generated file
219 import _cffi_backend
220
221 ffi = _cffi_backend.FFI('test_bitfield',
222 _version = 0x2601,
223 _types = b'\x00\x00\x07\x01\x00\x00\x05\x01\x00\x00\x00\x09',
224 _struct_unions = ((b'\x00\x00\x00\x02\x00\x00\x00\x02foo_s',b'\x00\x00\x00\x13\x00\x00\x00\x0Ay',b'\x00\x00\x01\x13\x00\x00\x00\x05x'),),
225 )
226 """