Author: Manuel Jacob <[email protected]>
Branch: py3.6
Changeset: r92171:ac3e33369ba0
Date: 2017-07-22 03:54 +0200
http://bitbucket.org/pypy/pypy/changeset/ac3e33369ba0/
Log: Test and fix type of result of <subclass of
bytes/bytearray>.fromhex().
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
@@ -204,7 +204,10 @@
data = _hexstring_to_array(space, hexstring)
# in CPython bytearray.fromhex is a staticmethod, so
# we ignore w_type and always return a bytearray
- return new_bytearray(space, space.w_bytearray, data)
+ w_result = new_bytearray(space, space.w_bytearray, data)
+ if w_bytearraytype is not space.w_bytearray:
+ w_result = space.call_function(w_bytearraytype, w_result)
+ return w_result
@unwrap_spec(encoding='text_or_none', errors='text_or_none')
def descr_init(self, space, w_source=None, encoding=None, errors=None):
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
@@ -564,7 +564,10 @@
from pypy.objspace.std.bytearrayobject import _hexstring_to_array
hexstring = space.unicode_w(w_hexstring)
bytes = ''.join(_hexstring_to_array(space, hexstring))
- return W_BytesObject(bytes)
+ w_result = W_BytesObject(bytes)
+ if w_type is not space.w_bytes:
+ w_result = space.call_function(w_type, w_result)
+ return w_result
def descr_repr(self, space):
return space.newtext(string_escape_encode(self._value, 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
@@ -439,6 +439,11 @@
raises(ValueError, bytearray.fromhex, '12 \x00 34')
raises(ValueError, bytearray.fromhex, '\u1234')
+ def test_fromhex_subclass(self):
+ class Sub(bytearray):
+ pass
+ assert type(Sub.fromhex("abcd")) is Sub
+
def test_extend(self):
b = bytearray(b'abc')
b.extend(bytearray(b'def'))
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
@@ -135,6 +135,11 @@
raises(TypeError, bytes.fromhex, True)
raises(ValueError, bytes.fromhex, "hello world")
+ def test_fromhex_subclass(self):
+ class Sub(bytes):
+ pass
+ assert type(Sub.fromhex("abcd")) is Sub
+
def test_format(self):
raises(TypeError, "foo".__mod__, "bar")
raises(TypeError, u"foo".__mod__, "bar")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit