Author: Amaury Forgeot d'Arc <[email protected]>
Branch:
Changeset: r54850:ac5690f5e02b
Date: 2012-05-01 18:02 +0200
http://bitbucket.org/pypy/pypy/changeset/ac5690f5e02b/
Log: cpyext: Implement _PyFloat_Unpack4() and _PyFloat_Unpack8(). They
are used by the py3k version of arraymodule.
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)
+@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)
+
+@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/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', [
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit