Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r91659:5a8f2cdedcd0
Date: 2017-06-29 19:25 +0100
http://bitbucket.org/pypy/pypy/changeset/5a8f2cdedcd0/

Log:    Don't use magical api object in pypy.module.cpyext.test.test_number

diff --git a/pypy/module/cpyext/number.py b/pypy/module/cpyext/number.py
--- a/pypy/module/cpyext/number.py
+++ b/pypy/module/cpyext/number.py
@@ -95,53 +95,58 @@
 def func_rename(newname):
     return lambda func: func_with_new_name(func, newname)
 
-def make_numbermethod(name, spacemeth):
+def make_numbermethod(cname, spacemeth):
     @cpython_api([PyObject, PyObject], PyObject)
-    @func_rename('PyNumber_%s' % (name,))
+    @func_rename(cname)
     def PyNumber_Method(space, w_o1, w_o2):
         meth = getattr(space, spacemeth)
         return meth(w_o1, w_o2)
+    return PyNumber_Method
 
 def make_unary_numbermethod(name, spacemeth):
     @cpython_api([PyObject], PyObject)
-    @func_rename('PyNumber_%s' % (name,))
+    @func_rename(cname)
     def PyNumber_Method(space, w_o1):
         meth = getattr(space, spacemeth)
         return meth(w_o1)
+    return PyNumber_Method
 
-def make_inplace_numbermethod(name, spacemeth):
+def make_inplace_numbermethod(cname, spacemeth):
     spacemeth = 'inplace_' + spacemeth.rstrip('_')
     @cpython_api([PyObject, PyObject], PyObject)
-    @func_rename('PyNumber_InPlace%s' % (name,))
+    @func_rename(cname)
     def PyNumber_Method(space, w_o1, w_o2):
         meth = getattr(space, spacemeth)
         return meth(w_o1, w_o2)
+    return PyNumber_Method
 
 for name, spacemeth in [
-    ('Add', 'add'),
-    ('Subtract', 'sub'),
-    ('Multiply', 'mul'),
-    ('Divide', 'div'),
-    ('FloorDivide', 'floordiv'),
-    ('TrueDivide', 'truediv'),
-    ('Remainder', 'mod'),
-    ('Lshift', 'lshift'),
-    ('Rshift', 'rshift'),
-    ('And', 'and_'),
-    ('Xor', 'xor'),
-    ('Or', 'or_'),
-    ('Divmod', 'divmod'),
-    ]:
-    make_numbermethod(name, spacemeth)
+        ('Add', 'add'),
+        ('Subtract', 'sub'),
+        ('Multiply', 'mul'),
+        ('Divide', 'div'),
+        ('FloorDivide', 'floordiv'),
+        ('TrueDivide', 'truediv'),
+        ('Remainder', 'mod'),
+        ('Lshift', 'lshift'),
+        ('Rshift', 'rshift'),
+        ('And', 'and_'),
+        ('Xor', 'xor'),
+        ('Or', 'or_'),
+        ('Divmod', 'divmod')]:
+    cname = 'PyNumber_%s' % (name,)
+    globals()[cname] = make_numbermethod(cname, spacemeth)
     if name != 'Divmod':
-        make_inplace_numbermethod(name, spacemeth)
+        cname = 'PyNumber_InPlace%s' % (name,)
+        globals()[cname] = make_inplace_numbermethod(cname, spacemeth)
 
 for name, spacemeth in [
-    ('Negative', 'neg'),
-    ('Positive', 'pos'),
-    ('Absolute', 'abs'),
-    ('Invert', 'invert')]:
-    make_unary_numbermethod(name, spacemeth)
+        ('Negative', 'neg'),
+        ('Positive', 'pos'),
+        ('Absolute', 'abs'),
+        ('Invert', 'invert')]:
+    cname = 'PyNumber_%s' % (name,)
+    globals()[cname] = make_unary_numbermethod(cname, spacemeth)
 
 @cpython_api([PyObject, PyObject, PyObject], PyObject)
 def PyNumber_Power(space, w_o1, w_o2, w_o3):
diff --git a/pypy/module/cpyext/test/test_number.py 
b/pypy/module/cpyext/test/test_number.py
--- a/pypy/module/cpyext/test/test_number.py
+++ b/pypy/module/cpyext/test/test_number.py
@@ -1,53 +1,64 @@
 import py
+import pytest
 from rpython.rtyper.lltypesystem import lltype
+from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_api import BaseApiTest
-from pypy.module.cpyext.pyobject import PyObjectP, from_ref, make_ref, 
Py_DecRef
+from pypy.module.cpyext.pyobject import (
+    PyObjectP, from_ref, make_ref, Py_DecRef)
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.module.cpyext.number import (
+    PyIndex_Check, PyNumber_Check, PyNumber_Long, PyNumber_Int,
+    PyNumber_Index, PyNumber_Coerce, PyNumber_CoerceEx, PyNumber_Add,
+    PyNumber_Multiply, PyNumber_InPlaceMultiply, PyNumber_Absolute,
+    PyNumber_Power, PyNumber_InPlacePower)
+from pypy.module.cpyext.floatobject import PyFloat_Check
+from pypy.module.cpyext.intobject import PyInt_CheckExact
+from pypy.module.cpyext.longobject import PyLong_CheckExact
+from pypy.module.cpyext.object import PyObject_Size
 
 class TestIterator(BaseApiTest):
-    def test_check(self, space, api):
-        assert api.PyIndex_Check(space.wrap(12))
-        assert api.PyIndex_Check(space.wraplong(-12L))
-        assert not api.PyIndex_Check(space.wrap(12.1))
-        assert not api.PyIndex_Check(space.wrap('12'))
+    def test_check(self, space):
+        assert PyIndex_Check(space, space.wrap(12))
+        assert PyIndex_Check(space, space.wraplong(-12L))
+        assert not PyIndex_Check(space, space.wrap(12.1))
+        assert not PyIndex_Check(space, space.wrap('12'))
 
-        assert api.PyNumber_Check(space.wrap(12))
-        assert api.PyNumber_Check(space.wraplong(-12L))
-        assert api.PyNumber_Check(space.wrap(12.1))
-        assert not api.PyNumber_Check(space.wrap('12'))
-        assert api.PyNumber_Check(space.wrap(1+3j))
+        assert PyNumber_Check(space, space.wrap(12))
+        assert PyNumber_Check(space, space.wraplong(-12L))
+        assert PyNumber_Check(space, space.wrap(12.1))
+        assert not PyNumber_Check(space, space.wrap('12'))
+        assert PyNumber_Check(space, space.wrap(1 + 3j))
 
-    def test_number_long(self, space, api):
-        w_l = api.PyNumber_Long(space.wrap(123))
-        assert api.PyLong_CheckExact(w_l)
-        w_l = api.PyNumber_Long(space.wrap("123"))
-        assert api.PyLong_CheckExact(w_l)
+    def test_number_long(self, space):
+        w_l = PyNumber_Long(space, space.wrap(123))
+        assert PyLong_CheckExact(space, w_l)
+        w_l = PyNumber_Long(space, space.wrap("123"))
+        assert PyLong_CheckExact(space, w_l)
 
-    def test_number_int(self, space, api):
-        w_l = api.PyNumber_Int(space.wraplong(123L))
-        assert api.PyInt_CheckExact(w_l)
-        w_l = api.PyNumber_Int(space.wrap(2 << 65))
-        assert api.PyLong_CheckExact(w_l)
-        w_l = api.PyNumber_Int(space.wrap(42.3))
-        assert api.PyInt_CheckExact(w_l)
-        w_l = api.PyNumber_Int(space.wrap("42"))
-        assert api.PyInt_CheckExact(w_l)
+    def test_number_int(self, space):
+        w_l = PyNumber_Int(space, space.wraplong(123L))
+        assert PyInt_CheckExact(space, w_l)
+        w_l = PyNumber_Int(space, space.wrap(2 << 65))
+        assert PyLong_CheckExact(space, w_l)
+        w_l = PyNumber_Int(space, space.wrap(42.3))
+        assert PyInt_CheckExact(space, w_l)
+        w_l = PyNumber_Int(space, space.wrap("42"))
+        assert PyInt_CheckExact(space, w_l)
 
-    def test_number_index(self, space, api):
-        w_l = api.PyNumber_Index(space.wraplong(123L))
-        assert api.PyLong_CheckExact(w_l)
-        w_l = api.PyNumber_Index(space.wrap(42.3))
-        assert w_l is None
-        api.PyErr_Clear()
+    def test_number_index(self, space):
+        w_l = PyNumber_Index(space, space.wraplong(123L))
+        assert PyLong_CheckExact(space, w_l)
+        with pytest.raises(OperationError):
+            PyNumber_Index(space, space.wrap(42.3))
 
-    def test_coerce(self, space, api):
+    def test_coerce(self, space):
         w_obj1 = space.wrap(123)
         w_obj2 = space.wrap(456.789)
         pp1 = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
         pp1[0] = make_ref(space, w_obj1)
         pp2 = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
         pp2[0] = make_ref(space, w_obj2)
-        assert api.PyNumber_Coerce(pp1, pp2) == 0
+        assert PyNumber_Coerce(space, pp1, pp2) == 0
         assert space.str_w(space.repr(from_ref(space, pp1[0]))) == '123.0'
         assert space.str_w(space.repr(from_ref(space, pp2[0]))) == '456.789'
         Py_DecRef(space, pp1[0])
@@ -58,7 +69,7 @@
         Py_DecRef(space, w_obj2)
         lltype.free(pp2, flavor='raw')
 
-    def test_number_coerce_ex(self, space, api):
+    def test_number_coerce_ex(self, space):
         pl = make_ref(space, space.wrap(123))
         pf = make_ref(space, space.wrap(42.))
         ppl = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
@@ -66,12 +77,12 @@
         ppl[0] = pl
         ppf[0] = pf
 
-        ret = api.PyNumber_CoerceEx(ppl, ppf)
+        ret = PyNumber_CoerceEx(space, ppl, ppf)
         assert ret == 0
 
         w_res = from_ref(space, ppl[0])
 
-        assert api.PyFloat_Check(w_res)
+        assert PyFloat_Check(space, w_res)
         assert space.unwrap(w_res) == 123.
         Py_DecRef(space, pl)
         Py_DecRef(space, pf)
@@ -80,31 +91,31 @@
         lltype.free(ppl, flavor='raw')
         lltype.free(ppf, flavor='raw')
 
-    def test_numbermethods(self, space, api):
+    def test_numbermethods(self, space):
         assert "ab" == space.unwrap(
-            api.PyNumber_Add(space.wrap("a"), space.wrap("b")))
+            PyNumber_Add(space, space.wrap("a"), space.wrap("b")))
         assert "aaa" == space.unwrap(
-            api.PyNumber_Multiply(space.wrap("a"), space.wrap(3)))
+            PyNumber_Multiply(space, space.wrap("a"), space.wrap(3)))
 
         w_l = space.newlist([1, 2, 3])
-        w_l2 = api.PyNumber_Multiply(w_l, space.wrap(3))
-        assert api.PyObject_Size(w_l2) == 9
-        assert api.PyObject_Size(w_l) == 3
+        w_l2 = PyNumber_Multiply(space, w_l, space.wrap(3))
+        assert PyObject_Size(space, w_l2) == 9
+        assert PyObject_Size(space, w_l) == 3
 
-        w_l3 = api.PyNumber_InPlaceMultiply(w_l, space.wrap(3))
-        assert api.PyObject_Size(w_l) == 9
+        w_l3 = PyNumber_InPlaceMultiply(space, w_l, space.wrap(3))
+        assert PyObject_Size(space, w_l) == 9
         assert w_l3 is w_l
 
         # unary function
-        assert 9 == space.unwrap(api.PyNumber_Absolute(space.wrap(-9)))
+        assert 9 == space.unwrap(PyNumber_Absolute(space, space.wrap(-9)))
 
         # power
         assert 9 == space.unwrap(
-            api.PyNumber_Power(space.wrap(3), space.wrap(2), space.w_None))
+            PyNumber_Power(space, space.wrap(3), space.wrap(2), space.w_None))
         assert 4 == space.unwrap(
-            api.PyNumber_Power(space.wrap(3), space.wrap(2), space.wrap(5)))
+            PyNumber_Power(space, space.wrap(3), space.wrap(2), space.wrap(5)))
         assert 9 == space.unwrap(
-            api.PyNumber_InPlacePower(space.wrap(3), space.wrap(2), 
space.w_None))
+            PyNumber_InPlacePower(space, space.wrap(3), space.wrap(2), 
space.w_None))
 
 class AppTestCNumber(AppTestCpythonExtensionBase):
     def test_app_coerce(self):
@@ -145,7 +156,7 @@
         assert tupl[1] == 1.
         assert isinstance(tupl[0], float)'''
 
-    def test_PyNumber_Check(self):        
+    def test_PyNumber_Check(self):
         mod = self.import_extension('foo', [
             ("test_PyNumber_Check", "METH_VARARGS",
              '''
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to