[pypy-svn] r34766 - in pypy/dist/pypy: module/_file module/_file/test rlib rlib/test

cfbolz at codespeak.net cfbolz at codespeak.net
Sun Nov 19 18:42:44 CET 2006


Author: cfbolz
Date: Sun Nov 19 18:42:42 2006
New Revision: 34766

Modified:
   pypy/dist/pypy/module/_file/app_file.py
   pypy/dist/pypy/module/_file/test/test_file.py
   pypy/dist/pypy/rlib/streamio.py
   pypy/dist/pypy/rlib/test/test_streamio.py
Log:
fix newlines attribute of files


Modified: pypy/dist/pypy/module/_file/app_file.py
==============================================================================
--- pypy/dist/pypy/module/_file/app_file.py	(original)
+++ pypy/dist/pypy/module/_file/app_file.py	Sun Nov 19 18:42:42 2006
@@ -50,10 +50,25 @@
         
     def getnewlines(self):
         "end-of-line convention used in this file"
-        if isinstance(self.stream, _sio.TextInputFilter):
-            return self.stream.getnewlines()
-        else:
+
+        newlines = self.stream.getnewlines()
+        if newlines == 0:
             return None
+        if newlines in [1, 2, 4]:
+            if newlines == 1:
+                return "\r"
+            elif newlines == 2:
+                return "\n"
+            else:
+                return "\r\n"
+        result = []
+        if newlines & 1:
+            result.append('\r')
+        if newlines & 2:
+            result.append('\n')
+        if newlines & 4:
+            result.append('\r\n')
+        return tuple(result)
 
     mode     = property(lambda self: self._mode,
                         doc = "file mode ('r', 'U', 'w', 'a', "

Modified: pypy/dist/pypy/module/_file/test/test_file.py
==============================================================================
--- pypy/dist/pypy/module/_file/test/test_file.py	(original)
+++ pypy/dist/pypy/module/_file/test/test_file.py	Sun Nov 19 18:42:42 2006
@@ -61,3 +61,13 @@
             f.close()
         assert oct(os.stat(self.temppath).st_mode & 0777 | umask) == oct(0666)
 
+    def test_newlines(self):
+        import _file, os
+        f = _file.file(self.temppath, "wb")
+        f.write("\r\n")
+        assert f.newlines is None
+        f.close()
+        f = _file.file(self.temppath, "rU")
+        res = f.read()
+        assert res == "\n"
+        assert f.newlines == "\r\n"

Modified: pypy/dist/pypy/rlib/streamio.py
==============================================================================
--- pypy/dist/pypy/rlib/streamio.py	(original)
+++ pypy/dist/pypy/rlib/streamio.py	Sun Nov 19 18:42:42 2006
@@ -206,6 +206,9 @@
     def try_to_find_file_descriptor(self):
         return -1
 
+    def getnewlines(self):
+        return 0
+
 
 class DiskFile(Stream):
 
@@ -352,6 +355,7 @@
     ("close", []),
     ("peek", []),
     ("try_to_find_file_descriptor", []),
+    ("getnewlines", []),
     ])
 
 def PassThrough(meth_name, flush_buffers):
@@ -735,25 +739,7 @@
         self.CRLF = False
 
     def getnewlines(self):
-        foundchars = self.CR * 1 + self.NL * 2 + self.CRLF * 4
-        if not foundchars:
-            return None
-        if foundchars in [1, 2, 4]:
-            if self.CR:
-                return '\r'
-            elif self.NL:
-                return '\n'
-            else:
-                return '\r\n'
-        else:
-            result = []
-            if self.CR:
-                result.append('\r')
-            if self.NL:
-                result.append('\n')
-            if self.CRLF:
-                result.append('\r\n')
-            return tuple(result)
+        return self.CR * 1 + self.NL * 2 + self.CRLF * 4
 
     def read(self, n):
         """Read up to n bytes."""

Modified: pypy/dist/pypy/rlib/test/test_streamio.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_streamio.py	(original)
+++ pypy/dist/pypy/rlib/test/test_streamio.py	Sun Nov 19 18:42:42 2006
@@ -735,15 +735,13 @@
         ]
 
     expected_newlines = [
-        (["abcd"], [None]),
-        (["abcd\n"], ["\n"]),
-        (["abcd\r\n"],["\r\n"]),
-        (["abcd\r"],[None]), # wrong, but requires precognition to fix
-        (["abcd\r", "\nefgh"], [None, "\r\n"]),
-        (["abcd", "\nefg\r", "hij", "k\r\n"], [None, "\n", ("\r", "\n"),
-                                               ("\r", "\n", "\r\n")]),
-        (["abcd", "\refg\r", "\nhij", "k\n"], [None, "\r", ("\r", "\r\n"),
-                                               ("\r", "\n", "\r\n")])
+        (["abcd"], [0]),
+        (["abcd\n"], [2]),
+        (["abcd\r\n"],[4]),
+        (["abcd\r"],[0]), # wrong, but requires precognition to fix
+        (["abcd\r", "\nefgh"], [0, 4]),
+        (["abcd", "\nefg\r", "hij", "k\r\n"], [0, 2, 3, 7]),
+        (["abcd", "\refg\r", "\nhij", "k\n"], [0, 1, 5, 7])
         ]
 
     def test_read(self):
@@ -792,7 +790,18 @@
                     bufs.append(data)
                 assert "".join(bufs) == all
         self.interpret(f, [])
-            
+
+    def test_newlines_attribute(self):
+        for packets, expected in self.expected_newlines:
+            base = TReader(packets)
+            filter = streamio.TextInputFilter(base)
+            def f():
+                for e in expected:
+                    filter.read(100)
+                    assert filter.getnewlines() == e
+            self.interpret(f, [])
+
+    
 class TestTextInputFilter(BaseTestTextInputFilter):
     def interpret(self, func, args):
         return func(*args)
@@ -921,22 +930,6 @@
                 filter.write(c)
             assert base.buf == data
 
-class OldDisabledTests:
-    def test_readlines(self):
-        # This also tests next() and __iter__()
-        file = self.makeStream()
-        assert file.readlines() == self.lines
-
-    
-    def test_newlines_attribute(self):
-
-        for packets, expected in self.expected_newlines:
-            base = TReader(packets)
-            filter = streamio.TextInputFilter(base)
-            for e in expected:
-                filter.read(100)
-                assert filter.newlines == e
-
 
 
 # Speed test



More information about the Pypy-commit mailing list