[pypy-svn] r49526 - in pypy/branch/pypy-interp-file: . module/_file module/_file/test

arigo at codespeak.net arigo at codespeak.net
Fri Dec 7 18:27:08 CET 2007


Author: arigo
Date: Fri Dec  7 18:27:06 2007
New Revision: 49526

Removed:
   pypy/branch/pypy-interp-file/module/_file/app_file.py
Modified:
   pypy/branch/pypy-interp-file/conftest.py
   pypy/branch/pypy-interp-file/module/_file/interp_file.py
   pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py
Log:
file.readinto().  With this, the interp-level version of the file type
should be complete.


Modified: pypy/branch/pypy-interp-file/conftest.py
==============================================================================
--- pypy/branch/pypy-interp-file/conftest.py	(original)
+++ pypy/branch/pypy-interp-file/conftest.py	Fri Dec  7 18:27:06 2007
@@ -126,6 +126,9 @@
     def call_function(self, func, *args, **kwds):
         return func(*args, **kwds)
 
+    def call_method(self, obj, name, *args, **kwds):
+        return getattr(obj, name)(*args, **kwds)
+
 def translation_test_so_skip_if_appdirect():
     if option.runappdirect:
         py.test.skip("translation test, skipped for appdirect")

Modified: pypy/branch/pypy-interp-file/module/_file/interp_file.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/interp_file.py	(original)
+++ pypy/branch/pypy-interp-file/module/_file/interp_file.py	Fri Dec  7 18:27:06 2007
@@ -363,6 +363,26 @@
         return self.getrepr(self.space, info)
     file__repr__.unwrap_spec = ['self']
 
+    def file_readinto(self, w_array):
+        """readinto() -> Undocumented.  Don't use this; it may go away."""
+        # completely inefficient and incomplete for now
+        space = self.space
+        w_len = space.appexec([space.wrap(self), w_array], """(self, a):
+            from array import array
+            if not isinstance(a, array):
+                raise TypeError('Can only read into array objects')
+            length = len(a)
+            data = self.read(length)
+            n = len(data)
+            if n < length:
+                data += a.tostring()[len(data):]
+            del a[:]
+            a.fromstring(data)
+            return n
+        """)
+        return w_len
+    file_readinto.unwrap_spec = ['self', W_Root]
+
 
 # ____________________________________________________________
 
@@ -445,6 +465,7 @@
                               cls=W_File,
                               doc="Support for 'print'."),
     __repr__ = interp2app(W_File.file__repr__),
+    readinto = interp2app(W_File.file_readinto),
     **dict([(name, interp2app(getattr(W_File, 'file_' + name)))
                 for name in W_File._exposed_method_names])
     )

Modified: pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py
==============================================================================
--- pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py	(original)
+++ pypy/branch/pypy-interp-file/module/_file/test/test_file_extra.py	Fri Dec  7 18:27:06 2007
@@ -206,9 +206,12 @@
     extra_args = ()
 
     def setup_method(self, method):
-        from pypy.module._file.interp_file import W_File
         space = self.space
-        w_filetype = space.gettypeobject(W_File.typedef)
+        if hasattr(space, 'gettypeobject'):
+            from pypy.module._file.interp_file import W_File
+            w_filetype = space.gettypeobject(W_File.typedef)
+        else:
+            w_filetype = file    # TinyObjSpace, for "py.test -A"
         self.w_file = space.call_function(
             w_filetype,
             space.wrap(self.expected_filename),
@@ -261,10 +264,14 @@
     extra_args = ()
 
     def setup_method(self, method):
-        from pypy.module._file.interp_file import W_File
         space = self.space
         O_BINARY = getattr(os, "O_BINARY", 0)
-        w_filetype = space.gettypeobject(W_File.typedef)
+        if hasattr(space, 'gettypeobject'):
+            from pypy.module._file.interp_file import W_File
+            w_filetype = space.gettypeobject(W_File.typedef)
+        else:
+            w_filetype = os    # TinyObjSpace, for "py.test -A"
+                               # (CPython has no file.fdopen, only os.fdopen)
         fd = os.open(AppTestFile.expected_filename, os.O_RDONLY | O_BINARY)
         self.w_file = space.call_method(
             w_filetype,
@@ -440,7 +447,7 @@
         f.close()
         assert os.stat(fn).st_size == 3
 
-        f = file(fn, 'wb', 100)
+        f = file(fn, 'wb', 1000)
         f.write('x')
         assert os.stat(fn).st_size == 0
         f.write('\n')
@@ -477,3 +484,17 @@
         data = f.read(123)
         assert data == 'hel'
         f.close()
+
+    def test_readinto(self):
+        from array import array
+        a = array('c')
+        a.fromstring('0123456789')
+        fn = self.temptestfile
+        f = open(fn, 'w+b')
+        f.write('foobar')
+        f.seek(0)
+        n = f.readinto(a)
+        f.close()
+        assert n == 6
+        assert len(a) == 10
+        assert a.tostring() == 'foobar6789'


More information about the pypy-svn mailing list