[pypy-svn] r15129 - in pypy/dist/pypy/module/marshal: . test

rxe at codespeak.net rxe at codespeak.net
Tue Jul 26 17:19:19 CEST 2005


Author: rxe
Date: Tue Jul 26 17:19:18 2005
New Revision: 15129

Modified:
   pypy/dist/pypy/module/marshal/app_marshal.py
   pypy/dist/pypy/module/marshal/test/test_marshal_extra.py
Log:
Christian/Richard

Use OsFileWrapper for marshal and add some tests to for load/dump and not just
loads/dumps.  Also show skipped any tests.



Modified: pypy/dist/pypy/module/marshal/app_marshal.py
==============================================================================
--- pypy/dist/pypy/module/marshal/app_marshal.py	(original)
+++ pypy/dist/pypy/module/marshal/app_marshal.py	Tue Jul 26 17:19:18 2005
@@ -4,6 +4,8 @@
 import types
 import os
 
+from pypy.tool.osfilewrapper import OsFileWrapper
+
 try:
     import new
 except ImportError:
@@ -37,12 +39,15 @@
     dispatch = {}
 
     def __init__(self, fd):
-        self.fd = fd
+        if fd >= 0:
+            self.fd_wrapper = OsFileWrapper(fd)
+        else:
+            self.fd_wrapper = None
         self.buffer = []
 
     def _write(self, data):
-        if self.fd >= 0:
-            os.write(self.fd, data)
+        if self.fd_wrapper is not None:
+            self.fd_wrapper.write(data)
         else:
             self.buffer.append(data)
 
@@ -233,22 +238,25 @@
     dispatch = {}
 
     def __init__(self, fd):
-        self.fd = fd
+        if fd >= 0:
+            self.fd_wrapper = OsFileWrapper(fd)
+        else:
+            self.fd_wrapper = None
         self.bufstr = ''
         self.bufpos = 0
         self._stringtable = []
 
     def _read(self):
-        if self.fd >= 0:
-            return self.fd.read(1)
+        if self.fd_wrapper is not None:
+            return self.fd_wrapper.read(1)
         else:
             ret = self.bufstr[self.bufpos]
             self.bufpos += 1
             return ret
 
     def _readn(self, n):
-        if self.fd >= 0:
-            return self.fd.read(n)
+        if self.fd_wrapper is not None:
+            return self.fd_wrapper.read(n)
         else:
             ret = self.bufstr[self.bufpos : self.bufpos+n]
             self.bufpos += n

Modified: pypy/dist/pypy/module/marshal/test/test_marshal_extra.py
==============================================================================
--- pypy/dist/pypy/module/marshal/test/test_marshal_extra.py	(original)
+++ pypy/dist/pypy/module/marshal/test/test_marshal_extra.py	Tue Jul 26 17:19:18 2005
@@ -1,8 +1,11 @@
+import py
 import autopath
 import sys
 import marshal as cpy_marshal
 from pypy.module.marshal import app_marshal as marshal
 
+from pypy.tool.udir import udir 
+
 hello = "he"
 hello += "llo"
 def func(x):
@@ -47,44 +50,90 @@
 
 def test_cases():
     for case in TESTCASES:
-        yield dump_and_reload, case
+        yield dumps_and_reload, case
+        yield loads_from_cpython, case
+        yield dumps_to_cpython, case
+        yield dumps_subclass, case
         yield load_from_cpython, case
         yield dump_to_cpython, case
-        yield dump_subclass, case
 
-def dump_and_reload(case):
+def dumps_and_reload(case):
     print 'dump_and_reload', `case`
     s = marshal.dumps(case)
     obj = marshal.loads(s)
     assert obj == case
 
-def load_from_cpython(case):
+def loads_from_cpython(case):
     print 'load_from_cpython', `case`
     try:
         s = cpy_marshal.dumps(case)
     except ValueError:
-        return   # this version of CPython doesn't support this object
+        py.test.skip("this version of CPython doesn't support this object") 
     obj = marshal.loads(s)
     assert obj == case
 
-def dump_to_cpython(case):
+def dumps_to_cpython(case):
     print 'dump_to_cpython', `case`
     try:
         cpy_marshal.dumps(case)
     except ValueError:
-        return   # this version of CPython doesn't support this object
+        py.test.skip("this version of CPython doesn't support this object") 
     s = marshal.dumps(case)
     obj = cpy_marshal.loads(s)
     assert obj == case
 
-def dump_subclass(case):
+def dumps_subclass(case):
     try:
         class Subclass(type(case)):
             pass
         case = Subclass(case)
     except TypeError:
-        return
-    print 'dump_subclass', `case`
+        py.test.skip("this version of CPython doesn't support this object") 
     s = marshal.dumps(case)
     obj = marshal.loads(s)
     assert obj == case
+
+def load_from_cpython(case):
+    p = str(udir.join('test.dat'))
+
+    f1 = open(p, "w")
+    try:
+        try:
+            s = cpy_marshal.dump(case, f1)
+        finally:
+            f1.close()
+    except ValueError:
+        py.test.skip("this version of CPython doesn't support this object") 
+
+    f2 = open(p, "r")
+    try:
+        obj = marshal.load(f2)
+    finally:
+        f2.close()
+    assert obj == case
+
+def dump_to_cpython(case):
+
+    try:
+        cpy_marshal.dumps(case)
+    except ValueError:
+        py.test.skip("this version of CPython doesn't support this object") 
+
+    p = str(udir.join('test.dat'))
+    f1 = open(p, "w")
+    try:
+        try:
+            s = marshal.dump(case, f1)
+        finally:
+            f1.close()
+    except ValueError:
+        py.test.skip("this version of CPython doesn't support this object") 
+
+    f2 = open(p, "r")
+    try:
+        obj = cpy_marshal.load(f2)
+    finally:
+        f2.close()
+    assert obj == case
+
+



More information about the Pypy-commit mailing list