mirror of https://github.com/python/cpython
gh-91020: Add `PyBytes_Type.tp_alloc` for subclass (GH-91686)
This commit is contained in:
parent
692aea6f38
commit
4d2403fd50
|
@ -0,0 +1,2 @@
|
|||
Add ``PyBytes_Type.tp_alloc`` to initialize ``PyBytesObject.ob_shash`` for
|
||||
bytes subclasses.
|
|
@ -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)
|
||||
{
|
||||
|
@ -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 */
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue