[pypy-commit] pypy default: Kill test-only unwrapper_catch() and fix tests

rlamy pypy.commits at gmail.com
Fri Jun 30 15:43:55 EDT 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: 
Changeset: r91660:32f6ab17f6c1
Date: 2017-06-30 20:43 +0100
http://bitbucket.org/pypy/pypy/changeset/32f6ab17f6c1/

Log:	Kill test-only unwrapper_catch() and fix tests

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -448,38 +448,10 @@
             error=_compute_error(error, restype), gil=gil,
             result_borrowed=result_borrowed, result_is_ll=result_is_ll)
         FUNCTIONS_BY_HEADER[header][func.__name__] = api_function
-
-        # ZZZ is this whole logic really needed???  It seems to be only
-        # for RPython code calling PyXxx() functions directly.  I would
-        # think that usually directly calling the function is clean
-        # enough now
-        def unwrapper_catch(space, *args):
-            try:
-                res = unwrapper(space, *args)
-            except OperationError as e:
-                if not hasattr(unwrapper.api_func, "error_value"):
-                    raise
-                state = space.fromcache(State)
-                state.set_exception(e)
-                if is_PyObject(restype):
-                    return None
-                else:
-                    return unwrapper.api_func.error_value
-            got_integer = isinstance(res, (int, long, float))
-            if isinstance(restype, lltype.Typedef):
-                real_restype = restype.OF
-            else:
-                real_restype = restype
-            expect_integer = (isinstance(real_restype, lltype.Primitive) and
-                            rffi.cast(restype, 0) == 0)
-            assert got_integer == expect_integer, (
-                'got %r not integer' % (res,))
-            return res
-        INTERPLEVEL_API[func.__name__] = unwrapper_catch  # used in tests
-
         unwrapper = api_function.get_unwrapper()
         unwrapper.func = func
         unwrapper.api_func = api_function
+        INTERPLEVEL_API[func.__name__] = unwrapper  # used in tests
         return unwrapper
     return decorate
 
diff --git a/pypy/module/cpyext/test/test_iterator.py b/pypy/module/cpyext/test/test_iterator.py
--- a/pypy/module/cpyext/test/test_iterator.py
+++ b/pypy/module/cpyext/test/test_iterator.py
@@ -1,5 +1,8 @@
+import pytest
+from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.module.cpyext.iterator import PyIter_Next
 
 
 class TestIterator(BaseApiTest):
@@ -14,13 +17,12 @@
         assert space.unwrap(api.PyIter_Next(w_iter)) == 1
         assert space.unwrap(api.PyIter_Next(w_iter)) == 2
         assert space.unwrap(api.PyIter_Next(w_iter)) == 3
-        assert api.PyIter_Next(w_iter) is None
-        assert not api.PyErr_Occurred()
+        assert PyIter_Next(space, w_iter) is None
 
-    def test_iternext_error(self,space, api):
-        assert api.PyIter_Next(space.w_None) is None
-        assert api.PyErr_Occurred() is space.w_TypeError
-        api.PyErr_Clear()
+    def test_iternext_error(self, space):
+        with pytest.raises(OperationError) as excinfo:
+            PyIter_Next(space, space.w_None)
+        assert excinfo.value.w_type is space.w_TypeError
 
 
 class AppTestIterator(AppTestCpythonExtensionBase):
diff --git a/pypy/module/cpyext/test/test_listobject.py b/pypy/module/cpyext/test/test_listobject.py
--- a/pypy/module/cpyext/test/test_listobject.py
+++ b/pypy/module/cpyext/test/test_listobject.py
@@ -1,5 +1,8 @@
+import pytest
+from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.module.cpyext.listobject import PyList_Size
 
 class TestListObject(BaseApiTest):
     def test_list(self, space, api):
@@ -19,19 +22,19 @@
 
         assert not api.PyList_Check(space.newtuple([]))
         assert not api.PyList_CheckExact(space.newtuple([]))
-    
+
     def test_get_size(self, space, api):
         l = api.PyList_New(0)
         assert api.PyList_GET_SIZE(l) == 0
         api.PyList_Append(l, space.wrap(3))
         assert api.PyList_GET_SIZE(l) == 1
-    
-    def test_size(self, space, api):
+
+    def test_size(self, space):
         l = space.newlist([space.w_None, space.w_None])
-        assert api.PyList_Size(l) == 2
-        assert api.PyList_Size(space.w_None) == -1
-        assert api.PyErr_Occurred() is space.w_TypeError
-        api.PyErr_Clear()
+        assert PyList_Size(space, l) == 2
+        with pytest.raises(OperationError) as excinfo:
+            PyList_Size(space, space.w_None)
+        assert excinfo.value.w_type is space.w_TypeError
 
     def test_insert(self, space, api):
         w_l = space.newlist([space.w_None, space.w_None])
@@ -49,7 +52,7 @@
         l = space.newlist([space.wrap(1), space.wrap(0), space.wrap(7000)])
         assert api.PyList_Sort(l) == 0
         assert space.eq_w(l, space.newlist([space.wrap(0), space.wrap(1), space.wrap(7000)]))
-    
+
     def test_reverse(self, space, api):
         l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
         assert api.PyList_Reverse(l) == 0
@@ -128,9 +131,9 @@
         l = L([1])
         module.setlistitem(l, 0)
         assert len(l) == 1
-        
+
         raises(SystemError, module.setlistitem, (1, 2, 3), 0)
-    
+
         l = []
         module.appendlist(l, 14)
         assert len(l) == 1
@@ -250,7 +253,7 @@
                 old_count2 = Py_REFCNT(i2);
 
                 ret = PyList_Append(o, i1);
-                if (ret != 0) 
+                if (ret != 0)
                     return NULL;
                 /* check the result of Append(), and also force the list
                    to use the CPyListStrategy now */
@@ -281,7 +284,7 @@
                 PyList_GetItem(o, 0);
                 CHECKCOUNT(0, 0, "PyList_Get_Item");
 
-                Py_DECREF(o); 
+                Py_DECREF(o);
                 #ifndef PYPY_VERSION
                 {
                     // PyPy deletes only at teardown
diff --git a/pypy/module/cpyext/test/test_longobject.py b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -1,11 +1,14 @@
-import sys, py
+import sys
+import pytest
+from pypy.interpreter.error import OperationError
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rlib.rarithmetic import maxint
-from pypy.objspace.std.intobject import W_IntObject
 from pypy.objspace.std.longobject import W_LongObject
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-
+from pypy.module.cpyext.longobject import (
+    PyLong_FromLong, PyLong_AsLong, PyLong_AsUnsignedLong, PyLong_AsLongLong,
+    PyLong_AsUnsignedLongLong, PyLong_AsUnsignedLongLongMask)
 
 class TestLongObject(BaseApiTest):
     def test_FromLong(self, space, api):
@@ -17,24 +20,25 @@
         assert isinstance(w_value, W_LongObject)
         assert space.unwrap(w_value) == sys.maxint
 
-    def test_aslong(self, space, api):
-        w_value = api.PyLong_FromLong((sys.maxint - 1) / 2)
+    def test_aslong(self, space):
+        w_value = PyLong_FromLong(space, (sys.maxint - 1) / 2)
         assert isinstance(w_value, W_LongObject)
 
         w_value = space.mul(w_value, space.wrap(2))
         assert isinstance(w_value, W_LongObject)
-        value = api.PyLong_AsLong(w_value)
+        value = PyLong_AsLong(space, w_value)
         assert value == (sys.maxint - 1)
 
         w_value = space.mul(w_value, space.wrap(2))
-
-        value = api.PyLong_AsLong(w_value)
-        assert value == -1 and api.PyErr_Occurred() is space.w_OverflowError
-        api.PyErr_Clear()
-        value = api.PyLong_AsUnsignedLong(w_value)
+        with pytest.raises(OperationError) as excinfo:
+            PyLong_AsLong(space, w_value)
+        assert excinfo.value.w_type is space.w_OverflowError
+        value = PyLong_AsUnsignedLong(space, w_value)
         assert value == (sys.maxint - 1) * 2
 
-        self.raises(space, api, OverflowError, api.PyLong_AsUnsignedLong, space.wrap(-1))
+        with pytest.raises(OperationError) as excinfo:
+            PyLong_AsUnsignedLong(space, space.newint(-1))
+        assert excinfo.value.w_type is space.w_OverflowError
 
     def test_as_ssize_t(self, space, api):
         w_value = space.newlong(2)
@@ -69,20 +73,22 @@
         assert api.PyLong_Check(l)
         assert not api.PyLong_CheckExact(l)
 
-    def test_as_longlong(self, space, api):
-        assert api.PyLong_AsLongLong(space.wrap(1<<62)) == 1<<62
-        assert api.PyLong_AsLongLong(space.wrap(1<<63)) == -1
-        api.PyErr_Clear()
+    def test_as_longlong(self, space):
+        assert PyLong_AsLongLong(space, space.wrap(1 << 62)) == 1 << 62
+        with pytest.raises(OperationError) as excinfo:
+            PyLong_AsLongLong(space, space.wrap(1 << 63))
+        assert excinfo.value.w_type is space.w_OverflowError
 
-        assert api.PyLong_AsUnsignedLongLong(space.wrap(1<<63)) == 1<<63
-        assert api.PyLong_AsUnsignedLongLong(space.wrap(1<<64)) == (1<<64) - 1
-        assert api.PyErr_Occurred()
-        api.PyErr_Clear()
+        assert PyLong_AsUnsignedLongLong(space, space.wrap(1 << 63)) == 1 << 63
+        with pytest.raises(OperationError) as excinfo:
+            PyLong_AsUnsignedLongLong(space, space.wrap(1 << 64))
+        assert excinfo.value.w_type is space.w_OverflowError
 
-        assert api.PyLong_AsUnsignedLongLongMask(
-            space.wrap(1<<64)) == 0
+        assert PyLong_AsUnsignedLongLongMask(space, space.wrap(1 << 64)) == 0
 
-        self.raises(space, api, OverflowError, api.PyLong_AsUnsignedLongLong, space.wrap(-1))
+        with pytest.raises(OperationError) as excinfo:
+            PyLong_AsUnsignedLongLong(space, space.newint(-1))
+        assert excinfo.value.w_type is space.w_OverflowError
 
     def test_as_long_and_overflow(self, space, api):
         overflow = lltype.malloc(rffi.CArrayPtr(rffi.INT_real).TO, 1, flavor='raw')
@@ -131,10 +137,6 @@
         assert api.PyLong_AsVoidPtr(w_l) == p
 
     def test_sign_and_bits(self, space, api):
-        if space.is_true(space.lt(space.sys.get('version_info'),
-                                  space.wrap((2, 7)))):
-            py.test.skip("unsupported before Python 2.7")
-
         assert api._PyLong_Sign(space.wraplong(0L)) == 0
         assert api._PyLong_Sign(space.wraplong(2L)) == 1
         assert api._PyLong_Sign(space.wraplong(-2L)) == -1
@@ -282,7 +284,7 @@
                     return PyLong_FromLong(3);
                  if (str + strlen(str) != end)
                     return PyLong_FromLong(4);
-                 return PyLong_FromLong(0); 
+                 return PyLong_FromLong(0);
              """)])
         assert module.from_str() == 0
 
@@ -338,4 +340,4 @@
         assert module.has_pow() == 0
         assert module.has_hex() == '0x2aL'
         assert module.has_oct() == '052L'
-                
+
diff --git a/pypy/module/cpyext/test/test_module.py b/pypy/module/cpyext/test/test_module.py
--- a/pypy/module/cpyext/test/test_module.py
+++ b/pypy/module/cpyext/test/test_module.py
@@ -1,4 +1,6 @@
-from pypy.module.cpyext.modsupport import PyModule_New
+import pytest
+from pypy.interpreter.error import OperationError
+from pypy.module.cpyext.modsupport import PyModule_New, PyModule_GetName
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from rpython.rtyper.lltypesystem import rffi
 
@@ -10,10 +12,12 @@
         assert space.eq_w(space.getattr(w_mod, space.newtext('__name__')),
                           space.newtext('testname'))
 
-    def test_module_getname(self, space, api):
+    def test_module_getname(self, space):
         w_sys = space.wrap(space.sys)
-        p = api.PyModule_GetName(w_sys)
+        p = PyModule_GetName(space, w_sys)
         assert rffi.charp2str(p) == 'sys'
-        p2 = api.PyModule_GetName(w_sys)
+        p2 = PyModule_GetName(space, w_sys)
         assert p2 == p
-        self.raises(space, api, SystemError, api.PyModule_GetName, space.w_True)
+        with pytest.raises(OperationError) as excinfo:
+            PyModule_GetName(space, space.w_True)
+        assert excinfo.value.w_type is space.w_SystemError
diff --git a/pypy/module/cpyext/test/test_ndarrayobject.py b/pypy/module/cpyext/test/test_ndarrayobject.py
--- a/pypy/module/cpyext/test/test_ndarrayobject.py
+++ b/pypy/module/cpyext/test/test_ndarrayobject.py
@@ -1,11 +1,14 @@
-import py
+import pytest
 import os
+from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.micronumpy.ndarray import W_NDimArray
 from pypy.module.micronumpy.descriptor import get_dtype_cache
 import pypy.module.micronumpy.constants as NPY
+from pypy.module.cpyext.ndarrayobject import (
+    _PyArray_FromAny, _PyArray_FromObject)
 
 def scalar(space):
     dtype = get_dtype_cache(space).w_float64dtype
@@ -87,19 +90,19 @@
         ptr = rffi.cast(rffi.DOUBLEP, api._PyArray_DATA(a))
         assert ptr[0] == 10.
 
-    def test_FromAny(self, space, api):
+    def test_FromAny(self, space):
         a = array(space, [10, 5, 3])
-        assert api._PyArray_FromAny(a, None, 0, 0, 0, NULL) is a
-        assert api._PyArray_FromAny(a, None, 1, 4, 0, NULL) is a
-        self.raises(space, api, ValueError, api._PyArray_FromAny,
-                    a, None, 4, 5, 0, NULL)
+        assert _PyArray_FromAny(space, a, None, 0, 0, 0, NULL) is a
+        assert _PyArray_FromAny(space, a, None, 1, 4, 0, NULL) is a
+        with pytest.raises(OperationError) as excinfo:
+            _PyArray_FromAny(space, a, None, 4, 5, 0, NULL)
 
-    def test_FromObject(self, space, api):
+    def test_FromObject(self, space):
         a = array(space, [10, 5, 3])
-        assert api._PyArray_FromObject(a, a.get_dtype().num, 0, 0) is a
-        exc = self.raises(space, api, ValueError, api._PyArray_FromObject,
-                    a, 11, 4, 5)
-        assert exc.errorstr(space).find('desired') >= 0
+        assert _PyArray_FromObject(space, a, a.get_dtype().num, 0, 0) is a
+        with pytest.raises(OperationError) as excinfo:
+            _PyArray_FromObject(space, a, 11, 4, 5)
+        assert excinfo.value.errorstr(space).find('desired') >= 0
 
     def test_list_from_fixedptr(self, space, api):
         A = lltype.GcArray(lltype.Float)
@@ -216,7 +219,7 @@
         assert res.get_scalar_value().imag == 4.
 
     def _test_Ufunc_FromFuncAndDataAndSignature(self, space, api):
-        py.test.skip('preliminary non-translated test')
+        pytest.skip('preliminary non-translated test')
         '''
         PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
         char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
@@ -363,7 +366,7 @@
     def test_ufunc(self):
         if self.runappdirect:
             from numpy import arange
-            py.test.xfail('segfaults on cpython: PyUFunc_API == NULL?')
+            pytest.xfail('segfaults on cpython: PyUFunc_API == NULL?')
         else:
             from _numpypy.multiarray import arange
         mod = self.import_extension('foo', [
diff --git a/pypy/module/cpyext/test/test_object.py b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -1,10 +1,17 @@
-import py, pytest
+import pytest
 
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import (
     Py_LT, Py_LE, Py_NE, Py_EQ, Py_GE, Py_GT)
+from pypy.module.cpyext.object import (
+    PyObject_IsTrue, PyObject_Not, PyObject_GetAttrString,
+    PyObject_DelAttrString, PyObject_GetAttr, PyObject_DelAttr,
+    PyObject_GetItem, PyObject_RichCompareBool,
+    PyObject_IsInstance, PyObject_IsSubclass, PyObject_AsFileDescriptor,
+    PyObject_Hash, PyObject_Cmp, PyObject_Unicode
+)
 
 class TestObject(BaseApiTest):
     def test_IsTrue(self, space, api):
@@ -25,9 +32,10 @@
                     raise ValueError
             return C()""")
 
-        assert api.PyObject_IsTrue(w_obj) == -1
-        assert api.PyObject_Not(w_obj) == -1
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PyObject_IsTrue(space, w_obj)
+        with raises_w(space, ValueError):
+            PyObject_Not(space, w_obj)
 
     def test_HasAttr(self, space, api):
         hasattr_ = lambda w_obj, name: api.PyObject_HasAttr(w_obj,
@@ -59,22 +67,21 @@
         rffi.free_charp(buf)
         assert space.unwrap(space.getattr(w_obj, space.wrap('test'))) == 20
 
-    def test_getattr(self, space, api):
+    def test_getattr(self, space):
         charp1 = rffi.str2charp("__len__")
         charp2 = rffi.str2charp("not_real")
-        assert api.PyObject_GetAttrString(space.wrap(""), charp1)
-        assert not api.PyObject_GetAttrString(space.wrap(""), charp2)
-        assert api.PyErr_Occurred() is space.w_AttributeError
-        api.PyErr_Clear()
-        assert api.PyObject_DelAttrString(space.wrap(""), charp1) == -1
-        assert api.PyErr_Occurred() is space.w_AttributeError
-        api.PyErr_Clear()
+        assert PyObject_GetAttrString(space, space.wrap(""), charp1)
+
+        with raises_w(space, AttributeError):
+            PyObject_GetAttrString(space, space.wrap(""), charp2)
+        with raises_w(space, AttributeError):
+            PyObject_DelAttrString(space, space.wrap(""), charp1)
         rffi.free_charp(charp1)
         rffi.free_charp(charp2)
 
-        assert api.PyObject_GetAttr(space.wrap(""), space.wrap("__len__"))
-        assert api.PyObject_DelAttr(space.wrap(""), space.wrap("__len__")) == -1
-        api.PyErr_Clear()
+        assert PyObject_GetAttr(space, space.wrap(""), space.wrap("__len__"))
+        with raises_w(space, AttributeError):
+            PyObject_DelAttr(space, space.wrap(""), space.wrap("__len__"))
 
     def test_getitem(self, space, api):
         w_t = space.wrap((1, 2, 3, 4, 5))
@@ -88,9 +95,8 @@
         assert space.getitem(w_d, space.wrap("key")) is space.w_None
 
         assert api.PyObject_DelItem(w_d, space.wrap("key")) == 0
-        assert api.PyObject_GetItem(w_d, space.wrap("key")) is None
-        assert api.PyErr_Occurred() is space.w_KeyError
-        api.PyErr_Clear()
+        with raises_w(space, KeyError):
+            PyObject_GetItem(space, w_d, space.wrap("key"))
 
     def test_size(self, space, api):
         assert api.PyObject_Size(space.newlist([space.w_None])) == 1
@@ -119,9 +125,9 @@
             w_o2 = space.wrap(o2)
 
             for opid, expected in [
-                    (Py_LT, o1 <  o2), (Py_LE, o1 <= o2),
+                    (Py_LT, o1 < o2), (Py_LE, o1 <= o2),
                     (Py_NE, o1 != o2), (Py_EQ, o1 == o2),
-                    (Py_GT, o1 >  o2), (Py_GE, o1 >= o2)]:
+                    (Py_GT, o1 > o2), (Py_GE, o1 >= o2)]:
                 assert compare(w_o1, w_o2, opid) == expected
 
         test_compare(1, 2)
@@ -129,9 +135,8 @@
         test_compare('2', '1')
 
         w_i = space.wrap(1)
-        assert api.PyObject_RichCompareBool(w_i, w_i, 123456) == -1
-        assert api.PyErr_Occurred() is space.w_SystemError
-        api.PyErr_Clear()
+        with raises_w(space, SystemError):
+            PyObject_RichCompareBool(space, w_i, w_i, 123456)
 
     def test_IsInstance(self, space, api):
         assert api.PyObject_IsInstance(space.wrap(1), space.w_int) == 1
@@ -140,8 +145,8 @@
         assert api.PyObject_IsInstance(
             space.wrap(1), space.newtuple([space.w_int, space.w_float])) == 1
         assert api.PyObject_IsInstance(space.w_type, space.w_type) == 1
-        assert api.PyObject_IsInstance(space.wrap(1), space.w_None) == -1
-        api.PyErr_Clear()
+        with raises_w(space, TypeError):
+            PyObject_IsInstance(space, space.wrap(1), space.w_None)
 
     def test_IsSubclass(self, space, api):
         assert api.PyObject_IsSubclass(space.w_type, space.w_type) == 1
@@ -149,14 +154,13 @@
         assert api.PyObject_IsSubclass(space.w_object, space.w_type) == 0
         assert api.PyObject_IsSubclass(
             space.w_type, space.newtuple([space.w_int, space.w_type])) == 1
-        assert api.PyObject_IsSubclass(space.wrap(1), space.w_type) == -1
-        api.PyErr_Clear()
+        with raises_w(space, TypeError):
+            PyObject_IsSubclass(space, space.wrap(1), space.w_type)
 
     def test_fileno(self, space, api):
         assert api.PyObject_AsFileDescriptor(space.wrap(1)) == 1
-        assert api.PyObject_AsFileDescriptor(space.wrap(-20)) == -1
-        assert api.PyErr_Occurred() is space.w_ValueError
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PyObject_AsFileDescriptor(space, space.wrap(-20))
 
         w_File = space.appexec([], """():
             class File:
@@ -169,9 +173,8 @@
     def test_hash(self, space, api):
         assert api.PyObject_Hash(space.wrap(72)) == 72
         assert api.PyObject_Hash(space.wrap(-1)) == -2
-        assert (api.PyObject_Hash(space.wrap([])) == -1 and
-            api.PyErr_Occurred() is space.w_TypeError)
-        api.PyErr_Clear()
+        with raises_w(space, TypeError):
+            PyObject_Hash(space, space.wrap([]))
 
     def test_hash_double(self, space, api):
         assert api._Py_HashDouble(72.0) == 72
@@ -191,16 +194,15 @@
             assert ptr[0] == -1
             assert api.PyObject_Cmp(w("a"), w("a"), ptr) == 0
             assert ptr[0] == 0
-            assert api.PyObject_Cmp(w(u"\xe9"), w("\xe9"), ptr) < 0
-            assert api.PyErr_Occurred()
-            api.PyErr_Clear()
+            with raises_w(space, UnicodeDecodeError):
+                PyObject_Cmp(space, w(u"\xe9"), w("\xe9"), ptr)
 
     def test_unicode(self, space, api):
         assert space.unwrap(api.PyObject_Unicode(None)) == u"<NULL>"
         assert space.unwrap(api.PyObject_Unicode(space.wrap([]))) == u"[]"
         assert space.unwrap(api.PyObject_Unicode(space.wrap("e"))) == u"e"
-        assert api.PyObject_Unicode(space.wrap("\xe9")) is None
-        api.PyErr_Clear()
+        with raises_w(space, UnicodeDecodeError):
+            PyObject_Unicode(space, space.wrap("\xe9"))
 
     def test_dir(self, space, api):
         w_dir = api.PyObject_Dir(space.sys)
@@ -218,7 +220,7 @@
         from pypy.interpreter import gateway
 
         AppTestCpythonExtensionBase.setup_class.im_func(cls)
-        tmpname = str(py.test.ensuretemp('out', dir=0))
+        tmpname = str(pytest.ensuretemp('out', dir=0))
         if cls.runappdirect:
             cls.tmpname = tmpname
         else:
diff --git a/pypy/module/cpyext/test/test_pyerrors.py b/pypy/module/cpyext/test/test_pyerrors.py
--- a/pypy/module/cpyext/test/test_pyerrors.py
+++ b/pypy/module/cpyext/test/test_pyerrors.py
@@ -5,9 +5,7 @@
 from pypy.module.cpyext.state import State
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from rpython.rtyper.lltypesystem import rffi, ll2ctypes
-
-from pypy.interpreter.gateway import interp2app
+from rpython.rtyper.lltypesystem import rffi
 
 class TestExceptions(BaseApiTest):
     def test_GivenExceptionMatches(self, space, api):
@@ -70,13 +68,6 @@
         assert space.eq_w(state.operror.w_type, space.w_MemoryError)
         api.PyErr_Clear()
 
-    def test_BadArgument(self, space, api):
-        ret = api.PyErr_BadArgument()
-        state = space.fromcache(State)
-        assert space.eq_w(state.operror.w_type, space.w_TypeError)
-        assert ret == 0
-        api.PyErr_Clear()
-
     def test_Warning(self, space, api, capfd):
         message = rffi.str2charp("this is a warning")
         api.PyErr_WarnEx(None, message, 1)
@@ -369,7 +360,7 @@
         "XXX seems to pass, but doesn't: 'py.test -s' shows errors in PyObject_Free")
     def test_GetSetExcInfo(self):
         import sys
-        if self.runappdirect and (sys.version_info.major < 3 or 
+        if self.runappdirect and (sys.version_info.major < 3 or
                                   sys.version_info.minor < 3):
             skip('PyErr_{GS}etExcInfo introduced in python 3.3')
         module = self.import_extension('foo', [
diff --git a/pypy/module/cpyext/test/test_pystrtod.py b/pypy/module/cpyext/test/test_pystrtod.py
--- a/pypy/module/cpyext/test/test_pystrtod.py
+++ b/pypy/module/cpyext/test/test_pystrtod.py
@@ -1,67 +1,62 @@
 import math
 
 from pypy.module.cpyext import pystrtod
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rtyper.lltypesystem import lltype
+from pypy.module.cpyext.pystrtod import PyOS_string_to_double
 
 
 class TestPyOS_string_to_double(BaseApiTest):
 
-    def test_simple_float(self, api):
+    def test_simple_float(self, space):
         s = rffi.str2charp('0.4')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, None)
+        r = PyOS_string_to_double(space, s, null, None)
         assert r == 0.4
         rffi.free_charp(s)
 
-    def test_empty_string(self, api):
+    def test_empty_string(self, space):
         s = rffi.str2charp('')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, None)
-        assert r == -1.0
-        raises(ValueError)
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PyOS_string_to_double(space, s, null, None)
         rffi.free_charp(s)
 
-    def test_bad_string(self, api):
+    def test_bad_string(self, space):
         s = rffi.str2charp(' 0.4')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, None)
-        assert r == -1.0
-        raises(ValueError)
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PyOS_string_to_double(space, s, null, None)
         rffi.free_charp(s)
 
-    def test_overflow_pos(self, api):
+    def test_overflow_pos(self, space):
         s = rffi.str2charp('1e500')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, None)
+        r = PyOS_string_to_double(space, s, null, None)
         assert math.isinf(r)
         assert r > 0
         rffi.free_charp(s)
 
-    def test_overflow_neg(self, api):
+    def test_overflow_neg(self, space):
         s = rffi.str2charp('-1e500')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, None)
+        r = PyOS_string_to_double(space, s, null, None)
         assert math.isinf(r)
         assert r < 0
         rffi.free_charp(s)
 
-    def test_overflow_exc(self, space, api):
+    def test_overflow_exc(self, space):
         s = rffi.str2charp('1e500')
         null = lltype.nullptr(rffi.CCHARPP.TO)
-        r = api.PyOS_string_to_double(s, null, space.w_ValueError)
-        assert r == -1.0
-        raises(ValueError)
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PyOS_string_to_double(space, s, null, space.w_ValueError)
         rffi.free_charp(s)
 
-    def test_endptr_number(self, api):
+    def test_endptr_number(self, space):
         s = rffi.str2charp('0.4')
         endp = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
-        r = api.PyOS_string_to_double(s, endp, None)
+        r = PyOS_string_to_double(space, s, endp, None)
         assert r == 0.4
         endp_addr = rffi.cast(rffi.LONG, endp[0])
         s_addr = rffi.cast(rffi.LONG, s)
@@ -69,10 +64,10 @@
         rffi.free_charp(s)
         lltype.free(endp, flavor='raw')
 
-    def test_endptr_tail(self, api):
+    def test_endptr_tail(self, space):
         s = rffi.str2charp('0.4 foo')
         endp = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
-        r = api.PyOS_string_to_double(s, endp, None)
+        r = PyOS_string_to_double(space, s, endp, None)
         assert r == 0.4
         endp_addr = rffi.cast(rffi.LONG, endp[0])
         s_addr = rffi.cast(rffi.LONG, s)
@@ -80,16 +75,14 @@
         rffi.free_charp(s)
         lltype.free(endp, flavor='raw')
 
-    def test_endptr_no_conversion(self, api):
+    def test_endptr_no_conversion(self, space):
         s = rffi.str2charp('foo')
         endp = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
-        r = api.PyOS_string_to_double(s, endp, None)
-        assert r == -1.0
-        raises(ValueError)
+        with raises_w(space, ValueError):
+            PyOS_string_to_double(space, s, endp, None)
         endp_addr = rffi.cast(rffi.LONG, endp[0])
         s_addr = rffi.cast(rffi.LONG, s)
         assert endp_addr == s_addr
-        api.PyErr_Clear()
         rffi.free_charp(s)
         lltype.free(endp, flavor='raw')
 
@@ -164,4 +157,4 @@
         r = api.PyOS_double_to_string(3.14, 'g', 3, 0, ptype)
         assert '3.14' == rffi.charp2str(r)
         assert ptype == lltype.nullptr(rffi.INTP.TO)
-        rffi.free_charp(r)
\ No newline at end of file
+        rffi.free_charp(r)
diff --git a/pypy/module/cpyext/test/test_sequence.py b/pypy/module/cpyext/test/test_sequence.py
--- a/pypy/module/cpyext/test/test_sequence.py
+++ b/pypy/module/cpyext/test/test_sequence.py
@@ -1,9 +1,12 @@
-from rpython.rtyper.lltypesystem import rffi, lltype
+from rpython.rtyper.lltypesystem import rffi
 from pypy.interpreter.error import OperationError
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from pypy.module.cpyext import sequence
-import py.test
+from pypy.module.cpyext.sequence import (
+    PySequence_Fast, PySequence_Contains, PySequence_Index,
+    PySequence_GetItem, PySequence_SetItem, PySequence_DelItem)
+
+import pytest
 
 class TestSequence(BaseApiTest):
     def test_check(self, space, api):
@@ -56,16 +59,12 @@
         w_t2 = api.PySequence_InPlaceRepeat(w_t1, 3)
         assert space.unwrap(w_t2) == [0, 1, 0, 1, 0, 1]
 
-    def test_exception(self, space, api):
+    def test_exception(self, space):
         message = rffi.str2charp("message")
-        assert not api.PySequence_Fast(space.wrap(3), message)
-        assert api.PyErr_Occurred() is space.w_TypeError
-        api.PyErr_Clear()
-
-        exc = raises(OperationError, sequence.PySequence_Fast,
-                     space, space.wrap(3), message)
-        assert exc.value.match(space, space.w_TypeError)
-        assert space.str_w(exc.value.get_w_value(space)) == "message"
+        with pytest.raises(OperationError) as excinfo:
+            PySequence_Fast(space, space.wrap(3), message)
+        assert excinfo.value.match(space, space.w_TypeError)
+        assert space.str_w(excinfo.value.get_w_value(space)) == "message"
         rffi.free_charp(message)
 
     def test_get_slice(self, space, api):
@@ -80,7 +79,7 @@
 
     def test_get_slice_fast(self, space, api):
         w_t = space.wrap([1, 2, 3, 4, 5])
-        api.PySequence_Fast(w_t, "foo") # converts
+        api.PySequence_Fast(w_t, "foo")  # converts
         assert space.unwrap(api.PySequence_GetSlice(w_t, 2, 4)) == [3, 4]
         assert space.unwrap(api.PySequence_GetSlice(w_t, 1, -1)) == [2, 3, 4]
 
@@ -97,13 +96,12 @@
         exc = raises(OperationError, space.next, w_iter)
         assert exc.value.match(space, space.w_StopIteration)
 
-    def test_contains(self, space, api):
+    def test_contains(self, space):
         w_t = space.wrap((1, 'ha'))
-        assert api.PySequence_Contains(w_t, space.wrap(u'ha'))
-        assert not api.PySequence_Contains(w_t, space.wrap(2))
-        assert api.PySequence_Contains(space.w_None, space.wrap(2)) == -1
-        assert api.PyErr_Occurred()
-        api.PyErr_Clear()
+        assert PySequence_Contains(space, w_t, space.wrap(u'ha'))
+        assert not PySequence_Contains(space, w_t, space.wrap(2))
+        with raises_w(space, TypeError):
+            PySequence_Contains(space, space.w_None, space.wrap(2))
 
     def test_setitem(self, space, api):
         w_value = space.wrap(42)
@@ -112,39 +110,33 @@
         result = api.PySequence_SetItem(l, 0, w_value)
         assert result != -1
         assert space.eq_w(space.getitem(l, space.wrap(0)), w_value)
-
-        self.raises(space, api, IndexError, api.PySequence_SetItem,
-                    l, 3, w_value)
+        with raises_w(space, IndexError):
+            PySequence_SetItem(space, l, 3, w_value)
 
         t = api.PyTuple_New(1)
         api.PyTuple_SetItem(t, 0, l)
-        self.raises(space, api, TypeError, api.PySequence_SetItem,
-                    t, 0, w_value)
-
-        self.raises(space, api, TypeError, api.PySequence_SetItem,
-                    space.newdict(), 0, w_value)
+        with raises_w(space, TypeError):
+            PySequence_SetItem(space, t, 0, w_value)
+        with raises_w(space, TypeError):
+            PySequence_SetItem(space, space.newdict(), 0, w_value)
 
     def test_delitem(self, space, api):
         w_l = space.wrap([1, 2, 3, 4])
-
         result = api.PySequence_DelItem(w_l, 2)
         assert result == 0
         assert space.eq_w(w_l, space.wrap([1, 2, 4]))
-
-        self.raises(space, api, IndexError, api.PySequence_DelItem,
-                    w_l, 3)
+        with raises_w(space, IndexError):
+            PySequence_DelItem(space, w_l, 3)
 
     def test_getitem(self, space, api):
         thelist = [8, 7, 6, 5, 4, 3, 2, 1]
         w_l = space.wrap(thelist)
-
         result = api.PySequence_GetItem(w_l, 4)
         assert space.is_true(space.eq(result, space.wrap(4)))
-
         result = api.PySequence_ITEM(w_l, 4)
         assert space.is_true(space.eq(result, space.wrap(4)))
-
-        self.raises(space, api, IndexError, api.PySequence_GetItem, w_l, 9000)
+        with raises_w(space, IndexError):
+            PySequence_GetItem(space, w_l, 9000)
 
     def test_index(self, space, api):
         thelist = [9, 8, 7, 6, 5, 4, 3, 2, 1]
@@ -155,10 +147,8 @@
         assert result == thelist.index(5)
 
         w_tofind = space.wrap(9001)
-        result = api.PySequence_Index(w_l, w_tofind)
-        assert result == -1
-        assert api.PyErr_Occurred() is space.w_ValueError
-        api.PyErr_Clear()
+        with raises_w(space, ValueError):
+            PySequence_Index(space, w_l, w_tofind)
 
         w_gen = space.appexec([], """():
            return (x ** 2 for x in range(40))""")
@@ -196,7 +186,7 @@
         assert space.int_w(space.len(w_l)) == 4
         assert space.int_w(space.getitem(w_l, space.wrap(1))) == 2
         assert space.int_w(space.getitem(w_l, space.wrap(0))) == 1
-        e = py.test.raises(OperationError, space.getitem, w_l, space.wrap(15))
+        e = pytest.raises(OperationError, space.getitem, w_l, space.wrap(15))
         assert "list index out of range" in e.value.errorstr(space)
         assert space.int_w(space.getitem(w_l, space.wrap(-1))) == 4
         space.setitem(w_l, space.wrap(1), space.wrap(13))
diff --git a/pypy/module/cpyext/test/test_setobject.py b/pypy/module/cpyext/test/test_setobject.py
--- a/pypy/module/cpyext/test/test_setobject.py
+++ b/pypy/module/cpyext/test/test_setobject.py
@@ -1,31 +1,28 @@
-import py
-
-from pypy.module.cpyext.pyobject import PyObject, PyObjectP, make_ref, from_ref
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from rpython.rtyper.lltypesystem import rffi, lltype
+from pypy.module.cpyext.setobject import PySet_Add, PySet_Size
 
 
 class TestTupleObject(BaseApiTest):
     def test_setobj(self, space, api):
         assert not api.PySet_Check(space.w_None)
         assert not api.PyFrozenSet_Check(space.w_None)
-        assert api.PySet_Add(space.w_None, space.w_None) == -1
-        api.PyErr_Clear()
+        with raises_w(space, SystemError):
+            PySet_Add(space, space.w_None, space.w_None)
         w_set = space.call_function(space.w_set)
         assert not api.PyFrozenSet_CheckExact(w_set)
-        space.call_method(w_set, 'update', space.wrap([1,2,3,4]))
+        space.call_method(w_set, 'update', space.wrap([1, 2, 3, 4]))
         assert api.PySet_Size(w_set) == 4
         assert api.PySet_GET_SIZE(w_set) == 4
-        raises(TypeError, api.PySet_Size(space.newlist([])))
-        api.PyErr_Clear()
+        with raises_w(space, TypeError):
+            PySet_Size(space, space.newlist([]))
 
     def test_set_add_discard(self, space, api):
         w_set = api.PySet_New(None)
         assert api.PySet_Size(w_set) == 0
-        w_set = api.PyFrozenSet_New(space.wrap([1,2,3,4]))
+        w_set = api.PyFrozenSet_New(space.wrap([1, 2, 3, 4]))
         assert api.PySet_Size(w_set) == 4
-        w_set = api.PySet_New(space.wrap([1,2,3,4]))
+        w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
         assert api.PySet_Size(w_set) == 4
         api.PySet_Add(w_set, space.wrap(6))
         assert api.PySet_Size(w_set) == 5
@@ -33,14 +30,14 @@
         assert api.PySet_Size(w_set) == 4
 
     def test_set_contains(self, space, api):
-        w_set = api.PySet_New(space.wrap([1,2,3,4]))
+        w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
         assert api.PySet_Contains(w_set, space.wrap(1))
         assert not api.PySet_Contains(w_set, space.wrap(0))
 
     def test_set_pop_clear(self, space, api):
-        w_set = api.PySet_New(space.wrap([1,2,3,4]))
+        w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
         w_obj = api.PySet_Pop(w_set)
-        assert space.int_w(w_obj) in (1,2,3,4)
+        assert space.int_w(w_obj) in (1, 2, 3, 4)
         assert space.len_w(w_set) == 3
         api.PySet_Clear(w_set)
         assert space.len_w(w_set) == 0
@@ -72,6 +69,5 @@
              PySet_GET_SIZE(dumb_pointer);
 
              return o;
-             """
-            )
+             """)
         ])
diff --git a/pypy/module/cpyext/test/test_tupleobject.py b/pypy/module/cpyext/test/test_tupleobject.py
--- a/pypy/module/cpyext/test/test_tupleobject.py
+++ b/pypy/module/cpyext/test/test_tupleobject.py
@@ -1,24 +1,26 @@
 import py
 
 from pypy.module.cpyext.pyobject import PyObject, PyObjectP, make_ref, from_ref
-from pypy.module.cpyext.tupleobject import PyTupleObject
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rlib.debug import FatalError
+from pypy.module.cpyext.tupleobject import (
+    PyTupleObject, PyTuple_SetItem, PyTuple_Size)
 
 
 class TestTupleObject(BaseApiTest):
 
     def test_tupleobject(self, space, api):
         assert not api.PyTuple_Check(space.w_None)
-        assert api.PyTuple_SetItem(space.w_None, 0, space.w_None) == -1
+        with raises_w(space, SystemError):
+            PyTuple_SetItem(space, space.w_None, 0, space.w_None)
         atuple = space.newtuple([space.wrap(0), space.wrap(1),
                                  space.wrap('yay')])
         assert api.PyTuple_Size(atuple) == 3
         #assert api.PyTuple_GET_SIZE(atuple) == 3  --- now a C macro
-        raises(TypeError, api.PyTuple_Size(space.newlist([])))
-        api.PyErr_Clear()
+        with raises_w(space, SystemError):
+            PyTuple_Size(space, space.newlist([]))
 
     def test_tuple_realize_refuses_nulls(self, space, api):
         py_tuple = api.PyTuple_New(1)
diff --git a/pypy/module/cpyext/test/test_weakref.py b/pypy/module/cpyext/test/test_weakref.py
--- a/pypy/module/cpyext/test/test_weakref.py
+++ b/pypy/module/cpyext/test/test_weakref.py
@@ -1,5 +1,6 @@
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
-from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
+from pypy.module.cpyext.weakrefobject import PyWeakref_NewRef
 
 class TestWeakReference(BaseApiTest):
     def test_weakref(self, space, api):
@@ -10,12 +11,11 @@
         assert space.is_w(api.PyWeakref_LockObject(w_ref), w_obj)
 
         w_obj = space.newtuple([])
-        assert api.PyWeakref_NewRef(w_obj, space.w_None) is None
-        assert api.PyErr_Occurred() is space.w_TypeError
-        api.PyErr_Clear()
+        with raises_w(space, TypeError):
+            PyWeakref_NewRef(space, w_obj, space.w_None)
 
     def test_proxy(self, space, api):
-        w_obj = space.w_Warning # some weakrefable object
+        w_obj = space.w_Warning  # some weakrefable object
         w_proxy = api.PyWeakref_NewProxy(w_obj, None)
         assert space.unwrap(space.str(w_proxy)) == "<type 'exceptions.Warning'>"
         assert space.unwrap(space.repr(w_proxy)).startswith('<weak')


More information about the pypy-commit mailing list