Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d2403f

Browse files
authored
gh-91020: Add PyBytes_Type.tp_alloc for subclass (GH-91686)
1 parent 692aea6 commit 4d2403f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``PyBytes_Type.tp_alloc`` to initialize ``PyBytesObject.ob_shash`` for
2+
bytes subclasses.

Objects/bytesobject.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2861,6 +2861,25 @@ PyBytes_FromObject(PyObject *x)
28612861
return NULL;
28622862
}
28632863

2864+
/* This allocator is needed for subclasses don't want to use __new__.
2865+
* See https://github.com/python/cpython/issues/91020#issuecomment-1096793239
2866+
*
2867+
* This allocator will be removed when ob_shash is removed.
2868+
*/
2869+
static PyObject *
2870+
bytes_alloc(PyTypeObject *self, Py_ssize_t nitems)
2871+
{
2872+
PyBytesObject *obj = (PyBytesObject*)PyType_GenericAlloc(self, nitems);
2873+
if (obj == NULL) {
2874+
return NULL;
2875+
}
2876+
_Py_COMP_DIAG_PUSH
2877+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
2878+
obj->ob_shash = -1;
2879+
_Py_COMP_DIAG_POP
2880+
return (PyObject*)obj;
2881+
}
2882+
28642883
static PyObject *
28652884
bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
28662885
{
@@ -2937,7 +2956,7 @@ PyTypeObject PyBytes_Type = {
29372956
0, /* tp_descr_set */
29382957
0, /* tp_dictoffset */
29392958
0, /* tp_init */
2940-
0, /* tp_alloc */
2959+
bytes_alloc, /* tp_alloc */
29412960
bytes_new, /* tp_new */
29422961
PyObject_Del, /* tp_free */
29432962
};

0 commit comments

Comments
 (0)