| 1 | from __future__ import with_statement |
| 2 | """ |
| 3 | This file is OBSCURE. Really. The purpose is to avoid copying and changing |
| 4 | 'test_c.py' from cffi/c/ in the original CFFI repository: |
| 5 | https://foss.heptapod.net/pypy/cffi/ |
| 6 | |
| 7 | Adding a test here involves: |
| 8 | 1. add a test to cffi/c/test.py |
| 9 | - if you need a C function to call, add it into _cffi_backend.c |
| 10 | as a testfuncNN(). |
| 11 | 2. have it pass when you run 'py.test test_c.py' in cffi |
| 12 | 3. check in and (if you can) push the changes |
| 13 | 4. copy test_c.py into _backend_test.py here, killing the few lines of header |
| 14 | - if you added a C function, it goes into _test_lib.c here |
| 15 | - if you could complete step 3, try running 'python2 pytest.py test_file.py' here |
| 16 | 5. make the test pass in pypy ('python2 pytest.py test_c.py') |
| 17 | """ |
| 18 | import py, sys, ctypes |
| 19 | |
| 20 | from rpython.tool.udir import udir |
| 21 | from pypy.interpreter import gateway |
| 22 | from pypy.module._cffi_backend.moduledef import Module |
| 23 | from pypy.module._cffi_backend.newtype import _clean_cache, UniqueCache |
| 24 | from rpython.translator import cdir |
| 25 | from rpython.translator.platform import host |
| 26 | from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 27 | |
| 28 | from .. import VERSION as TEST_VERSION |
| 29 | |
| 30 | |
| 31 | class AppTestC(object): |
| 32 | """Populated below, hack hack hack.""" |
| 33 | |
| 34 | spaceconfig = dict(usemodules=('_cffi_backend', '_io', 'array')) |
| 35 | |
| 36 | def setup_class(cls): |
| 37 | if cls.runappdirect: |
| 38 | _cffi_backend = py.test.importorskip('_cffi_backend') |
| 39 | if _cffi_backend.__version__ != TEST_VERSION: |
| 40 | py.test.skip( |
| 41 | "These tests are for cffi version %s, this Python " |
| 42 | "has version %s installed" % |
| 43 | (TEST_VERSION, _cffi_backend.__version__)) |
| 44 | |
| 45 | |
| 46 | testfuncs_w = [] |
| 47 | keepalive_funcs = [] |
| 48 | UniqueCache.for_testing = True |
| 49 | |
| 50 | test_lib_c = tmpdir.join('_test_lib.c') |
| 51 | src_test_lib_c = py.path.local(__file__).dirpath().join('_test_lib.c') |
| 52 | src_test_lib_c.copy(test_lib_c) |
| 53 | eci = ExternalCompilationInfo(include_dirs=[cdir]) |
| 54 | test_lib = str(host.compile([test_lib_c], eci, standalone=False)) |
| 55 | |
| 56 | cdll = ctypes.CDLL(test_lib) |
| 57 | cdll.gettestfunc.restype = ctypes.c_void_p |
| 58 | |
| 59 | space = cls.space |
| 60 | if cls.runappdirect: |
| 61 | def find_and_load_library_for_test(name, is_global=False): |
| 62 | if name is None: |
| 63 | path = None |
| 64 | else: |
| 65 | import ctypes.util |
| 66 | path = ctypes.util.find_library(name) |
| 67 | if path is None and name == 'c' and sys.platform == 'win32': |
| 68 | skip("dlopen(None) cannot work on Windows") |
| 69 | import _cffi_backend |
| 70 | return _cffi_backend.load_library(path, is_global) |
| 71 | |
| 72 | def w_testfunc_for_test(num): |
| 73 | import ctypes |
| 74 | cdll = ctypes.CDLL(str(self.test_lib)) |
| 75 | cdll.gettestfunc.restype = ctypes.c_void_p |
| 76 | return cdll.gettestfunc(num) |
| 77 | |
| 78 | cls.w_test_lib = space.wrap(test_lib) |
| 79 | cls.w_func = find_and_load_library_for_test |
| 80 | cls.w_testfunc = w_testfunc_for_test |
| 81 | else: |
| 82 | def find_and_load_library_for_test(space, w_name, w_is_global=None): |
| 83 | if w_is_global is None: |
| 84 | w_is_global = space.wrap(0) |
| 85 | if space.is_w(w_name, space.w_None): |
| 86 | path = None |
| 87 | w_name = space.newtext('None') |
| 88 | else: |
| 89 | import ctypes.util |
| 90 | path = ctypes.util.find_library(space.text_w(w_name)) |
| 91 | if path is None and sys.platform == 'win32': |
| 92 | py.test.skip("cannot find library '%s'" % (space.text_w(w_name),)) |
| 93 | return space.appexec([space.wrap(path), w_is_global], |
| 94 | """(path, is_global): |
| 95 | import _cffi_backend |
| 96 | return _cffi_backend.load_library(path, is_global)""") |
| 97 | |
| 98 | def testfunc_for_test(space, w_num): |
| 99 | if hasattr(space, 'int_w'): |
| 100 | w_num = space.int_w(w_num) |
| 101 | addr = cdll.gettestfunc(w_num) |
| 102 | return space.wrap(addr) |
| 103 | |
| 104 | cls.w_func = space.wrap(gateway.interp2app(find_and_load_library_for_test)) |
| 105 | cls.w_testfunc = space.wrap(gateway.interp2app(testfunc_for_test)) |
| 106 | cls.w_zz_init = space.appexec( |
| 107 | [space.wrap(str(tmpdir)), cls.w_func, cls.w_testfunc, |
| 108 | space.wrap(sys.version[:3])], |
| 109 | """(path, func, testfunc, underlying_version): |
| 110 | import sys |
| 111 | sys.path.append(path) |
| 112 | is_musl = False |
| 113 | import _all_test_c |
| 114 | _all_test_c.PY_DOT_PY = underlying_version |
| 115 | _all_test_c.find_and_load_library = func |
| 116 | _all_test_c._testfunc = testfunc |
| 117 | """) |
| 118 | |
| 119 | def teardown_method(self, method): |
| 120 | _clean_cache(self.space) |
| 121 | |
| 122 | def teardown_class(cls): |
| 123 | UniqueCache.for_testing = False |
| 124 | |
| 125 | |
| 126 | all_names = ', '.join(Module.interpleveldefs.keys()) |
| 127 | |
| 128 | backend_test_c = py.path.local(__file__).join('..', '_backend_test_c.py') |
| 129 | |
| 130 | lst = [] |
| 131 | |
| 132 | with backend_test_c.open('r') as f: |
| 133 | for line in f: |
| 134 | if line.startswith('def test_'): |
| 135 | line = line[4:] |
| 136 | line = line[:line.index('():')] |
| 137 | lst.append(line) |
| 138 | |
| 139 | tmpdir = udir.join('test_c').ensure(dir=1) |
| 140 | |
| 141 | tmpname = tmpdir.join('_test_c.py') |
| 142 | with tmpname.open('w') as f: |
| 143 | print >> f, "import pytest" |
| 144 | for func in lst: |
| 145 | print >> f, 'def %s(self):' % (func,) |
| 146 | print >> f, ' import _all_test_c' |
| 147 | print >> f, ' try:' |
| 148 | print >> f, ' _all_test_c.%s()' % (func,) |
| 149 | print >> f, ' except BaseException as e:' |
| 150 | print >> f, ' if "Skipped" in str(type(e)):' |
| 151 | print >> f, ' skip(e.msg)' |
| 152 | print >> f, ' raise' |
| 153 | |
| 154 | tmpname2 = tmpdir.join('_all_test_c.py') |
| 155 | with tmpname2.open('w') as f: |
| 156 | print >> f, 'import sys' |
| 157 | print >> f, 'from _cffi_backend import %s' % all_names |
| 158 | print >> f, 'is_musl = False' |
| 159 | print >> f, 'class py:' |
| 160 | print >> f, ' class test:' |
| 161 | print >> f, ' raises = staticmethod(raises)' |
| 162 | print >> f, ' skip = staticmethod(skip)' |
| 163 | print >> f, 'pytest = py.test' |
| 164 | print >> f, backend_test_c.read() |
| 165 | |
| 166 | |
| 167 | mod = tmpname.pyimport() |
| 168 | for key, value in mod.__dict__.items(): |
| 169 | if key.startswith('test_'): |
| 170 | setattr(AppTestC, key, value) |