Author: Stefan H. Muller <shmuell...@gmail.com>
Branch: pypy-pyarray
Changeset: r66336:eba88c40cdfa
Date: 2013-07-30 20:24 +0200
http://bitbucket.org/pypy/pypy/changeset/eba88c40cdfa/

Log:    - cpyext/number.py, cpyext/test/test_number.py: Implement
        PyNumber_CoerceEx() and PyNumber_Coerce().

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
@@ -1,6 +1,6 @@
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.api import cpython_api, CANNOT_FAIL, Py_ssize_t
-from pypy.module.cpyext.pyobject import PyObject
+from pypy.module.cpyext.pyobject import PyObject, PyObjectP, from_ref, 
make_ref, Py_DecRef
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.tool.sourcetools import func_with_new_name
 
@@ -56,6 +56,38 @@
     """
     return space.index(w_obj)
 
+@cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1)
+def PyNumber_CoerceEx(space, pp1, pp2):
+    """
+    """
+    w_obj1 = from_ref(space, pp1[0])
+    w_obj2 = from_ref(space, pp2[0])
+    w_res = space.try_coerce(w_obj1, w_obj2)
+    if w_res is None:
+        return 1
+    else:
+        Py_DecRef(space, pp1[0])
+        Py_DecRef(space, pp2[0])
+        pp1[0] = make_ref(space, space.getitem(w_res, space.wrap(0)))
+        pp2[0] = make_ref(space, space.getitem(w_res, space.wrap(1)))
+        return 0
+
+@cpython_api([PyObjectP, PyObjectP], rffi.INT_real, error=-1)
+def PyNumber_Coerce(space, pp1, pp2):
+    """
+    """
+    w_obj1 = from_ref(space, pp1[0])
+    w_obj2 = from_ref(space, pp2[0])
+    w_res = space.coerce(w_obj1, w_obj2)
+    if w_res is None:
+        return 1
+    else:
+        Py_DecRef(space, pp1[0])
+        Py_DecRef(space, pp2[0])
+        pp1[0] = make_ref(space, space.getitem(w_res, space.wrap(0)))
+        pp2[0] = make_ref(space, space.getitem(w_res, space.wrap(1)))
+        return 0
+
 def func_rename(newname):
     return lambda func: func_with_new_name(func, newname)
 
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
@@ -2,6 +2,7 @@
 from pypy.interpreter.error import OperationError
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext import sequence
+from pypy.module.cpyext.pyobject import PyObject, PyObjectP, from_ref, 
make_ref, Py_DecRef
 
 class TestIterator(BaseApiTest):
     def test_check(self, space, api):
@@ -35,6 +36,26 @@
         assert w_l is None
         api.PyErr_Clear()
 
+    def test_number_coerce_ex(self, space, api):
+        pl = make_ref(space, space.wrap(123))
+        pf = make_ref(space, space.wrap(42.))
+        ppl = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
+        ppf = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
+        ppl[0] = pl
+        ppf[0] = pf
+        
+        ret = api.PyNumber_CoerceEx(ppl, ppf)
+        assert ret == 0
+
+        w_res = from_ref(space, ppl[0])
+
+        assert api.PyFloat_Check(w_res)
+        assert space.unwrap(w_res) == 123.
+        Py_DecRef(space, ppl[0])
+        Py_DecRef(space, ppf[0])
+        lltype.free(ppl, flavor='raw')
+        lltype.free(ppf, flavor='raw')
+       
     def test_numbermethods(self, space, api):
         assert "ab" == space.unwrap(
             api.PyNumber_Add(space.wrap("a"), space.wrap("b")))
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to