[pypy-commit] pypy py3.5-memoryview: hex method for bytes and bytearray + test
plan_rich
pypy.commits at gmail.com
Tue Aug 16 05:56:28 EDT 2016
Author: Richard Plangger <planrichi at gmail.com>
Branch: py3.5-memoryview
Changeset: r86215:75e21c6f7535
Date: 2016-08-16 11:55 +0200
http://bitbucket.org/pypy/pypy/changeset/75e21c6f7535/
Log: hex method for bytes and bytearray + test
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
@@ -441,6 +441,9 @@
def descr_copy(self, space):
return self._new(self.data[:])
+ def descr_hex(self, space):
+ return _array_to_hexstring(space, self.data, len(self.data), True)
+
# ____________________________________________________________
# helpers for slow paths, moved out because they contain loops
@@ -494,15 +497,22 @@
HEXDIGITS = "0123456789abcdef"
PY_SIZE_T_MAX = 2**(rffi.sizeof(rffi.SIZE_T)*8)-1
-def _array_to_hexstring(space, buf):
- length = buf.getlength()
+ at specialize.arg(3) # raw access
+def _array_to_hexstring(space, buf, len=0, rawaccess=False):
+ if rawaccess:
+ length = len
+ else:
+ length = buf.getlength()
hexstring = StringBuilder(length*2)
if length > PY_SIZE_T_MAX/2:
raise OperationError(space.w_MemoryError)
for i in range(length):
- byte = ord(buf.getitem(i))
+ if rawaccess:
+ byte = ord(buf[i])
+ else:
+ byte = ord(buf.getitem(i))
c = (byte >> 4 & 0xf)
hexstring.append(HEXDIGITS[c])
c = (byte & 0xf)
@@ -944,6 +954,12 @@
of the specified width. B is never truncated.
"""
+ def hex():
+ """B.hex() -> unicode
+ Return a string object containing two hexadecimal digits
+ for each byte in the instance B.
+ """
+
W_BytearrayObject.typedef = TypeDef(
"bytearray",
@@ -1093,6 +1109,8 @@
doc=BytearrayDocstrings.clear.__doc__),
copy = interp2app(W_BytearrayObject.descr_copy,
doc=BytearrayDocstrings.copy.__doc__),
+ hex = interp2app(W_BytearrayObject.descr_hex,
+ doc=BytearrayDocstrings.hex.__doc__),
)
W_BytearrayObject.typedef.flag_sequence_bug_compat = True
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -394,6 +394,12 @@
of the specified width. The string S is never truncated.
"""
+ def descr_hex(self, space):
+ """S.hex() -> string
+
+ Creates a hexadecimal string of the bytes object
+ """
+
class W_BytesObject(W_AbstractBytesObject):
import_from_mixin(StringMethods)
@@ -648,6 +654,11 @@
def descr_upper(self, space):
return W_BytesObject(self._value.upper())
+ def descr_hex(self, space):
+ from pypy.objspace.std.bytearrayobject import _array_to_hexstring
+ return _array_to_hexstring(space, self.buffer_w(space, space.BUF_SIMPLE))
+
+
def _create_list_from_bytes(value):
# need this helper function to allow the jit to look inside and inline
@@ -827,6 +838,7 @@
fromhex = interp2app(W_BytesObject.descr_fromhex, as_classmethod=True),
maketrans = interp2app(W_BytesObject.descr_maketrans, as_classmethod=True),
+ hex = interp2app(W_BytesObject.descr_hex)
)
W_BytesObject.typedef.flag_sequence_bug_compat = True
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -523,3 +523,7 @@
result = bytearray.maketrans(b'abc', b'xyz')
assert result == table
assert type(result) is bytes
+
+ def test_hex(self):
+ assert bytearray(b'santa claus').hex() == "73616e746120636c617573"
+
diff --git a/pypy/objspace/std/test/test_bytesobject.py b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -870,3 +870,10 @@
def __int__(self):
return 42
raises(TypeError, bytes, A())
+
+ def test_hex(self):
+ assert bytes('santa claus', 'ascii').hex() == "73616e746120636c617573"
+ assert bytes([0x73,0x61,0x6e,0x74,0x61,0x20,0x63,0x6c,0x61,0x75,0x73]).hex() == \
+ "73616e746120636c617573"
+ assert bytes(64).hex() == "00"*64
+
More information about the pypy-commit
mailing list