diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-07-01 14:22:31 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-07-01 14:22:31 (GMT) |
commit | 0855e706aa7fed842e18b0ce14e18d6574318643 (patch) | |
tree | 2fa36bacf92f87ea7361122e8a49d8690c7d5e1b /Lib/test/test_bytes.py | |
parent | cf8b42e9043766338c0b16d0dca3ed5ca70a812d (diff) | |
download | cpython-0855e706aa7fed842e18b0ce14e18d6574318643.zip cpython-0855e706aa7fed842e18b0ce14e18d6574318643.tar.gz cpython-0855e706aa7fed842e18b0ce14e18d6574318643.tar.bz2 |
Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
now return an instance of corresponding subclass.
Diffstat (limited to 'Lib/test/test_bytes.py')
-rw-r--r-- | Lib/test/test_bytes.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 1cedd3a..9d878ca 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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): |