[pypy-commit] pypy py3.5: enhance _array_to_hexstring, it takes now the iteration direction into account

plan_rich pypy.commits at gmail.com
Fri Jan 13 07:12:23 EST 2017


Author: Richard Plangger <planrichi at gmail.com>
Branch: py3.5
Changeset: r89539:f293437a43a6
Date: 2017-01-13 11:32 +0100
http://bitbucket.org/pypy/pypy/changeset/f293437a43a6/

Log:	enhance _array_to_hexstring, it takes now the iteration direction
	into account

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -579,17 +579,15 @@
 PY_SIZE_T_MAX = intmask(2**(rffi.sizeof(rffi.SIZE_T)*8-1)-1)
 
 @specialize.arg(3) # raw access
-def _array_to_hexstring(space, buf, len=0, rawaccess=False):
-    if rawaccess:
-        length = len
-    else:
-        length = buf.getlength()
+def _array_to_hexstring(space, buf, start, step, length, rawaccess=False):
     hexstring = StringBuilder(length*2)
 
     if length > PY_SIZE_T_MAX/2:
         raise OperationError(space.w_MemoryError, space.w_None)
 
-    for i in range(length):
+    stepped = 0
+    i = start
+    while stepped < length:
         if rawaccess:
             byte = ord(buf[i])
         else:
@@ -598,6 +596,8 @@
         hexstring.append(HEXDIGITS[c])
         c = (byte & 0xf)
         hexstring.append(HEXDIGITS[c])
+        i += step
+        stepped += 1
 
     return space.wrap(hexstring.build())
 
diff --git a/pypy/objspace/std/memoryobject.py b/pypy/objspace/std/memoryobject.py
--- a/pypy/objspace/std/memoryobject.py
+++ b/pypy/objspace/std/memoryobject.py
@@ -675,7 +675,13 @@
     def descr_hex(self, space):
         from pypy.objspace.std.bytearrayobject import _array_to_hexstring
         self._check_released(space)
-        return _array_to_hexstring(space, self.buf)
+        if isinstance(self.buf, SubBuffer):
+            step = self.strides[0]
+            return _array_to_hexstring(space, self.buf.buffer,
+                                       self.buf.offset, step,
+                                       self.getlength())
+        else:
+            return _array_to_hexstring(space, self.buf, 0, 1, self.getlength())
 
 def is_byte_format(char):
     return char == 'b' or char == 'B' or char == 'c'
diff --git a/pypy/objspace/std/test/test_memoryobject.py b/pypy/objspace/std/test/test_memoryobject.py
--- a/pypy/objspace/std/test/test_memoryobject.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -194,10 +194,10 @@
         assert memoryview(b"abc").hex() == u'616263'
 
     def test_hex_long(self):
-        x = b'0' * 200000
+        x = b'01' * 100000
         m1 = memoryview(x)
         m2 = m1[::-1]
-        assert m2.hex() == '30' * 200000
+        assert m2.hex() == '3130' * 100000
 
     def test_memoryview_cast(self):
         m1 = memoryview(b'abcdefgh')


More information about the pypy-commit mailing list