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

afa at codespeak.net afa at codespeak.net
Wed Apr 21 01:40:18 CEST 2010


Author: afa
Date: Wed Apr 21 01:40:16 2010
New Revision: 73925

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:
PyLong_FromString


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	Wed Apr 21 01:40:16 2010
@@ -1,5 +1,6 @@
 from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.module.cpyext.api import cpython_api, PyObject, build_type_checkers, ADDR
+from pypy.module.cpyext.api import (cpython_api, PyObject, build_type_checkers,
+                                    CONST_STRING, ADDR)
 from pypy.objspace.std.longobject import W_LongObject
 from pypy.interpreter.error import OperationError
 
@@ -47,6 +48,24 @@
     raised."""
     return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(w_long))
 
+ at cpython_api([CONST_STRING, rffi.CCHARP, rffi.INT_real], PyObject)
+def PyLong_FromString(space, str, pend, base):
+    """Return a new PyLongObject based on the string value in str, which is
+    interpreted according to the radix in base.  If pend is non-NULL,
+    *pend will point to the first character in str which follows the
+    representation of the number.  If base is 0, the radix will be determined
+    based on the leading characters of str: if str starts with '0x' or
+    '0X', radix 16 will be used; if str starts with '0', radix 8 will be
+    used; otherwise radix 10 will be used.  If base is not 0, it must be
+    between 2 and 36, inclusive.  Leading spaces are ignored.  If there are
+    no digits, ValueError will be raised."""
+    s = rffi.charp2str(str)
+    w_str = space.wrap(s)
+    w_base = space.wrap(base)
+    if pend:
+        pend[0] = rffi.ptradd(str, len(s))
+    return space.call_function(space.w_long, w_str, w_base)
+
 @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	Wed Apr 21 01:40:16 2010
@@ -3435,19 +3435,6 @@
     NULL on failure."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP, {char**}, rffi.INT_real], PyObject)
-def PyLong_FromString(space, str, pend, base):
-    """Return a new PyLongObject based on the string value in str, which is
-    interpreted according to the radix in base.  If pend is non-NULL,
-    *pend will point to the first character in str which follows the
-    representation of the number.  If base is 0, the radix will be determined
-    based on the leading characters of str: if str starts with '0x' or
-    '0X', radix 16 will be used; if str starts with '0', radix 8 will be
-    used; otherwise radix 10 will be used.  If base is not 0, it must be
-    between 2 and 36, inclusive.  Leading spaces are ignored.  If there are
-    no digits, ValueError will be raised."""
-    raise NotImplementedError
-
 @cpython_api([{Py_UNICODE*}, Py_ssize_t, rffi.INT_real], PyObject)
 def PyLong_FromUnicode(space, u, length, base):
     """Convert a sequence of Unicode digits to a Python long integer value.  The first

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	Wed Apr 21 01:40:16 2010
@@ -64,3 +64,12 @@
              """)])
         assert module.from_longlong() == -1
         assert module.from_unsignedlonglong() == (1<<64) - 1
+
+    def test_fromstring(self):
+        module = self.import_extension('foo', [
+            ("from_string", "METH_NOARGS",
+             """
+                 return PyLong_FromString("0x1234", NULL, 0);
+             """),
+            ])
+        assert module.from_string() == 0x1234



More information about the Pypy-commit mailing list