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

afa at codespeak.net afa at codespeak.net
Thu Apr 22 14:16:20 CEST 2010


Author: afa
Date: Thu Apr 22 14:16:18 2010
New Revision: 73971

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/intobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_intobject.py
Log:
Py_AsUnsignedLong


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/intobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/intobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/intobject.py	Thu Apr 22 14:16:18 2010
@@ -21,6 +21,13 @@
     there was an error, or whether the value just happened to be -1."""
     return space.int_w(space.int(w_obj))
 
+ at cpython_api([PyObject], lltype.Unsigned, error=-1)
+def PyInt_AsUnsignedLong(space, w_obj):
+    """Return a C unsigned long representation of the contents of pylong.
+    If pylong is greater than ULONG_MAX, an OverflowError is
+    raised."""
+    return space.uint_w(space.int(w_obj))
+
 @cpython_api([PyObject], lltype.Signed, error=CANNOT_FAIL)
 def PyInt_AS_LONG(space, w_int):
     """Return the value of the object w_int. No error checking is performed."""

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_intobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_intobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_intobject.py	Thu Apr 22 14:16:18 2010
@@ -19,6 +19,11 @@
         assert api.PyErr_Occurred() is space.w_TypeError
         api.PyErr_Clear()
 
+        assert api.PyInt_AsUnsignedLong(space.wrap(sys.maxint)) == sys.maxint
+        assert api.PyInt_AsUnsignedLong(space.wrap(-5)) == sys.maxint * 2 + 1
+        assert api.PyErr_Occurred() is space.w_ValueError
+        api.PyErr_Clear()
+
     def test_coerce(self, space, api):
         class Coerce(object):
             def __int__(self):



More information about the Pypy-commit mailing list