From b0638e2da3ef7e99cde6a203ee58939fcbe1450c Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 23 Aug 2021 10:36:58 +0900 Subject: [PATCH 1/3] bpo-24234: Implement bytes.__bytes__ --- Lib/test/test_bytes.py | 14 +++++++++++++ Lib/test/test_typing.py | 2 +- .../2021-08-23-10-36-55.bpo-24234.MGVUQi.rst | 3 +++ Objects/bytesobject.c | 20 +++++++++++++++++++ Objects/clinic/bytesobject.c.h | 20 ++++++++++++++++++- Tools/clinic/clinic.py | 1 - 6 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-08-23-10-36-55.bpo-24234.MGVUQi.rst diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 13ad238242d0c5..07d89901998c40 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -981,6 +981,20 @@ def test_sq_item(self): class BytesTest(BaseBytesTest, unittest.TestCase): type2test = bytes + + def test__bytes__(self): + foo = b'foo' + self.assertEqual(foo.__bytes__(), foo) + self.assertEqual(type(foo.__bytes__()), self.type2test) + + class bytes_subclass(bytes): + pass + + bar = bytes_subclass(b'bar') + self.assertEqual(bar.__bytes__(), bar) + self.assertEqual(type(bar.__bytes__()), self.type2test) + + def test_getitem_error(self): b = b'python' msg = "byte indices must be integers or slices" diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 84521ee7dd95f5..de4db51bdc7075 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1543,11 +1543,11 @@ def __complex__(self): def test_supports_bytes(self): - # Note: bytes itself doesn't have __bytes__. class B: def __bytes__(self): return b'' + self.assertIsSubclass(bytes, typing.SupportsBytes) self.assertIsSubclass(B, typing.SupportsBytes) self.assertNotIsSubclass(str, typing.SupportsBytes) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-08-23-10-36-55.bpo-24234.MGVUQi.rst b/Misc/NEWS.d/next/Core and Builtins/2021-08-23-10-36-55.bpo-24234.MGVUQi.rst new file mode 100644 index 00000000000000..3f724da08898a1 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-08-23-10-36-55.bpo-24234.MGVUQi.rst @@ -0,0 +1,3 @@ +Implement the :meth:`__bytes__` special method on the :class:`bytes` type, +so a bytes object ``b`` passes an ``isinstance(b, typing.SupportsBytes)`` +check. diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index eaedb0b5689b2a..13f94b4c82fbc5 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -1687,6 +1687,25 @@ static PyBufferProcs bytes_as_buffer = { }; +/*[clinic input] +bytes.__bytes__ +Convert this value to exact type bytes. +[clinic start generated code]*/ + +static PyObject * +bytes___bytes___impl(PyBytesObject *self) +/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/ +{ + if (PyBytes_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } + else { + return PyBytes_FromString(self->ob_sval); + } +} + + #define LEFTSTRIP 0 #define RIGHTSTRIP 1 #define BOTHSTRIP 2 @@ -2474,6 +2493,7 @@ bytes_getnewargs(PyBytesObject *v, PyObject *Py_UNUSED(ignored)) static PyMethodDef bytes_methods[] = { {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, + BYTES___BYTES___METHODDEF {"capitalize", stringlib_capitalize, METH_NOARGS, _Py_capitalize__doc__}, STRINGLIB_CENTER_METHODDEF diff --git a/Objects/clinic/bytesobject.c.h b/Objects/clinic/bytesobject.c.h index 9e365ce1a088ba..103a3642813218 100644 --- a/Objects/clinic/bytesobject.c.h +++ b/Objects/clinic/bytesobject.c.h @@ -2,6 +2,24 @@ preserve [clinic start generated code]*/ +PyDoc_STRVAR(bytes___bytes____doc__, +"__bytes__($self, /)\n" +"--\n" +"\n" +"Convert this value to exact type bytes."); + +#define BYTES___BYTES___METHODDEF \ + {"__bytes__", (PyCFunction)bytes___bytes__, METH_NOARGS, bytes___bytes____doc__}, + +static PyObject * +bytes___bytes___impl(PyBytesObject *self); + +static PyObject * +bytes___bytes__(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) +{ + return bytes___bytes___impl(self); +} + PyDoc_STRVAR(bytes_split__doc__, "split($self, /, sep=None, maxsplit=-1)\n" "--\n" @@ -878,4 +896,4 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=b3f0ec2753246b9c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d706344859f40122 input=a9049054013a1b77]*/ diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index ed17fabf8d2a14..b5a884536a9ebe 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2179,7 +2179,6 @@ def __repr__(self): __abs__ __add__ __and__ -__bytes__ __call__ __delitem__ __divmod__ From 8f283dadaabbae193c63cbe30897edc651beb36e Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 23 Aug 2021 10:44:46 +0900 Subject: [PATCH 2/3] bpo-24234: nit --- Lib/test/test_bytes.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 07d89901998c40..c45c69f3f22c1f 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -981,7 +981,6 @@ def test_sq_item(self): class BytesTest(BaseBytesTest, unittest.TestCase): type2test = bytes - def test__bytes__(self): foo = b'foo' self.assertEqual(foo.__bytes__(), foo) @@ -994,7 +993,6 @@ class bytes_subclass(bytes): self.assertEqual(bar.__bytes__(), bar) self.assertEqual(type(bar.__bytes__()), self.type2test) - def test_getitem_error(self): b = b'python' msg = "byte indices must be integers or slices" From a6ba24a5805bb839b9e5ccb7ce81e42714f040a8 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 23 Aug 2021 17:42:05 +0900 Subject: [PATCH 3/3] bpo-24234: Add whatsnew 3.11 --- Doc/whatsnew/3.11.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index cb8f84c4b625e5..306385c2a90aaf 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -169,6 +169,14 @@ Other Language Changes (Contributed by Serhiy Storchaka in :issue:`12022`.) +Other CPython Implementation Changes +==================================== + +* Special methods :meth:`complex.__complex__` and :meth:`bytes.__bytes__` are implemented to + support :class:`typing.SupportsComplex` and :class:`typing.SupportsBytes` protocols. + (Contributed by Mark Dickinson and Dong-hee Na in :issue:`24234`.) + + New Modules ===========