Author: Antonio Cuni <anto.c...@gmail.com>
Branch: faster-rstruct-2
Changeset: r91320:cce350f856ce
Date: 2017-05-17 11:48 +0200
http://bitbucket.org/pypy/pypy/changeset/cce350f856ce/

Log:    move setzeros into the base class rlib.buffer.Buffer, because it is
        used by rstruct to pack and needs to be available on all buffers

diff --git a/pypy/module/struct/test/test_struct.py 
b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -535,4 +535,9 @@
         buf = bytearray(len(expected))
         self.struct.pack_into("ii", buf, 0, 42, 43)
         assert buf == expected
-        
+
+    def test_pack_into_bytearray_padding(self):
+        expected = self.struct.pack("xxi", 42)
+        buf = bytearray(len(expected))
+        self.struct.pack_into("xxi", buf, 0, 42)
+        assert buf == expected
diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -80,6 +80,12 @@
         for i in range(len(string)):
             self.setitem(start + i, string[i])
 
+    @jit.look_inside_iff(lambda self, index, count:
+                         jit.isconstant(count) and count <= 8)
+    def setzeros(self, index, count):
+        for i in range(index, index+count):
+            self.setitem(i, '\x00')
+
     @specialize.ll_and_arg(1)
     def typed_read(self, TP, byte_offset):
         """
diff --git a/rpython/rlib/mutbuffer.py b/rpython/rlib/mutbuffer.py
--- a/rpython/rlib/mutbuffer.py
+++ b/rpython/rlib/mutbuffer.py
@@ -48,12 +48,6 @@
     def setitem(self, index, char):
         self.ll_val.chars[index] = char
 
-    @jit.look_inside_iff(lambda self, index, count:
-                         jit.isconstant(count) and count <= 8)
-    def setzeros(self, index, count):
-        for i in range(index, index+count):
-            self.setitem(i, '\x00')
-
     @staticmethod
     def _get_gc_data_offset():
         return (llmemory.offsetof(STR, 'chars') +
diff --git a/rpython/rlib/test/test_buffer.py b/rpython/rlib/test/test_buffer.py
--- a/rpython/rlib/test/test_buffer.py
+++ b/rpython/rlib/test/test_buffer.py
@@ -24,6 +24,10 @@
     def as_str(self):
         return rffi.charpsize2str(self._buf, self._n)
 
+    def setitem(self, i, char):
+        assert not self.readonly
+        self._buf[i] = char
+
     def __del__(self):
         lltype.free(self._buf, flavor='raw')
         self._buf = None
@@ -73,6 +77,13 @@
     assert addr[4] == b'o'
     assert addr[6] == b'w'
 
+def test_setzeros():
+    buf = MyRawBuffer('ABCDEFGH', readonly=False)
+    buf.setzeros(2, 3)
+    assert buf.as_str() == 'AB\x00\x00\x00FGH'
+
+    
+
 
 class BaseTypedReadTest:
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to