8000 bpo-24234: Implement bytes.__bytes__ by corona10 · Pull Request #27901 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24234: Implement bytes.__bytes__ #27901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ Other Language Changes
(Contributed by Serhiy Storchaka in :issue:`12022`.)


Other CPython Implementation Changes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdickinson Can you please take a look? Sorry, I am not a good English writer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corona10 What you have looks fine to me!

====================================

* 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
===========

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,18 @@ 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"
Expand Down
8000
2 changes: 1 addition & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion Objects/clinic/bytesobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,6 @@ def __repr__(self):
__abs__
__add__
__and__
__bytes__
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__call__
__delitem__
__divmod__
Expand Down
0