Skip to content
Open
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
10 changes: 8 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2970,10 +2970,16 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
"errors without a string argument");
return NULL;
}
/* bytes is immutable and bytes.__bytes__() returns an exact bytes
unchanged, so skip the lookup and the call. */
else if (PyBytes_CheckExact(x)) {
bytes = Py_NewRef(x);
}
/* We'd like to call PyObject_Bytes here, but we need to check for an
integer argument before deferring to PyBytes_FromObject, something
PyObject_Bytes doesn't do. */
else if ((func = _PyObject_LookupSpecial(x, &_Py_ID(__bytes__))) != NULL) {
PyObject_Bytes doesn't do. int is immutable and has no __bytes__. */
else if (!PyLong_CheckExact(x) &&
(func = _PyObject_LookupSpecial(x, &_Py_ID(__bytes__))) != NULL) {
bytes = _PyObject_CallNoArgs(func);
Py_DECREF(func);
if (bytes == NULL)
Expand Down
Loading