Author: Ronan Lamy <ronan.l...@gmail.com> Branch: cleanup-test_lib_pypy Changeset: r95452:007da6e72200 Date: 2018-12-11 20:46 +0000 http://bitbucket.org/pypy/pypy/changeset/007da6e72200/
Log: Convert some test methods to functions diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_anon.py b/pypy/module/test_lib_pypy/ctypes_tests/test_anon.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_anon.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_anon.py @@ -1,56 +1,54 @@ import pytest from ctypes import * -from .support import BaseCTypesTestChecker -class TestAnon(BaseCTypesTestChecker): - def test_nested(self): - class ANON_S(Structure): - _fields_ = [("a", c_int)] +def test_nested(): + class ANON_S(Structure): + _fields_ = [("a", c_int)] - class ANON_U(Union): - _fields_ = [("_", ANON_S), - ("b", c_int)] - _anonymous_ = ["_"] + class ANON_U(Union): + _fields_ = [("_", ANON_S), + ("b", c_int)] + _anonymous_ = ["_"] - class Y(Structure): - _fields_ = [("x", c_int), - ("_", ANON_U), - ("y", c_int)] - _anonymous_ = ["_"] + class Y(Structure): + _fields_ = [("x", c_int), + ("_", ANON_U), + ("y", c_int)] + _anonymous_ = ["_"] - assert Y.x.offset == 0 - assert Y.a.offset == sizeof(c_int) - assert Y.b.offset == sizeof(c_int) - assert Y._.offset == sizeof(c_int) - assert Y.y.offset == sizeof(c_int) * 2 + assert Y.x.offset == 0 + assert Y.a.offset == sizeof(c_int) + assert Y.b.offset == sizeof(c_int) + assert Y._.offset == sizeof(c_int) + assert Y.y.offset == sizeof(c_int) * 2 - assert Y._names_ == ['x', 'a', 'b', 'y'] + assert Y._names_ == ['x', 'a', 'b', 'y'] - def test_anonymous_fields_on_instance(self): - # this is about the *instance-level* access of anonymous fields, - # which you'd guess is the most common, but used not to work - # (issue #2230) +def test_anonymous_fields_on_instance(): + # this is about the *instance-level* access of anonymous fields, + # which you'd guess is the most common, but used not to work + # (issue #2230) - class B(Structure): - _fields_ = [("x", c_int), ("y", c_int), ("z", c_int)] - class A(Structure): - _anonymous_ = ["b"] - _fields_ = [("b", B)] + class B(Structure): + _fields_ = [("x", c_int), ("y", c_int), ("z", c_int)] + class A(Structure): + _anonymous_ = ["b"] + _fields_ = [("b", B)] - a = A() - a.x = 5 - assert a.x == 5 - assert a.b.x == 5 - a.b.x += 1 - assert a.x == 6 + a = A() + a.x = 5 + assert a.x == 5 + assert a.b.x == 5 + a.b.x += 1 + assert a.x == 6 - class C(Structure): - _anonymous_ = ["a"] - _fields_ = [("v", c_int), ("a", A)] + class C(Structure): + _anonymous_ = ["a"] + _fields_ = [("v", c_int), ("a", A)] - c = C() - c.v = 3 - c.y = -8 - assert c.v == 3 - assert c.y == c.a.y == c.a.b.y == -8 - assert not hasattr(c, 'b') + c = C() + c.v = 3 + c.y = -8 + assert c.v == 3 + assert c.y == c.a.y == c.a.b.y == -8 + assert not hasattr(c, 'b') diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_array.py b/pypy/module/test_lib_pypy/ctypes_tests/test_array.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_array.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_array.py @@ -1,67 +1,64 @@ import pytest from ctypes import * -from .support import BaseCTypesTestChecker -class TestArray(BaseCTypesTestChecker): - def test_slice(self): - values = range(5) - numarray = c_int * 5 +def test_slice(): + values = range(5) + numarray = c_int * 5 - na = numarray(*(c_int(x) for x in values)) + na = numarray(*(c_int(x) for x in values)) - assert list(na[0:0]) == [] - assert list(na[:]) == values - assert list(na[:10]) == values + assert list(na[0:0]) == [] + assert list(na[:]) == values + assert list(na[:10]) == values - def test_init_again(self): - sz = (c_char * 3)() - addr1 = addressof(sz) - sz.__init__(*"foo") - addr2 = addressof(sz) - assert addr1 == addr2 +def test_init_again(): + sz = (c_char * 3)() + addr1 = addressof(sz) + sz.__init__(*"foo") + addr2 = addressof(sz) + assert addr1 == addr2 -class TestSophisticatedThings(BaseCTypesTestChecker): - def test_array_of_structures(self): - class X(Structure): - _fields_ = [('x', c_int), ('y', c_int)] +def test_array_of_structures(): + class X(Structure): + _fields_ = [('x', c_int), ('y', c_int)] - Y = X * 2 - y = Y() - x = X() - x.y = 3 - y[1] = x - assert y[1].y == 3 + Y = X * 2 + y = Y() + x = X() + x.y = 3 + y[1] = x + assert y[1].y == 3 - def test_output_simple(self): - A = c_char * 10 - TP = POINTER(A) - x = TP(A()) - assert x[0] != '' +def test_output_simple(): + A = c_char * 10 + TP = POINTER(A) + x = TP(A()) + assert x[0] != '' - A = c_wchar * 10 - TP = POINTER(A) - x = TP(A()) - assert x[0] != '' + A = c_wchar * 10 + TP = POINTER(A) + x = TP(A()) + assert x[0] != '' - def test_output_simple_array(self): - A = c_char * 10 - AA = A * 10 - aa = AA() - assert aa[0] != '' +def test_output_simple_array(): + A = c_char * 10 + AA = A * 10 + aa = AA() + assert aa[0] != '' - def test_output_complex_test(self): - class Car(Structure): - _fields_ = [("brand", c_char * 10), - ("speed", c_float), - ("owner", c_char * 10)] +def test_output_complex_test(): + class Car(Structure): + _fields_ = [("brand", c_char * 10), + ("speed", c_float), + ("owner", c_char * 10)] - assert isinstance(Car("abcdefghi", 42.0, "12345").brand, bytes) - assert Car("abcdefghi", 42.0, "12345").brand == "abcdefghi" - assert Car("abcdefghio", 42.0, "12345").brand == "abcdefghio" - with pytest.raises(ValueError): - Car("abcdefghiop", 42.0, "12345") + assert isinstance(Car("abcdefghi", 42.0, "12345").brand, bytes) + assert Car("abcdefghi", 42.0, "12345").brand == "abcdefghi" + assert Car("abcdefghio", 42.0, "12345").brand == "abcdefghio" + with pytest.raises(ValueError): + Car("abcdefghiop", 42.0, "12345") - A = Car._fields_[2][1] - TP = POINTER(A) - x = TP(A()) - assert x[0] != '' + A = Car._fields_[2][1] + TP = POINTER(A) + x = TP(A()) + assert x[0] != '' diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_base.py b/pypy/module/test_lib_pypy/ctypes_tests/test_base.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_base.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_base.py @@ -1,26 +1,23 @@ -from .support import WhiteBoxTests - from ctypes import * # WhiteBoxTests -class TestCTypesBase(WhiteBoxTests): - def test_pointer(self): - p = pointer(pointer(c_int(2))) - x = p[0] - assert x._base is p +def test_pointer(): + p = pointer(pointer(c_int(2))) + x = p[0] + assert x._base is p - def test_structure(self): - class X(Structure): - _fields_ = [('x', POINTER(c_int)), - ('y', POINTER(c_int))] +def test_structure(): + class X(Structure): + _fields_ = [('x', POINTER(c_int)), + ('y', POINTER(c_int))] - x = X() - assert x.y._base is x - assert x.y._index == 1 + x = X() + assert x.y._base is x + assert x.y._index == 1 - def test_array(self): - X = POINTER(c_int) * 24 - x = X() - assert x[16]._base is x - assert x[16]._index == 16 +def test_array(): + X = POINTER(c_int) * 24 + x = X() + assert x[16]._base is x + assert x[16]._index == 16 diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py b/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_bitfields.py @@ -2,21 +2,18 @@ from ctypes import * -class TestBitField: - def test_set_fields_attr(self): - class A(Structure): - pass - A._fields_ = [("a", c_byte), - ("b", c_ubyte)] +def test_set_fields_attr(): + class A(Structure): + pass + A._fields_ = [("a", c_byte), ("b", c_ubyte)] - def test_set_fields_attr_bitfields(self): - class A(Structure): - pass - A._fields_ = [("a", POINTER(A)), - ("b", c_ubyte, 4)] +def test_set_fields_attr_bitfields(): + class A(Structure): + pass + A._fields_ = [("a", POINTER(A)), ("b", c_ubyte, 4)] - def test_set_fields_cycle_fails(self): - class A(Structure): - pass - with pytest.raises(AttributeError): - A._fields_ = [("a", A)] +def test_set_fields_cycle_fails(): + class A(Structure): + pass + with pytest.raises(AttributeError): + A._fields_ = [("a", A)] diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py b/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py @@ -1,41 +1,38 @@ from ctypes import * -from .support import BaseCTypesTestChecker -class TestStringBuffer(BaseCTypesTestChecker): +def test_buffer(): + b = create_string_buffer(32) + assert len(b) == 32 + assert sizeof(b) == 32 * sizeof(c_char) + assert type(b[0]) is str - def test_buffer(self): - b = create_string_buffer(32) - assert len(b) == 32 - assert sizeof(b) == 32 * sizeof(c_char) - assert type(b[0]) is str + b = create_string_buffer(33L) + assert len(b) == 33 + assert sizeof(b) == 33 * sizeof(c_char) + assert type(b[0]) is str - b = create_string_buffer(33L) - assert len(b) == 33 - assert sizeof(b) == 33 * sizeof(c_char) - assert type(b[0]) is str + b = create_string_buffer("abc") + assert len(b) == 4 # trailing nul char + assert sizeof(b) == 4 * sizeof(c_char) + assert type(b[0]) is str + assert b[0] == "a" + assert b[:] == "abc\0" - b = create_string_buffer("abc") - assert len(b) == 4 # trailing nul char - assert sizeof(b) == 4 * sizeof(c_char) - assert type(b[0]) is str - assert b[0] == "a" - assert b[:] == "abc\0" +def test_from_buffer(): + b1 = bytearray("abcde") + b = (c_char * 5).from_buffer(b1) + assert b[2] == "c" + # + b1 = bytearray("abcd") + b = c_int.from_buffer(b1) + assert b.value in (1684234849, # little endian + 1633837924) # big endian - def test_from_buffer(self): - b1 = bytearray("abcde") - b = (c_char * 5).from_buffer(b1) - assert b[2] == "c" - # - b1 = bytearray("abcd") - b = c_int.from_buffer(b1) - assert b.value in (1684234849, # little endian - 1633837924) # big endian - - def test_from_buffer_keepalive(self): - # Issue #2878 - b1 = bytearray("ab") - array = (c_uint16 * 32)() - array[6] = c_uint16.from_buffer(b1) - # this is also what we get on CPython. I don't think it makes - # sense because the array contains just a copy of the number. - assert array._objects == {'6': b1} +def test_from_buffer_keepalive(): + # Issue #2878 + b1 = bytearray("ab") + array = (c_uint16 * 32)() + array[6] = c_uint16.from_buffer(b1) + # this is also what we get on CPython. I don't think it makes + # sense because the array contains just a copy of the number. + assert array._objects == {'6': b1} diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py b/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_callbacks.py @@ -5,10 +5,6 @@ class TestCallbacks(BaseCTypesTestChecker): functype = CFUNCTYPE -## def tearDown(self): -## import gc -## gc.collect() - def callback(self, *args): self.got_args = args return args[-1] diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_cast.py b/pypy/module/test_lib_pypy/ctypes_tests/test_cast.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_cast.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_cast.py @@ -1,31 +1,30 @@ +import pytest + from ctypes import * -import sys, py -from .support import BaseCTypesTestChecker -class TestCast(BaseCTypesTestChecker): +def test_cast_functype(dll): + # make sure we can cast function type + my_sqrt = dll.my_sqrt + saved_objects = my_sqrt._objects.copy() + sqrt = cast(cast(my_sqrt, c_void_p), CFUNCTYPE(c_double, c_double)) + assert sqrt(4.0) == 2.0 + assert not cast(0, CFUNCTYPE(c_int)) + # + assert sqrt._objects is my_sqrt._objects # on CPython too + my_sqrt._objects.clear() + my_sqrt._objects.update(saved_objects) - def test_cast_functype(self, dll): - # make sure we can cast function type - my_sqrt = dll.my_sqrt - saved_objects = my_sqrt._objects.copy() - sqrt = cast(cast(my_sqrt, c_void_p), CFUNCTYPE(c_double, c_double)) - assert sqrt(4.0) == 2.0 - assert not cast(0, CFUNCTYPE(c_int)) - # - assert sqrt._objects is my_sqrt._objects # on CPython too - my_sqrt._objects.clear() - my_sqrt._objects.update(saved_objects) +def test_cast_argumenterror(): + param = c_uint(42) + with pytest.raises(ArgumentError): + cast(param, c_void_p) - def test_cast_argumenterror(self): - param = c_uint(42) - py.test.raises(ArgumentError, "cast(param, c_void_p)") - - def test_c_bool(self): - x = c_bool(42) - assert x.value is True - x = c_bool(0.0) - assert x.value is False - x = c_bool("") - assert x.value is False - x = c_bool(['yadda']) - assert x.value is True +def test_c_bool(): + x = c_bool(42) + assert x.value is True + x = c_bool(0.0) + assert x.value is False + x = c_bool("") + assert x.value is False + x = c_bool(['yadda']) + assert x.value is True diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_errno.py b/pypy/module/test_lib_pypy/ctypes_tests/test_errno.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_errno.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_errno.py @@ -3,19 +3,17 @@ import ctypes _rawffi = pytest.importorskip('_rawffi') # PyPy-only -class TestErrno: +def test_errno_saved_and_restored(): + def check(): + assert _rawffi.get_errno() == 42 + assert ctypes.get_errno() == old + check.free_temp_buffers = lambda *args: None + f = ctypes._CFuncPtr() + old = _rawffi.get_errno() + f._flags_ = _rawffi.FUNCFLAG_USE_ERRNO + ctypes.set_errno(42) + f._call_funcptr(check) + assert _rawffi.get_errno() == old + ctypes.set_errno(0) - def test_errno_saved_and_restored(self): - def check(): - assert _rawffi.get_errno() == 42 - assert ctypes.get_errno() == old - check.free_temp_buffers = lambda *args: None - f = ctypes._CFuncPtr() - old = _rawffi.get_errno() - f._flags_ = _rawffi.FUNCFLAG_USE_ERRNO - ctypes.set_errno(42) - f._call_funcptr(check) - assert _rawffi.get_errno() == old - ctypes.set_errno(0) - - # see also test_functions.test_errno +# see also test_functions.test_errno diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_extra.py b/pypy/module/test_lib_pypy/ctypes_tests/test_extra.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_extra.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_extra.py @@ -5,241 +5,239 @@ import py from ctypes import * -from .support import BaseCTypesTestChecker -class TestExtra(BaseCTypesTestChecker): - def test_primitive_pointer(self): - x = c_int(5) - assert x.value == 5 - x.value = 6 - assert x.value == 6 +def test_primitive_pointer(): + x = c_int(5) + assert x.value == 5 + x.value = 6 + assert x.value == 6 - p = pointer(x) # p ---> x = 6 - assert isinstance(p.contents, c_int) - p.contents.value += 1 - assert x.value == 7 # p ---> x = 7 + p = pointer(x) # p ---> x = 6 + assert isinstance(p.contents, c_int) + p.contents.value += 1 + assert x.value == 7 # p ---> x = 7 - y = c_int(12) - p.contents = y # p ---> y = 12 - p.contents.value += 2 # p ---> y = 14 - assert y.value == 14 - assert x.value == 7 + y = c_int(12) + p.contents = y # p ---> y = 12 + p.contents.value += 2 # p ---> y = 14 + assert y.value == 14 + assert x.value == 7 - pp = pointer(p) # pp ---> p ---> y = 14 - pp.contents.contents = x # pp ---> p ---> x = 7 - p.contents.value += 2 # pp ---> p ---> x = 9 - assert x.value == 9 + pp = pointer(p) # pp ---> p ---> y = 14 + pp.contents.contents = x # pp ---> p ---> x = 7 + p.contents.value += 2 # pp ---> p ---> x = 9 + assert x.value == 9 - assert isinstance(p[0], int) - p[0] += 1 # pp ---> p ---> x = 10 - assert x.value == 10 - z = c_int(86) - p[0] = z # pp ---> p ---> x = 86 (not z!) - assert x.value == 86 - z.value = 84 - assert x.value == 86 + assert isinstance(p[0], int) + p[0] += 1 # pp ---> p ---> x = 10 + assert x.value == 10 + z = c_int(86) + p[0] = z # pp ---> p ---> x = 86 (not z!) + assert x.value == 86 + z.value = 84 + assert x.value == 86 - assert isinstance(pp[0], POINTER(c_int)) - assert pp[0].contents.value == x.value == 86 - pp[0].contents = z # pp ---> p ---> z = 84 - assert p.contents.value == z.value == 84 + assert isinstance(pp[0], POINTER(c_int)) + assert pp[0].contents.value == x.value == 86 + pp[0].contents = z # pp ---> p ---> z = 84 + assert p.contents.value == z.value == 84 - ## *** the rest is commented out because it should work but occasionally - ## *** trigger a ctypes bug (SourceForge bug #1467852). *** - ## q = pointer(y) - ## pp[0] = q # pp ---> p ---> y = 14 - ## assert y.value == 14 # (^^^ not q! ) - ## assert p.contents.value == 14 - ## assert pp.contents.contents.value == 14 - ## q.contents = x - ## assert pp.contents.contents.value == 14 +## *** the rest is commented out because it should work but occasionally +## *** trigger a ctypes bug (SourceForge bug #1467852). *** +## q = pointer(y) +## pp[0] = q # pp ---> p ---> y = 14 +## assert y.value == 14 # (^^^ not q! ) +## assert p.contents.value == 14 +## assert pp.contents.contents.value == 14 +## q.contents = x +## assert pp.contents.contents.value == 14 - def test_char_p(self): - x = c_char_p("hello\x00world") - assert x.value == "hello" - x.value = "world" - assert x.value == "world" +def test_char_p(): + x = c_char_p("hello\x00world") + assert x.value == "hello" + x.value = "world" + assert x.value == "world" - p = pointer(x) - assert p[0] == x.value == "world" - p[0] = "other" - assert x.value == p.contents.value == p[0] == "other" + p = pointer(x) + assert p[0] == x.value == "world" + p[0] = "other" + assert x.value == p.contents.value == p[0] == "other" - myarray = (c_char_p * 10)() - myarray[7] = "hello" - assert isinstance(myarray[7], str) - assert myarray[7] == "hello" + myarray = (c_char_p * 10)() + myarray[7] = "hello" + assert isinstance(myarray[7], str) + assert myarray[7] == "hello" - def test_struct(self): - class tagpoint(Structure): - _fields_ = [('x', c_int), - ('p', POINTER(c_short))] +def test_struct(): + class tagpoint(Structure): + _fields_ = [('x', c_int), + ('p', POINTER(c_short))] - y = c_short(123) - z = c_short(-33) - s = tagpoint() - s.p.contents = z - assert s.p.contents.value == -33 - s.p = pointer(y) - assert s.p.contents.value == 123 - s.p.contents.value = 124 - assert y.value == 124 + y = c_short(123) + z = c_short(-33) + s = tagpoint() + s.p.contents = z + assert s.p.contents.value == -33 + s.p = pointer(y) + assert s.p.contents.value == 123 + s.p.contents.value = 124 + assert y.value == 124 - s = tagpoint(x=12) - assert s.x == 12 - s = tagpoint(17, p=pointer(z)) - assert s.x == 17 - assert s.p.contents.value == -33 + s = tagpoint(x=12) + assert s.x == 12 + s = tagpoint(17, p=pointer(z)) + assert s.x == 17 + assert s.p.contents.value == -33 - def test_ptr_array(self): - a = (POINTER(c_ushort) * 5)() - x = c_ushort(52) - y = c_ushort(1000) +def test_ptr_array(): + a = (POINTER(c_ushort) * 5)() + x = c_ushort(52) + y = c_ushort(1000) - a[2] = pointer(x) - assert a[2].contents.value == 52 - a[2].contents.value += 1 - assert x.value == 53 + a[2] = pointer(x) + assert a[2].contents.value == 52 + a[2].contents.value += 1 + assert x.value == 53 - a[3].contents = y - assert a[3].contents.value == 1000 - a[3].contents.value += 1 - assert y.value == 1001 + a[3].contents = y + assert a[3].contents.value == 1000 + a[3].contents.value += 1 + assert y.value == 1001 - def test_void_p(self): - x = c_int(12) - p1 = cast(pointer(x), c_void_p) - p2 = cast(p1, POINTER(c_int)) - assert p2.contents.value == 12 +def test_void_p(): + x = c_int(12) + p1 = cast(pointer(x), c_void_p) + p2 = cast(p1, POINTER(c_int)) + assert p2.contents.value == 12 - def test_char_array(self): - a = (c_char * 3)() - a[0] = 'x' - a[1] = 'y' - assert a.value == 'xy' - a[2] = 'z' - assert a.value == 'xyz' +def test_char_array(): + a = (c_char * 3)() + a[0] = 'x' + a[1] = 'y' + assert a.value == 'xy' + a[2] = 'z' + assert a.value == 'xyz' - b = create_string_buffer(3) - assert type(b) is type(a) - assert len(b) == 3 + b = create_string_buffer(3) + assert type(b) is type(a) + assert len(b) == 3 - b.value = "nxw" - assert b[0] == 'n' - assert b[1] == 'x' - assert b[2] == 'w' + b.value = "nxw" + assert b[0] == 'n' + assert b[1] == 'x' + assert b[2] == 'w' - b.value = "?" - assert b[0] == '?' - assert b[1] == '\x00' - assert b[2] == 'w' + b.value = "?" + assert b[0] == '?' + assert b[1] == '\x00' + assert b[2] == 'w' - class S(Structure): - _fields_ = [('p', POINTER(c_char))] + class S(Structure): + _fields_ = [('p', POINTER(c_char))] - s = S() - s.p = b - s.p.contents.value = '!' - assert b.value == '!' + s = S() + s.p = b + s.p.contents.value = '!' + assert b.value == '!' - assert len(create_string_buffer(0)) == 0 + assert len(create_string_buffer(0)) == 0 - def test_array(self): - a = (c_int * 10)() +def test_array(): + a = (c_int * 10)() - class S(Structure): - _fields_ = [('p', POINTER(c_int))] + class S(Structure): + _fields_ = [('p', POINTER(c_int))] - s = S() - s.p = a - s.p.contents.value = 42 - assert a[0] == 42 + s = S() + s.p = a + s.p.contents.value = 42 + assert a[0] == 42 - a = (c_int * 5)(5, 6, 7) - assert list(a) == [5, 6, 7, 0, 0] + a = (c_int * 5)(5, 6, 7) + assert list(a) == [5, 6, 7, 0, 0] - def test_truth_value(self): - p = POINTER(c_int)() - assert not p - p.contents = c_int(12) - assert p - # I can't figure out how to reset p to NULL... +def test_truth_value(): + p = POINTER(c_int)() + assert not p + p.contents = c_int(12) + assert p + # I can't figure out how to reset p to NULL... - assert c_int(12) - assert not c_int(0) # a bit strange, if you ask me - assert c_int(-1) - assert not c_byte(0) - assert not c_char('\x00') # hum - assert not c_float(0.0) - assert not c_double(0.0) - assert not c_ulonglong(0) - assert c_ulonglong(2L**42) + assert c_int(12) + assert not c_int(0) # a bit strange, if you ask me + assert c_int(-1) + assert not c_byte(0) + assert not c_char('\x00') # hum + assert not c_float(0.0) + assert not c_double(0.0) + assert not c_ulonglong(0) + assert c_ulonglong(2L**42) - assert c_char_p("hello") - assert c_char_p("") - assert not c_char_p(None) + assert c_char_p("hello") + assert c_char_p("") + assert not c_char_p(None) - assert not c_void_p() + assert not c_void_p() - def test_sizeof(self): - x = create_string_buffer(117) - assert sizeof(x) == 117 # assumes that chars are one byte each - x = (c_int * 42)() - assert sizeof(x) == 42 * sizeof(c_int) +def test_sizeof(): + x = create_string_buffer(117) + assert sizeof(x) == 117 # assumes that chars are one byte each + x = (c_int * 42)() + assert sizeof(x) == 42 * sizeof(c_int) - def test_convert_pointers(self, dll): - func = dll._testfunc_p_p - func.restype = c_char_p +def test_convert_pointers(dll): + func = dll._testfunc_p_p + func.restype = c_char_p - # automatic conversions to c_char_p - func.argtypes = [c_char_p] - assert func("hello") == "hello" - assert func(c_char_p("hello")) == "hello" - assert func((c_char * 6)(*"hello")) == "hello" - assert func(create_string_buffer("hello")) == "hello" + # automatic conversions to c_char_p + func.argtypes = [c_char_p] + assert func("hello") == "hello" + assert func(c_char_p("hello")) == "hello" + assert func((c_char * 6)(*"hello")) == "hello" + assert func(create_string_buffer("hello")) == "hello" - # automatic conversions to c_void_p - func.argtypes = [c_void_p] - assert func("hello") == "hello" - assert func(c_char_p("hello")) == "hello" - assert func((c_char * 6)(*"hello")) == "hello" - assert func((c_byte * 6)(104,101,108,108,111)) =="hello" - assert func(create_string_buffer("hello")) == "hello" + # automatic conversions to c_void_p + func.argtypes = [c_void_p] + assert func("hello") == "hello" + assert func(c_char_p("hello")) == "hello" + assert func((c_char * 6)(*"hello")) == "hello" + assert func((c_byte * 6)(104,101,108,108,111)) =="hello" + assert func(create_string_buffer("hello")) == "hello" - def test_varsize_cast(self): - import struct - N = struct.calcsize("l") - x = c_long() - p = cast(pointer(x), POINTER(c_ubyte*N)) - for i, c in enumerate(struct.pack("l", 12345678)): - p.contents[i] = ord(c) - assert x.value == 12345678 +def test_varsize_cast(): + import struct + N = struct.calcsize("l") + x = c_long() + p = cast(pointer(x), POINTER(c_ubyte*N)) + for i, c in enumerate(struct.pack("l", 12345678)): + p.contents[i] = ord(c) + assert x.value == 12345678 - def test_cfunctype_inspection(self): - T = CFUNCTYPE(c_int, c_ubyte) - # T.argtypes and T.restype don't work, must use a dummy instance - assert list(T().argtypes) == [c_ubyte] - assert T().restype == c_int +def test_cfunctype_inspection(): + T = CFUNCTYPE(c_int, c_ubyte) + # T.argtypes and T.restype don't work, must use a dummy instance + assert list(T().argtypes) == [c_ubyte] + assert T().restype == c_int - def test_from_param(self): - # other working cases of from_param - assert isinstance(c_void_p.from_param((c_int * 4)()), c_int*4) +def test_from_param(): + # other working cases of from_param + assert isinstance(c_void_p.from_param((c_int * 4)()), c_int * 4) - def test_array_mul(self): - assert c_int * 10 == 10 * c_int == c_int * 10L == 10L * c_int - py.test.raises(TypeError, 'c_int * c_int') - py.test.raises(TypeError, 'c_int * (-1.5)') - py.test.raises(TypeError, 'c_int * "foo"') - py.test.raises(TypeError, '(-1.5) * c_int') - py.test.raises(TypeError, '"foo" * c_int') +def test_array_mul(): + assert c_int * 10 == 10 * c_int == c_int * 10L == 10L * c_int + py.test.raises(TypeError, 'c_int * c_int') + py.test.raises(TypeError, 'c_int * (-1.5)') + py.test.raises(TypeError, 'c_int * "foo"') + py.test.raises(TypeError, '(-1.5) * c_int') + py.test.raises(TypeError, '"foo" * c_int') - def test_cast_none(self): - def check(P): - x = cast(None, P) - assert isinstance(x, P) - assert not x - check(c_void_p) - check(c_char_p) - check(POINTER(c_int)) - check(POINTER(c_int * 10)) +def test_cast_none(): + def check(P): + x = cast(None, P) + assert isinstance(x, P) + assert not x + check(c_void_p) + check(c_char_p) + check(POINTER(c_int)) + check(POINTER(c_int * 10)) diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_funcptr.py b/pypy/module/test_lib_pypy/ctypes_tests/test_funcptr.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_funcptr.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_funcptr.py @@ -1,6 +1,5 @@ from ctypes import * -class TestCFuncPtr: - def test_restype(self, dll): - foo = dll.my_unused_function - assert foo.restype is c_int # by default +def test_restype(dll): + foo = dll.my_unused_function + assert foo.restype is c_int # by default diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py b/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py --- a/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py +++ b/pypy/module/test_lib_pypy/ctypes_tests/test_functions.py @@ -7,238 +7,236 @@ return CDLL(str(sofile), use_errno=True) -class TestFunctions(object): +def test_char_result(dll): + f = dll._testfunc_i_bhilfd + f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] + f.restype = c_char + result = f(0, 0, 0, 0, 0, 0) + assert result == '\x00' - def test_char_result(self, dll): - f = dll._testfunc_i_bhilfd - f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] - f.restype = c_char - result = f(0, 0, 0, 0, 0, 0) - assert result == '\x00' +def test_boolresult(dll): + f = dll._testfunc_i_bhilfd + f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] + f.restype = c_bool + false_result = f(0, 0, 0, 0, 0, 0) + assert false_result is False + true_result = f(1, 0, 0, 0, 0, 0) + assert true_result is True - def test_boolresult(self, dll): - f = dll._testfunc_i_bhilfd - f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] - f.restype = c_bool - false_result = f(0, 0, 0, 0, 0, 0) - assert false_result is False - true_result = f(1, 0, 0, 0, 0, 0) - assert true_result is True +def test_unicode_function_name(dll): + f = dll[u'_testfunc_i_bhilfd'] + f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] + f.restype = c_int + result = f(1, 2, 3, 4, 5.0, 6.0) + assert result == 21 - def test_unicode_function_name(self, dll): - f = dll[u'_testfunc_i_bhilfd'] - f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] - f.restype = c_int - result = f(1, 2, 3, 4, 5.0, 6.0) - assert result == 21 +def test_truncate_python_longs(dll): + f = dll._testfunc_i_bhilfd + f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] + f.restype = c_int + x = sys.maxint * 2 + result = f(x, x, x, x, 0, 0) + assert result == -8 - def test_truncate_python_longs(self, dll): - f = dll._testfunc_i_bhilfd - f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double] - f.restype = c_int - x = sys.maxint * 2 - result = f(x, x, x, x, 0, 0) - assert result == -8 +def test_convert_pointers(dll): + f = dll.deref_LP_c_char_p + f.restype = c_char + f.argtypes = [POINTER(c_char_p)] + # + s = c_char_p('hello world') + ps = pointer(s) + assert f(ps) == 'h' + assert f(s) == 'h' # automatic conversion from char** to char* - def test_convert_pointers(self, dll): - f = dll.deref_LP_c_char_p - f.restype = c_char - f.argtypes = [POINTER(c_char_p)] - # - s = c_char_p('hello world') - ps = pointer(s) - assert f(ps) == 'h' - assert f(s) == 'h' # automatic conversion from char** to char* +################################################################ - ################################################################ +def test_call_some_args(dll): + f = dll.my_strchr + f.argtypes = [c_char_p] + f.restype = c_char_p + result = f("abcd", ord("b")) + assert result == "bcd" - def test_call_some_args(self, dll): - f = dll.my_strchr - f.argtypes = [c_char_p] - f.restype = c_char_p - result = f("abcd", ord("b")) - assert result == "bcd" +def test_keepalive_buffers(monkeypatch, dll): + import gc + f = dll.my_strchr + f.argtypes = [c_char_p] + f.restype = c_char_p + # + orig__call_funcptr = f._call_funcptr + def _call_funcptr(funcptr, *newargs): + gc.collect() + gc.collect() + gc.collect() + return orig__call_funcptr(funcptr, *newargs) + monkeypatch.setattr(f, '_call_funcptr', _call_funcptr) + # + result = f("abcd", ord("b")) + assert result == "bcd" - def test_keepalive_buffers(self, monkeypatch, dll): - import gc - f = dll.my_strchr - f.argtypes = [c_char_p] - f.restype = c_char_p - # - orig__call_funcptr = f._call_funcptr - def _call_funcptr(funcptr, *newargs): - gc.collect() - gc.collect() - gc.collect() - return orig__call_funcptr(funcptr, *newargs) - monkeypatch.setattr(f, '_call_funcptr', _call_funcptr) - # - result = f("abcd", ord("b")) - assert result == "bcd" +def test_caching_bug_1(dll): + # the same test as test_call_some_args, with two extra lines + # in the middle that trigger caching in f._ptr, which then + # makes the last two lines fail + f = dll.my_strchr + f.argtypes = [c_char_p, c_int] + f.restype = c_char_p + result = f("abcd", ord("b")) + assert result == "bcd" + result = f("abcd", ord("b"), 42) + assert result == "bcd" - def test_caching_bug_1(self, dll): - # the same test as test_call_some_args, with two extra lines - # in the middle that trigger caching in f._ptr, which then - # makes the last two lines fail - f = dll.my_strchr - f.argtypes = [c_char_p, c_int] - f.restype = c_char_p - result = f("abcd", ord("b")) - assert result == "bcd" - result = f("abcd", ord("b"), 42) - assert result == "bcd" +def test_argument_conversion_and_checks(dll): + #This test is designed to check for segfaults if the wrong type of argument is passed as parameter + strlen = dll.my_strchr + strlen.argtypes = [c_char_p, c_int] + strlen.restype = c_char_p + assert strlen("eggs", ord("g")) == "ggs" - def test_argument_conversion_and_checks(self, dll): - #This test is designed to check for segfaults if the wrong type of argument is passed as parameter - strlen = dll.my_strchr - strlen.argtypes = [c_char_p, c_int] - strlen.restype = c_char_p - assert strlen("eggs", ord("g")) == "ggs" + # Should raise ArgumentError, not segfault + with pytest.raises(ArgumentError): + strlen(0, 0) + with pytest.raises(ArgumentError): + strlen(False, 0) - # Should raise ArgumentError, not segfault - with pytest.raises(ArgumentError): - strlen(0, 0) - with pytest.raises(ArgumentError): - strlen(False, 0) +def test_union_as_passed_value(dll): + class UN(Union): + _fields_ = [("x", c_short), + ("y", c_long)] + dll.ret_un_func.restype = UN + dll.ret_un_func.argtypes = [UN] + A = UN * 2 + a = A() + a[1].x = 33 + u = dll.ret_un_func(a[1]) + assert u.y == 33 * 10000 - def test_union_as_passed_value(self, dll): - class UN(Union): - _fields_ = [("x", c_short), - ("y", c_long)] - dll.ret_un_func.restype = UN - dll.ret_un_func.argtypes = [UN] - A = UN * 2 - a = A() - a[1].x = 33 - u = dll.ret_un_func(a[1]) - assert u.y == 33 * 10000 +def test_cache_funcptr(dll): + tf_b = dll.tf_b + tf_b.restype = c_byte + tf_b.argtypes = (c_byte,) + assert tf_b(-126) == -42 + ptr = tf_b._ptr + assert ptr is not None + assert tf_b(-126) == -42 + assert tf_b._ptr is ptr - def test_cache_funcptr(self, dll): - tf_b = dll.tf_b - tf_b.restype = c_byte - tf_b.argtypes = (c_byte,) - assert tf_b(-126) == -42 - ptr = tf_b._ptr - assert ptr is not None - assert tf_b(-126) == -42 - assert tf_b._ptr is ptr +def test_custom_from_param(dll): + class A(c_byte): + @classmethod + def from_param(cls, obj): + seen.append(obj) + return -126 + tf_b = dll.tf_b + tf_b.restype = c_byte + tf_b.argtypes = (c_byte,) + tf_b.argtypes = [A] + seen = [] + assert tf_b("yadda") == -42 + assert seen == ["yadda"] - def test_custom_from_param(self, dll): - class A(c_byte): - @classmethod - def from_param(cls, obj): - seen.append(obj) - return -126 - tf_b = dll.tf_b - tf_b.restype = c_byte - tf_b.argtypes = (c_byte,) - tf_b.argtypes = [A] - seen = [] - assert tf_b("yadda") == -42 - assert seen == ["yadda"] +@pytest.mark.xfail(reason="warnings are disabled") +def test_warnings(dll): + import warnings + warnings.simplefilter("always") + with warnings.catch_warnings(record=True) as w: + dll.get_an_integer() + assert len(w) == 1 + assert issubclass(w[0].category, RuntimeWarning) + assert "C function without declared arguments called" in str(w[0].message) - @pytest.mark.xfail(reason="warnings are disabled") - def test_warnings(self, dll): - import warnings - warnings.simplefilter("always") - with warnings.catch_warnings(record=True) as w: - dll.get_an_integer() - assert len(w) == 1 - assert issubclass(w[0].category, RuntimeWarning) - assert "C function without declared arguments called" in str(w[0].message) +@pytest.mark.xfail +def test_errcheck(dll): + import warnings + def errcheck(result, func, args): + assert result == -42 + assert type(result) is int + arg, = args + assert arg == -126 + assert type(arg) is int + return result + # + tf_b = dll.tf_b + tf_b.restype = c_byte + tf_b.argtypes = (c_byte,) + tf_b.errcheck = errcheck + assert tf_b(-126) == -42 + del tf_b.errcheck + with warnings.catch_warnings(record=True) as w: + dll.get_an_integer.argtypes = [] + dll.get_an_integer() + assert len(w) == 1 + assert issubclass(w[0].category, RuntimeWarning) + assert "C function without declared return type called" in str(w[0].message) - @pytest.mark.xfail - def test_errcheck(self, dll): - import warnings - def errcheck(result, func, args): - assert result == -42 - assert type(result) is int - arg, = args - assert arg == -126 - assert type(arg) is int - return result - # - tf_b = dll.tf_b - tf_b.restype = c_byte - tf_b.argtypes = (c_byte,) - tf_b.errcheck = errcheck - assert tf_b(-126) == -42 - del tf_b.errcheck - with warnings.catch_warnings(record=True) as w: - dll.get_an_integer.argtypes = [] - dll.get_an_integer() - assert len(w) == 1 - assert issubclass(w[0].category, RuntimeWarning) - assert "C function without declared return type called" in str(w[0].message) + with warnings.catch_warnings(record=True) as w: + dll.get_an_integer.restype = None + dll.get_an_integer() + assert len(w) == 0 - with warnings.catch_warnings(record=True) as w: - dll.get_an_integer.restype = None - dll.get_an_integer() - assert len(w) == 0 + warnings.resetwarnings() - warnings.resetwarnings() +def test_errno(dll): + test_errno = dll.test_errno + test_errno.restype = c_int + set_errno(42) + res = test_errno() + n = get_errno() + assert (res, n) == (42, 43) + set_errno(0) + assert get_errno() == 0 - def test_errno(self, dll): - test_errno = dll.test_errno - test_errno.restype = c_int - set_errno(42) - res = test_errno() - n = get_errno() - assert (res, n) == (42, 43) - set_errno(0) - assert get_errno() == 0 +def test_issue1655(dll): + def ret_list_p(icount): + def sz_array_p(obj, func, args): + assert ('.LP_c_int object' in repr(obj) or + '.LP_c_long object' in repr(obj)) + assert repr(args) in ("('testing!', c_int(4))", + "('testing!', c_long(4))") + assert args[icount].value == 4 + return [obj[i] for i in range(args[icount].value)] + return sz_array_p - def test_issue1655(self, dll): - def ret_list_p(icount): - def sz_array_p(obj, func, args): - assert ('.LP_c_int object' in repr(obj) or - '.LP_c_long object' in repr(obj)) - assert repr(args) in ("('testing!', c_int(4))", - "('testing!', c_long(4))") - assert args[icount].value == 4 - return [obj[i] for i in range(args[icount].value)] - return sz_array_p + get_data_prototype = CFUNCTYPE(POINTER(c_int), + c_char_p, POINTER(c_int)) + get_data_paramflag = ((1,), (2,)) + get_data_signature = ('test_issue1655', dll) - get_data_prototype = CFUNCTYPE(POINTER(c_int), - c_char_p, POINTER(c_int)) - get_data_paramflag = ((1,), (2,)) - get_data_signature = ('test_issue1655', dll) + get_data = get_data_prototype(get_data_signature, get_data_paramflag) + assert get_data('testing!') == 4 - get_data = get_data_prototype(get_data_signature, get_data_paramflag) - assert get_data('testing!') == 4 + get_data.errcheck = ret_list_p(1) + assert get_data('testing!') == [-1, -2, -3, -4] - get_data.errcheck = ret_list_p(1) - assert get_data('testing!') == [-1, -2, -3, -4] +def test_issue2533(tmpdir): + import cffi + ffi = cffi.FFI() + ffi.cdef("int **fetchme(void);") + ffi.set_source("_x_cffi", """ + int **fetchme(void) + { + static int a = 42; + static int *pa = &a; + return &pa; + } + """) + ffi.compile(verbose=True, tmpdir=str(tmpdir)) - def test_issue2533(self, tmpdir): - import cffi - ffi = cffi.FFI() - ffi.cdef("int **fetchme(void);") - ffi.set_source("_x_cffi", """ - int **fetchme(void) - { - static int a = 42; - static int *pa = &a; - return &pa; - } - """) - ffi.compile(verbose=True, tmpdir=str(tmpdir)) + import sys + sys.path.insert(0, str(tmpdir)) + try: + from _x_cffi import ffi, lib + finally: + sys.path.pop(0) + fetchme = ffi.addressof(lib, 'fetchme') + fetchme = int(ffi.cast("intptr_t", fetchme)) - import sys - sys.path.insert(0, str(tmpdir)) - try: - from _x_cffi import ffi, lib - finally: - sys.path.pop(0) - fetchme = ffi.addressof(lib, 'fetchme') - fetchme = int(ffi.cast("intptr_t", fetchme)) + FN = CFUNCTYPE(POINTER(POINTER(c_int))) + ff = cast(fetchme, FN) - FN = CFUNCTYPE(POINTER(POINTER(c_int))) - ff = cast(fetchme, FN) + g = ff() + assert g.contents.contents.value == 42 - g = ff() - assert g.contents.contents.value == 42 - - h = c_int(43) - g[0] = pointer(h) # used to crash here - assert g.contents.contents.value == 43 + h = c_int(43) + g[0] = pointer(h) # used to crash here + assert g.contents.contents.value == 43 _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit