[pypy-svn] r79790 - in pypy/branch/fast-forward/pypy/module/cpyext: . test
afa at codespeak.net
afa at codespeak.net
Fri Dec 3 18:54:43 CET 2010
Author: afa
Date: Fri Dec 3 18:54:41 2010
New Revision: 79790
Modified:
pypy/branch/fast-forward/pypy/module/cpyext/floatobject.py
pypy/branch/fast-forward/pypy/module/cpyext/intobject.py
pypy/branch/fast-forward/pypy/module/cpyext/stubs.py
pypy/branch/fast-forward/pypy/module/cpyext/test/test_floatobject.py
pypy/branch/fast-forward/pypy/module/cpyext/test/test_intobject.py
Log:
PyFloat_FromString, PyInt_FromString
Modified: pypy/branch/fast-forward/pypy/module/cpyext/floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/floatobject.py (original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/floatobject.py Fri Dec 3 18:54:41 2010
@@ -1,6 +1,6 @@
from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.module.cpyext.api import (CANNOT_FAIL, cpython_api, PyObject,
- build_type_checkers)
+from pypy.module.cpyext.api import (
+ CANNOT_FAIL, cpython_api, PyObject, build_type_checkers, CONST_STRING)
from pypy.interpreter.error import OperationError
PyFloat_Check, PyFloat_CheckExact = build_type_checkers("Float")
@@ -24,4 +24,12 @@
"""
Returns the o converted to a float object on success, or NULL on failure.
This is the equivalent of the Python expression float(o)."""
- return space.float(w_obj)
+ return space.call_function(space.w_float, w_obj)
+
+ at cpython_api([PyObject, rffi.CCHARPP], PyObject)
+def PyFloat_FromString(space, w_obj, _):
+ """Create a PyFloatObject object based on the string value in str, or
+ NULL on failure. The pend argument is ignored. It remains only for
+ backward compatibility."""
+ return space.call_function(space.w_float, w_obj)
+
Modified: pypy/branch/fast-forward/pypy/module/cpyext/intobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/intobject.py (original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/intobject.py Fri Dec 3 18:54:41 2010
@@ -1,7 +1,8 @@
from pypy.rpython.lltypesystem import rffi, lltype
-from pypy.module.cpyext.api import (cpython_api, PyObject, CANNOT_FAIL,
- build_type_checkers, Py_ssize_t)
+from pypy.module.cpyext.api import (
+ cpython_api, build_type_checkers, PyObject,
+ CONST_STRING, CANNOT_FAIL, Py_ssize_t)
from pypy.rlib.rarithmetic import r_uint
PyInt_Check, PyInt_CheckExact = build_type_checkers("Int")
@@ -63,3 +64,25 @@
"""
return space.wrap(ival) # XXX this is wrong on win64
+ at cpython_api([CONST_STRING, rffi.CCHARPP, rffi.INT_real], PyObject)
+def PyInt_FromString(space, str, pend, base):
+ """Return a new PyIntObject or 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. If the string represents
+ a number too large to be contained within the machine's long int type
+ and overflow warnings are being suppressed, a PyLongObject will be
+ returned. If overflow warnings are not being suppressed, NULL will be
+ returned in this case."""
+ s = rffi.charp2str(str)
+ w_str = space.wrap(s)
+ w_base = space.wrap(rffi.cast(lltype.Signed, base))
+ if pend:
+ pend[0] = rffi.ptradd(str, len(s))
+ return space.call_function(space.w_int, w_str, w_base)
+
Modified: pypy/branch/fast-forward/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/stubs.py (original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/stubs.py Fri Dec 3 18:54:41 2010
@@ -842,13 +842,6 @@
failure; the appropriate exception will be set."""
raise NotImplementedError
- at cpython_api([PyObject, rffi.CCHARPP], PyObject)
-def PyFloat_FromString(space, str, pend):
- """Create a PyFloatObject object based on the string value in str, or
- NULL on failure. The pend argument is ignored. It remains only for
- backward compatibility."""
- raise NotImplementedError
-
@cpython_api([rffi.VOIDP_real], PyObject)
def PyFloat_GetInfo(space, info):
"""Return a structseq instance which contains information about the
@@ -1735,23 +1728,6 @@
"""
raise NotImplementedError
- at cpython_api([rffi.CCHARP, rffi.CCHARPP, rffi.INT_real], PyObject)
-def PyInt_FromString(space, str, pend, base):
- """Return a new PyIntObject or 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. If the string represents
- a number too large to be contained within the machine's long int type
- and overflow warnings are being suppressed, a PyLongObject will be
- returned. If overflow warnings are not being suppressed, NULL will be
- returned in this case."""
- raise NotImplementedError
-
@cpython_api([rffi.SIZE_T], PyObject)
def PyInt_FromSize_t(space, ival):
"""Create a new integer object with a value of ival. If the value exceeds
Modified: pypy/branch/fast-forward/pypy/module/cpyext/test/test_floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/test/test_floatobject.py (original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/test/test_floatobject.py Fri Dec 3 18:54:41 2010
@@ -1,4 +1,5 @@
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
class TestFloatObject(BaseApiTest):
def test_floatobject(self, space, api):
@@ -11,9 +12,24 @@
def test_coerce(self, space, api):
assert space.type(api.PyNumber_Float(space.wrap(3))) is space.w_float
+ assert space.type(api.PyNumber_Float(space.wrap("3"))) is space.w_float
class Coerce(object):
def __float__(self):
return 42.5
assert space.eq_w(api.PyNumber_Float(space.wrap(Coerce())),
space.wrap(42.5))
+
+class AppTestFloatObject(AppTestCpythonExtensionBase):
+ def test_fromstring(self):
+ module = self.import_extension('foo', [
+ ("from_string", "METH_NOARGS",
+ """
+ PyObject* str = PyString_FromString("1234.56");
+ PyObject* res = PyFloat_FromString(str, NULL);
+ Py_DECREF(str);
+ return res;
+ """),
+ ])
+ assert module.from_string() == 1234.56
+ assert type(module.from_string()) is float
Modified: pypy/branch/fast-forward/pypy/module/cpyext/test/test_intobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/cpyext/test/test_intobject.py (original)
+++ pypy/branch/fast-forward/pypy/module/cpyext/test/test_intobject.py Fri Dec 3 18:54:41 2010
@@ -1,4 +1,5 @@
from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
import sys
class TestIntObject(BaseApiTest):
@@ -34,3 +35,14 @@
def __int__(self):
return 42
assert api.PyInt_AsLong(space.wrap(Coerce())) == 42
+
+class AppTestIntObject(AppTestCpythonExtensionBase):
+ def test_fromstring(self):
+ module = self.import_extension('foo', [
+ ("from_string", "METH_NOARGS",
+ """
+ return PyInt_FromString("1234", NULL, 16);
+ """),
+ ])
+ assert module.from_string() == 0x1234
+ assert type(module.from_string()) is int
More information about the Pypy-commit
mailing list