File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 1+ Add ``PyBytes_Type.tp_alloc `` to initialize ``PyBytesObject.ob_shash `` for
2+ bytes subclasses.
Original file line number Diff line number Diff 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+
28642883static PyObject *
28652884bytes_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};
You can’t perform that action at this time.
0 commit comments