Debian Codegraph @

extra_tests/ctypes_tests/conftest.py

raw symbols SCIP
1 import py
2 import pytest
3 import sys
4 import os
5
6 # XXX: copied from pypy/tool/cpyext/extbuild.py
7 if os.name != 'nt':
8 so_ext = 'so'
9 else:
10 so_ext = 'dll'
11
12 def _build(cfilenames, outputfilename, compile_extra, link_extra,
13 include_dirs, libraries, library_dirs):
14 try:
15 # monkeypatch distutils for some versions of msvc compiler
16 import setuptools
17 except ImportError:
18 # XXX if this fails and is required,
19 # we must call pypy -mensurepip after translation
20 pass
21 from distutils.ccompiler import new_compiler
22 from distutils import sysconfig
23
24 # XXX for Darwin running old versions of CPython 2.7.x
25 sysconfig.get_config_vars()
26
27 compiler = new_compiler(force=1)
28 sysconfig.customize_compiler(compiler) # XXX
29 objects = []
30 for cfile in cfilenames:
31 cfile = py.path.local(cfile)
32 old = cfile.dirpath().chdir()
33 try:
34 res = compiler.compile([cfile.basename],
35 include_dirs=include_dirs, extra_preargs=compile_extra)
36 assert len(res) == 1
37 cobjfile = py.path.local(res[0])
38 assert cobjfile.check()
39 objects.append(str(cobjfile))
40 finally:
41 old.chdir()
42
43 compiler.link_shared_object(
44 objects, str(outputfilename),
45 libraries=libraries,
46 extra_preargs=link_extra,
47 library_dirs=library_dirs)
48
49 def c_compile(cfilenames, outputfilename,
50 compile_extra=None, link_extra=None,
51 include_dirs=None, libraries=None, library_dirs=None):
52 compile_extra = compile_extra or []
53 link_extra = link_extra or []
54 include_dirs = include_dirs or []
55 libraries = libraries or []
56 library_dirs = library_dirs or []
57 if sys.platform == 'win32':
58 link_extra = link_extra + ['/DEBUG'] # generate .pdb file
59 if sys.platform == 'darwin':
60 # support Fink & Darwinports
61 for s in ('/sw/', '/opt/local/'):
62 if (s + 'include' not in include_dirs
63 and os.path.exists(s + 'include')):
64 include_dirs.append(s + 'include')
65 if s + 'lib' not in library_dirs and os.path.exists(s + 'lib'):
66 library_dirs.append(s + 'lib')
67
68 outputfilename = py.path.local(outputfilename).new(ext=so_ext)
69 saved_environ = os.environ.copy()
70 try:
71 _build(
72 cfilenames, outputfilename,
73 compile_extra, link_extra,
74 include_dirs, libraries, library_dirs)
75 finally:
76 # workaround for a distutils bugs where some env vars can
77 # become longer and longer every time it is used
78 for key, value in saved_environ.items():
79 if os.environ.get(key) != value:
80 os.environ[key] = value
81 return outputfilename
82 # end copy
83
84 def compile_so_file(udir):
85 cfile = py.path.local(__file__).dirpath().join("_ctypes_test.c")
86
87 if sys.platform == 'win32':
88 libraries = ['oleaut32']
89 else:
90 libraries = []
91
92 return c_compile([cfile], str(udir / '_ctypes_test'), libraries=libraries)
93
94 @pytest.fixture(scope='session')
95 def sofile(tmpdir_factory):
96 udir = tmpdir_factory.mktemp('_ctypes_test')
97 return str(compile_so_file(udir))
98
99 @pytest.fixture
100 def dll(sofile):
101 from ctypes import CDLL
102 return CDLL(str(sofile))