GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-24234: Implement bytes.__bytes__ by corona10 · Pull Request #27901 · python/cpython

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.

Sign up for GitHub

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
corona10 merged 3 commits into python:main from corona10:bpo-24234
Aug 23, 2021

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-24234: Implement bytes.__bytes__ #27901

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
b0638e2
bpo-24234: Implement bytes.__bytes__
corona10 Aug 23, 2021
8f283da
bpo-24234: nit
corona10 Aug 23, 2021
a6ba24a
bpo-24234: Add whatsnew 3.11
corona10 Aug 23, 2021
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 8 additions & 0 deletions 8 Doc/whatsnew/3.11.rst
Show comments
View file
Open in desktop
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
Comment thread Outdated

@corona10 corona10 Aug 23, 2021

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mdickinson mdickinson Aug 23, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

corona10 reacted with rocket emoji
All reactions
  • 🚀 1 reaction
====================================

* 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 12 Lib/test/test_bytes.py
Show comments
View file
Open in desktop
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
2 changes: 1 addition & 1 deletion 2 Lib/test/test_typing.py
Show comments
View file
Open in desktop
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
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Core and Builtins/2021-08-23-10-36-55.bpo-24234.MGVUQi.rst
Show comments
View file
Open in desktop
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 20 Objects/bytesobject.c
Show comments
View file
Open in desktop
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 20 Objects/clinic/bytesobject.c.h
Show comments
View file
Open in desktop

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

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

1 change: 0 additions & 1 deletion 1 Tools/clinic/clinic.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2179,7 +2179,6 @@ def __repr__(self):
__abs__
__add__
__and__
__bytes__
Comment thread Outdated

@corona10 corona10 Aug 23, 2021

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

https://docs.python.org/3/c-api/typeobj.html?highlight=nb_int#sub-slots

Same reason as __complex__
#27886

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
__call__
__delitem__
__divmod__
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.