[pypy-svn] r46367 - in pypy/dist/pypy/rlib: . test

arigo at codespeak.net arigo at codespeak.net
Thu Sep 6 15:05:48 CEST 2007


Author: arigo
Date: Thu Sep  6 15:05:47 2007
New Revision: 46367

Modified:
   pypy/dist/pypy/rlib/rmarshal.py
   pypy/dist/pypy/rlib/test/test_rmarshal.py
Log:
Marshal/unmarshal floats.


Modified: pypy/dist/pypy/rlib/rmarshal.py
==============================================================================
--- pypy/dist/pypy/rlib/rmarshal.py	(original)
+++ pypy/dist/pypy/rlib/rmarshal.py	Thu Sep  6 15:05:47 2007
@@ -7,6 +7,7 @@
 from pypy.annotation.listdef import ListDef, TooLateForChange
 from pypy.annotation.pairtype import pair, pairtype
 from pypy.rlib.rarithmetic import formatd, r_longlong, intmask
+from pypy.rlib.rarithmetic import break_up_float, parts_to_float
 from pypy.rlib.unroll import unrolling_iterable
 
 class CannotMarshal(Exception):
@@ -191,11 +192,11 @@
 add_dumper(annmodel.SomeFloat(), dump_float)
 
 def load_float(loader):
-    if loader.readchr() != TYPE_FLOAT:
+    if readchr(loader) != TYPE_FLOAT:
         raise ValueError("expected a float")
-    length = ord(loader.readchr())
-    s = loader.read(length)
-    return xxx # ...mess...
+    length = ord(readchr(loader))
+    s = readstr(loader, length)
+    return parts_to_float(*break_up_float(s))
 add_loader(annmodel.SomeFloat(), load_float)
 
 def dump_string_or_none(buf, x):

Modified: pypy/dist/pypy/rlib/test/test_rmarshal.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_rmarshal.py	(original)
+++ pypy/dist/pypy/rlib/test/test_rmarshal.py	Thu Sep  6 15:05:47 2007
@@ -2,6 +2,7 @@
 import marshal
 from pypy.rlib.rmarshal import *
 from pypy.annotation import model as annmodel
+from pypy.rlib.rarithmetic import formatd
 
 types_that_can_be_none = [
     [int],
@@ -53,8 +54,8 @@
     buf = 'i\x05\x00\x00\x00'
     assert get_unmarshaller(int)(buf) == 5
 
-##    buf = 'f\x043.25'
-##    assert get_unmarshaller(float)(buf) == 3.25
+    buf = 'f\x043.25'
+    assert get_unmarshaller(float)(buf) == 3.25
 
     buf = 's\x0c\x00\x00\x00hello, world'
     assert get_unmarshaller(str)(buf) == "hello, world"
@@ -90,31 +91,33 @@
 
 def test_llinterp_marshal():
     from pypy.rpython.test.test_llinterp import interpret
-    marshaller = get_marshaller([(int, str)])
+    marshaller = get_marshaller([(int, str, float)])
     def f():
         buf = []
-        marshaller(buf, [(5, "hello"), (7, "world")])
+        marshaller(buf, [(5, "hello", -0.5), (7, "world", 1E100)])
         return ''.join(buf)
     res = interpret(f, [])
     res = ''.join(res.chars)
-    assert res == ('[\x02\x00\x00\x00(\x02\x00\x00\x00i\x05\x00\x00\x00'
-                   's\x05\x00\x00\x00hello(\x02\x00\x00\x00i\x07\x00\x00\x00'
-                   's\x05\x00\x00\x00world')
+    assert res == ('[\x02\x00\x00\x00(\x03\x00\x00\x00i\x05\x00\x00\x00'
+                   's\x05\x00\x00\x00hellof\x04-0.5(\x03\x00\x00\x00'
+                   'i\x07\x00\x00\x00s\x05\x00\x00\x00world'
+                   'f\x061e+100')
 
 def test_llinterp_unmarshal():
     from pypy.rpython.test.test_llinterp import interpret
-    unmarshaller = get_unmarshaller([(int, str)])
-    buf = ('[\x02\x00\x00\x00(\x02\x00\x00\x00i\x05\x00\x00\x00'
-           's\x05\x00\x00\x00hello(\x02\x00\x00\x00i\x07\x00\x00\x00'
-           's\x05\x00\x00\x00world')
+    unmarshaller = get_unmarshaller([(int, str, float)])
+    buf = ('[\x02\x00\x00\x00(\x03\x00\x00\x00i\x05\x00\x00\x00'
+           's\x05\x00\x00\x00hellof\x04-0.5(\x03\x00\x00\x00'
+           'i\x07\x00\x00\x00s\x05\x00\x00\x00world'
+           'f\x061e+100')
     def f():
         result = ''
-        for num, string in unmarshaller(buf):
-            result += '%d=%s;' % (num, string)
+        for num, string, fval in unmarshaller(buf):
+            result += '%d=%s/%s;' % (num, string, formatd('%.17g', fval))
         return result
     res = interpret(f, [])
     res = ''.join(res.chars)
-    assert res == '5=hello;7=world;'
+    assert res == '5=hello/-0.5;7=world/1e+100;'
 
 def test_stat_result():
     import os



More information about the Pypy-commit mailing list