Author: Brian Kearns <bdkea...@gmail.com>
Branch: stdlib-2.7.8
Changeset: r73028:6749e168ae74
Date: 2014-08-24 13:33 -0400
http://bitbucket.org/pypy/pypy/changeset/6749e168ae74/

Log:    get/setslice for mmap

diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -200,6 +200,46 @@
                     self.mmap.setitem(start, value[i])
                     start += step
 
+    def descr_getslice(self, space, w_ilow, w_ihigh):
+        self.check_valid()
+        i = space.getindex_w(w_ilow, None)
+        j = space.getindex_w(w_ihigh, None)
+        if i < 0:
+            i = 0
+        elif i > self.mmap.size:
+            i = self.mmap.size
+        if j < 0:
+            j = 0
+        if j < i:
+            j = i
+        elif j > self.mmap.size:
+            j = self.mmap.size
+        return space.wrap(self.mmap.getslice(i, (j - i)))
+
+    def descr_setslice(self, space, w_ilow, w_ihigh, w_item):
+        self.check_valid()
+        i = space.getindex_w(w_ilow, None)
+        j = space.getindex_w(w_ihigh, None)
+        if i < 0:
+            i = 0
+        elif i > self.mmap.size:
+            i = self.mmap.size
+        if j < 0:
+            j = 0
+        if j < i:
+            j = i
+        elif j > self.mmap.size:
+            j = self.mmap.size
+        if not space.isinstance_w(w_item, space.w_str):
+            raise OperationError(space.w_IndexError, space.wrap(
+                "mmap slice assignment must be a string"))
+        value = space.realstr_w(w_item)
+        if len(value) != (j - i):
+            raise OperationError(space.w_IndexError, space.wrap(
+                "mmap slice assignment is wrong size"))
+        self.check_writeable()
+        self.mmap.setslice(i, value)
+
 if rmmap._POSIX:
 
     @unwrap_spec(fileno=int, length=int, flags=int,
@@ -255,6 +295,8 @@
     __len__ = interp2app(W_MMap.__len__),
     __getitem__ = interp2app(W_MMap.descr_getitem),
     __setitem__ = interp2app(W_MMap.descr_setitem),
+    __getslice__ = interp2app(W_MMap.descr_getslice),
+    __setslice__ = interp2app(W_MMap.descr_setslice),
 )
 
 constants = rmmap.constants
diff --git a/pypy/module/mmap/test/test_mmap.py 
b/pypy/module/mmap/test/test_mmap.py
--- a/pypy/module/mmap/test/test_mmap.py
+++ b/pypy/module/mmap/test/test_mmap.py
@@ -524,6 +524,11 @@
         f.seek(0)
         m = mmap(f.fileno(), 6)
         assert m[-3:7] == "bar"
+        assert m.__getslice__(-3, 7) == "foobar"
+        m.__setslice__(2, 4, "zz")
+        assert m.__getslice__(-3, 7) == "fozzar"
+        raises(TypeError, m.__getslice__, "abc", 2)
+        raises(IndexError, m.__setslice__, 2, 4, None)
 
         assert m[1:0:1] == ""
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to