Skip to content

Commit

Permalink
gh-91020: Add PyBytes_Type.tp_alloc for subclass (GH-91686)
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Apr 20, 2022
1 parent 692aea6 commit 4d2403f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
@@ -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
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

0 comments on commit 4d2403f

Please sign in to comment.