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

rhymes at codespeak.net rhymes at codespeak.net
Sat Jul 22 16:05:20 CEST 2006


Author: rhymes
Date: Sat Jul 22 16:05:17 2006
New Revision: 30357

Modified:
   pypy/dist/pypy/module/mmap/interp_mmap.py
   pypy/dist/pypy/module/mmap/test/test_mmap.py
Log:
move()

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	Sat Jul 22 16:05:17 2006
@@ -80,6 +80,9 @@
 linux_msync.argtypes = [c_void_p, size_t, c_int]
 linux_msync.restype = c_int
 
+libc.memmove.argtypes = [c_char_p, c_char_p, size_t]
+libc.memmove.restype = c_void_p
+
 if _POSIX:
     def _get_page_size():
         return libc.getpagesize()
@@ -400,6 +403,28 @@
         
         return self.space.wrap(0)
     flush.unwrap_spec = ['self', int, int]
+    
+    def move(self, dest, src, count):
+        self._check_valid()
+        
+        self._check_writeable()
+        
+        # check boundings
+        if (src + count > self._size) or (dest + count > self._size):
+            raise OperationError(self.space.w_ValueError,
+                self.space.wrap("source or destination out of range"))
+        
+        data_dest = c_char_p("".join([self._data[i] for i in range(dest, self._size)]))
+        data_src = c_char_p("".join([self._data[i] for i in range(src, src+count)]))
+        libc.memmove(data_dest, data_src, count)
+        
+        assert dest >= 0
+        str_left = self.space.str_w(self._to_str())[0:dest]
+        final_str = "%s%s" % (str_left, data_dest.value)
+        
+        p = c_char_p(final_str)
+        libc.memcpy(self._data, p, len(final_str))
+    move.unwrap_spec = ['self', int, int, int]
 
 _mmap.typedef = TypeDef("_mmap",
     _to_str = interp2app(_mmap._to_str, unwrap_spec=_mmap._to_str.unwrap_spec),
@@ -423,6 +448,7 @@
     write_byte = interp2app(_mmap.write_byte,
         unwrap_spec=_mmap.write_byte.unwrap_spec),
     flush = interp2app(_mmap.flush, unwrap_spec=_mmap.flush.unwrap_spec),
+    move = interp2app(_mmap.move, unwrap_spec=_mmap.move.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	Sat Jul 22 16:05:17 2006
@@ -249,23 +249,26 @@
         assert m.flush() == 0
         m.close()
         f.close()
-# 
-#     def test_move(self):
-#         self.f.seek(0)
-#         self.f.write("foobar")
-#         m = mmap(self.f.fileno(), 6, access=cmmap.ACCESS_READ)
-#         py.test.raises(TypeError, m.move, 1)
-#         py.test.raises(TypeError, m.move, 1, "foo", 2)
-#         m = mmap(self.f.fileno(), 6, access=cmmap.ACCESS_WRITE)
-#         py.test.raises(ValueError, m.move, 7, 1, 2)
-#         py.test.raises(ValueError, m.move, 1, 7, 2)
-#         m.move(1, 3, 3)
-#         assert m.read(6) == "fbarar"
-#         m.seek(0)
-#         m.move(1, 3, 2)
-#         a = m.read(6)
-#         assert a == "frarar"
-#         m.close()
+
+    def test_move(self):
+        import mmap
+        f = open("foo", "w+")
+        
+        f.write("foobar")
+        f.flush()
+        m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_READ)
+        raises(TypeError, m.move, 1)
+        raises(TypeError, m.move, 1, "foo", 2)
+        m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_WRITE)
+        raises(ValueError, m.move, 7, 1, 2)
+        raises(ValueError, m.move, 1, 7, 2)
+        m.move(1, 3, 3)
+        assert m.read(6) == "fbarar"
+        m.seek(0)
+        m.move(1, 3, 2)
+        a = m.read(6)
+        assert a == "frarar"
+        m.close()
 #     
 #     def test_resize(self):
 #         if "darwin" in sys.platform or _FREEBSD:



More information about the Pypy-commit mailing list