| 1 |
|
| 2 |
import sys, os, re |
| 3 |
import shutil, subprocess, time |
| 4 |
import pytest |
| 5 |
from extra_tests.cffi_tests.udir import udir |
| 6 |
import cffi |
| 7 |
|
| 8 |
|
| 9 |
local_dir = os.path.dirname(os.path.abspath(__file__)) |
| 10 |
_link_error = '?' |
| 11 |
|
| 12 |
def check_lib_python_found(tmpdir): |
| 13 |
global _link_error |
| 14 |
if _link_error == '?': |
| 15 |
ffi = cffi.FFI() |
| 16 |
kwds = {} |
| 17 |
ffi._apply_embedding_fix(kwds) |
| 18 |
ffi.set_source("_test_lib_python_found", "", **kwds) |
| 19 |
try: |
| 20 |
ffi.compile(tmpdir=tmpdir, verbose=True) |
| 21 |
except cffi.VerificationError as e: |
| 22 |
_link_error = e |
| 23 |
else: |
| 24 |
_link_error = None |
| 25 |
if _link_error: |
| 26 |
pytest.skip(str(_link_error)) |
| 27 |
|
| 28 |
|
| 29 |
def prefix_pythonpath(): |
| 30 |
cffi_base = os.path.dirname(os.path.dirname(local_dir)) |
| 31 |
pythonpath = org_env.get('PYTHONPATH', '').split(os.pathsep) |
| 32 |
if cffi_base not in pythonpath: |
| 33 |
pythonpath.insert(0, cffi_base) |
| 34 |
return os.pathsep.join(pythonpath) |
| 35 |
|
| 36 |
def copy_away_env(): |
| 37 |
global org_env |
| 38 |
try: |
| 39 |
org_env |
| 40 |
except NameError: |
| 41 |
org_env = os.environ.copy() |
| 42 |
|
| 43 |
|
| 44 |
class EmbeddingTests: |
| 45 |
_compiled_modules = {} |
| 46 |
|
| 47 |
def setup_method(self, meth): |
| 48 |
check_lib_python_found(str(udir.ensure('embedding', dir=1))) |
| 49 |
self._path = udir.join('embedding', meth.__name__) |
| 50 |
if sys.platform == "win32" or sys.platform == "darwin": |
| 51 |
self._compiled_modules.clear() |
| 52 |
|
| 53 |
def get_path(self): |
| 54 |
return str(self._path.ensure(dir=1)) |
| 55 |
|
| 56 |
def _run_base(self, args, **kwds): |
| 57 |
print('RUNNING:', args, kwds) |
| 58 |
return subprocess.Popen(args, **kwds) |
| 59 |
|
| 60 |
def _run(self, args): |
| 61 |
popen = self._run_base(args, cwd=self.get_path(), |
| 62 |
stdout=subprocess.PIPE, |
| 63 |
universal_newlines=True) |
| 64 |
output, _ = popen.communicate() |
| 65 |
err = popen.returncode |
| 66 |
if err: |
| 67 |
raise OSError(("popen failed with exit code %r: %r\n\n%s" % ( |
| 68 |
err, args, output)).rstrip()) |
| 69 |
print(output.rstrip()) |
| 70 |
return output |
| 71 |
|
| 72 |
def prepare_module(self, name): |
| 73 |
self.patch_environment() |
| 74 |
if name not in self._compiled_modules: |
| 75 |
path = self.get_path() |
| 76 |
filename = '%s.py' % name |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
pathname = os.path.join(path, filename) |
| 84 |
with open(pathname, 'w') as g: |
| 85 |
g.write(''' |
| 86 |
# https://bugs.python.org/issue23246 |
| 87 |
import sys |
| 88 |
if sys.platform == 'win32': |
| 89 |
try: |
| 90 |
import setuptools |
| 91 |
except ImportError: |
| 92 |
pass |
| 93 |
''') |
| 94 |
with open(os.path.join(local_dir, filename), 'r') as f: |
| 95 |
g.write(f.read()) |
| 96 |
|
| 97 |
output = self._run([sys.executable, pathname]) |
| 98 |
match = re.compile(r"\bFILENAME: (.+)").search(output) |
| 99 |
assert match |
| 100 |
dynamic_lib_name = match.group(1) |
| 101 |
if sys.platform == 'win32': |
| 102 |
assert dynamic_lib_name.endswith('_cffi.dll') |
| 103 |
elif sys.platform == 'darwin': |
| 104 |
assert dynamic_lib_name.endswith('_cffi.dylib') |
| 105 |
else: |
| 106 |
assert dynamic_lib_name.endswith('_cffi.so') |
| 107 |
self._compiled_modules[name] = dynamic_lib_name |
| 108 |
return self._compiled_modules[name] |
| 109 |
|
| 110 |
def compile(self, name, modules, opt=False, threads=False, defines={}): |
| 111 |
path = self.get_path() |
| 112 |
filename = '%s.c' % name |
| 113 |
shutil.copy(os.path.join(local_dir, filename), path) |
| 114 |
shutil.copy(os.path.join(local_dir, 'thread-test.h'), path) |
| 115 |
import distutils.ccompiler |
| 116 |
curdir = os.getcwd() |
| 117 |
try: |
| 118 |
os.chdir(self.get_path()) |
| 119 |
c = distutils.ccompiler.new_compiler() |
| 120 |
print('compiling %s with %r' % (name, modules)) |
| 121 |
extra_preargs = [] |
| 122 |
debug = True |
| 123 |
if sys.platform == 'win32': |
| 124 |
libfiles = [] |
| 125 |
for m in modules: |
| 126 |
m = os.path.basename(m) |
| 127 |
assert m.endswith('.dll') |
| 128 |
libfiles.append('Release\\%s.lib' % m[:-4]) |
| 129 |
modules = libfiles |
| 130 |
extra_preargs.append('/MANIFEST') |
| 131 |
debug = False |
| 132 |
|
| 133 |
elif threads: |
| 134 |
extra_preargs.append('-pthread') |
| 135 |
objects = c.compile([filename], macros=sorted(defines.items()), |
| 136 |
debug=debug) |
| 137 |
c.link_executable(objects + modules, name, extra_preargs=extra_preargs) |
| 138 |
finally: |
| 139 |
os.chdir(curdir) |
| 140 |
|
| 141 |
def patch_environment(self): |
| 142 |
copy_away_env() |
| 143 |
path = self.get_path() |
| 144 |
|
| 145 |
path = os.path.split(sys.executable)[0] + os.path.pathsep + path |
| 146 |
env_extra = {'PYTHONPATH': prefix_pythonpath()} |
| 147 |
if sys.platform == 'win32': |
| 148 |
envname = 'PATH' |
| 149 |
else: |
| 150 |
envname = 'LD_LIBRARY_PATH' |
| 151 |
libpath = org_env.get(envname) |
| 152 |
if libpath: |
| 153 |
libpath = path + os.path.pathsep + libpath |
| 154 |
else: |
| 155 |
libpath = path |
| 156 |
env_extra[envname] = libpath |
| 157 |
for key, value in sorted(env_extra.items()): |
| 158 |
if os.environ.get(key) != value: |
| 159 |
print('* setting env var %r to %r' % (key, value)) |
| 160 |
os.environ[key] = value |
| 161 |
|
| 162 |
def execute(self, name): |
| 163 |
path = self.get_path() |
| 164 |
print('running %r in %r' % (name, path)) |
| 165 |
executable_name = name |
| 166 |
if sys.platform == 'win32': |
| 167 |
executable_name = os.path.join(path, executable_name + '.exe') |
| 168 |
else: |
| 169 |
executable_name = os.path.join('.', executable_name) |
| 170 |
popen = self._run_base([executable_name], cwd=path, |
| 171 |
stdout=subprocess.PIPE, |
| 172 |
universal_newlines=True) |
| 173 |
result, _ = popen.communicate() |
| 174 |
err = popen.returncode |
| 175 |
if err: |
| 176 |
raise OSError("%r failed with exit code %r" % ( |
| 177 |
os.path.join(path, executable_name), err)) |
| 178 |
return result |
| 179 |
|
| 180 |
|
| 181 |
class TestBasic(EmbeddingTests): |
| 182 |
def test_empty(self): |
| 183 |
empty_cffi = self.prepare_module('empty') |
| 184 |
self.compile('empty-test', [empty_cffi]) |
| 185 |
output = self.execute('empty-test') |
| 186 |
assert output == 'OK\n' |
| 187 |
|
| 188 |
def test_basic(self): |
| 189 |
add1_cffi = self.prepare_module('add1') |
| 190 |
self.compile('add1-test', [add1_cffi]) |
| 191 |
output = self.execute('add1-test') |
| 192 |
assert output == ("preparing...\n" |
| 193 |
"adding 40 and 2\n" |
| 194 |
"adding 100 and -5\n" |
| 195 |
"got: 42 95\n") |
| 196 |
|
| 197 |
def test_two_modules(self): |
| 198 |
add1_cffi = self.prepare_module('add1') |
| 199 |
add2_cffi = self.prepare_module('add2') |
| 200 |
self.compile('add2-test', [add1_cffi, add2_cffi]) |
| 201 |
output = self.execute('add2-test') |
| 202 |
assert output == ("preparing...\n" |
| 203 |
"adding 40 and 2\n" |
| 204 |
"prepADD2\n" |
| 205 |
"adding 100 and -5 and -20\n" |
| 206 |
"got: 42 75\n") |
| 207 |
|
| 208 |
def test_init_time_error(self): |
| 209 |
initerror_cffi = self.prepare_module('initerror') |
| 210 |
self.compile('add1-test', [initerror_cffi]) |
| 211 |
output = self.execute('add1-test') |
| 212 |
assert output == "got: 0 0\n" |
| 213 |
|
| 214 |
def test_embedding_with_unicode(self): |
| 215 |
withunicode_cffi = self.prepare_module('withunicode') |
| 216 |
self.compile('add1-test', [withunicode_cffi]) |
| 217 |
output = self.execute('add1-test') |
| 218 |
assert output == "255\n4660\n65244\ngot: 0 0\n" |