diff options
Diffstat (limited to 'c/test_c.py')
-rw-r--r-- | c/test_c.py | 91 |
1 files changed, 87 insertions, 4 deletions
diff --git a/c/test_c.py b/c/test_c.py index 654584d..cde83b8 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -1,5 +1,15 @@ import py import pytest +import sys + +is_musl = False +if sys.platform == 'linux': + try: + from packaging.tags import platform_tags + is_musl = any(t.startswith('musllinux') for t in platform_tags()) + del platform_tags + except ImportError: + pass def _setup_path(): import os, sys @@ -17,7 +27,7 @@ from _cffi_backend import __version__ # ____________________________________________________________ import sys -assert __version__ == "1.15.0", ("This test_c.py file is for testing a version" +assert __version__ == "1.15.1", ("This test_c.py file is for testing a version" " of cffi that differs from the one that we" " get from 'import _cffi_backend'") if sys.version_info < (3,): @@ -93,7 +103,8 @@ def test_all_rtld_symbols(): if sys.platform.startswith("linux"): RTLD_NODELETE RTLD_NOLOAD - RTLD_DEEPBIND + if not is_musl: + RTLD_DEEPBIND def test_new_primitive_type(): py.test.raises(KeyError, new_primitive_type, "foo") @@ -1296,7 +1307,7 @@ def test_read_variable_as_unknown_length_array(): def test_write_variable(): ## FIXME: this test assumes glibc specific behavior, it's not compliant with C standard ## https://bugs.pypy.org/issue1643 - if not sys.platform.startswith("linux"): + if not sys.platform.startswith("linux") or is_musl: py.test.skip("untested") BVoidP = new_pointer_type(new_void_type()) ll = find_and_load_library('c') @@ -1331,9 +1342,11 @@ def test_callback_exception(): except ImportError: import io as cStringIO # Python 3 import linecache - def matches(istr, ipattern, ipattern38): + def matches(istr, ipattern, ipattern38, ipattern311): if sys.version_info >= (3, 8): ipattern = ipattern38 + if sys.version_info >= (3, 11): + ipattern = ipattern311 str, pattern = istr, ipattern while '$' in pattern: i = pattern.index('$') @@ -1387,6 +1400,16 @@ Traceback (most recent call last): File "$", line $, in check_value $ ValueError: 42 +""", """\ +Exception ignored from cffi callback <function$Zcb1 at 0x$>: +Traceback (most recent call last): + File "$", line $, in Zcb1 + $ + $ + File "$", line $, in check_value + $ + $ +ValueError: 42 """) sys.stderr = cStringIO.StringIO() bigvalue = 20000 @@ -1401,6 +1424,13 @@ Traceback (most recent call last): File "$", line $, in test_callback_exception $ OverflowError: integer 60000 does not fit 'short' +""", """\ +Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C: +Traceback (most recent call last): + File "$", line $, in test_callback_exception + $ + $ +OverflowError: integer 60000 does not fit 'short' """) sys.stderr = cStringIO.StringIO() bigvalue = 20000 @@ -1449,6 +1479,19 @@ Traceback (most recent call last): File "$", line $, in test_callback_exception $ TypeError: $integer$ +""", """\ +Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C: +Traceback (most recent call last): + File "$", line $, in test_callback_exception + $ + $ +OverflowError: integer 60000 does not fit 'short' +Exception ignored during handling of the above exception by 'onerror': +Traceback (most recent call last): + File "$", line $, in test_callback_exception + $ + $ +TypeError: $integer$ """) # sys.stderr = cStringIO.StringIO() @@ -1478,6 +1521,19 @@ Traceback (most recent call last): File "$", line $, in oops $ AttributeError: 'str' object has no attribute 'append$ +""", """\ +Exception ignored from cffi callback <function$Zcb1 at 0x$>, trying to convert the result back to C: +Traceback (most recent call last): + File "$", line $, in test_callback_exception + $ + $ +OverflowError: integer 60000 does not fit 'short' +Exception ignored during handling of the above exception by 'onerror': +Traceback (most recent call last): + File "$", line $, in oops + $ + $ +AttributeError: 'str' object has no attribute 'append$ """) finally: sys.stderr = orig_stderr @@ -3453,6 +3509,18 @@ def test_bitfield_as_ppc_gcc(): _test_bitfield_details(flag=SF_GCC_X86_BITFIELDS|SF_GCC_BIG_ENDIAN) +def buffer_warning(cdata): + import warnings + buf = buffer(cdata) + bytes = len(buf) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + buffer(cdata, bytes) + assert len(w) == 0 + buffer(cdata, bytes + 1) + assert len(w) <= 1 + return len(w) == 1 + def test_struct_array_no_length(): BInt = new_primitive_type("int") BIntP = new_pointer_type(BInt) @@ -3567,6 +3635,7 @@ def test_struct_array_no_length(): assert p.a[1] == 20 assert p.a[2] == 30 assert p.a[3] == 0 + assert buffer_warning(p) # # struct of struct of varsized array BStruct2 = new_struct_type("bar") @@ -3575,6 +3644,20 @@ def test_struct_array_no_length(): for i in range(2): # try to detect heap overwrites p = newp(new_pointer_type(BStruct2), [100, [200, list(range(50))]]) assert p.tail.y[49] == 49 + assert buffer_warning(p) + assert not buffer_warning(cast(new_pointer_type(BStruct2), p)) + assert not buffer_warning(cast(BIntP, p)) + +def test_more_buffer_warning(): + BChar = new_primitive_type("unsigned char") + BCharP = new_pointer_type(BChar) + BArray = new_array_type(BCharP, 10) # char[10] + p = newp(BArray) + assert buffer_warning(p) + assert not buffer_warning(cast(BCharP, p)) + p = newp(BCharP) + assert buffer_warning(p) + assert not buffer_warning(cast(BCharP, p)) def test_struct_array_no_length_explicit_position(): |