Skip to content

Commit 24b63c6

Browse files
authored
bpo-24234: Implement bytes.__bytes__ (GH-27901)
1 parent 6082bb5 commit 24b63c6

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ Other Language Changes
169169
(Contributed by Serhiy Storchaka in :issue:`12022`.)
170170

171171

172+
Other CPython Implementation Changes
173+
====================================
174+
175+
* Special methods :meth:`complex.__complex__` and :meth:`bytes.__bytes__` are implemented to
176+
support :class:`typing.SupportsComplex` and :class:`typing.SupportsBytes` protocols.
177+
(Contributed by Mark Dickinson and Dong-hee Na in :issue:`24234`.)
178+
179+
172180
New Modules
173181
===========
174182

Lib/test/test_bytes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,18 @@ def test_sq_item(self):
981981
class BytesTest(BaseBytesTest, unittest.TestCase):
982982
type2test = bytes
983983

984+
def test__bytes__(self):
985+
foo = b'foo'
986+
self.assertEqual(foo.__bytes__(), foo)
987+
self.assertEqual(type(foo.__bytes__()), self.type2test)
988+
989+
class bytes_subclass(bytes):
990+
pass
991+
992+
bar = bytes_subclass(b'bar')
993+
self.assertEqual(bar.__bytes__(), bar)
994+
self.assertEqual(type(bar.__bytes__()), self.type2test)
995+
984996
def test_getitem_error(self):
985997
b = b'python'
986998
msg = "byte indices must be integers or slices"

Lib/test/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,11 +1543,11 @@ def __complex__(self):
15431543

15441544
def test_supports_bytes(self):
15451545

1546-
# Note: bytes itself doesn't have __bytes__.
15471546
class B:
15481547
def __bytes__(self):
15491548
return b''
15501549

1550+
self.assertIsSubclass(bytes, typing.SupportsBytes)
15511551
self.assertIsSubclass(B, typing.SupportsBytes)
15521552
self.assertNotIsSubclass(str, typing.SupportsBytes)
15531553

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Implement the :meth:`__bytes__` special method on the :class:`bytes` type,
2+
so a bytes object ``b`` passes an ``isinstance(b, typing.SupportsBytes)``
3+
check.

Objects/bytesobject.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,25 @@ static PyBufferProcs bytes_as_buffer = {
16871687
};
16881688

16891689

1690+
/*[clinic input]
1691+
bytes.__bytes__
1692+
Convert this value to exact type bytes.
1693+
[clinic start generated code]*/
1694+
1695+
static PyObject *
1696+
bytes___bytes___impl(PyBytesObject *self)
1697+
/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
1698+
{
1699+
if (PyBytes_CheckExact(self)) {
1700+
Py_INCREF(self);
1701+
return (PyObject *)self;
1702+
}
1703+
else {
1704+
return PyBytes_FromString(self->ob_sval);
1705+
}
1706+
}
1707+
1708+
16901709
#define LEFTSTRIP 0
16911710
#define RIGHTSTRIP 1
16921711
#define BOTHSTRIP 2
@@ -2474,6 +2493,7 @@ bytes_getnewargs(PyBytesObject *v, PyObject *Py_UNUSED(ignored))
24742493
static PyMethodDef
24752494
bytes_methods[] = {
24762495
{"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS},
2496+
BYTES___BYTES___METHODDEF
24772497
{"capitalize", stringlib_capitalize, METH_NOARGS,
24782498
_Py_capitalize__doc__},
24792499
STRINGLIB_CENTER_METHODDEF

Objects/clinic/bytesobject.c.h

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/clinic.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,6 @@ def __repr__(self):
21792179
__abs__
21802180
__add__
21812181
__and__
2182-
__bytes__
21832182
__call__
21842183
__delitem__
21852184
__divmod__

0 commit comments

Comments
 (0)