Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: cleanup-test_lib_pypy
Changeset: r95439:9bf67667765c
Date: 2018-12-08 06:38 +0000
http://bitbucket.org/pypy/pypy/changeset/9bf67667765c/

Log:    Use a fixture to inject the test dll; random cleanups

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/conftest.py 
b/pypy/module/test_lib_pypy/ctypes_tests/conftest.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/conftest.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/conftest.py
@@ -96,8 +96,11 @@
 
     return c_compile([cfile], str(udir / '_ctypes_test'), libraries=libraries)
 
-# we need to run after the "tmpdir" plugin which installs pytest.ensuretemp
-@pytest.mark.trylast
-def pytest_configure(config):
-    global sofile
-    sofile = compile_so_file()
+@pytest.fixture(scope='session')
+def sofile():
+    return str(compile_so_file())
+
+@pytest.fixture
+def dll(sofile):
+    from ctypes import CDLL
+    return CDLL(str(sofile))
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
@@ -145,11 +145,7 @@
         assert res == 1111
         assert rect.left == -1000   # must not have been changed!
 
-    def test_callback_from_c_with_struct_argument(self):
-        import conftest
-        _ctypes_test = str(conftest.sofile)
-        dll = CDLL(_ctypes_test)
-
+    def test_callback_from_c_with_struct_argument(self, dll):
         class RECT(Structure):
             _fields_ = [("left", c_long), ("top", c_long),
                         ("right", c_long), ("bottom", c_long)]
@@ -177,11 +173,7 @@
             proto(lambda r: 0)
 
 
-    def test_qsort(self):
-        import conftest
-        _ctypes_test = str(conftest.sofile)
-        dll = CDLL(_ctypes_test)
-
+    def test_qsort(self, dll):
         PI = POINTER(c_int)
         A = c_int*5
         a = A()
@@ -205,11 +197,7 @@
 
         assert res == [1,2,3,4,5]
 
-    def test_pyobject_as_opaque(self):
-        import conftest
-        _ctypes_test = str(conftest.sofile)
-        dll = CDLL(_ctypes_test)
-
+    def test_pyobject_as_opaque(self, dll):
         def callback(arg):
             return arg()
 
@@ -220,11 +208,7 @@
         res = cfunc(CTP(callback), lambda : 3)
         assert res == 3
 
-    def test_callback_void(self, capsys):
-        import conftest
-        _ctypes_test = str(conftest.sofile)
-        dll = CDLL(_ctypes_test)
-
+    def test_callback_void(self, capsys, dll):
         def callback():
             pass
 
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
@@ -2,15 +2,11 @@
 import sys, py
 from .support import BaseCTypesTestChecker
 
-def setup_module(mod):
-    import conftest
-    mod.lib = CDLL(str(conftest.sofile))
-
 class TestCast(BaseCTypesTestChecker):
 
-    def test_cast_functype(self):
+    def test_cast_functype(self, dll):
         # make sure we can cast function type
-        my_sqrt = lib.my_sqrt
+        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
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py
deleted file mode 100644
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py
+++ /dev/null
@@ -1,37 +0,0 @@
-import pytest
-import sys
-
-from ctypes import *
-
-def setup_module(mod):
-    import conftest
-    mod.dll = CDLL(str(conftest.sofile))
-
-class CHECKED(c_int):
-    def _check_retval_(value):
-        # Receives a CHECKED instance.
-        return str(value.value)
-    _check_retval_ = staticmethod(_check_retval_)
-
-class TestRetval:
-
-    def test_checkretval(self):
-        assert 42 == dll._testfunc_p_p(42)
-
-        dll._testfunc_p_p.restype = CHECKED
-        assert "42" == dll._testfunc_p_p(42)
-
-        dll._testfunc_p_p.restype = None
-        assert None == dll._testfunc_p_p(42)
-
-        del dll._testfunc_p_p.restype
-        assert 42 == dll._testfunc_p_p(42)
-
-    try:
-        oledll
-    except NameError:
-        pass
-    else:
-        def test_oledll(self):
-            with pytest.raises(WindowsError):
-                oledll.oleaut32.CreateTypeLib2(0, 0, 0)
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
@@ -1,12 +1,7 @@
-import py
+import pytest
 
 import ctypes
-from _ctypes import function
-
-try:
-    import _rawffi
-except ImportError:
-    py.test.skip("app-level test only for PyPy")
+_rawffi = pytest.importorskip('_rawffi')  # PyPy-only
 
 class TestErrno:
 
@@ -15,7 +10,7 @@
             assert _rawffi.get_errno() == 42
             assert ctypes.get_errno() == old
         check.free_temp_buffers = lambda *args: None
-        f = function.CFuncPtr()
+        f = ctypes._CFuncPtr()
         old = _rawffi.get_errno()
         f._flags_ = _rawffi.FUNCFLAG_USE_ERRNO
         ctypes.set_errno(42)
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
@@ -188,10 +188,7 @@
         x = (c_int * 42)()
         assert sizeof(x) == 42 * sizeof(c_int)
 
-    def test_convert_pointers(self):
-        import conftest
-        _ctypes_test = str(conftest.sofile)
-        dll = CDLL(_ctypes_test)
+    def test_convert_pointers(self, dll):
         func = dll._testfunc_p_p
         func.restype = c_char_p
 
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,12 +1,6 @@
 from ctypes import *
 
-def setup_module(mod):
-    import conftest
-    _ctypes_test = str(conftest.sofile)
-    mod.lib = CDLL(_ctypes_test)
-
-
 class TestCFuncPtr:
-    def test_restype(self):
-        foo = lib.my_unused_function
+    def test_restype(self, 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
@@ -1,32 +1,22 @@
 from ctypes import *
 import sys
 import pytest
-from .support import BaseCTypesTestChecker
 
-try:
-    WINFUNCTYPE
-except NameError:
-    # fake to enable this test on Linux
-    WINFUNCTYPE = CFUNCTYPE
+@pytest.fixture
+def dll(sofile):
+    return CDLL(str(sofile), use_errno=True)
 
 
-def setup_module(mod):
-    import conftest
-    _ctypes_test = str(conftest.sofile)
-    mod.dll = CDLL(_ctypes_test, use_errno=True)
-    if sys.platform == "win32":
-        mod.windll = WinDLL(_ctypes_test)
+class TestFunctions(object):
 
-class TestFunctions(BaseCTypesTestChecker):
-
-    def test_char_result(self):
+    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(self):
+    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
@@ -35,14 +25,14 @@
         true_result = f(1, 0, 0, 0, 0, 0)
         assert true_result is True
 
-    def test_unicode_function_name(self):
+    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(self):
+    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
@@ -50,8 +40,7 @@
         result = f(x, x, x, x, 0, 0)
         assert result == -8
 
-
-    def test_convert_pointers(self):
+    def test_convert_pointers(self, dll):
         f = dll.deref_LP_c_char_p
         f.restype = c_char
         f.argtypes = [POINTER(c_char_p)]
@@ -63,15 +52,14 @@
 
     ################################################################
 
-
-    def test_call_some_args(self):
+    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(self, monkeypatch):
+    def test_keepalive_buffers(self, monkeypatch, dll):
         import gc
         f = dll.my_strchr
         f.argtypes = [c_char_p]
@@ -88,7 +76,7 @@
         result = f("abcd", ord("b"))
         assert result == "bcd"
 
-    def test_caching_bug_1(self):
+    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
@@ -100,7 +88,7 @@
         result = f("abcd", ord("b"), 42)
         assert result == "bcd"
 
-    def test_argument_conversion_and_checks(self):
+    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]
@@ -113,7 +101,7 @@
         with pytest.raises(ArgumentError):
             strlen(False, 0)
 
-    def test_union_as_passed_value(self):
+    def test_union_as_passed_value(self, dll):
         class UN(Union):
             _fields_ = [("x", c_short),
                         ("y", c_long)]
@@ -123,9 +111,9 @@
         a = A()
         a[1].x = 33
         u = dll.ret_un_func(a[1])
-        assert u.y == 33*10000
+        assert u.y == 33 * 10000
 
-    def test_cache_funcptr(self):
+    def test_cache_funcptr(self, dll):
         tf_b = dll.tf_b
         tf_b.restype = c_byte
         tf_b.argtypes = (c_byte,)
@@ -135,7 +123,7 @@
         assert tf_b(-126) == -42
         assert tf_b._ptr is ptr
 
-    def test_custom_from_param(self):
+    def test_custom_from_param(self, dll):
         class A(c_byte):
             @classmethod
             def from_param(cls, obj):
@@ -150,7 +138,7 @@
         assert seen == ["yadda"]
 
     @pytest.mark.xfail(reason="warnings are disabled")
-    def test_warnings(self):
+    def test_warnings(self, dll):
         import warnings
         warnings.simplefilter("always")
         with warnings.catch_warnings(record=True) as w:
@@ -160,7 +148,8 @@
             assert "C function without declared arguments called" in 
str(w[0].message)
 
     @pytest.mark.xfail
-    def test_errcheck(self):
+    def test_errcheck(self, dll):
+        import warnings
         def errcheck(result, func, args):
             assert result == -42
             assert type(result) is int
@@ -189,8 +178,7 @@
 
         warnings.resetwarnings()
 
-
-    def test_errno(self):
+    def test_errno(self, dll):
         test_errno = dll.test_errno
         test_errno.restype = c_int
         set_errno(42)
@@ -200,7 +188,7 @@
         set_errno(0)
         assert get_errno() == 0
 
-    def test_issue1655(self):
+    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
@@ -208,7 +196,7 @@
                 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 [obj[i] for i in range(args[icount].value)]
             return sz_array_p
 
         get_data_prototype = CFUNCTYPE(POINTER(c_int),
@@ -216,7 +204,7 @@
         get_data_paramflag = ((1,), (2,))
         get_data_signature = ('test_issue1655', dll)
 
-        get_data = get_data_prototype( get_data_signature, get_data_paramflag )
+        get_data = get_data_prototype(get_data_signature, get_data_paramflag)
         assert get_data('testing!') == 4
 
         get_data.errcheck = ret_list_p(1)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_guess_argtypes.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_guess_argtypes.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_guess_argtypes.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_guess_argtypes.py
@@ -28,14 +28,12 @@
         pass
     s = Stuff()
     s._as_parameter_ = None
-    
+
     assert guess(s) == c_void_p
 
-def test_guess_unicode():
+def test_guess_unicode(dll):
     if not hasattr(sys, 'pypy_translation_info') and sys.platform != 'win32':
         py.test.skip("CPython segfaults: see http://bugs.python.org/issue5203";)
-    import conftest
-    dll = CDLL(str(conftest.sofile))
     wcslen = dll.my_wcslen
     text = u"Some long unicode string"
     assert wcslen(text) == len(text)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_loading.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_loading.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_loading.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_loading.py
@@ -1,9 +1,5 @@
-import pytest
-from ctypes import *
-import sys
-import os, StringIO
+from ctypes import CDLL
 from ctypes.util import find_library
-from ctypes.test import is_resource_enabled
 
 class TestLoader:
 
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_numbers.py
@@ -1,26 +1,6 @@
 import pytest
 from ctypes import *
 from .support import BaseCTypesTestChecker
-import sys, struct
-
-def valid_ranges(*types):
-    # given a sequence of numeric types, collect their _type_
-    # attribute, which is a single format character compatible with
-    # the struct module, use the struct module to calculate the
-    # minimum and maximum value allowed for this format.
-    # Returns a list of (min, max) values.
-    result = []
-    for t in types:
-        fmt = t._type_
-        size = struct.calcsize(fmt)
-        a = struct.unpack(fmt, ("\x00"*32)[:size])[0]
-        b = struct.unpack(fmt, ("\xFF"*32)[:size])[0]
-        c = struct.unpack(fmt, ("\x7F"+"\x00"*32)[:size])[0]
-        d = struct.unpack(fmt, ("\x80"+"\xFF"*32)[:size])[0]
-        result.append((min(a, b, c, d), max(a, b, c, d)))
-    return result
-
-ArgType = type(byref(c_int(0)))
 
 unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
 signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
@@ -36,9 +16,6 @@
     unsigned_types.append(c_ulonglong)
     signed_types.append(c_longlong)
 
-unsigned_ranges = valid_ranges(*unsigned_types)
-signed_ranges = valid_ranges(*signed_types)
-
 ################################################################
 
 class TestNumber(BaseCTypesTestChecker):
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_parameters.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_parameters.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_parameters.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_parameters.py
@@ -1,6 +1,3 @@
-import pytest
-import sys
-
 class TestSimpleTypes:
 
     def test_pointer_subclasses(self):
@@ -15,13 +12,10 @@
 
         assert Void_pp.from_param(o) is o
 
-    def test_multiple_signature(self):
+    def test_multiple_signature(self, dll):
         # when .argtypes is not set, calling a function with a certain
         # set of parameters should not prevent another call with
         # another set.
-        from ctypes import CDLL, byref
-        import conftest
-        dll = CDLL(str(conftest.sofile))
         func = dll._testfunc_p_p
 
         # This is call has too many arguments
@@ -29,4 +23,3 @@
 
         # This one is normal
         assert func(None) == 0
-
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_pointers.py
@@ -7,10 +7,6 @@
 python_types = [int, int, int, int, int, long,
                 int, long, long, long, float, float]
 
-def setup_module(mod):
-    import conftest
-    mod._ctypes_test = str(conftest.sofile)
-
 class TestPointers(BaseCTypesTestChecker):
 
     def test_get_ffi_argtype(self):
@@ -36,8 +32,7 @@
         assert p2.contents.contents.value == 42
         assert p1.contents.value == 42
 
-    def test_c_char_p_byref(self):
-        dll = CDLL(_ctypes_test)
+    def test_c_char_p_byref(self, dll):
         TwoOutArgs = dll.TwoOutArgs
         TwoOutArgs.restype = None
         TwoOutArgs.argtypes = [c_int, c_void_p, c_int, c_void_p]
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_prototypes.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_prototypes.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_prototypes.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_prototypes.py
@@ -2,20 +2,15 @@
 from ctypes import *
 from .support import BaseCTypesTestChecker
 
-def setup_module(mod):
-    import conftest
-    _ctypes_test = str(conftest.sofile)
-    mod.testdll = CDLL(_ctypes_test)
-
 class TestFuncPrototypes(BaseCTypesTestChecker):
 
-    def test_restype_setattr(self):
-        func = testdll._testfunc_p_p
+    def test_restype_setattr(self, dll):
+        func = dll._testfunc_p_p
         with pytest.raises(TypeError):
             setattr(func, 'restype', 20)
 
-    def test_argtypes_setattr(self):
-        func = testdll._testfunc_p_p
+    def test_argtypes_setattr(self, dll):
+        func = dll._testfunc_p_p
         with pytest.raises(TypeError):
             setattr(func, 'argtypes', 20)
         with pytest.raises(TypeError):
@@ -34,10 +29,10 @@
             setattr(func, 'paramflags', ((1,), ('a',)))
         func.paramflags = (1,), (1|4,)
 
-    def test_kwargs(self):
+    def test_kwargs(self, dll):
         proto = CFUNCTYPE(c_char_p, c_char_p, c_int)
         paramflags = (1, 'text', "tavino"), (1, 'letter', ord('v'))
-        func = proto(('my_strchr', testdll), paramflags)
+        func = proto(('my_strchr', dll), paramflags)
         assert func.argtypes == (c_char_p, c_int)
         assert func.restype == c_char_p
 
@@ -63,9 +58,9 @@
         assert result == "possible"
 
 class TestArray(BaseCTypesTestChecker):
-    def test_array_to_ptr_wrongtype(self):
+    def test_array_to_ptr_wrongtype(self, dll):
         ARRAY = c_byte * 8
-        func = testdll._testfunc_ai8
+        func = dll._testfunc_ai8
         func.restype = POINTER(c_int)
         func.argtypes = [c_int * 8]
         array = ARRAY(1, 2, 3, 4, 5, 6, 7, 8)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_unions.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_unions.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_unions.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_unions.py
@@ -28,4 +28,3 @@
         u = UnionofStuff()
         u.one.x = 3
         assert u.two.x == 3
-
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_values.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_values.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_values.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_values.py
@@ -5,15 +5,10 @@
 from ctypes import *
 from .support import BaseCTypesTestChecker
 
-def setup_module(mod):
-    import conftest
-    _ctypes_test = str(conftest.sofile)
-    mod.ctdll = CDLL(_ctypes_test)
-
 class TestValues(BaseCTypesTestChecker):
 
-    def test_a_string(self):
-        a_string = (c_char * 16).in_dll(ctdll, "a_string")
+    def test_a_string(self, dll):
+        a_string = (c_char * 16).in_dll(dll, "a_string")
         assert a_string.raw == "0123456789abcdef"
         a_string[15] = '$'
-        assert ctdll.get_a_string_char(15) == ord('$')
+        assert dll.get_a_string_char(15) == ord('$')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to