[pypy-svn] r73982 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Thu Apr 22 19:48:04 CEST 2010


Author: afa
Date: Thu Apr 22 19:48:03 2010
New Revision: 73982

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/number.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_number.py
Log:
Add PyNumber_Check and PyNumber_Long


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/number.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/number.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/number.py	Thu Apr 22 19:48:03 2010
@@ -15,6 +15,16 @@
     except OperationError:
         return 0
 
+ at cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
+def PyNumber_Check(space, w_obj):
+    """Returns 1 if the object o provides numeric protocols, and false otherwise.
+    This function always succeeds."""
+    try:
+        space.float_w(w_obj)
+        return 1
+    except OperationError:
+        return 0
+
 @cpython_api([PyObject, PyObject], Py_ssize_t, error=-1)
 def PyNumber_AsSsize_t(space, w_obj, w_exc):
     """Returns o converted to a Py_ssize_t value if o can be interpreted as an
@@ -25,7 +35,13 @@
     exception is cleared and the value is clipped to PY_SSIZE_T_MIN for a negative
     integer or PY_SSIZE_T_MAX for a positive integer.
     """
-    return space.int_w(w_obj) #XXX: this is wrong
+    return space.int_w(w_obj) #XXX: this is wrong on win64
+
+ at cpython_api([PyObject], PyObject)
+def PyNumber_Long(space, w_obj):
+    """    Returns the o converted to a long integer object on success, or NULL on
+    failure.  This is the equivalent of the Python expression long(o)."""
+    return space.long(w_obj)
 
 def func_rename(newname):
     return lambda func: func_with_new_name(func, newname)

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_number.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_number.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_number.py	Thu Apr 22 19:48:03 2010
@@ -4,10 +4,22 @@
 from pypy.module.cpyext import sequence
 
 class TestIterator(BaseApiTest):
-    def test_index(self, space, api):
+    def test_check(self, space, api):
         assert api.PyIndex_Check(space.wrap(12))
+        assert api.PyIndex_Check(space.wrap(-12L))
+        assert not api.PyIndex_Check(space.wrap(12.1))
         assert not api.PyIndex_Check(space.wrap('12'))
 
+        assert api.PyNumber_Check(space.wrap(12))
+        assert api.PyNumber_Check(space.wrap(-12L))
+        assert api.PyNumber_Check(space.wrap(12.1))
+        assert not api.PyNumber_Check(space.wrap('12'))
+        assert not api.PyNumber_Check(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)
+
     def test_numbermethods(self, space, api):
         assert "ab" == space.unwrap(
             api.PyNumber_Add(space.wrap("a"), space.wrap("b")))



More information about the Pypy-commit mailing list