Debian Codegraph @

extra_tests/cffi_tests/cffi1/test_zdist.py

raw symbols SCIP
1 # Generated by pypy/tool/import_cffi.py
2 import sys, os
3 import pytest
4 import subprocess
5 import cffi
6 from extra_tests.cffi_tests.udir import udir
7 from shutil import rmtree
8 from tempfile import mkdtemp
9
10
11 def chdir_to_tmp(f):
12 f.chdir_to_tmp = True
13 return f
14
15 def from_outside(f):
16 f.chdir_to_tmp = False
17 return f
18
19
20 class TestDist(object):
21
22 def setup_method(self, meth):
23 self.executable = os.path.abspath(sys.executable)
24 self.rootdir = os.path.abspath(os.path.dirname(os.path.dirname(
25 cffi.__file__)))
26 self.udir = udir.join(meth.__name__)
27 os.mkdir(str(self.udir))
28 if meth.chdir_to_tmp:
29 self.saved_cwd = os.getcwd()
30 os.chdir(str(self.udir))
31
32 def teardown_method(self, meth):
33 if hasattr(self, 'saved_cwd'):
34 os.chdir(self.saved_cwd)
35
36 def run(self, args, cwd=None):
37 env = os.environ.copy()
38 # a horrible hack to prevent distutils from finding ~/.pydistutils.cfg
39 # (there is the --no-user-cfg option, but not in Python 2.6...)
40 # NOTE: pointing $HOME to a nonexistent directory can break certain things
41 # that look there for configuration (like ccache).
42 tmp_home = mkdtemp()
43 assert tmp_home != None, "cannot create temporary homedir"
44 env['HOME'] = tmp_home
45 pathlist = sys.path[:]
46 if cwd is None:
47 pathlist.insert(0, self.rootdir)
48 if sys.version_info > (3, 0, 0):
49 env['PYTHONPATH'] = os.pathsep.join(pathlist)
50 try:
51 subprocess.check_call([self.executable] + args, cwd=cwd, env=env)
52 finally:
53 rmtree(tmp_home)
54
55 def _prepare_setuptools(self):
56 if hasattr(TestDist, '_setuptools_ready'):
57 return
58 try:
59 import setuptools
60 except ImportError:
61 pytest.skip("setuptools not found")
62 if os.path.exists(os.path.join(self.rootdir, 'setup.py')):
63 self.run(['setup.py', 'egg_info'], cwd=self.rootdir)
64 TestDist._setuptools_ready = True
65
66 def check_produced_files(self, content, curdir=None):
67 if curdir is None:
68 curdir = str(self.udir)
69 found_so = None
70 for name in os.listdir(curdir):
71 if (name.endswith('.so') or name.endswith('.pyd') or
72 name.endswith('.dylib') or name.endswith('.dll')):
73 found_so = os.path.join(curdir, name)
74 # foo.so => foo
75 parts = name.split('.')
76 del parts[-1]
77 if len(parts) > 1 and parts[-1] != 'bar':
78 # foo.cpython-34m.so => foo, but foo.bar.so => foo.bar
79 del parts[-1]
80 name = '.'.join(parts)
81 # foo_d => foo (Python 2 debug builds)
82 if name.endswith('_d') and hasattr(sys, 'gettotalrefcount'):
83 name = name[:-2]
84 name += '.SO'
85 if name.startswith('pycparser') and name.endswith('.egg'):
86 continue # no clue why this shows up sometimes and not others
87 if name == '.eggs':
88 continue # seems new in 3.5, ignore it
89 assert name in content, "found unexpected file %r" % (
90 os.path.join(curdir, name),)
91 value = content.pop(name)
92 if value is None:
93 assert name.endswith('.SO') or (
94 os.path.isfile(os.path.join(curdir, name)))
95 else:
96 subdir = os.path.join(curdir, name)
97 assert os.path.isdir(subdir)
98 if value == '?':
99 continue
100 found_so = self.check_produced_files(value, subdir) or found_so
101 assert content == {}, "files or dirs not produced in %r: %r" % (
102 curdir, content.keys())
103 return found_so
104
105 @chdir_to_tmp
106 def test_empty(self):
107 self.check_produced_files({})
108
109 @chdir_to_tmp
110 def test_abi_emit_python_code_1(self):
111 ffi = cffi.FFI()
112 ffi.set_source("package_name_1.mymod", None)
113 ffi.emit_python_code('xyz.py')
114 self.check_produced_files({'xyz.py': None})
115
116 @chdir_to_tmp
117 def test_abi_emit_python_code_2(self):
118 ffi = cffi.FFI()
119 ffi.set_source("package_name_1.mymod", None)
120 pytest.raises(IOError, ffi.emit_python_code, 'unexisting/xyz.py')
121
122 @from_outside
123 def test_abi_emit_python_code_3(self):
124 ffi = cffi.FFI()
125 ffi.set_source("package_name_1.mymod", None)
126 ffi.emit_python_code(str(self.udir.join('xyt.py')))
127 self.check_produced_files({'xyt.py': None})
128
129 @chdir_to_tmp
130 def test_abi_compile_1(self):
131 ffi = cffi.FFI()
132 ffi.set_source("mod_name_in_package.mymod", None)
133 x = ffi.compile()
134 self.check_produced_files({'mod_name_in_package': {'mymod.py': None}})
135 assert x == os.path.join('.', 'mod_name_in_package', 'mymod.py')
136
137 @chdir_to_tmp
138 def test_abi_compile_2(self):
139 ffi = cffi.FFI()
140 ffi.set_source("mod_name_in_package.mymod", None)
141 x = ffi.compile('build2')
142 self.check_produced_files({'build2': {
143 'mod_name_in_package': {'mymod.py': None}}})
144 assert x == os.path.join('build2', 'mod_name_in_package', 'mymod.py')
145
146 @from_outside
147 def test_abi_compile_3(self):
148 ffi = cffi.FFI()
149 ffi.set_source("mod_name_in_package.mymod", None)
150 tmpdir = str(self.udir.join('build3'))
151 x = ffi.compile(tmpdir)
152 self.check_produced_files({'build3': {
153 'mod_name_in_package': {'mymod.py': None}}})
154 assert x == os.path.join(tmpdir, 'mod_name_in_package', 'mymod.py')
155
156 @chdir_to_tmp
157 def test_api_emit_c_code_1(self):
158 ffi = cffi.FFI()
159 ffi.set_source("package_name_1.mymod", "/*code would be here*/")
160 ffi.emit_c_code('xyz.c')
161 self.check_produced_files({'xyz.c': None})
162
163 @chdir_to_tmp
164 def test_api_emit_c_code_2(self):
165 ffi = cffi.FFI()
166 ffi.set_source("package_name_1.mymod", "/*code would be here*/")
167 pytest.raises(IOError, ffi.emit_c_code, 'unexisting/xyz.c')
168
169 @from_outside
170 def test_api_emit_c_code_3(self):
171 ffi = cffi.FFI()
172 ffi.set_source("package_name_1.mymod", "/*code would be here*/")
173 ffi.emit_c_code(str(self.udir.join('xyu.c')))
174 self.check_produced_files({'xyu.c': None})
175
176 @chdir_to_tmp
177 def test_api_compile_1(self):
178 ffi = cffi.FFI()
179 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
180 x = ffi.compile()
181 if sys.platform != 'win32':
182 sofile = self.check_produced_files({
183 'mod_name_in_package': {'mymod.SO': None,
184 'mymod.c': None,
185 'mymod.o': None}})
186 assert os.path.isabs(x) and os.path.samefile(x, sofile)
187 else:
188 self.check_produced_files({
189 'mod_name_in_package': {'mymod.SO': None,
190 'mymod.c': None},
191 'Release': '?'})
192
193 @chdir_to_tmp
194 def test_api_compile_2(self):
195 ffi = cffi.FFI()
196 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
197 x = ffi.compile('output')
198 if sys.platform != 'win32':
199 sofile = self.check_produced_files({
200 'output': {'mod_name_in_package': {'mymod.SO': None,
201 'mymod.c': None,
202 'mymod.o': None}}})
203 assert os.path.isabs(x) and os.path.samefile(x, sofile)
204 else:
205 self.check_produced_files({
206 'output': {'mod_name_in_package': {'mymod.SO': None,
207 'mymod.c': None},
208 'Release': '?'}})
209
210 @from_outside
211 def test_api_compile_3(self):
212 ffi = cffi.FFI()
213 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
214 x = ffi.compile(str(self.udir.join('foo')))
215 if sys.platform != 'win32':
216 sofile = self.check_produced_files({
217 'foo': {'mod_name_in_package': {'mymod.SO': None,
218 'mymod.c': None,
219 'mymod.o': None}}})
220 assert os.path.isabs(x) and os.path.samefile(x, sofile)
221 else:
222 self.check_produced_files({
223 'foo': {'mod_name_in_package': {'mymod.SO': None,
224 'mymod.c': None},
225 'Release': '?'}})
226
227 @chdir_to_tmp
228 def test_api_compile_explicit_target_1(self):
229 ffi = cffi.FFI()
230 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
231 x = ffi.compile(target="foo.bar.*")
232 if sys.platform != 'win32':
233 sofile = self.check_produced_files({
234 'mod_name_in_package': {'foo.bar.SO': None,
235 'mymod.c': None,
236 'mymod.o': None}})
237 assert os.path.isabs(x) and os.path.samefile(x, sofile)
238 else:
239 self.check_produced_files({
240 'mod_name_in_package': {'foo.bar.SO': None,
241 'mymod.c': None},
242 'Release': '?'})
243
244 @chdir_to_tmp
245 def test_api_compile_explicit_target_3(self):
246 ffi = cffi.FFI()
247 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
248 x = ffi.compile(target="foo.bar.baz")
249 if sys.platform != 'win32':
250 self.check_produced_files({
251 'mod_name_in_package': {'foo.bar.baz': None,
252 'mymod.c': None,
253 'mymod.o': None}})
254 sofile = os.path.join(str(self.udir),
255 'mod_name_in_package', 'foo.bar.baz')
256 assert os.path.isabs(x) and os.path.samefile(x, sofile)
257 else:
258 self.check_produced_files({
259 'mod_name_in_package': {'foo.bar.baz': None,
260 'mymod.c': None},
261 'Release': '?'})
262
263 @chdir_to_tmp
264 def test_api_distutils_extension_1(self):
265 ffi = cffi.FFI()
266 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
267 ext = ffi.distutils_extension()
268 self.check_produced_files({'build': {
269 'mod_name_in_package': {'mymod.c': None}}})
270 if hasattr(os.path, 'samefile'):
271 assert os.path.samefile(ext.sources[0],
272 'build/mod_name_in_package/mymod.c')
273
274 @from_outside
275 def test_api_distutils_extension_2(self):
276 ffi = cffi.FFI()
277 ffi.set_source("mod_name_in_package.mymod", "/*code would be here*/")
278 ext = ffi.distutils_extension(str(self.udir.join('foo')))
279 self.check_produced_files({'foo': {
280 'mod_name_in_package': {'mymod.c': None}}})
281 if hasattr(os.path, 'samefile'):
282 assert os.path.samefile(ext.sources[0],
283 str(self.udir.join('foo/mod_name_in_package/mymod.c')))
284
285
286 def _make_distutils_api(self):
287 os.mkdir("src")
288 os.mkdir(os.path.join("src", "pack1"))
289 with open(os.path.join("src", "pack1", "__init__.py"), "w") as f:
290 pass
291 with open("setup.py", "w") as f:
292 f.write("""if 1:
293 # https://bugs.python.org/issue23246
294 import setuptools
295 import cffi
296 ffi = cffi.FFI()
297 ffi.set_source("pack1.mymod", "/*code would be here*/")
298
299 from distutils.core import setup
300 setup(name='example1',
301 version='0.1',
302 packages=['pack1'],
303 package_dir={'': 'src'},
304 ext_modules=[ffi.distutils_extension()])
305 """)
306
307 @chdir_to_tmp
308 def test_distutils_api_1(self):
309 self._make_distutils_api()
310 self.run(["setup.py", "build"])
311 self.check_produced_files({'setup.py': None,
312 'build': '?',
313 'src': {'pack1': {'__init__.py': None}}})
314
315 @chdir_to_tmp
316 def test_distutils_api_2(self):
317 self._make_distutils_api()
318 self.run(["setup.py", "build_ext", "-i"])
319 self.check_produced_files({'setup.py': None,
320 'build': '?',
321 'src': {'pack1': {'__init__.py': None,
322 'mymod.SO': None}}})
323
324 def _make_setuptools_abi(self):
325 self._prepare_setuptools()
326 os.mkdir("src0")
327 os.mkdir(os.path.join("src0", "pack2"))
328 with open(os.path.join("src0", "pack2", "__init__.py"), "w") as f:
329 pass
330 with open(os.path.join("src0", "pack2", "_build.py"), "w") as f:
331 f.write("""if 1:
332 import cffi
333 ffi = cffi.FFI()
334 ffi.set_source("pack2.mymod", None)
335 """)
336 with open("setup.py", "w") as f:
337 f.write("""if 1:
338 from setuptools import setup
339 setup(name='example1',
340 version='0.1',
341 packages=['pack2'],
342 package_dir={'': 'src0'},
343 cffi_modules=["src0/pack2/_build.py:ffi"])
344 """)
345
346 @chdir_to_tmp
347 def test_setuptools_abi_1(self):
348 self._make_setuptools_abi()
349 self.run(["setup.py", "build"])
350 self.check_produced_files({'setup.py': None,
351 'build': '?',
352 'src0': {'pack2': {'__init__.py': None,
353 '_build.py': None}}})
354
355 @chdir_to_tmp
356 def test_setuptools_abi_2(self):
357 self._make_setuptools_abi()
358 self.run(["setup.py", "build_ext", "-i"])
359 self.check_produced_files({'setup.py': None,
360 'src0': {'pack2': {'__init__.py': None,
361 '_build.py': None,
362 'mymod.py': None}}})
363
364 def _make_setuptools_api(self):
365 self._prepare_setuptools()
366 os.mkdir("src1")
367 os.mkdir(os.path.join("src1", "pack3"))
368 with open(os.path.join("src1", "pack3", "__init__.py"), "w") as f:
369 pass
370 with open(os.path.join("src1", "pack3", "_build.py"), "w") as f:
371 f.write("""if 1:
372 import cffi
373 ffi = cffi.FFI()
374 ffi.set_source("pack3.mymod", "/*code would be here*/")
375 ffi._hi_there = 42
376 """)
377 with open("setup.py", "w") as f:
378 f.write("from __future__ import print_function\n"
379 """if 1:
380 from setuptools import setup
381 from distutils.command.build_ext import build_ext
382 import os
383
384 class TestBuildExt(build_ext):
385 def pre_run(self, ext, ffi):
386 print('_make_setuptools_api: in pre_run:', end=" ")
387 assert ffi._hi_there == 42
388 assert ext.name == "pack3.mymod"
389 fn = os.path.join(os.path.dirname(self.build_lib),
390 '..', 'see_me')
391 print('creating %r' % (fn,))
392 open(fn, 'w').close()
393
394 setup(name='example1',
395 version='0.1',
396 packages=['pack3'],
397 package_dir={'': 'src1'},
398 cffi_modules=["src1/pack3/_build.py:ffi"],
399 cmdclass={'build_ext': TestBuildExt},
400 )
401 """)
402
403 @chdir_to_tmp
404 def test_setuptools_api_1(self):
405 self._make_setuptools_api()
406 self.run(["setup.py", "build"])
407 self.check_produced_files({'setup.py': None,
408 'build': '?',
409 'see_me': None,
410 'src1': {'pack3': {'__init__.py': None,
411 '_build.py': None}}})
412
413 @chdir_to_tmp
414 def test_setuptools_api_2(self):
415 self._make_setuptools_api()
416 self.run(["setup.py", "build_ext", "-i"])
417 self.check_produced_files({'setup.py': None,
418 'build': '?',
419 'see_me': None,
420 'src1': {'pack3': {'__init__.py': None,
421 '_build.py': None,
422 'mymod.SO': None}}})