[Python-checkins] cpython: Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
serhiy.storchaka
python-checkins at python.org
Fri Jul 1 10:23:08 EDT 2016
https://hg.python.org/cpython/rev/62375fd21de8
changeset: 102242:62375fd21de8
user: Serhiy Storchaka <storchaka at gmail.com>
date: Fri Jul 01 17:22:31 2016 +0300
summary:
Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
now return an instance of corresponding subclass.
files:
Lib/test/test_bytes.py | 27 +++++++++++++++++-
Misc/NEWS | 6 ++++
Objects/bytearrayobject.c | 12 +++++--
Objects/bytesobject.c | 7 ++++-
Objects/clinic/bytearrayobject.c.h | 8 ++--
5 files changed, 50 insertions(+), 10 deletions(-)
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1589,7 +1589,32 @@
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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -7,6 +7,12 @@
*Release date: XXXX-XX-XX*
+Core and Builtins
+-----------------
+
+- Issue #27007: The fromhex() class methods of bytes and bytearray subclasses
+ now return an instance of corresponding subclass.
+
Library
-------
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -1968,7 +1968,6 @@
@classmethod
bytearray.fromhex
- cls: self(type="PyObject*")
string: unicode
/
@@ -1979,10 +1978,15 @@
[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
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2303,7 +2303,12 @@
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
--- a/Objects/clinic/bytearrayobject.c.h
+++ b/Objects/clinic/bytearrayobject.c.h
@@ -636,10 +636,10 @@
{"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 @@
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 @@
{
return bytearray_sizeof_impl(self);
}
-/*[clinic end generated code: output=044a6c26a836bcfe input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a32f183ebef159cc input=a9049054013a1b77]*/
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list