Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0855e70

Browse files
Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
now return an instance of corresponding subclass.
1 parent cf8b42e commit 0855e70

5 files changed

Lines changed: 50 additions & 10 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,32 @@ def test_copy(self):
15891589
self.assertEqual(type(a), type(b))
15901590
self.assertEqual(type(a.y), type(b.y))
15911591

1592-
test_fromhex = BaseBytesTest.test_fromhex
1592+
def test_fromhex(self):
1593+
b = self.type2test.fromhex('1a2B30')
1594+
self.assertEqual(b, b'\x1a\x2b\x30')
1595+
self.assertIs(type(b), self.type2test)
1596+
1597+
class B1(self.basetype):
1598+
def __new__(cls, value):
1599+
me = self.basetype.__new__(cls, value)
1600+
me.foo = 'bar'
1601+
return me
1602+
1603+
b = B1.fromhex('1a2B30')
1604+
self.assertEqual(b, b'\x1a\x2b\x30')
1605+
self.assertIs(type(b), B1)
1606+
self.assertEqual(b.foo, 'bar')
1607+
1608+
class B2(self.basetype):
1609+
def __init__(me, *args, **kwargs):
1610+
if self.basetype is not bytes:
1611+
self.basetype.__init__(me, *args, **kwargs)
1612+
me.foo = 'bar'
1613+
1614+
b = B2.fromhex('1a2B30')
1615+
self.assertEqual(b, b'\x1a\x2b\x30')
1616+
self.assertIs(type(b), B2)
1617+
self.assertEqual(b.foo, 'bar')
15931618

15941619

15951620
class ByteArraySubclass(bytearray):

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ What's New in Python 3.6.0 alpha 3
77

88
*Release date: XXXX-XX-XX*
99

10+
Core and Builtins
11+
-----------------
12+
13+
- Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
14+
now return an instance of corresponding subclass.
15+
1016
Library
1117
-------
1218

Objects/bytearrayobject.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,6 @@ bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
19681968
@classmethod
19691969
bytearray.fromhex
19701970
1971-
cls: self(type="PyObject*")
19721971
string: unicode
19731972
/
19741973
@@ -1979,10 +1978,15 @@ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
19791978
[clinic start generated code]*/
19801979

19811980
static PyObject *
1982-
bytearray_fromhex_impl(PyObject*cls, PyObject *string)
1983-
/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
1981+
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string)
1982+
/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/
19841983
{
1985-
return _PyBytes_FromHex(string, 1);
1984+
PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type);
1985+
if (type != &PyByteArray_Type && result != NULL) {
1986+
Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
1987+
result, NULL));
1988+
}
1989+
return result;
19861990
}
19871991

19881992
PyDoc_STRVAR(hex__doc__,

Objects/bytesobject.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,12 @@ static PyObject *
23032303
bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
23042304
/*[clinic end generated code: output=0973acc63661bb2e input=bf4d1c361670acd3]*/
23052305
{
2306-
return _PyBytes_FromHex(string, 0);
2306+
PyObject *result = _PyBytes_FromHex(string, 0);
2307+
if (type != &PyBytes_Type && result != NULL) {
2308+
Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type,
2309+
result, NULL));
2310+
}
2311+
return result;
23072312
}
23082313

23092314
PyObject*

Objects/clinic/bytearrayobject.c.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,18 +636,18 @@ PyDoc_STRVAR(bytearray_fromhex__doc__,
636636
{"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__},
637637

638638
static PyObject *
639-
bytearray_fromhex_impl(PyObject*cls, PyObject *string);
639+
bytearray_fromhex_impl(PyTypeObject *type, PyObject *string);
640640

641641
static PyObject *
642-
bytearray_fromhex(PyTypeObject *cls, PyObject *arg)
642+
bytearray_fromhex(PyTypeObject *type, PyObject *arg)
643643
{
644644
PyObject *return_value = NULL;
645645
PyObject *string;
646646

647647
if (!PyArg_Parse(arg, "U:fromhex", &string)) {
648648
goto exit;
649649
}
650-
return_value = bytearray_fromhex_impl((PyObject*)cls, string);
650+
return_value = bytearray_fromhex_impl(type, string);
651651

652652
exit:
653653
return return_value;
@@ -716,4 +716,4 @@ bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
716716
{
717717
return bytearray_sizeof_impl(self);
718718
}
719-
/*[clinic end generated code: output=044a6c26a836bcfe input=a9049054013a1b77]*/
719+
/*[clinic end generated code: output=a32f183ebef159cc input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)