| 1 |
import py |
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
import os |
| 5 |
|
| 6 |
|
| 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 |
|
| 16 |
import setuptools |
| 17 |
except ImportError: |
| 18 |
|
| 19 |
|
| 20 |
pass |
| 21 |
from distutils.ccompiler import new_compiler |
| 22 |
from distutils import sysconfig |
| 23 |
|
| 24 |
|
| 25 |
sysconfig.get_config_vars() |
| 26 |
|
| 27 |
compiler = new_compiler(force=1) |
| 28 |
sysconfig.customize_compiler(compiler) |
| 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'] |
| 59 |
if sys.platform == 'darwin': |
| 60 |
|
| 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 |
|
| 77 |
|
| 78 |
for key, value in saved_environ.items(): |
| 79 |
if os.environ.get(key) != value: |
| 80 |
os.environ[key] = value |
| 81 |
return outputfilename |
| 82 |
|
| 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)) |