| 1 |
import py |
| 2 |
import sys, shutil, os |
| 3 |
from rpython.tool.udir import udir |
| 4 |
from pypy.interpreter.gateway import interp2app |
| 5 |
from pypy.module._cffi_backend.newtype import _clean_cache |
| 6 |
|
| 7 |
if sys.platform == 'win32': |
| 8 |
WIN32 = True |
| 9 |
else: |
| 10 |
WIN32 = False |
| 11 |
|
| 12 |
class AppTestRecompilerPython: |
| 13 |
spaceconfig = dict(usemodules=['_cffi_backend']) |
| 14 |
|
| 15 |
def setup_class(cls): |
| 16 |
try: |
| 17 |
from cffi import FFI |
| 18 |
from cffi import recompiler |
| 19 |
from cffi import ffiplatform |
| 20 |
except ImportError: |
| 21 |
py.test.skip("system cffi module not found or older than 1.0.0") |
| 22 |
space = cls.space |
| 23 |
SRC = """ |
| 24 |
#define FOOBAR (-42) |
| 25 |
static const int FOOBAZ = -43; |
| 26 |
#define BIGPOS 420000000000L |
| 27 |
#define BIGNEG -420000000000L |
| 28 |
int add42(int x) { return x + 42; } |
| 29 |
int globalvar42 = 1234; |
| 30 |
const int globalconst42 = 4321; |
| 31 |
const char *const globalconsthello = "hello"; |
| 32 |
struct foo_s; |
| 33 |
typedef struct bar_s { int x; signed char a[]; } bar_t; |
| 34 |
enum foo_e { AA, BB, CC }; |
| 35 |
struct B { struct C* c; }; struct C { struct B b; }; |
| 36 |
|
| 37 |
void init_test_re_python(void) { } /* windows hack */ |
| 38 |
void PyInit__test_re_python(void) { } /* windows hack */ |
| 39 |
""" |
| 40 |
tmpdir = udir.join('test_re_python') |
| 41 |
tmpdir.ensure(dir=1) |
| 42 |
c_file = tmpdir.join('_test_re_python.c') |
| 43 |
c_file.write(SRC) |
| 44 |
ext = ffiplatform.get_extension(str(c_file), '_test_re_python', |
| 45 |
export_symbols=['add42', 'globalvar42', |
| 46 |
'globalconst42', 'globalconsthello']) |
| 47 |
outputfilename = ffiplatform.compile(str(tmpdir), ext) |
| 48 |
cls.w_extmod = space.wrap(outputfilename) |
| 49 |
if WIN32: |
| 50 |
unicode_name = u'load\u03betest.dll' |
| 51 |
else: |
| 52 |
unicode_name = u'load_caf\xe9' + os.path.splitext(outputfilename)[1] |
| 53 |
try: |
| 54 |
unicode_name.encode(sys.getfilesystemencoding()) |
| 55 |
except UnicodeEncodeError: |
| 56 |
unicode_name = None |
| 57 |
if unicode_name is not None: |
| 58 |
outputfileUname = os.path.join(unicode(udir), unicode_name) |
| 59 |
shutil.copyfile(outputfilename, outputfileUname) |
| 60 |
cls.w_extmodU = space.wrap(outputfileUname) |
| 61 |
|
| 62 |
|
| 63 |
ffi = FFI() |
| 64 |
ffi.cdef(""" |
| 65 |
#define FOOBAR -42 |
| 66 |
static const int FOOBAZ = -43; |
| 67 |
#define BIGPOS 420000000000L |
| 68 |
#define BIGNEG -420000000000L |
| 69 |
int add42(int); |
| 70 |
int globalvar42; |
| 71 |
const int globalconst42; |
| 72 |
const char *const globalconsthello = "hello"; |
| 73 |
int no_such_function(int); |
| 74 |
int no_such_globalvar; |
| 75 |
struct foo_s; |
| 76 |
typedef struct bar_s { int x; signed char a[]; } bar_t; |
| 77 |
enum foo_e { AA, BB, CC }; |
| 78 |
struct B { struct C* c; }; struct C { struct B b; }; |
| 79 |
typedef struct selfref { struct selfref *next; } *selfref_ptr_t; |
| 80 |
|
| 81 |
void *dlopen(const char *filename, int flags); |
| 82 |
int dlclose(void *handle); |
| 83 |
""") |
| 84 |
ffi.set_source('re_python_pysrc', None) |
| 85 |
ffi.emit_python_code(str(tmpdir.join('re_python_pysrc.py'))) |
| 86 |
|
| 87 |
sub_ffi = FFI() |
| 88 |
sub_ffi.cdef("static const int k2 = 121212;") |
| 89 |
sub_ffi.include(ffi) |
| 90 |
assert 'macro FOOBAR' in ffi._parser._declarations |
| 91 |
assert 'macro FOOBAZ' in ffi._parser._declarations |
| 92 |
sub_ffi.set_source('re_py_subsrc', None) |
| 93 |
sub_ffi.emit_python_code(str(tmpdir.join('re_py_subsrc.py'))) |
| 94 |
|
| 95 |
cls.w_fix_path = space.appexec([space.wrap(str(tmpdir))], """(path): |
| 96 |
def fix_path(ignored=None): |
| 97 |
import _cffi_backend # force it to be initialized |
| 98 |
import sys |
| 99 |
if path not in sys.path: |
| 100 |
sys.path.insert(0, path) |
| 101 |
return fix_path |
| 102 |
""") |
| 103 |
|
| 104 |
cls.w_dl_libpath = space.w_None |
| 105 |
if sys.platform != 'win32': |
| 106 |
import ctypes.util |
| 107 |
cls.w_dl_libpath = space.wrap(ctypes.util.find_library('dl')) |
| 108 |
|
| 109 |
def teardown_method(self, meth): |
| 110 |
self.space.appexec([], """(): |
| 111 |
import sys |
| 112 |
for name in ['re_py_subsrc', 're_python_pysrc']: |
| 113 |
if name in sys.modules: |
| 114 |
del sys.modules[name] |
| 115 |
""") |
| 116 |
_clean_cache(self.space) |
| 117 |
|
| 118 |
|
| 119 |
def test_constant_1(self): |
| 120 |
self.fix_path() |
| 121 |
from re_python_pysrc import ffi |
| 122 |
assert ffi.integer_const('FOOBAR') == -42 |
| 123 |
assert ffi.integer_const('FOOBAZ') == -43 |
| 124 |
|
| 125 |
def test_large_constant(self): |
| 126 |
self.fix_path() |
| 127 |
from re_python_pysrc import ffi |
| 128 |
assert ffi.integer_const('BIGPOS') == 420000000000 |
| 129 |
assert ffi.integer_const('BIGNEG') == -420000000000 |
| 130 |
|
| 131 |
def test_function(self): |
| 132 |
import _cffi_backend |
| 133 |
self.fix_path() |
| 134 |
from re_python_pysrc import ffi |
| 135 |
lib = ffi.dlopen(self.extmod) |
| 136 |
assert lib.add42(-10) == 32 |
| 137 |
assert type(lib.add42) is _cffi_backend.FFI.CData |
| 138 |
|
| 139 |
def test_dlopen_unicode(self): |
| 140 |
if not getattr(self, 'extmodU', None): |
| 141 |
skip("no unicode file name") |
| 142 |
import _cffi_backend, sys |
| 143 |
sys.pypy_initfsencoding() |
| 144 |
self.fix_path() |
| 145 |
from re_python_pysrc import ffi |
| 146 |
lib = ffi.dlopen(self.extmodU) |
| 147 |
assert lib.add42(-10) == 32 |
| 148 |
|
| 149 |
def test_dlclose(self): |
| 150 |
import _cffi_backend |
| 151 |
self.fix_path() |
| 152 |
from re_python_pysrc import ffi |
| 153 |
lib = ffi.dlopen(self.extmod) |
| 154 |
ffi.dlclose(lib) |
| 155 |
e = raises(ffi.error, getattr, lib, 'add42') |
| 156 |
assert str(e.value) == ( |
| 157 |
"library '%s' has been closed" % (self.extmod,)) |
| 158 |
ffi.dlclose(lib) |
| 159 |
|
| 160 |
def test_constant_via_lib(self): |
| 161 |
self.fix_path() |
| 162 |
from re_python_pysrc import ffi |
| 163 |
lib = ffi.dlopen(self.extmod) |
| 164 |
assert lib.FOOBAR == -42 |
| 165 |
assert lib.FOOBAZ == -43 |
| 166 |
|
| 167 |
def test_opaque_struct(self): |
| 168 |
self.fix_path() |
| 169 |
from re_python_pysrc import ffi |
| 170 |
ffi.cast("struct foo_s *", 0) |
| 171 |
raises(TypeError, ffi.new, "struct foo_s *") |
| 172 |
|
| 173 |
def test_nonopaque_struct(self): |
| 174 |
self.fix_path() |
| 175 |
from re_python_pysrc import ffi |
| 176 |
for p in [ffi.new("struct bar_s *", [5, b"foobar"]), |
| 177 |
ffi.new("bar_t *", [5, b"foobar"])]: |
| 178 |
assert p.x == 5 |
| 179 |
assert p.a[0] == ord('f') |
| 180 |
assert p.a[5] == ord('r') |
| 181 |
|
| 182 |
def test_enum(self): |
| 183 |
self.fix_path() |
| 184 |
from re_python_pysrc import ffi |
| 185 |
assert ffi.integer_const("BB") == 1 |
| 186 |
e = ffi.cast("enum foo_e", 2) |
| 187 |
assert ffi.string(e) == "CC" |
| 188 |
|
| 189 |
def test_include_1(self): |
| 190 |
self.fix_path() |
| 191 |
from re_py_subsrc import ffi |
| 192 |
assert ffi.integer_const('FOOBAR') == -42 |
| 193 |
assert ffi.integer_const('FOOBAZ') == -43 |
| 194 |
assert ffi.integer_const('k2') == 121212 |
| 195 |
lib = ffi.dlopen(self.extmod) |
| 196 |
assert lib.FOOBAR == -42 |
| 197 |
assert lib.FOOBAZ == -43 |
| 198 |
assert lib.k2 == 121212 |
| 199 |
|
| 200 |
p = ffi.new("bar_t *", [5, b"foobar"]) |
| 201 |
assert p.a[4] == ord('a') |
| 202 |
|
| 203 |
def test_global_var(self): |
| 204 |
self.fix_path() |
| 205 |
from re_python_pysrc import ffi |
| 206 |
lib = ffi.dlopen(self.extmod) |
| 207 |
assert lib.globalvar42 == 1234 |
| 208 |
p = ffi.addressof(lib, 'globalvar42') |
| 209 |
lib.globalvar42 += 5 |
| 210 |
assert p[0] == 1239 |
| 211 |
p[0] -= 1 |
| 212 |
assert lib.globalvar42 == 1238 |
| 213 |
|
| 214 |
def test_global_const_int(self): |
| 215 |
self.fix_path() |
| 216 |
from re_python_pysrc import ffi |
| 217 |
lib = ffi.dlopen(self.extmod) |
| 218 |
assert lib.globalconst42 == 4321 |
| 219 |
raises(AttributeError, ffi.addressof, lib, 'globalconst42') |
| 220 |
|
| 221 |
def test_global_const_nonint(self): |
| 222 |
self.fix_path() |
| 223 |
from re_python_pysrc import ffi |
| 224 |
lib = ffi.dlopen(self.extmod) |
| 225 |
assert ffi.string(lib.globalconsthello, 8) == b"hello" |
| 226 |
raises(AttributeError, ffi.addressof, lib, 'globalconsthello') |
| 227 |
|
| 228 |
def test_rtld_constants(self): |
| 229 |
self.fix_path() |
| 230 |
from re_python_pysrc import ffi |
| 231 |
ffi.RTLD_NOW |
| 232 |
ffi.RTLD_LAZY |
| 233 |
ffi.RTLD_GLOBAL |
| 234 |
|
| 235 |
def test_no_such_function_or_global_var(self): |
| 236 |
self.fix_path() |
| 237 |
from re_python_pysrc import ffi |
| 238 |
lib = ffi.dlopen(self.extmod) |
| 239 |
e = raises(ffi.error, getattr, lib, 'no_such_function') |
| 240 |
assert str(e.value).startswith( |
| 241 |
"symbol 'no_such_function' not found in library '") |
| 242 |
e = raises(ffi.error, getattr, lib, 'no_such_globalvar') |
| 243 |
assert str(e.value).startswith( |
| 244 |
"symbol 'no_such_globalvar' not found in library '") |
| 245 |
|
| 246 |
def test_check_version(self): |
| 247 |
import _cffi_backend |
| 248 |
e = raises(ImportError, _cffi_backend.FFI, |
| 249 |
"foobar", _version=0x2594) |
| 250 |
assert str(e.value).startswith( |
| 251 |
"cffi out-of-line Python module 'foobar' has unknown version") |
| 252 |
|
| 253 |
def test_selfref(self): |
| 254 |
|
| 255 |
self.fix_path() |
| 256 |
from re_python_pysrc import ffi |
| 257 |
ffi.new("selfref_ptr_t") |
| 258 |
|
| 259 |
@py.test.mark.skipif('WIN32', reason='uses "dl" explicitly') |
| 260 |
def test_dlopen_handle(self): |
| 261 |
import _cffi_backend, sys |
| 262 |
self.fix_path() |
| 263 |
from re_python_pysrc import ffi |
| 264 |
lib1 = ffi.dlopen(self.dl_libpath) |
| 265 |
handle = lib1.dlopen(self.extmod.encode(sys.getfilesystemencoding()), |
| 266 |
_cffi_backend.RTLD_LAZY) |
| 267 |
assert ffi.typeof(handle) == ffi.typeof("void *") |
| 268 |
assert handle |
| 269 |
|
| 270 |
lib = ffi.dlopen(handle) |
| 271 |
assert lib.add42(-10) == 32 |
| 272 |
assert type(lib.add42) is _cffi_backend.FFI.CData |
| 273 |
|
| 274 |
err = lib1.dlclose(handle) |
| 275 |
assert err == 0 |
| 276 |
|
| 277 |
def test_rec_structs_1(self): |
| 278 |
self.fix_path() |
| 279 |
from re_python_pysrc import ffi |
| 280 |
sz = ffi.sizeof("struct B") |
| 281 |
assert sz == ffi.sizeof("int *") |
| 282 |
|
| 283 |
def test_rec_structs_2(self): |
| 284 |
self.fix_path() |
| 285 |
from re_python_pysrc import ffi |
| 286 |
sz = ffi.sizeof("struct C") |
| 287 |
assert sz == ffi.sizeof("int *") |
| 288 |
|