File tree 2 files changed +22
-1
lines changed 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)
2861
2861
return NULL ;
2862
2862
}
2863
2863
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
+
2864
2883
static PyObject *
2865
2884
bytes_subtype_new (PyTypeObject * type , PyObject * tmp )
2866
2885
{
@@ -2937,7 +2956,7 @@ PyTypeObject PyBytes_Type = {
2937
2956
0 , /* tp_descr_set */
2938
2957
0 , /* tp_dictoffset */
2939
2958
0 , /* tp_init */
2940
- 0 , /* tp_alloc */
2959
+ bytes_alloc , /* tp_alloc */
2941
2960
bytes_new , /* tp_new */
2942
2961
PyObject_Del , /* tp_free */
2943
2962
};
You can’t perform that action at this time.
0 commit comments