Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: 
Changeset: r93550:edd970341375
Date: 2017-12-22 15:24 +0100
http://bitbucket.org/pypy/pypy/changeset/edd970341375/

Log:    Clean up tests and don't rely on magical injection of raises() into
        globals

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,3 +1,4 @@
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 
@@ -22,18 +23,15 @@
 
     def test_anon_nonseq(self):
         # TypeError: _anonymous_ must be a sequence
-        raises(TypeError,
-                              lambda: type(Structure)("Name",
-                                                      (Structure,),
-                                                      {"_fields_": [], 
"_anonymous_": 42}))
+        with pytest.raises(TypeError):
+            type(Structure)(
+                "Name", (Structure,), {"_fields_": [], "_anonymous_": 42})
 
     def test_anon_nonmember(self):
         # AttributeError: type object 'Name' has no attribute 'x'
-        raises(AttributeError,
-                              lambda: type(Structure)("Name",
-                                                      (Structure,),
-                                                      {"_fields_": [],
-                                                       "_anonymous_": ["x"]}))
+        with pytest.raises(AttributeError):
+            type(Structure)(
+                "Name", (Structure,), {"_fields_": [], "_anonymous_": ["x"]})
 
     def test_nested(self):
         class ANON_S(Structure):
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,5 +1,4 @@
-
-import py
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 
@@ -40,7 +39,8 @@
             assert values == [0] * len(init)
 
             # Too many in itializers should be caught
-            py.test.raises(IndexError, int_array, *range(alen*2))
+            with pytest.raises(IndexError):
+                int_array(*range(alen*2))
 
         CharArray = ARRAY(c_char, 3)
 
@@ -48,7 +48,8 @@
 
         # Should this work? It doesn't:
         # CharArray("abc")
-        py.test.raises(TypeError, CharArray, "abc")
+        with pytest.raises(TypeError):
+            CharArray("abc")
 
         assert ca[0] == "a"
         assert ca[1] == "b"
@@ -61,10 +62,12 @@
 
         # slicing is now supported, but not extended slicing (3-argument)!
         from operator import getslice, delitem
-        py.test.raises(TypeError, getslice, ca, 0, 1, -1)
+        with pytest.raises(TypeError):
+            getslice(ca, 0, 1, -1)
 
         # cannot delete items
-        py.test.raises(TypeError, delitem, ca, 0)
+        with pytest.raises(TypeError):
+            delitem(ca, 0)
 
     def test_numeric_arrays(self):
 
@@ -165,7 +168,8 @@
         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"
-        raises(ValueError, Car, "abcdefghiop", 42.0, "12345")
+        with pytest.raises(ValueError):
+            Car("abcdefghiop", 42.0, "12345")
 
         A = Car._fields_[2][1]
         TP = POINTER(A)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_as_parameter.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_as_parameter.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_as_parameter.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_as_parameter.py
@@ -1,5 +1,5 @@
 from ctypes import *
-import py
+import pytest
 from support import BaseCTypesTestChecker
 
 def setup_module(mod):
@@ -104,7 +104,8 @@
         # check that the prototype works: we call f with wrong
         # argument types
         cb = AnotherCallback(callback)
-        raises(ArgumentError, f, self.wrap(-10), self.wrap(cb))
+        with pytest.raises(ArgumentError):
+            f(self.wrap(-10), self.wrap(cb))
 
     def test_callbacks_2(self):
         # Can also use simple datatypes as argument type specifiers
@@ -213,4 +214,4 @@
     wrap = AsParamPropertyWrapper
 
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-    
+
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
@@ -1,4 +1,4 @@
-import py
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 import os
@@ -245,7 +245,5 @@
     def test_set_fields_cycle_fails(self):
         class A(Structure):
             pass
-        import pytest
-        pytest.raises(AttributeError, """
+        with pytest.raises(AttributeError):
             A._fields_ = [("a", A)]
-                      """)
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
@@ -1,5 +1,5 @@
 from ctypes import *
-import py
+import pytest
 from support import BaseCTypesTestChecker
 
 class TestCallbacks(BaseCTypesTestChecker):
@@ -22,7 +22,7 @@
             c_uint: (int, long),
             c_ulong: (int, long),
             }
-        
+
         PROTO = self.functype.im_func(typ, typ)
         cfunc = PROTO(self.callback)
         result = cfunc(arg)
@@ -101,15 +101,18 @@
 ##        self.check_type(c_char_p, "abc")
 ##        self.check_type(c_char_p, "def")
 
+
+    @pytest.mark.xfail(
+        reason="we are less strict about callback return type sanity")
     def test_unsupported_restype_1(self):
-        py.test.skip("we are less strict about callback return type sanity")
         # Only "fundamental" result types are supported for callback
         # functions, the type must have a non-NULL stgdict->setfunc.
         # POINTER(c_double), for example, is not supported.
 
         prototype = self.functype.im_func(POINTER(c_double))
         # The type is checked when the prototype is called
-        raises(TypeError, prototype, lambda: None)
+        with pytest.raises(TypeError):
+            prototype(lambda: None)
 
 try:
     WINFUNCTYPE
@@ -193,9 +196,10 @@
         class RECT(Structure):
             _fields_ = [("left", c_int), ("top", c_int),
                         ("right", c_int), ("bottom", c_int)]
-        
+
         proto = CFUNCTYPE(RECT, c_int)
-        raises(TypeError, proto, lambda r: 0)
+        with pytest.raises(TypeError):
+            proto(lambda r: 0)
 
 
     def test_qsort(self):
@@ -210,7 +214,7 @@
             a[i] = 5-i
 
         assert a[0] == 5 # sanity
-        
+
         def comp(a, b):
             a = a.contents.value
             b = b.contents.value
@@ -273,4 +277,5 @@
         FUNC = CFUNCTYPE(None, c_void_p)
         cfunc = FUNC(callback)
         param = c_uint(42)
-        py.test.raises(ArgumentError, "cfunc(param)")
+        with pytest.raises(ArgumentError):
+            cfunc(param)
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_checkretval.py
@@ -1,4 +1,4 @@
-import py
+import pytest
 import sys
 
 from ctypes import *
@@ -33,6 +33,5 @@
         pass
     else:
         def test_oledll(self):
-            raises(WindowsError,
-                   oledll.oleaut32.CreateTypeLib2,
-                   0, 0, 0)
+            with pytest.raises(WindowsError):
+                oledll.oleaut32.CreateTypeLib2(0, 0, 0)
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,4 +1,4 @@
-import py
+import pytest
 import sys, os, unittest
 from ctypes import *
 
@@ -27,6 +27,8 @@
         assert sizeof(x) == sizeof(c_voidp)
         assert sizeof(X) == sizeof(c_voidp)
 
+    @pytest.mark.xfail(
+        reason="cdecl funcptrs ignoring extra args is not implemented")
     def test_first(self):
         StdCallback = WINFUNCTYPE(c_int, c_int, c_int)
         CdeclCallback = CFUNCTYPE(c_int, c_int, c_int)
@@ -42,14 +44,13 @@
         # The following no longer raises a TypeError - it is now
         # possible, as in C, to call cdecl functions with more parameters.
         #self.assertRaises(TypeError, c, 1, 2, 3)
-        py.test.skip("cdecl funcptrs ignoring extra args is not implemented")
         assert c(1, 2, 3, 4, 5, 6) == 3
         if not WINFUNCTYPE is CFUNCTYPE and os.name != "ce":
-            raises(TypeError, s, 1, 2, 3)
+            with pytest.raises(TypeError):
+                s(1, 2, 3)
 
+    @pytest.mark.skipif("sys.platform != 'win32'")
     def test_structures(self):
-        if sys.platform != 'win32':
-            py.test.skip("win32 related")
         WNDPROC = WINFUNCTYPE(c_long, c_int, c_int, c_int, c_int)
 
         def wndproc(hwnd, msg, wParam, lParam):
@@ -130,9 +131,10 @@
         assert strtok(None, "\n") == "c"
         assert strtok(None, "\n") == None
 
+    @pytest.mark.xfail(
+        reason="This test needs mmap to make sure the code is executable, "
+            "please rewrite me")
     def test_from_address(self):
-        py.test.skip("This test needs mmap to make sure the"
-                     " code is executable, please rewrite me")
         def make_function():
             proto = CFUNCTYPE(c_int)
             a=create_string_buffer(
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
@@ -8,7 +8,7 @@
 from __future__ import with_statement
 from ctypes import *
 import sys
-import py
+import pytest
 from support import BaseCTypesTestChecker
 
 try:
@@ -140,7 +140,8 @@
         assert type(result) == int
 
         # You cannot assing character format codes as restype any longer
-        raises(TypeError, setattr, f, "restype", "i")
+        with pytest.raises(TypeError):
+            setattr(f, "restype", "i")
 
     def test_unicode_function_name(self):
         f = dll[u'_testfunc_i_bhilfd']
@@ -237,7 +238,8 @@
         result = f(arg)
         assert not result.contents == v.value
 
-        raises(ArgumentError, f, byref(c_short(22)))
+        with pytest.raises(ArgumentError):
+            f(byref(c_short(22)))
 
         # It is dangerous, however, because you don't control the lifetime
         # of the pointer:
@@ -262,7 +264,8 @@
         class X(Structure):
             _fields_ = [("y", c_int)]
 
-        raises(ArgumentError, f, X()) #cannot convert parameter
+        with pytest.raises(ArgumentError):
+            f(X()) #cannot convert parameter
 
     ################################################################
     def test_shorts(self):
@@ -310,7 +313,8 @@
         # check that the prototype works: we call f with wrong
         # argument types
         cb = AnotherCallback(callback)
-        raises(ArgumentError, f, -10, cb)
+        with pytest.raises(ArgumentError):
+            f(-10, cb)
 
 
     def test_callbacks_2(self):
@@ -351,8 +355,10 @@
         assert 13577625587 == f(1000000000000, cb)
 
     def test_errors_2(self):
-        raises(AttributeError, getattr, dll, "_xxx_yyy")
-        raises(ValueError, c_int.in_dll, dll, "_xxx_yyy")
+        with pytest.raises(AttributeError):
+            getattr(dll, "_xxx_yyy")
+        with pytest.raises(ValueError):
+            c_int.in_dll(dll, "_xxx_yyy")
 
     def test_byval(self):
         # without prototype
@@ -466,16 +472,16 @@
         result = f("abcd", ord("b"), 42)
         assert result == "bcd"
 
+    @pytest.mark.xfail(reason="we are less strict in checking callback 
parameters")
     def test_sf1651235(self):
-        py.test.skip("we are less strict in checking callback parameters")
         # see http://www.python.org/sf/1651235
-
         proto = CFUNCTYPE(c_int, RECT, POINT)
         def callback(*args):
             return 0
 
         callback = proto(callback)
-        raises(ArgumentError, lambda: callback((1, 2, 3, 4), POINT()))
+        with pytest.raises(ArgumentError):
+            callback((1, 2, 3, 4), POINT())
 
     def test_argument_conversion_and_checks(self):
         #This test is designed to check for segfaults if the wrong type of 
argument is passed as parameter
@@ -485,8 +491,10 @@
         assert strlen("eggs", ord("g")) == "ggs"
 
         # Should raise ArgumentError, not segfault
-        py.test.raises(ArgumentError, strlen, 0, 0)
-        py.test.raises(ArgumentError, strlen, False, 0)
+        with pytest.raises(ArgumentError):
+            strlen(0, 0)
+        with pytest.raises(ArgumentError):
+            strlen(False, 0)
 
     def test_union_as_passed_value(self):
         class UN(Union):
@@ -524,8 +532,8 @@
         assert tf_b("yadda") == -42
         assert seen == ["yadda"]
 
+    @pytest.mark.xfail(reason="warnings are disabled")
     def test_warnings(self):
-        py.test.skip("warnings are disabled")
         import warnings
         warnings.simplefilter("always")
         with warnings.catch_warnings(record=True) as w:
@@ -534,8 +542,8 @@
             assert issubclass(w[0].category, RuntimeWarning)
             assert "C function without declared arguments called" in 
str(w[0].message)
 
+    @pytest.mark.xfail
     def test_errcheck(self):
-        py.test.skip('fixme')
         def errcheck(result, func, args):
             assert result == -42
             assert type(result) is int
@@ -556,12 +564,12 @@
             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
-            
+
         warnings.resetwarnings()
 
 
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,4 +1,4 @@
-import py
+import pytest
 from ctypes import *
 import sys
 import os, StringIO
@@ -27,14 +27,17 @@
         def test_load(self):
             CDLL(libc_name)
             CDLL(os.path.basename(libc_name))
-            raises(OSError, CDLL, self.unknowndll)
+            with pytest.raises(OSError):
+                CDLL(self.unknowndll)
 
     if libc_name is not None and os.path.basename(libc_name) == "libc.so.6":
         def test_load_version(self):
             cdll.LoadLibrary("libc.so.6")
             # linux uses version, libc 9 should not exist
-            raises(OSError, cdll.LoadLibrary, "libc.so.9")
-            raises(OSError, cdll.LoadLibrary, self.unknowndll)
+            with pytest.raises(OSError):
+                cdll.LoadLibrary("libc.so.9")
+            with pytest.raises(OSError):
+                cdll.LoadLibrary(self.unknowndll)
 
     def test_find(self):
         for name in ("c", "m"):
@@ -80,7 +83,5 @@
             f_name_addr = c_void_p.from_address(a_name).value
             assert hex(f_ord_addr) == hex(f_name_addr)
 
-            raises(AttributeError, dll.__getitem__, 1234)
-
-if __name__ == "__main__":
-    unittest.main()
+            with pytest.raises(AttributeError):
+                dll[1234]
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,4 +1,4 @@
-import py
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 import sys, struct
@@ -65,8 +65,10 @@
         # Only numbers are allowed in the contructor,
         # otherwise TypeError is raised
         for t in signed_types + unsigned_types + float_types:
-            raises(TypeError, t, "")
-            raises(TypeError, t, None)
+            with pytest.raises(TypeError):
+                t("")
+            with pytest.raises(TypeError):
+                t(None)
 
 ##    def test_valid_ranges(self):
 ##        # invalid values of the correct type
@@ -75,16 +77,16 @@
 ##            self.assertRaises(ValueError, t, l-1)
 ##            self.assertRaises(ValueError, t, h+1)
 
+    @pytest.mark.xfail(reason="testing implementation internals")
     def test_from_param(self):
         # the from_param class method attribute always
         # returns PyCArgObject instances
-        py.test.skip("testing implementation internals")
         for t in signed_types + unsigned_types + float_types:
             assert ArgType == type(t.from_param(0))
 
+    @pytest.mark.xfail(reason="testing implementation internals")
     def test_byref(self):
         # calling byref returns also a PyCArgObject instance
-        py.test.skip("testing implementation internals")
         for t in signed_types + unsigned_types + float_types:
             parm = byref(t())
             assert ArgType == type(parm)
@@ -108,7 +110,8 @@
     def test_integers(self):
         # integers cannot be constructed from floats
         for t in signed_types + unsigned_types:
-            raises(TypeError, t, 3.14)
+            with pytest.raises(TypeError):
+                t(3.14)
 
     def test_sizes(self):
         for t in signed_types + unsigned_types + float_types:
@@ -185,7 +188,8 @@
         # c_int() can be initialized from Python's int, and c_int.
         # Not from c_long or so, which seems strange, abd should
         # probably be changed:
-        raises(TypeError, c_int, c_long(42))
+        with pytest.raises(TypeError):
+            c_int(c_long(42))
 
     def test_subclass(self):
         class enum(c_int):
@@ -195,14 +199,13 @@
             _fields_ = [('t', enum)]
         assert isinstance(S().t, enum)
 
+    #@pytest.mark.xfail("'__pypy__' not in sys.builtin_module_names")
+    @pytest.mark.xfail
     def test_no_missing_shape_to_ffi_type(self):
         # whitebox test
-        import sys
-        if '__pypy__' not in sys.builtin_module_names:
-            skip("only for pypy's ctypes")
-        skip("re-enable after adding 'g' to _shape_to_ffi_type.typemap, "
-             "which I think needs fighting all the way up from "
-             "rpython.rlib.libffi")
+        "re-enable after adding 'g' to _shape_to_ffi_type.typemap, "
+        "which I think needs fighting all the way up from "
+        "rpython.rlib.libffi"
         from _ctypes.basics import _shape_to_ffi_type
         from _rawffi import Array
         for i in range(1, 256):
@@ -213,7 +216,7 @@
             else:
                 assert chr(i) in _shape_to_ffi_type.typemap
 
-    @py.test.mark.xfail
+    @pytest.mark.xfail
     def test_pointer_to_long_double(self):
         import ctypes
         ctypes.POINTER(ctypes.c_longdouble)
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,4 +1,4 @@
-import py
+import pytest
 import sys
 
 class TestSimpleTypes:
@@ -60,10 +60,10 @@
         o = My_void_pp()
 
         assert Void_pp.from_param(o) is o
-        
+
     # XXX Replace by c_char_p tests
+    @pytest.mark.xfail(reason="testing implementation internals")
     def test_cstrings(self):
-        py.test.skip("testing implementation internals")
         from ctypes import c_char_p, byref
 
         # c_char_p.from_param on a Python String packs the string
@@ -73,17 +73,19 @@
 
         # new in 0.9.1: convert (encode) unicode to ascii
         assert c_char_p.from_param(u"123")._obj == "123"
-        raises(UnicodeEncodeError, c_char_p.from_param, u"123\377")
+        with pytest.raises(UnicodeEncodeError):
+            c_char_p.from_param(u"123\377")
 
-        raises(TypeError, c_char_p.from_param, 42)
+        with pytest.raises(TypeError):
+            c_char_p.from_param(42)
 
         # calling c_char_p.from_param with a c_char_p instance
         # returns the argument itself:
         a = c_char_p("123")
         assert c_char_p.from_param(a) is a
 
+    @pytest.mark.xfail(reason="testing implementation internals")
     def test_cw_strings(self):
-        py.test.skip("testing implementation internals")
         from ctypes import byref
         try:
             from ctypes import c_wchar_p
@@ -93,11 +95,13 @@
         s = u"123"
         if sys.platform == "win32":
             assert c_wchar_p.from_param(s)._obj is s
-            raises(TypeError, c_wchar_p.from_param, 42)
+            with pytest.raises(TypeError):
+                c_wchar_p.from_param(42)
 
             # new in 0.9.1: convert (decode) ascii to unicode
             assert c_wchar_p.from_param("123")._obj == u"123"
-        raises(UnicodeDecodeError, c_wchar_p.from_param, "123\377")
+            with pytest.raises(UnicodeDecodeError):
+                c_wchar_p.from_param("123\377")
 
         pa = c_wchar_p.from_param(c_wchar_p(u"123"))
         assert type(pa) == c_wchar_p
@@ -115,9 +119,12 @@
         assert not LPINT.from_param(None)
 
         if c_int != c_long:
-            raises(TypeError, LPINT.from_param, pointer(c_long(42)))
-        raises(TypeError, LPINT.from_param, pointer(c_uint(42)))
-        raises(TypeError, LPINT.from_param, pointer(c_short(42)))
+            with pytest.raises(TypeError):
+                LPINT.from_param(pointer(c_long(42)))
+        with pytest.raises(TypeError):
+            LPINT.from_param(pointer(c_uint(42)))
+        with pytest.raises(TypeError):
+            LPINT.from_param(pointer(c_short(42)))
 
     def test_byref_pointer(self):
         # The from_param class method of POINTER(typ) classes accepts what is
@@ -127,10 +134,13 @@
 
         LPINT.from_param(byref(c_int(42)))
 
-        raises(TypeError, LPINT.from_param, byref(c_short(22)))
+        with pytest.raises(TypeError):
+            LPINT.from_param(byref(c_short(22)))
         if c_int != c_long:
-            raises(TypeError, LPINT.from_param, byref(c_long(22)))
-        raises(TypeError, LPINT.from_param, byref(c_uint(22)))
+            with pytest.raises(TypeError):
+                LPINT.from_param(byref(c_long(22)))
+        with pytest.raises(TypeError):
+            LPINT.from_param(byref(c_uint(22)))
 
     def test_byref_pointerpointer(self):
         # See above
@@ -139,10 +149,13 @@
         LPLPINT = POINTER(POINTER(c_int))
         LPLPINT.from_param(byref(pointer(c_int(42))))
 
-        raises(TypeError, LPLPINT.from_param, byref(pointer(c_short(22))))
+        with pytest.raises(TypeError):
+            LPLPINT.from_param(byref(pointer(c_short(22))))
         if c_int != c_long:
-            raises(TypeError, LPLPINT.from_param, byref(pointer(c_long(22))))
-        raises(TypeError, LPLPINT.from_param, byref(pointer(c_uint(22))))
+            with pytest.raises(TypeError):
+                LPLPINT.from_param(byref(pointer(c_long(22))))
+        with pytest.raises(TypeError):
+            LPLPINT.from_param(byref(pointer(c_uint(22))))
 
     def test_array_pointers(self):
         from ctypes import c_short, c_uint, c_int, c_long, POINTER
@@ -155,15 +168,18 @@
         # the same type!
         LPINT = POINTER(c_int)
         LPINT.from_param((c_int*3)())
-        raises(TypeError, LPINT.from_param, c_short*3)
-        raises(TypeError, LPINT.from_param, c_long*3)
-        raises(TypeError, LPINT.from_param, c_uint*3)
+        with pytest.raises(TypeError):
+            LPINT.from_param(c_short*3)
+        with pytest.raises(TypeError):
+            LPINT.from_param(c_long*3)
+        with pytest.raises(TypeError):
+            LPINT.from_param(c_uint*3)
 
 ##    def test_performance(self):
 ##        check_perf()
 
+    @pytest.mark.xfail(reason="testing implementation internals")
     def test_noctypes_argtype(self):
-        py.test.skip("we implement details differently")
         from ctypes import CDLL, c_void_p, ArgumentError
         import conftest
         dll = CDLL(str(conftest.sofile))
@@ -171,7 +187,8 @@
         func = dll._testfunc_p_p
         func.restype = c_void_p
         # TypeError: has no from_param method
-        raises(TypeError, setattr, func, "argtypes", (object,))
+        with pytest.raises(TypeError):
+            setattr(func, "argtypes", (object,))
 
         class Adapter(object):
             def from_param(cls, obj):
@@ -187,7 +204,8 @@
 
         func.argtypes = (Adapter(),)
         # don't know how to convert parameter 1
-        raises(ArgumentError, func, object())
+        with pytest.raises(ArgumentError):
+            func(object())
         assert func(c_void_p(42)) == 42
 
         class Adapter(object):
@@ -196,7 +214,8 @@
 
         func.argtypes = (Adapter(),)
         # ArgumentError: argument 1: ValueError: 99
-        raises(ArgumentError, func, 99)
+        with pytest.raises(ArgumentError):
+            func(99)
 
     def test_multiple_signature(self):
         # when .argtypes is not set, calling a function with a certain
@@ -212,4 +231,4 @@
 
         # This one is normal
         assert func(None) == 0
-        
+
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
@@ -1,4 +1,4 @@
-import py
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 
@@ -31,21 +31,27 @@
 
     def test_restype_setattr(self):
         func = testdll._testfunc_p_p
-        raises(TypeError, setattr, func, 'restype', 20)
+        with pytest.raises(TypeError):
+            setattr(func, 'restype', 20)
 
     def test_argtypes_setattr(self):
         func = testdll._testfunc_p_p
-        raises(TypeError, setattr, func, 'argtypes', 20)
-        raises(TypeError, setattr, func, 'argtypes', [20])
+        with pytest.raises(TypeError):
+            setattr(func, 'argtypes', 20)
+        with pytest.raises(TypeError):
+            setattr(func, 'argtypes', [20])
 
         func = CFUNCTYPE(c_long, c_void_p, c_long)(lambda: None)
         assert func.argtypes == (c_void_p, c_long)
 
     def test_paramflags_setattr(self):
         func = CFUNCTYPE(c_long, c_void_p, c_long)(lambda: None)
-        raises(TypeError, setattr, func, 'paramflags', 'spam')
-        raises(ValueError, setattr, func, 'paramflags', (1, 2, 3, 4))
-        raises(TypeError, setattr, func, 'paramflags', ((1,), ('a',)))
+        with pytest.raises(TypeError):
+            setattr(func, 'paramflags', 'spam')
+        with pytest.raises(ValueError):
+            setattr(func, 'paramflags', (1, 2, 3, 4))
+        with pytest.raises(TypeError):
+            setattr(func, 'paramflags', ((1,), ('a',)))
         func.paramflags = (1,), (1|4,)
 
     def test_kwargs(self):
@@ -107,13 +113,16 @@
                              positive_address(func(byref(ci))))
 
         func.argtypes = c_char_p,
-        raises(ArgumentError, func, byref(ci))
+        with pytest.raises(ArgumentError):
+            func(byref(ci))
 
         func.argtypes = POINTER(c_short),
-        raises(ArgumentError, func, byref(ci))
+        with pytest.raises(ArgumentError):
+            func(byref(ci))
 
         func.argtypes = POINTER(c_double),
-        raises(ArgumentError, func, byref(ci))
+        with pytest.raises(ArgumentError):
+            func(byref(ci))
 
     def test_POINTER_c_char_arg(self):
         func = testdll._testfunc_p_p
@@ -252,7 +261,8 @@
         func.restype = POINTER(c_int)
         func.argtypes = [c_int * 8]
         array = ARRAY(1, 2, 3, 4, 5, 6, 7, 8)
-        py.test.raises(ArgumentError, "func(array)")
+        with pytest.raises(ArgumentError):
+            func(array)
 
 ################################################################
 
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_returnfuncptrs.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_returnfuncptrs.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_returnfuncptrs.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_returnfuncptrs.py
@@ -1,4 +1,4 @@
-import py
+import pytest
 
 from ctypes import *
 
@@ -16,8 +16,10 @@
         strchr = get_strchr()
         assert strchr("abcdef", "b") == "bcdef"
         assert strchr("abcdef", "x") == None
-        raises(ArgumentError, strchr, "abcdef", 3)
-        raises(TypeError, strchr, "abcdef")
+        with pytest.raises(ArgumentError):
+            strchr("abcdef", 3)
+        with pytest.raises(TypeError):
+            strchr("abcdef")
 
     def test_without_prototype(self):
         get_strchr = dll.get_strchr
@@ -29,5 +31,7 @@
         strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr)
         assert strchr("abcdef", "b"), "bcdef"
         assert strchr("abcdef", "x") == None
-        raises(ArgumentError, strchr, "abcdef", 3)
-        raises(TypeError, strchr, "abcdef")
+        with pytest.raises(ArgumentError):
+            strchr("abcdef", 3)
+        with pytest.raises(TypeError):
+            strchr("abcdef")
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_slicing.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_slicing.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_slicing.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_slicing.py
@@ -1,4 +1,4 @@
-import py
+import pytest
 from support import BaseCTypesTestChecker
 from ctypes import *
 
@@ -30,13 +30,17 @@
         from operator import setslice
 
         # TypeError: int expected instead of str instance
-        raises(TypeError, setslice, a, 0, 5, "abcde")
+        with pytest.raises(TypeError):
+            setslice(a, 0, 5, "abcde")
         # TypeError: int expected instead of str instance
-        raises(TypeError, setslice, a, 0, 5, ["a", "b", "c", "d", "e"])
+        with pytest.raises(TypeError):
+            setslice(a, 0, 5, ["a", "b", "c", "d", "e"])
         # TypeError: int expected instead of float instance
-        raises(TypeError, setslice, a, 0, 5, [1, 2, 3, 4, 3.14])
+        with pytest.raises(TypeError):
+            setslice(a, 0, 5, [1, 2, 3, 4, 3.14])
         # ValueError: Can only assign sequence of same size
-        raises(ValueError, setslice, a, 0, 5, range(32))
+        with pytest.raises(ValueError):
+            setslice(a, 0, 5, range(32))
 
     def test_char_ptr(self):
         s = "abcdefghijklmnopqrstuvwxyz"
@@ -47,8 +51,8 @@
         assert res[:len(s)] == s
 
         import operator
-        raises(TypeError, operator.setslice,
-                          res, 0, 5, u"abcde")
+        with pytest.raises(TypeError):
+            operator.setslice(res, 0, 5, u"abcde")
         dll.my_free(res)
 
         dll.my_strdup.restype = POINTER(c_byte)
@@ -99,8 +103,8 @@
             assert res[:len(s)] == s
 
             import operator
-            raises(TypeError, operator.setslice,
-                              res, 0, 5, u"abcde")
+            with pytest.raises(TypeError):
+                operator.setslice(res, 0, 5, u"abcde")
             dll.my_free(res)
 
             if sizeof(c_wchar) == sizeof(c_short):
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_stringptr.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_stringptr.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_stringptr.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_stringptr.py
@@ -1,4 +1,4 @@
-import py
+import pytest
 from support import BaseCTypesTestChecker
 from ctypes import *
 
@@ -16,7 +16,8 @@
         x = X()
 
         # NULL pointer access
-        raises(ValueError, getattr, x.str, "contents")
+        with pytest.raises(ValueError):
+            x.str.contents
         b = c_buffer("Hello, World")
         #from sys import getrefcount as grc
         #assert grc(b) == 2
@@ -31,7 +32,6 @@
         # XXX pypy  modified:
         #raises(TypeError, setattr, x, "str", "Hello, World")
         x = b = None
-        py.test.skip("test passes! but modified to avoid getrefcount and 
detail issues")
 
     def test__c_char_p(self):
         class X(Structure):
@@ -47,7 +47,6 @@
         #b = c_buffer("Hello, World")
         #raises(TypeError, setattr, x, "str", b)
         x = None
-        py.test.skip("test passes! but modified to avoid detail issues")
 
 
     def test_functions(self):
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_struct_fields.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_struct_fields.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_struct_fields.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_struct_fields.py
@@ -1,4 +1,4 @@
-import py
+import pytest
 from support import BaseCTypesTestChecker
 from ctypes import *
 
@@ -18,31 +18,37 @@
             pass
         assert sizeof(X) == 0 # not finalized
         X._fields_ = [] # finalized
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
     def test_1_B(self):
         class X(Structure):
             _fields_ = [] # finalized
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
     def test_2(self):
         class X(Structure):
             pass
         X()
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
     def test_3(self):
         class X(Structure):
             pass
         class Y(Structure):
             _fields_ = [("x", X)] # finalizes X
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
     def test_4(self):
         class X(Structure):
             pass
         class Y(X):
             pass
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
         Y._fields_ = []
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_structures.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_structures.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_structures.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_structures.py
@@ -2,7 +2,7 @@
 from struct import calcsize
 from support import BaseCTypesTestChecker
 
-import py
+import pytest
 
 
 class TestSubclasses(BaseCTypesTestChecker):
@@ -143,8 +143,10 @@
         assert X.y.size == sizeof(c_char)
 
         # readonly
-        raises((TypeError, AttributeError), setattr, X.x, "offset", 92)
-        raises((TypeError, AttributeError), setattr, X.x, "size", 92)
+        with pytest.raises((TypeError, AttributeError)):
+            X.x.offset = 92
+        with pytest.raises((TypeError, AttributeError)):
+            X.x.size = 92
 
         class X(Union):
             _fields_ = [("x", c_int),
@@ -157,8 +159,10 @@
         assert X.y.size == sizeof(c_char)
 
         # readonly
-        raises((TypeError, AttributeError), setattr, X.x, "offset", 92)
-        raises((TypeError, AttributeError), setattr, X.x, "size", 92)
+        with pytest.raises((TypeError, AttributeError)):
+            X.x.offset = 92
+        with pytest.raises((TypeError, AttributeError)):
+            X.x.size = 92
 
         # XXX Should we check nested data types also?
         # offset is always relative to the class...
@@ -202,23 +206,28 @@
         d = {"_fields_": [("a", "b"),
                           ("b", "q")],
              "_pack_": -1}
-        raises(ValueError, type(Structure), "X", (Structure,), d)
+        with pytest.raises(ValueError):
+            type(Structure)("X", (Structure,), d)
 
     def test_initializers(self):
         class Person(Structure):
             _fields_ = [("name", c_char*6),
                         ("age", c_int)]
 
-        raises(TypeError, Person, 42)
-        raises(ValueError, Person, "asldkjaslkdjaslkdj")
-        raises(TypeError, Person, "Name", "HI")
+        with pytest.raises(TypeError):
+            Person(42)
+        with pytest.raises(ValueError):
+            Person("asldkjaslkdjaslkdj")
+        with pytest.raises(TypeError):
+            Person("Name", "HI")
 
         # short enough
         assert Person("12345", 5).name == "12345"
         # exact fit
         assert Person("123456", 5).name == "123456"
         # too long
-        raises(ValueError, Person, "1234567", 5)
+        with pytest.raises(ValueError):
+            Person("1234567", 5)
 
 
     def test_keyword_initializers(self):
@@ -246,7 +255,8 @@
     def test_invalid_field_types(self):
         class POINT(Structure):
             pass
-        raises(TypeError, setattr, POINT, "_fields_", [("x", 1), ("y", 2)])
+        with pytest.raises(TypeError):
+            POINT._fields_ = [("x", 1), ("y", 2)]
 
     def test_intarray_fields(self):
         class SomeInts(Structure):
@@ -257,7 +267,8 @@
         assert SomeInts((1, 2, 3, 4)).a[:] == [1, 2, 3, 4]
         # too long
         # XXX Should raise ValueError?, not RuntimeError
-        raises(RuntimeError, SomeInts, (1, 2, 3, 4, 5))
+        with pytest.raises(RuntimeError):
+            SomeInts((1, 2, 3, 4, 5))
 
     def test_nested_initializers(self):
         # test initializing nested structures
@@ -278,7 +289,7 @@
         assert p.age == 5
 
     def test_structures_with_wchar(self):
-        py.test.skip("need unicode support on _rawffi level")
+        pytest.skip("need unicode support on _rawffi level")
         try:
             c_wchar
         except NameError:
@@ -296,10 +307,11 @@
         # exact fit
         assert PersonW(u"123456789012").name == u"123456789012"
         #too long
-        raises(ValueError, PersonW, u"1234567890123")
+        with pytest.raises(ValueError):
+            PersonW(u"1234567890123")
 
     def test_init_errors(self):
-        py.test.skip("not implemented error details")
+        pytest.skip("not implemented error details")
         class Phone(Structure):
             _fields_ = [("areacode", c_char*6),
                         ("number", c_char*12)]
@@ -347,7 +359,7 @@
 ##                             (AttributeError, "class must define a 
'_fields_' attribute"))
 
     def test_abstract_class(self):
-        py.test.skip("_abstract_ semantics not implemented")
+        pytest.skip("_abstract_ semantics not implemented")
         class X(Structure):
             _abstract_ = "something"
         # try 'X()'
@@ -373,7 +385,7 @@
         assert p.age == 6
 
     def test_subclassing_field_is_a_tuple(self):
-        py.test.skip("subclassing semantics not implemented")
+        pytest.skip("subclassing semantics not implemented")
         class Person(Structure):
             _fields_ = (("name", c_char*6),
                         ("age", c_int))
@@ -547,7 +559,7 @@
             raise AssertionError("Structure or union cannot contain itself")
 
     def test_vice_versa(self):
-        py.test.skip("mutually dependent lazily defined structures error 
semantics")
+        pytest.skip("mutually dependent lazily defined structures error 
semantics")
         class First(Structure):
             pass
         class Second(Structure):
@@ -568,18 +580,21 @@
             pass
         assert sizeof(X) == 0
         X._fields_ = [("a", c_int),]
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
         class X(Structure):
             pass
         X()
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
 
         class X(Structure):
             pass
         class Y(X):
             pass
-        raises(AttributeError, setattr, X, "_fields_", [])
+        with pytest.raises(AttributeError):
+            X._fields_ = []
         Y.__fields__ = []
 
 
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_unicode.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_unicode.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_unicode.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_unicode.py
@@ -1,6 +1,6 @@
 # coding: latin-1
 import ctypes
-import py
+import pytest
 from support import BaseCTypesTestChecker
 
 try:
@@ -33,7 +33,7 @@
             assert wcslen(u"ab\u2070") == 3
             # string args are converted
             assert wcslen("abc") == 3
-            py.test.raises(ctypes.ArgumentError, wcslen, "ab&#65533;")
+            pytest.raises(ctypes.ArgumentError, wcslen, "ab&#65533;")
 
         def test_ascii_replace(self):
             ctypes.set_conversion_mode("ascii", "replace")
@@ -86,7 +86,8 @@
             ctypes.set_conversion_mode("ascii", "strict")
             assert func("abc") == "abc"
             assert func(u"abc") == "abc"
-            raises(ctypes.ArgumentError, func, u"ab&#65533;")
+            with pytest.raises(ctypes.ArgumentError):
+                func(u"ab&#65533;")
 
         def test_ascii_ignore(self):
             ctypes.set_conversion_mode("ascii", "ignore")
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
@@ -2,7 +2,7 @@
 A testcase which accesses *values* in a dll.
 """
 
-import py
+import pytest
 from ctypes import *
 from support import BaseCTypesTestChecker
 
@@ -27,12 +27,13 @@
         assert ctdll.get_a_string_char(15) == ord('$')
 
     def test_undefined(self):
-        raises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol")
+        with pytest.raises(ValueError):
+            c_int.in_dll(ctdll, "Undefined_Symbol")
 
 class TestWin_Values(BaseCTypesTestChecker):
     """This test only works when python itself is a dll/shared library"""
     def setup_class(cls):
-        py.test.skip("tests expect and access cpython dll")
+        pytest.skip("tests expect and access cpython dll")
 
     def test_optimizeflag(self):
         # This test accesses the Py_OptimizeFlag intger, which is
@@ -86,7 +87,8 @@
         del _pointer_type_cache[struct_frozen]
 
     def test_undefined(self):
-        raises(ValueError, c_int.in_dll, pydll, "Undefined_Symbol")
+        with pytest.raises(ValueError):
+            c_int.in_dll(pydll, "Undefined_Symbol")
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_varsize_struct.py 
b/pypy/module/test_lib_pypy/ctypes_tests/test_varsize_struct.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_varsize_struct.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_varsize_struct.py
@@ -1,10 +1,10 @@
-import py
+import pytest
 from support import BaseCTypesTestChecker
 from ctypes import *
 
 class TestVarSize(BaseCTypesTestChecker):
     def test_resize(self):
-        py.test.skip("resizing not implemented")
+        pytest.skip("resizing not implemented")
         class X(Structure):
             _fields_ = [("item", c_int),
                         ("array", c_int * 1)]
@@ -35,15 +35,23 @@
 
     def test_array_invalid_length(self):
         # cannot create arrays with non-positive size
-        raises(ValueError, lambda: c_int * -1)
-        raises(ValueError, lambda: c_int * -3)
+        with pytest.raises(ValueError):
+            c_int * -1
+        with pytest.raises(ValueError):
+            c_int * -3
 
     def test_zerosized_array(self):
         array = (c_int * 0)()
         # accessing elements of zero-sized arrays raise IndexError
-        raises(IndexError, array.__setitem__, 0, None)
-        raises(IndexError, array.__getitem__, 0)
-        raises(IndexError, array.__setitem__, 1, None)
-        raises(IndexError, array.__getitem__, 1)
-        raises(IndexError, array.__setitem__, -1, None)
-        raises(IndexError, array.__getitem__, -1)
+        with pytest.raises(IndexError):
+            array.__setitem__(0, None)
+        with pytest.raises(IndexError):
+            array.__getitem__(0)
+        with pytest.raises(IndexError):
+            array.__setitem__(1, None)
+        with pytest.raises(IndexError):
+            array.__getitem__(1)
+        with pytest.raises(IndexError):
+            array.__setitem__(-1, None)
+        with pytest.raises(IndexError):
+            array.__getitem__(-1)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to