gh-91020: Add `PyBytes_Type.tp_alloc` for subclass (GH-91686)

This commit is contained in:
Inada Naoki 2022-04-20 14:06:29 +09:00 committed by GitHub
parent 692aea6f38
commit 4d2403fd50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -0,0 +1,2 @@
Add ``PyBytes_Type.tp_alloc`` to initialize ``PyBytesObject.ob_shash`` for
bytes subclasses.

View File

@ -2861,6 +2861,25 @@ PyBytes_FromObject(PyObject *x)
return NULL; 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 * static PyObject *
bytes_subtype_new(PyTypeObject *type, PyObject *tmp) bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
{ {
@ -2937,7 +2956,7 @@ PyTypeObject PyBytes_Type = {
0, /* tp_descr_set */ 0, /* tp_descr_set */
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
0, /* tp_init */ 0, /* tp_init */
0, /* tp_alloc */ bytes_alloc, /* tp_alloc */
bytes_new, /* tp_new */ bytes_new, /* tp_new */
PyObject_Del, /* tp_free */ PyObject_Del, /* tp_free */
}; };