[pypy-commit] pypy faster-rstruct-2: fix module/struct to use the new functionality. Add a passing test to check that struct.unpack(..., <str>) uses the fast-path; test_unpack_from still failing though

antocuni pypy.commits at gmail.com
Fri May 5 06:09:46 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91182:1a2232f24cad
Date: 2017-05-04 19:19 +0200
http://bitbucket.org/pypy/pypy/changeset/1a2232f24cad/

Log:	fix module/struct to use the new functionality. Add a passing test
	to check that struct.unpack(..., <str>) uses the fast-path;
	test_unpack_from still failing though

diff --git a/pypy/module/struct/formatiterator.py b/pypy/module/struct/formatiterator.py
--- a/pypy/module/struct/formatiterator.py
+++ b/pypy/module/struct/formatiterator.py
@@ -182,9 +182,8 @@
     def get_pos(self):
         return self.pos
 
-    def get_buffer_as_string_maybe(self):
-        string, pos = self.buf.as_str_and_offset_maybe()
-        return string, pos+self.pos
+    def get_buffer_and_pos(self):
+        return self.buf, self.pos
 
     def skip(self, size):
         self.read(size) # XXX, could avoid taking the slice
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
@@ -499,6 +499,10 @@
         from rpython.rlib.rstruct import standardfmttable
         standardfmttable.ALLOW_SLOWPATH = True
 
+    def test_unpack_simple(self):
+        buf = self.struct.pack("iii", 0, 42, 43)
+        assert self.struct.unpack("iii", buf) == (0, 42, 43)
+
     def test_unpack_from(self):
         buf = self.struct.pack("iii", 0, 42, 43)
         offset = self.struct.calcsize("i")
diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -37,6 +37,7 @@
         # May be overridden.
         return self.getslice(0, self.getlength(), 1, self.getlength())
 
+    # XXX kill me
     def as_str_and_offset_maybe(self):
         """
         If the buffer is backed by a string, return a pair (string, offset), where
diff --git a/rpython/rlib/rstruct/standardfmttable.py b/rpython/rlib/rstruct/standardfmttable.py
--- a/rpython/rlib/rstruct/standardfmttable.py
+++ b/rpython/rlib/rstruct/standardfmttable.py
@@ -149,8 +149,11 @@
             # buf.typed_read to raise CannotRead in case it is not aligned
             # *and* it is not supported.
             raise CannotRead
+        # we need to call skip *after* we typed_read(), because if it raises
+        # we do not want to skip
+        result = buf.typed_read(TYPE, pos)
         fmtiter.skip(size)
-        return buf.typed_read(TYPE, pos)
+        return result
     return do_unpack_fastpath
 
 @specialize.argtype(0)


More information about the pypy-commit mailing list