| 1 |
|
| 2 |
import sys, re |
| 3 |
import pytest |
| 4 |
from cffi import FFI, FFIError, CDefError, VerificationError |
| 5 |
from .backend_tests import needs_dlopen_none |
| 6 |
from extra_tests.cffi_tests.support import is_musl |
| 7 |
|
| 8 |
|
| 9 |
class FakeBackend(object): |
| 10 |
|
| 11 |
def nonstandard_integer_types(self): |
| 12 |
return {} |
| 13 |
|
| 14 |
def sizeof(self, name): |
| 15 |
return 1 |
| 16 |
|
| 17 |
def load_library(self, name, flags): |
| 18 |
if sys.platform == 'win32': |
| 19 |
assert name is None or "msvcr" in name |
| 20 |
else: |
| 21 |
assert name is None or "libc" in name or "libm" in name |
| 22 |
return FakeLibrary() |
| 23 |
|
| 24 |
def new_function_type(self, args, result, has_varargs): |
| 25 |
args = [arg.cdecl for arg in args] |
| 26 |
result = result.cdecl |
| 27 |
return FakeType( |
| 28 |
'<func (%s), %s, %s>' % (', '.join(args), result, has_varargs)) |
| 29 |
|
| 30 |
def new_primitive_type(self, name): |
| 31 |
assert name == name.lower() |
| 32 |
return FakeType('<%s>' % name) |
| 33 |
|
| 34 |
def new_pointer_type(self, itemtype): |
| 35 |
return FakeType('<pointer to %s>' % (itemtype,)) |
| 36 |
|
| 37 |
def new_struct_type(self, name): |
| 38 |
return FakeStruct(name) |
| 39 |
|
| 40 |
def complete_struct_or_union(self, s, fields, tp=None, |
| 41 |
totalsize=-1, totalalignment=-1, sflags=0): |
| 42 |
assert isinstance(s, FakeStruct) |
| 43 |
s.fields = fields |
| 44 |
|
| 45 |
def new_array_type(self, ptrtype, length): |
| 46 |
return FakeType('<array %s x %s>' % (ptrtype, length)) |
| 47 |
|
| 48 |
def new_void_type(self): |
| 49 |
return FakeType("<void>") |
| 50 |
def cast(self, x, y): |
| 51 |
return 'casted!' |
| 52 |
def _get_types(self): |
| 53 |
return "CData", "CType" |
| 54 |
|
| 55 |
buffer = "buffer type" |
| 56 |
|
| 57 |
class FakeType(object): |
| 58 |
def __init__(self, cdecl): |
| 59 |
self.cdecl = cdecl |
| 60 |
def __str__(self): |
| 61 |
return self.cdecl |
| 62 |
|
| 63 |
class FakeStruct(object): |
| 64 |
def __init__(self, name): |
| 65 |
self.name = name |
| 66 |
def __str__(self): |
| 67 |
return ', '.join([str(y) + str(x) for x, y, z in self.fields]) |
| 68 |
|
| 69 |
class FakeLibrary(object): |
| 70 |
|
| 71 |
def load_function(self, BType, name): |
| 72 |
return FakeFunction(BType, name) |
| 73 |
|
| 74 |
class FakeFunction(object): |
| 75 |
|
| 76 |
def __init__(self, BType, name): |
| 77 |
self.BType = str(BType) |
| 78 |
self.name = name |
| 79 |
|
| 80 |
lib_m = "m" |
| 81 |
if sys.platform == 'win32': |
| 82 |
|
| 83 |
import distutils.ccompiler |
| 84 |
if distutils.ccompiler.get_default_compiler() == 'msvc': |
| 85 |
lib_m = 'msvcrt' |
| 86 |
elif is_musl: |
| 87 |
lib_m = 'c' |
| 88 |
|
| 89 |
|
| 90 |
def test_simple(): |
| 91 |
ffi = FFI(backend=FakeBackend()) |
| 92 |
ffi.cdef("double sin(double x);") |
| 93 |
m = ffi.dlopen(lib_m) |
| 94 |
func = m.sin |
| 95 |
assert func.name == 'sin' |
| 96 |
assert func.BType == '<func (<double>), <double>, False>' |
| 97 |
|
| 98 |
def test_pipe(): |
| 99 |
ffi = FFI(backend=FakeBackend()) |
| 100 |
ffi.cdef("int pipe(int pipefd[2]);") |
| 101 |
needs_dlopen_none() |
| 102 |
C = ffi.dlopen(None) |
| 103 |
func = C.pipe |
| 104 |
assert func.name == 'pipe' |
| 105 |
assert func.BType == '<func (<pointer to <int>>), <int>, False>' |
| 106 |
|
| 107 |
def test_vararg(): |
| 108 |
ffi = FFI(backend=FakeBackend()) |
| 109 |
ffi.cdef("short foo(int, ...);") |
| 110 |
needs_dlopen_none() |
| 111 |
C = ffi.dlopen(None) |
| 112 |
func = C.foo |
| 113 |
assert func.name == 'foo' |
| 114 |
assert func.BType == '<func (<int>), <short>, True>' |
| 115 |
|
| 116 |
def test_no_args(): |
| 117 |
ffi = FFI(backend=FakeBackend()) |
| 118 |
ffi.cdef(""" |
| 119 |
int foo(void); |
| 120 |
""") |
| 121 |
needs_dlopen_none() |
| 122 |
C = ffi.dlopen(None) |
| 123 |
assert C.foo.BType == '<func (), <int>, False>' |
| 124 |
|
| 125 |
def test_typedef(): |
| 126 |
ffi = FFI(backend=FakeBackend()) |
| 127 |
ffi.cdef(""" |
| 128 |
typedef unsigned int UInt; |
| 129 |
typedef UInt UIntReally; |
| 130 |
UInt foo(void); |
| 131 |
""") |
| 132 |
needs_dlopen_none() |
| 133 |
C = ffi.dlopen(None) |
| 134 |
assert str(ffi.typeof("UIntReally")) == '<unsigned int>' |
| 135 |
assert C.foo.BType == '<func (), <unsigned int>, False>' |
| 136 |
|
| 137 |
def test_typedef_more_complex(): |
| 138 |
ffi = FFI(backend=FakeBackend()) |
| 139 |
ffi.cdef(""" |
| 140 |
typedef struct { int a, b; } foo_t, *foo_p; |
| 141 |
int foo(foo_p[]); |
| 142 |
""") |
| 143 |
needs_dlopen_none() |
| 144 |
C = ffi.dlopen(None) |
| 145 |
assert str(ffi.typeof("foo_t")) == '<int>a, <int>b' |
| 146 |
assert str(ffi.typeof("foo_p")) == '<pointer to <int>a, <int>b>' |
| 147 |
assert C.foo.BType == ('<func (<pointer to <pointer to ' |
| 148 |
'<int>a, <int>b>>), <int>, False>') |
| 149 |
|
| 150 |
def test_typedef_array_convert_array_to_pointer(): |
| 151 |
ffi = FFI(backend=FakeBackend()) |
| 152 |
ffi.cdef(""" |
| 153 |
typedef int (*fn_t)(int[5]); |
| 154 |
""") |
| 155 |
with ffi._lock: |
| 156 |
type = ffi._parser.parse_type("fn_t") |
| 157 |
BType = ffi._get_cached_btype(type) |
| 158 |
assert str(BType) == '<func (<pointer to <int>>), <int>, False>' |
| 159 |
|
| 160 |
def test_remove_comments(): |
| 161 |
ffi = FFI(backend=FakeBackend()) |
| 162 |
ffi.cdef(""" |
| 163 |
double /*comment here*/ sin // blah blah |
| 164 |
/* multi- |
| 165 |
line- |
| 166 |
//comment */ ( |
| 167 |
// foo |
| 168 |
double // bar /* <- ignored, because it's in a comment itself |
| 169 |
x, double/*several*//*comment*/y) /*on the same line*/ |
| 170 |
; |
| 171 |
""") |
| 172 |
m = ffi.dlopen(lib_m) |
| 173 |
func = m.sin |
| 174 |
assert func.name == 'sin' |
| 175 |
assert func.BType == '<func (<double>, <double>), <double>, False>' |
| 176 |
|
| 177 |
def test_remove_line_continuation_comments(): |
| 178 |
ffi = FFI(backend=FakeBackend()) |
| 179 |
ffi.cdef(""" |
| 180 |
double // blah \\ |
| 181 |
more comments |
| 182 |
x(void); |
| 183 |
double // blah // blah\\\\ |
| 184 |
y(void); |
| 185 |
double // blah\\ \ |
| 186 |
etc |
| 187 |
z(void); |
| 188 |
""") |
| 189 |
m = ffi.dlopen(lib_m) |
| 190 |
m.x |
| 191 |
m.y |
| 192 |
m.z |
| 193 |
|
| 194 |
def test_dont_remove_comment_in_line_directives(): |
| 195 |
ffi = FFI(backend=FakeBackend()) |
| 196 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 197 |
\t # \t line \t 8 \t "baz.c" \t |
| 198 |
|
| 199 |
some syntax error here |
| 200 |
""") |
| 201 |
assert str(e.value).startswith("parse error\nbaz.c:9:") |
| 202 |
|
| 203 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 204 |
#line 7 "foo//bar.c" |
| 205 |
|
| 206 |
some syntax error here |
| 207 |
""") |
| 208 |
|
| 209 |
assert str(e.value).startswith("parse error\nfoo//bar.c:8:") |
| 210 |
ffi = FFI(backend=FakeBackend()) |
| 211 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 212 |
\t # \t 8 \t "baz.c" \t |
| 213 |
|
| 214 |
some syntax error here |
| 215 |
""") |
| 216 |
assert str(e.value).startswith("parse error\nbaz.c:9:") |
| 217 |
|
| 218 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 219 |
# 7 "foo//bar.c" |
| 220 |
|
| 221 |
some syntax error here |
| 222 |
""") |
| 223 |
assert str(e.value).startswith("parse error\nfoo//bar.c:8:") |
| 224 |
|
| 225 |
def test_multiple_line_directives(): |
| 226 |
ffi = FFI(backend=FakeBackend()) |
| 227 |
e = pytest.raises(CDefError, ffi.cdef, |
| 228 |
""" #line 5 "foo.c" |
| 229 |
extern int xx; |
| 230 |
#line 6 "bar.c" |
| 231 |
extern int yy; |
| 232 |
#line 7 "baz.c" |
| 233 |
some syntax error here |
| 234 |
#line 8 "yadda.c" |
| 235 |
extern int zz; |
| 236 |
""") |
| 237 |
assert str(e.value).startswith("parse error\nbaz.c:7:") |
| 238 |
|
| 239 |
e = pytest.raises(CDefError, ffi.cdef, |
| 240 |
""" # 5 "foo.c" |
| 241 |
extern int xx; |
| 242 |
# 6 "bar.c" |
| 243 |
extern int yy; |
| 244 |
# 7 "baz.c" |
| 245 |
some syntax error here |
| 246 |
# 8 "yadda.c" |
| 247 |
extern int zz; |
| 248 |
""") |
| 249 |
assert str(e.value).startswith("parse error\nbaz.c:7:") |
| 250 |
|
| 251 |
def test_commented_line_directive(): |
| 252 |
ffi = FFI(backend=FakeBackend()) |
| 253 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 254 |
/* |
| 255 |
#line 5 "foo.c" |
| 256 |
*/ |
| 257 |
void xx(void); |
| 258 |
|
| 259 |
#line 6 "bar.c" |
| 260 |
/* |
| 261 |
#line 35 "foo.c" |
| 262 |
*/ |
| 263 |
some syntax error |
| 264 |
""") |
| 265 |
|
| 266 |
assert str(e.value).startswith("parse error\nbar.c:9:") |
| 267 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 268 |
/* |
| 269 |
# 5 "foo.c" |
| 270 |
*/ |
| 271 |
void xx(void); |
| 272 |
|
| 273 |
# 6 "bar.c" |
| 274 |
/* |
| 275 |
# 35 "foo.c" |
| 276 |
*/ |
| 277 |
some syntax error |
| 278 |
""") |
| 279 |
assert str(e.value).startswith("parse error\nbar.c:9:") |
| 280 |
|
| 281 |
def test_line_continuation_in_defines(): |
| 282 |
ffi = FFI(backend=FakeBackend()) |
| 283 |
ffi.cdef(""" |
| 284 |
#define ABC\\ |
| 285 |
42 |
| 286 |
#define BCD \\ |
| 287 |
43 |
| 288 |
""") |
| 289 |
m = ffi.dlopen(lib_m) |
| 290 |
assert m.ABC == 42 |
| 291 |
assert m.BCD == 43 |
| 292 |
|
| 293 |
def test_define_not_supported_for_now(): |
| 294 |
ffi = FFI(backend=FakeBackend()) |
| 295 |
e = pytest.raises(CDefError, ffi.cdef, '#define FOO "blah"') |
| 296 |
assert str(e.value) == ( |
| 297 |
'only supports one of the following syntax:\n' |
| 298 |
' #define FOO ... (literally dot-dot-dot)\n' |
| 299 |
' #define FOO NUMBER (with NUMBER an integer' |
| 300 |
' constant, decimal/hex/octal)\n' |
| 301 |
'got:\n' |
| 302 |
' #define FOO "blah"') |
| 303 |
|
| 304 |
def test_unnamed_struct(): |
| 305 |
ffi = FFI(backend=FakeBackend()) |
| 306 |
ffi.cdef("typedef struct { int x; } foo_t;\n" |
| 307 |
"typedef struct { int y; } *bar_p;\n") |
| 308 |
assert 'typedef foo_t' in ffi._parser._declarations |
| 309 |
assert 'typedef bar_p' in ffi._parser._declarations |
| 310 |
assert 'anonymous foo_t' in ffi._parser._declarations |
| 311 |
type_foo = ffi._parser.parse_type("foo_t") |
| 312 |
type_bar = ffi._parser.parse_type("bar_p").totype |
| 313 |
assert repr(type_foo) == "<foo_t>" |
| 314 |
assert repr(type_bar) == "<struct $1>" |
| 315 |
pytest.raises(VerificationError, type_bar.get_c_name) |
| 316 |
assert type_foo.get_c_name() == "foo_t" |
| 317 |
|
| 318 |
def test_override(): |
| 319 |
ffi = FFI(backend=FakeBackend()) |
| 320 |
needs_dlopen_none() |
| 321 |
C = ffi.dlopen(None) |
| 322 |
ffi.cdef("int foo(void);") |
| 323 |
pytest.raises(FFIError, ffi.cdef, "long foo(void);") |
| 324 |
assert C.foo.BType == '<func (), <int>, False>' |
| 325 |
ffi.cdef("long foo(void);", override=True) |
| 326 |
assert C.foo.BType == '<func (), <long>, False>' |
| 327 |
|
| 328 |
def test_cannot_have_only_variadic_part(): |
| 329 |
|
| 330 |
ffi = FFI() |
| 331 |
e = pytest.raises(CDefError, ffi.cdef, "int foo(...);") |
| 332 |
assert str(e.value) == ( |
| 333 |
"<cdef source string>:1: foo: a function with only '(...)' " |
| 334 |
"as argument is not correct C") |
| 335 |
|
| 336 |
def test_parse_error(): |
| 337 |
ffi = FFI() |
| 338 |
e = pytest.raises(CDefError, ffi.cdef, " x y z ") |
| 339 |
assert str(e.value).startswith( |
| 340 |
'cannot parse "x y z"\n<cdef source string>:1:') |
| 341 |
e = pytest.raises(CDefError, ffi.cdef, "\n\n\n x y z ") |
| 342 |
assert str(e.value).startswith( |
| 343 |
'cannot parse "x y z"\n<cdef source string>:4:') |
| 344 |
|
| 345 |
def test_error_custom_lineno(): |
| 346 |
ffi = FFI() |
| 347 |
e = pytest.raises(CDefError, ffi.cdef, """ |
| 348 |
# 42 "foobar" |
| 349 |
|
| 350 |
a b c d |
| 351 |
""") |
| 352 |
assert str(e.value).startswith('parse error\nfoobar:43:') |
| 353 |
|
| 354 |
def test_cannot_declare_enum_later(): |
| 355 |
ffi = FFI() |
| 356 |
e = pytest.raises(NotImplementedError, ffi.cdef, |
| 357 |
"typedef enum foo_e foo_t; enum foo_e { AA, BB };") |
| 358 |
assert str(e.value) == ( |
| 359 |
"enum foo_e: the '{}' declaration should appear on the " |
| 360 |
"first time the enum is mentioned, not later") |
| 361 |
|
| 362 |
def test_unknown_name(): |
| 363 |
ffi = FFI() |
| 364 |
e = pytest.raises(CDefError, ffi.cast, "foobarbazunknown", 0) |
| 365 |
assert str(e.value) == "unknown identifier 'foobarbazunknown'" |
| 366 |
e = pytest.raises(CDefError, ffi.cast, "foobarbazunknown*", 0) |
| 367 |
assert str(e.value).startswith('cannot parse "foobarbazunknown*"') |
| 368 |
e = pytest.raises(CDefError, ffi.cast, "int(*)(foobarbazunknown)", 0) |
| 369 |
assert 'foobarbazunknown' in str(e.value) |
| 370 |
|
| 371 |
def test_redefine_common_type(): |
| 372 |
prefix = "" if sys.version_info < (3,) else "b" |
| 373 |
ffi = FFI() |
| 374 |
ffi.cdef("typedef char FILE;") |
| 375 |
assert repr(ffi.cast("FILE", 123)) == "<cdata 'char' %s'{'>" % prefix |
| 376 |
ffi.cdef("typedef char int32_t;") |
| 377 |
assert repr(ffi.cast("int32_t", 123)) == "<cdata 'char' %s'{'>" % prefix |
| 378 |
ffi = FFI() |
| 379 |
ffi.cdef("typedef int bool, *FILE;") |
| 380 |
assert repr(ffi.cast("bool", 123)) == "<cdata 'int' 123>" |
| 381 |
assert re.match(r"<cdata 'int [*]' 0[xX]?0*7[bB]>", |
| 382 |
repr(ffi.cast("FILE", 123))) |
| 383 |
ffi = FFI() |
| 384 |
ffi.cdef("typedef bool (*fn_t)(bool, bool);") |
| 385 |
|
| 386 |
def test_bool(): |
| 387 |
ffi = FFI() |
| 388 |
ffi.cdef("void f(bool);") |
| 389 |
|
| 390 |
ffi = FFI() |
| 391 |
ffi.cdef("typedef _Bool bool; void f(bool);") |
| 392 |
|
| 393 |
def test_unknown_argument_type(): |
| 394 |
ffi = FFI() |
| 395 |
e = pytest.raises(CDefError, ffi.cdef, "void f(foobarbazzz);") |
| 396 |
assert str(e.value) == ("<cdef source string>:1: f arg 1:" |
| 397 |
" unknown type 'foobarbazzz' (if you meant" |
| 398 |
" to use the old C syntax of giving untyped" |
| 399 |
" arguments, it is not supported)") |
| 400 |
|
| 401 |
def test_void_renamed_as_only_arg(): |
| 402 |
ffi = FFI() |
| 403 |
ffi.cdef("typedef void void_t1;" |
| 404 |
"typedef void_t1 void_t;" |
| 405 |
"typedef int (*func_t)(void_t);") |
| 406 |
assert ffi.typeof("func_t").args == () |
| 407 |
|
| 408 |
def test_WPARAM_on_windows(): |
| 409 |
if sys.platform != 'win32': |
| 410 |
pytest.skip("Only for Windows") |
| 411 |
ffi = FFI() |
| 412 |
ffi.cdef("void f(WPARAM);") |
| 413 |
|
| 414 |
|
| 415 |
ffi = FFI() |
| 416 |
value = int(ffi.cast("WPARAM", -42)) |
| 417 |
assert value == sys.maxsize * 2 - 40 |
| 418 |
|
| 419 |
def test__is_constant_globalvar(): |
| 420 |
import warnings |
| 421 |
for input, expected_output in [ |
| 422 |
("int a;", False), |
| 423 |
("const int a;", True), |
| 424 |
("int *a;", False), |
| 425 |
("const int *a;", False), |
| 426 |
("int const *a;", False), |
| 427 |
("int *const a;", True), |
| 428 |
("int a[5];", False), |
| 429 |
("const int a[5];", False), |
| 430 |
("int *a[5];", False), |
| 431 |
("const int *a[5];", False), |
| 432 |
("int const *a[5];", False), |
| 433 |
("int *const a[5];", False), |
| 434 |
("int a[5][6];", False), |
| 435 |
("const int a[5][6];", False), |
| 436 |
]: |
| 437 |
ffi = FFI() |
| 438 |
with warnings.catch_warnings(record=True) as log: |
| 439 |
warnings.simplefilter("always") |
| 440 |
ffi.cdef(input) |
| 441 |
declarations = ffi._parser._declarations |
| 442 |
assert ('constant a' in declarations) == expected_output |
| 443 |
assert ('variable a' in declarations) == (not expected_output) |
| 444 |
assert len(log) == (1 - expected_output) |
| 445 |
|
| 446 |
def test_restrict(): |
| 447 |
from cffi import model |
| 448 |
for input, expected_output in [ |
| 449 |
("int a;", False), |
| 450 |
("restrict int a;", True), |
| 451 |
("int *a;", False), |
| 452 |
]: |
| 453 |
ffi = FFI() |
| 454 |
ffi.cdef("extern " + input) |
| 455 |
tp, quals = ffi._parser._declarations['variable a'] |
| 456 |
assert bool(quals & model.Q_RESTRICT) == expected_output |
| 457 |
|
| 458 |
def test_different_const_funcptr_types(): |
| 459 |
lst = [] |
| 460 |
for input in [ |
| 461 |
"int(*)(int *a)", |
| 462 |
"int(*)(int const *a)", |
| 463 |
"int(*)(int * const a)", |
| 464 |
"int(*)(int const a[])"]: |
| 465 |
ffi = FFI(backend=FakeBackend()) |
| 466 |
lst.append(ffi._parser.parse_type(input)) |
| 467 |
assert lst[0] != lst[1] |
| 468 |
assert lst[0] == lst[2] |
| 469 |
assert lst[1] == lst[3] |
| 470 |
|
| 471 |
def test_const_pointer_to_pointer(): |
| 472 |
from cffi import model |
| 473 |
ffi = FFI(backend=FakeBackend()) |
| 474 |
|
| 475 |
tp, qual = ffi._parser.parse_type_and_quals("char * * (* const)") |
| 476 |
assert (str(tp), qual) == ("<char * * *>", model.Q_CONST) |
| 477 |
tp, qual = ffi._parser.parse_type_and_quals("char * (* const (*))") |
| 478 |
assert (str(tp), qual) == ("<char * * const *>", 0) |
| 479 |
tp, qual = ffi._parser.parse_type_and_quals("char (* const (* (*)))") |
| 480 |
assert (str(tp), qual) == ("<char * const * *>", 0) |
| 481 |
tp, qual = ffi._parser.parse_type_and_quals("char const * * *") |
| 482 |
assert (str(tp), qual) == ("<char const * * *>", 0) |
| 483 |
tp, qual = ffi._parser.parse_type_and_quals("const char * * *") |
| 484 |
assert (str(tp), qual) == ("<char const * * *>", 0) |
| 485 |
|
| 486 |
tp, qual = ffi._parser.parse_type_and_quals("char * * * const const") |
| 487 |
assert (str(tp), qual) == ("<char * * *>", model.Q_CONST) |
| 488 |
tp, qual = ffi._parser.parse_type_and_quals("char * * volatile *") |
| 489 |
assert (str(tp), qual) == ("<char * * volatile *>", 0) |
| 490 |
tp, qual = ffi._parser.parse_type_and_quals("char * volatile restrict * *") |
| 491 |
assert (str(tp), qual) == ("<char * __restrict volatile * *>", 0) |
| 492 |
tp, qual = ffi._parser.parse_type_and_quals("char const volatile * * *") |
| 493 |
assert (str(tp), qual) == ("<char volatile const * * *>", 0) |
| 494 |
tp, qual = ffi._parser.parse_type_and_quals("const char * * *") |
| 495 |
assert (str(tp), qual) == ("<char const * * *>", 0) |
| 496 |
|
| 497 |
tp, qual = ffi._parser.parse_type_and_quals( |
| 498 |
"int(char*const*, short****const*)") |
| 499 |
assert (str(tp), qual) == ( |
| 500 |
"<int()(char * const *, short * * * * const *)>", 0) |
| 501 |
tp, qual = ffi._parser.parse_type_and_quals( |
| 502 |
"char*const*(short*const****)") |
| 503 |
assert (str(tp), qual) == ( |
| 504 |
"<char * const *()(short * const * * * *)>", 0) |
| 505 |
|
| 506 |
def test_enum(): |
| 507 |
ffi = FFI() |
| 508 |
ffi.cdef(""" |
| 509 |
enum Enum { |
| 510 |
POS = +1, |
| 511 |
TWO = 2, |
| 512 |
NIL = 0, |
| 513 |
NEG = -1, |
| 514 |
ADDSUB = (POS+TWO)-1, |
| 515 |
DIVMULINT = (3 * 3) / 2, |
| 516 |
SHIFT = (1 << 3) >> 1, |
| 517 |
BINOPS = (0x7 & 0x1) | 0x8, |
| 518 |
XOR = 0xf ^ 0xa |
| 519 |
}; |
| 520 |
""") |
| 521 |
needs_dlopen_none() |
| 522 |
C = ffi.dlopen(None) |
| 523 |
assert C.POS == 1 |
| 524 |
assert C.TWO == 2 |
| 525 |
assert C.NIL == 0 |
| 526 |
assert C.NEG == -1 |
| 527 |
assert C.ADDSUB == 2 |
| 528 |
assert C.DIVMULINT == 4 |
| 529 |
assert C.SHIFT == 4 |
| 530 |
assert C.BINOPS == 0b1001 |
| 531 |
assert C.XOR == 0b0101 |
| 532 |
|
| 533 |
def test_stdcall(): |
| 534 |
ffi = FFI() |
| 535 |
tp = ffi.typeof("int(*)(int __stdcall x(int)," |
| 536 |
" long (__cdecl*y)(void)," |
| 537 |
" short(WINAPI *z)(short))") |
| 538 |
if sys.platform == 'win32' and sys.maxsize < 2**32: |
| 539 |
stdcall = '__stdcall ' |
| 540 |
else: |
| 541 |
stdcall = '' |
| 542 |
assert str(tp) == ( |
| 543 |
"<ctype 'int(*)(int(%s*)(int), " |
| 544 |
"long(*)(), " |
| 545 |
"short(%s*)(short))'>" % (stdcall, stdcall)) |
| 546 |
|
| 547 |
def test_extern_python(): |
| 548 |
ffi = FFI() |
| 549 |
ffi.cdef(""" |
| 550 |
int bok(int, int); |
| 551 |
extern "Python" int foobar(int, int); |
| 552 |
int baz(int, int); |
| 553 |
""") |
| 554 |
assert sorted(ffi._parser._declarations) == [ |
| 555 |
'extern_python foobar', 'function baz', 'function bok'] |
| 556 |
assert (ffi._parser._declarations['function bok'] == |
| 557 |
ffi._parser._declarations['extern_python foobar'] == |
| 558 |
ffi._parser._declarations['function baz']) |
| 559 |
|
| 560 |
def test_extern_python_group(): |
| 561 |
ffi = FFI() |
| 562 |
ffi.cdef(""" |
| 563 |
int bok(int); |
| 564 |
extern "Python" {int foobar(int, int);int bzrrr(int);} |
| 565 |
int baz(int, int); |
| 566 |
""") |
| 567 |
assert sorted(ffi._parser._declarations) == [ |
| 568 |
'extern_python bzrrr', 'extern_python foobar', |
| 569 |
'function baz', 'function bok'] |
| 570 |
assert (ffi._parser._declarations['function baz'] == |
| 571 |
ffi._parser._declarations['extern_python foobar'] != |
| 572 |
ffi._parser._declarations['function bok'] == |
| 573 |
ffi._parser._declarations['extern_python bzrrr']) |
| 574 |
|
| 575 |
def test_error_invalid_syntax_for_cdef(): |
| 576 |
ffi = FFI() |
| 577 |
e = pytest.raises(CDefError, ffi.cdef, 'void foo(void) {}') |
| 578 |
assert str(e.value) == ('<cdef source string>:1: unexpected <FuncDef>: ' |
| 579 |
'this construct is valid C but not valid in cdef()') |
| 580 |
|
| 581 |
def test_unsigned_int_suffix_for_constant(): |
| 582 |
ffi = FFI() |
| 583 |
ffi.cdef("""enum e { |
| 584 |
bin_0=0b10, |
| 585 |
bin_1=0b10u, |
| 586 |
bin_2=0b10U, |
| 587 |
bin_3=0b10l, |
| 588 |
bin_4=0b10L, |
| 589 |
bin_5=0b10ll, |
| 590 |
bin_6=0b10LL, |
| 591 |
oct_0=010, |
| 592 |
oct_1=010u, |
| 593 |
oct_2=010U, |
| 594 |
oct_3=010l, |
| 595 |
oct_4=010L, |
| 596 |
oct_5=010ll, |
| 597 |
oct_6=010LL, |
| 598 |
dec_0=10, |
| 599 |
dec_1=10u, |
| 600 |
dec_2=10U, |
| 601 |
dec_3=10l, |
| 602 |
dec_4=10L, |
| 603 |
dec_5=10ll, |
| 604 |
dec_6=10LL, |
| 605 |
hex_0=0x10, |
| 606 |
hex_1=0x10u, |
| 607 |
hex_2=0x10U, |
| 608 |
hex_3=0x10l, |
| 609 |
hex_4=0x10L, |
| 610 |
hex_5=0x10ll, |
| 611 |
hex_6=0x10LL,};""") |
| 612 |
needs_dlopen_none() |
| 613 |
C = ffi.dlopen(None) |
| 614 |
for base, expected_result in (('bin', 2), ('oct', 8), ('dec', 10), ('hex', 16)): |
| 615 |
for index in range(7): |
| 616 |
assert getattr(C, '{base}_{index}'.format(base=base, index=index)) == expected_result |
| 617 |
|
| 618 |
def test_missing_newline_bug(): |
| 619 |
ffi = FFI(backend=FakeBackend()) |
| 620 |
ffi.cdef("#pragma foobar") |
| 621 |
ffi.cdef("#pragma foobar") |