[pypy-commit] pypy py3k: hg merge default

amauryfa noreply at buildbot.pypy.org
Wed May 2 01:04:07 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r54861:3b723dd7df4f
Date: 2012-05-01 18:04 +0200
http://bitbucket.org/pypy/pypy/changeset/3b723dd7df4f/

Log:	hg merge default

diff --git a/pypy/module/cpyext/floatobject.py b/pypy/module/cpyext/floatobject.py
--- a/pypy/module/cpyext/floatobject.py
+++ b/pypy/module/cpyext/floatobject.py
@@ -2,6 +2,7 @@
 from pypy.module.cpyext.api import (
     CANNOT_FAIL, cpython_api, PyObject, build_type_checkers, CONST_STRING)
 from pypy.interpreter.error import OperationError
+from pypy.rlib.rstruct import runpack
 
 PyFloat_Check, PyFloat_CheckExact = build_type_checkers("Float")
 
@@ -33,3 +34,19 @@
     backward compatibility."""
     return space.call_function(space.w_float, w_obj)
 
+ at cpython_api([CONST_STRING, rffi.INT_real], rffi.DOUBLE, error=-1.0)
+def _PyFloat_Unpack4(space, ptr, le):
+    input = rffi.charpsize2str(ptr, 4)
+    if rffi.cast(lltype.Signed, le):
+        return runpack.runpack("<f", input)
+    else:
+        return runpack.runpack(">f", input)
+
+ at cpython_api([CONST_STRING, rffi.INT_real], rffi.DOUBLE, error=-1.0)
+def _PyFloat_Unpack8(space, ptr, le):
+    input = rffi.charpsize2str(ptr, 8)
+    if rffi.cast(lltype.Signed, le):
+        return runpack.runpack("<d", input)
+    else:
+        return runpack.runpack(">d", input)
+
diff --git a/pypy/module/cpyext/longobject.py b/pypy/module/cpyext/longobject.py
--- a/pypy/module/cpyext/longobject.py
+++ b/pypy/module/cpyext/longobject.py
@@ -16,6 +16,13 @@
     """Return a new PyLongObject object from v, or NULL on failure."""
     return space.newlong(val)
 
+ at cpython_api([Py_ssize_t], PyObject)
+def PyLong_FromSsize_t(space, val):
+    """Return a new PyLongObject object from a C Py_ssize_t, or
+    NULL on failure.
+    """
+    return space.newlong(val)
+
 @cpython_api([rffi.LONGLONG], PyObject)
 def PyLong_FromLongLong(space, val):
     """Return a new PyLongObject object from a C long long, or NULL
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1405,13 +1405,6 @@
     """
     raise NotImplementedError
 
- at cpython_api([Py_ssize_t], PyObject)
-def PyLong_FromSsize_t(space, v):
-    """Return a new PyLongObject object from a C Py_ssize_t, or
-    NULL on failure.
-    """
-    raise NotImplementedError
-
 @cpython_api([rffi.SIZE_T], PyObject)
 def PyLong_FromSize_t(space, v):
     """Return a new PyLongObject object from a C size_t, or
diff --git a/pypy/module/cpyext/test/test_floatobject.py b/pypy/module/cpyext/test/test_floatobject.py
--- a/pypy/module/cpyext/test/test_floatobject.py
+++ b/pypy/module/cpyext/test/test_floatobject.py
@@ -1,5 +1,6 @@
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+from pypy.rpython.lltypesystem import rffi
 
 class TestFloatObject(BaseApiTest):
     def test_floatobject(self, space, api):
@@ -20,6 +21,16 @@
         assert space.eq_w(api.PyNumber_Float(space.wrap(Coerce())),
                           space.wrap(42.5))
 
+    def test_unpack(self, space, api):
+        with rffi.scoped_str2charp("\x9a\x99\x99?") as ptr:
+            assert abs(api._PyFloat_Unpack4(ptr, 1) - 1.2) < 1e-7
+        with rffi.scoped_str2charp("?\x99\x99\x9a") as ptr:
+            assert abs(api._PyFloat_Unpack4(ptr, 0) - 1.2) < 1e-7
+        with rffi.scoped_str2charp("\x1f\x85\xebQ\xb8\x1e\t@") as ptr:
+            assert abs(api._PyFloat_Unpack8(ptr, 1) - 3.14) < 1e-15
+        with rffi.scoped_str2charp("@\t\x1e\xb8Q\xeb\x85\x1f") as ptr:
+            assert abs(api._PyFloat_Unpack8(ptr, 0) - 3.14) < 1e-15
+
 class AppTestFloatObject(AppTestCpythonExtensionBase):
     def test_fromstring(self):
         module = self.import_extension('foo', [
diff --git a/pypy/module/cpyext/test/test_longobject.py b/pypy/module/cpyext/test/test_longobject.py
--- a/pypy/module/cpyext/test/test_longobject.py
+++ b/pypy/module/cpyext/test/test_longobject.py
@@ -35,6 +35,7 @@
         w_value = space.newlong(2)
         value = api.PyLong_AsSsize_t(w_value)
         assert value == 2
+        assert space.eq_w(w_value, api.PyLong_FromSsize_t(2))
 
     def test_fromdouble(self, space, api):
         w_value = api.PyLong_FromDouble(-12.74)


More information about the pypy-commit mailing list