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

afa at codespeak.net afa at codespeak.net
Tue Apr 20 11:07:24 CEST 2010


Author: afa
Date: Tue Apr 20 11:07:22 2010
New Revision: 73899

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/longobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_longobject.py
Log:
Add PyLong <-> LongLong conversions


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	Tue Apr 20 11:07:22 2010
@@ -11,7 +11,19 @@
     """Return a new PyLongObject object from v, or NULL on failure."""
     return space.newlong(val)
 
- at cpython_api([PyObject], rffi.ULONG, error=0)
+ at cpython_api([rffi.LONGLONG], PyObject)
+def PyLong_FromLongLong(space, val):
+    """Return a new PyLongObject object from a C long long, or NULL
+    on failure."""
+    return space.wrap(val)
+
+ at cpython_api([rffi.ULONGLONG], PyObject)
+def PyLong_FromUnsignedLongLong(space, val):
+    """Return a new PyLongObject object from a C unsigned long long,
+    or NULL on failure."""
+    return space.wrap(val)
+
+ at cpython_api([PyObject], rffi.ULONG, error=-1)
 def PyLong_AsUnsignedLong(space, w_long):
     """
     Return a C unsigned long representation of the contents of pylong.
@@ -19,6 +31,22 @@
     raised."""
     return rffi.cast(rffi.ULONG, space.uint_w(w_long))
 
+ at cpython_api([PyObject], rffi.LONGLONG, error=-1)
+def PyLong_AsLongLong(space, w_long):
+    """
+    Return a C unsigned long representation of the contents of pylong.
+    If pylong is greater than ULONG_MAX, an OverflowError is
+    raised."""
+    return rffi.cast(rffi.LONGLONG, space.r_longlong_w(w_long))
+
+ at cpython_api([PyObject], rffi.ULONGLONG, error=-1)
+def PyLong_AsUnsignedLongLong(space, w_long):
+    """
+    Return a C unsigned long representation of the contents of pylong.
+    If pylong is greater than ULONG_MAX, an OverflowError is
+    raised."""
+    return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(w_long))
+
 @cpython_api([rffi.VOIDP], PyObject)
 def PyLong_FromVoidPtr(space, p):
     """Create a Python integer or long integer from the pointer p. The pointer value

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Tue Apr 20 11:07:22 2010
@@ -3429,18 +3429,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([{PY_LONG_LONG}], PyObject)
-def PyLong_FromLongLong(space, v):
-    """Return a new PyLongObject object from a C long long, or NULL
-    on failure."""
-    raise NotImplementedError
-
- at cpython_api([{unsigned PY_LONG_LONG}], PyObject)
-def PyLong_FromUnsignedLongLong(space, v):
-    """Return a new PyLongObject object from a C unsigned long long,
-    or NULL on failure."""
-    raise NotImplementedError
-
 @cpython_api([{double}], PyObject)
 def PyLong_FromDouble(space, v):
     """Return a new PyLongObject object from the integer part of v, or

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	Tue Apr 20 11:07:22 2010
@@ -4,6 +4,7 @@
 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
 
 
 class TestLongObject(BaseApiTest):
@@ -39,3 +40,27 @@
         l = space.call_function(L)
         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()
+
+        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()
+
+class AppTestLongObject(AppTestCpythonExtensionBase):
+    def test_fromlonglong(self):
+        module = self.import_extension('foo', [
+            ("from_longlong", "METH_NOARGS",
+             """
+                 return PyLong_FromLongLong((long long)-1);
+             """),
+            ("from_unsignedlonglong", "METH_NOARGS",
+             """
+                 return PyLong_FromUnsignedLongLong((unsigned long long)-1);
+             """)])
+        assert module.from_longlong() == -1
+        assert module.from_unsignedlonglong() == (1<<64) - 1



More information about the Pypy-commit mailing list