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

afa at codespeak.net afa at codespeak.net
Thu Apr 22 20:19:00 CEST 2010


Author: afa
Date: Thu Apr 22 20:18:58 2010
New Revision: 73987

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/longobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_longobject.py
Log:
PyLong_AsLong, PyLong_FromDouble


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/longobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/longobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/longobject.py	Thu Apr 22 20:18:58 2010
@@ -32,6 +32,14 @@
     raised."""
     return rffi.cast(rffi.ULONG, space.uint_w(w_long))
 
+ at cpython_api([PyObject], lltype.Signed, error=-1)
+def PyLong_AsLong(space, w_long):
+    """
+    Return a C long representation of the contents of pylong.  If
+    pylong is greater than LONG_MAX, an OverflowError is raised
+    and -1 will be returned."""
+    return space.int_w(w_long)
+
 @cpython_api([PyObject], rffi.LONGLONG, error=-1)
 def PyLong_AsLongLong(space, w_long):
     """
@@ -48,6 +56,11 @@
     raised."""
     return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(w_long))
 
+ at cpython_api([lltype.Float], PyObject)
+def PyLong_FromDouble(space, val):
+    """Return a new PyLongObject object from v, or NULL on failure."""
+    return space.long(space.wrap(val))
+
 @cpython_api([CONST_STRING, rffi.CCHARPP, rffi.INT_real], PyObject)
 def PyLong_FromString(space, str, pend, base):
     """Return a new PyLongObject based on the string value in str, which is

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_longobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_longobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_longobject.py	Thu Apr 22 20:18:58 2010
@@ -16,12 +16,25 @@
         value = api.PyLong_FromLong(sys.maxint + 1)
         assert isinstance(value, W_LongObject)
         assert space.unwrap(value) == sys.maxint + 1 # should obviously fail but doesnt
-    
-    def test_asulong(self, space, api):
+
+    def test_aslong(self, space, api):
         w_value = api.PyLong_FromLong((sys.maxint - 1) / 2)
-        w_value = space.mul(w_value, space.wrap(4))
+
+        w_value = space.mul(w_value, space.wrap(2))
+        value = api.PyLong_AsLong(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)
         assert value == (sys.maxint - 1) * 2
+
+    def test_fromdouble(self, space, api):
+        w_value = api.PyLong_FromDouble(-12.74)
+        assert space.unwrap(w_value) == -12
     
     def test_type_check(self, space, api):
         w_l = space.wrap(sys.maxint + 1)



More information about the Pypy-commit mailing list