Issue #27007: The fromhex() class methods of bytes and bytearray subclasses

now return an instance of corresponding subclass.
This commit is contained in:
Serhiy Storchaka 2016-07-01 17:22:31 +03:00
parent cf8b42e904
commit 0855e706aa
5 changed files with 50 additions and 10 deletions

View file

@ -1589,7 +1589,32 @@ class SubclassTest:
self.assertEqual(type(a), type(b))
self.assertEqual(type(a.y), type(b.y))
test_fromhex = BaseBytesTest.test_fromhex
def test_fromhex(self):
b = self.type2test.fromhex('1a2B30')
self.assertEqual(b, b'\x1a\x2b\x30')
self.assertIs(type(b), self.type2test)
class B1(self.basetype):
def __new__(cls, value):
me = self.basetype.__new__(cls, value)
me.foo = 'bar'
return me
b = B1.fromhex('1a2B30')
self.assertEqual(b, b'\x1a\x2b\x30')
self.assertIs(type(b), B1)
self.assertEqual(b.foo, 'bar')
class B2(self.basetype):
def __init__(me, *args, **kwargs):
if self.basetype is not bytes:
self.basetype.__init__(me, *args, **kwargs)
me.foo = 'bar'
b = B2.fromhex('1a2B30')
self.assertEqual(b, b'\x1a\x2b\x30')
self.assertIs(type(b), B2)
self.assertEqual(b.foo, 'bar')
class ByteArraySubclass(bytearray):