[pypy-svn] r30222 - in pypy/dist/pypy/module/mmap: . test

rhymes at codespeak.net rhymes at codespeak.net
Wed Jul 19 13:57:51 CEST 2006


Author: rhymes
Date: Wed Jul 19 13:57:48 2006
New Revision: 30222

Modified:
   pypy/dist/pypy/module/mmap/interp_mmap.py
   pypy/dist/pypy/module/mmap/test/test_mmap.py
Log:
readline() and read()


Modified: pypy/dist/pypy/module/mmap/interp_mmap.py
==============================================================================
--- pypy/dist/pypy/module/mmap/interp_mmap.py	(original)
+++ pypy/dist/pypy/module/mmap/interp_mmap.py	Wed Jul 19 13:57:48 2006
@@ -200,6 +200,46 @@
             raise OperationError(self.space.w_ValueError,
                 self.space.wrap("read byte out of range"))
     read_byte.unwrap_spec = ['self']
+    
+    def readline(self):
+        self._check_valid()
+
+        found = False
+        for pos in range(self._pos, self._size):
+            if self._data[pos] == '\n':
+                found = True
+                break
+
+        if not found:
+            eol = self._size
+        else:
+            eol = pos + 1 # we're interested in the position after new line
+
+        # res = self._data[self._pos:eol-self._pos] XXX: can't use this slicing
+        # in translation step
+        res = "".join([self._data[i] for i in range(self._pos, eol-self._pos)])
+        self._pos += eol - self._pos
+        return self.space.wrap(res)
+    readline.unwrap_spec = ['self']
+    
+    def read(self, num):
+        self._check_valid()
+        
+        num_bytes = num
+        
+        # silently adjust out of range requests
+        if self._pos + num_bytes > self._size:
+            num_bytes -= (self._pos + num_bytes) - self._size
+        
+        # due to slicing of python, the last char is not always returned
+        if num_bytes < self._size - 1:
+            res = "".join([self._data[i] for i in range(self._pos, num_bytes)])
+        else:
+            res = "".join([self._data[i] for i in range(self._pos, self._size)])
+        self._pos += num_bytes
+        return self.space.wrap(res)
+    read.unwrap_spec = ['self', int]
+
 
 _mmap.typedef = TypeDef("_mmap",
     _to_str = interp2app(_mmap._to_str, unwrap_spec=_mmap._to_str.unwrap_spec),
@@ -208,6 +248,9 @@
     close = interp2app(_mmap.close, unwrap_spec=_mmap.close.unwrap_spec),
     read_byte = interp2app(_mmap.read_byte,
         unwrap_spec=_mmap.read_byte.unwrap_spec),
+    readline = interp2app(_mmap.readline,
+        unwrap_spec=_mmap.readline.unwrap_spec),
+    read = interp2app(_mmap.read, unwrap_spec=_mmap.read.unwrap_spec),
 )
 
 def _check_map_size(space, size):

Modified: pypy/dist/pypy/module/mmap/test/test_mmap.py
==============================================================================
--- pypy/dist/pypy/module/mmap/test/test_mmap.py	(original)
+++ pypy/dist/pypy/module/mmap/test/test_mmap.py	Wed Jul 19 13:57:48 2006
@@ -96,30 +96,37 @@
         raises(ValueError, m.read_byte)
         m.close()
         f.close()
-# 
-#     def test_readline(self):
-#         self.f.seek(0)
-#         self.f.write("foo\n")
-#         self.f.flush()
-#         m = mmap(self.f.fileno(), 4)
-#         if _MS_WINDOWS:
-#             # windows replaces \n with \r. it's time to change to \n only MS!
-#             assert m.readline() == "foo\r"
-#         elif _POSIX:
-#             assert m.readline() == "foo\n"
-#         assert m.readline() == ""
-#         m.close()
-# 
-#     def test_read(self):
-#         self.f.seek(0)
-#         self.f.write("foobar")
-#         self.f.flush()
-#         m = mmap(self.f.fileno(), 6)
-#         py.test.raises(TypeError, m.read, "foo")
-#         assert m.read(1) == "f"
-#         assert m.read(6) == "oobar"
-#         assert m.read(1) == ""
-#         m.close()
+
+    def test_readline(self):
+        from mmap import mmap
+        import os
+        f = open("foo", "w+")
+
+        f.write("foo\n")
+        f.flush()
+        m = mmap(f.fileno(), 4)
+        # if _MS_WINDOWS:
+        #     # windows replaces \n with \r. it's time to change to \n only MS!
+        #     assert m.readline() == "foo\r"
+        if os.name == "posix":
+            assert m.readline() == "foo\n"
+        assert m.readline() == ""
+        m.close()
+        f.close()
+
+    def test_read(self):
+        from mmap import mmap
+        f = open("foo", "w+")
+        
+        f.write("foobar")
+        f.flush()
+        m = mmap(f.fileno(), 6)
+        raises(TypeError, m.read, "foo")
+        assert m.read(1) == "f"
+        assert m.read(6) == "oobar"
+        assert m.read(1) == ""
+        m.close()
+        f.close()
 # 
 #     def test_find(self):
 #         self.f.seek(0)



More information about the Pypy-commit mailing list