Skip to content

gh-91020: Add PyBytes_Type.tp_alloc for subclass #91686

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 2 commits into from
Apr 20, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``PyBytes_Type.tp_alloc`` to initialize ``PyBytesObject.ob_shash`` for
bytes subclasses.
21 changes: 20 additions & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,25 @@ PyBytes_FromObject(PyObject *x)
return NULL;
}

/* This allocator is needed for subclasses don't want to use __new__.
* See https://github.com/python/cpython/issues/91020#issuecomment-1096793239
*
* This allocator will be removed when ob_shash is removed.
*/
static PyObject *
bytes_alloc(PyTypeObject *self, Py_ssize_t nitems)
{
PyBytesObject *obj = (PyBytesObject*)PyType_GenericAlloc(self, nitems);
if (obj == NULL) {
return NULL;
}
_Py_COMP_DIAG_PUSH
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
obj->ob_shash = -1;
_Py_COMP_DIAG_POP
return (PyObject*)obj;
}

static PyObject *
bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
{
Expand Down Expand Up @@ -2937,7 +2956,7 @@ PyTypeObject PyBytes_Type = {
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
bytes_alloc, /* tp_alloc */
bytes_new, /* tp_new */
PyObject_Del, /* tp_free */
};
Expand Down