diff options
author | Inada Naoki <songofacandy@gmail.com> | 2022-04-20 05:06:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-20 05:06:29 (GMT) |
commit | 4d2403fd5017324515016a8fad8545935a5c81ef (patch) | |
tree | 4437b6eac49c5a8cbc0a7b41c8d5d0e62632862a | |
parent | 692aea6f3823df48b7fc267ba0aa1ccc45ac606d (diff) | |
download | cpython-4d2403fd5017324515016a8fad8545935a5c81ef.zip cpython-4d2403fd5017324515016a8fad8545935a5c81ef.tar.gz cpython-4d2403fd5017324515016a8fad8545935a5c81ef.tar.bz2 |
gh-91020: Add `PyBytes_Type.tp_alloc` for subclass (GH-91686)
-rw-r--r-- | Misc/NEWS.d/next/C API/2022-04-19-17-05-39.gh-issue-91020.BVJ8F3.rst | 2 | ||||
-rw-r--r-- | Objects/bytesobject.c | 21 |
2 files changed, 22 insertions, 1 deletions
diff --git a/Misc/NEWS.d/next/C API/2022-04-19-17-05-39.gh-issue-91020.BVJ8F3.rst b/Misc/NEWS.d/next/C API/2022-04-19-17-05-39.gh-issue-91020.BVJ8F3.rst new file mode 100644 index 0000000..1572c96 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-04-19-17-05-39.gh-issue-91020.BVJ8F3.rst @@ -0,0 +1,2 @@ +Add ``PyBytes_Type.tp_alloc`` to initialize ``PyBytesObject.ob_shash`` for +bytes subclasses. diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index d0124c0..510a836 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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 */ }; |