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 /Objects | |
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 'Objects')
-rw-r--r-- | Objects/bytearrayobject.c | 12 | ||||
-rw-r--r-- | Objects/bytesobject.c | 7 | ||||
-rw-r--r-- | Objects/clinic/bytearrayobject.c.h | 8 |
3 files changed, 18 insertions, 9 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b7dfd6f..85990e0 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1968,7 +1968,6 @@ bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) @classmethod bytearray.fromhex - cls: self(type="PyObject*") string: unicode / @@ -1979,10 +1978,15 @@ Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') [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 --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 83776aa..8ad2782 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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 --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h index f1ccaf1..e1cf03b 100644 --- a/Objects/clinic/bytearrayobject.c.h +++ b/Objects/clinic/bytearrayobject.c.h @@ -636,10 +636,10 @@ 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; @@ -647,7 +647,7 @@ bytearray_fromhex(PyTypeObject *cls, PyObject *arg) 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; @@ -716,4 +716,4 @@ bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) { return bytearray_sizeof_impl(self); } -/*[clinic end generated code: output=044a6c26a836bcfe input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a32f183ebef159cc input=a9049054013a1b77]*/ |