diff -r 2b32cb33fa8b Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Thu May 12 12:33:41 2016 +0300 +++ b/Lib/test/test_bytes.py Thu May 12 13:02:21 2016 +0300 @@ -1582,7 +1582,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): diff -r 2b32cb33fa8b Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Thu May 12 12:33:41 2016 +0300 +++ b/Objects/bytearrayobject.c Thu May 12 13:02:21 2016 +0300 @@ -1971,7 +1971,6 @@ bytearray_splitlines_impl(PyByteArrayObj @classmethod bytearray.fromhex - cls: self(type="PyObject*") string: unicode / @@ -1982,10 +1981,15 @@ Example: bytearray.fromhex('B9 01EF') -> [clinic start generated code]*/ static PyObject * -bytearray_fromhex_impl(PyObject*cls, PyObject *string) -/*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/ +bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) +/*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ { - return _PyBytes_FromHex(string, 1); + PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); + if (type != &PyByteArray_Type && result != NULL) { + Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, + result, NULL)); + } + return result; } PyDoc_STRVAR(hex__doc__, diff -r 2b32cb33fa8b Objects/bytesobject.c --- a/Objects/bytesobject.c Thu May 12 12:33:41 2016 +0300 +++ b/Objects/bytesobject.c Thu May 12 13:02:21 2016 +0300 @@ -2303,7 +2303,12 @@ static PyObject * bytes_fromhex_impl(PyTypeObject *type, PyObject *string) /*[clinic end generated code: output=0973acc63661bb2e input=bf4d1c361670acd3]*/ { - return _PyBytes_FromHex(string, 0); + PyObject *result = _PyBytes_FromHex(string, 0); + if (type != &PyBytes_Type && result != NULL) { + Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, + result, NULL)); + } + return result; } PyObject* diff -r 2b32cb33fa8b Objects/clinic/bytearrayobject.c.h --- a/Objects/clinic/bytearrayobject.c.h Thu May 12 12:33:41 2016 +0300 +++ b/Objects/clinic/bytearrayobject.c.h Thu May 12 13:02:21 2016 +0300 @@ -617,17 +617,17 @@ PyDoc_STRVAR(bytearray_fromhex__doc__, {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__}, static PyObject * -bytearray_fromhex_impl(PyObject*cls, PyObject *string); +bytearray_fromhex_impl(PyTypeObject *type, PyObject *string); static PyObject * -bytearray_fromhex(PyTypeObject *cls, PyObject *arg) +bytearray_fromhex(PyTypeObject *type, PyObject *arg) { PyObject *return_value = NULL; PyObject *string; if (!PyArg_Parse(arg, "U:fromhex", &string)) goto exit; - return_value = bytearray_fromhex_impl((PyObject*)cls, string); + return_value = bytearray_fromhex_impl(type, string); exit: return return_value; @@ -695,4 +695,4 @@ bytearray_sizeof(PyByteArrayObject *self { return bytearray_sizeof_impl(self); } -/*[clinic end generated code: output=966c15ff22c5e243 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=247cf308f5ed8efa input=a9049054013a1b77]*/