[Python-checkins] bpo-46864: Deprecate PyBytesObject.ob_shash. (GH-31598)

methane webhook-mailer at python.org
Sat Mar 5 21:39:33 EST 2022


https://github.com/python/cpython/commit/2d8b764210c8de10893665aaeec8277b687975cd
commit: 2d8b764210c8de10893665aaeec8277b687975cd
branch: main
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2022-03-06T11:39:10+09:00
summary:

bpo-46864: Deprecate PyBytesObject.ob_shash. (GH-31598)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-02-26-19-26-36.bpo-46864.EmLgFp.rst
M Doc/whatsnew/3.11.rst
M Include/cpython/bytesobject.h
M Objects/bytesobject.c

diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst
index 5843287ae8e18..4a64e044c4a16 100644
--- a/Doc/whatsnew/3.11.rst
+++ b/Doc/whatsnew/3.11.rst
@@ -985,6 +985,9 @@ Deprecated
   <init-config>` instead (:pep:`587`).
   (Contributed by Victor Stinner in :issue:`44113`.)
 
+* Deprecate the ``ob_shash`` member of the :c:type:`PyBytesObject`. Use :c:func:`PyObject_Hash` instead.
+  (Contributed by Inada Naoki in :issue:`46864`.)
+
 Removed
 -------
 
diff --git a/Include/cpython/bytesobject.h b/Include/cpython/bytesobject.h
index 6b3f55224fc55..2c6d631f0b21e 100644
--- a/Include/cpython/bytesobject.h
+++ b/Include/cpython/bytesobject.h
@@ -4,7 +4,7 @@
 
 typedef struct {
     PyObject_VAR_HEAD
-    Py_hash_t ob_shash;
+    Py_DEPRECATED(3.11) Py_hash_t ob_shash;
     char ob_sval[1];
 
     /* Invariants:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-26-19-26-36.bpo-46864.EmLgFp.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-26-19-26-36.bpo-46864.EmLgFp.rst
new file mode 100644
index 0000000000000..82657155d7228
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-26-19-26-36.bpo-46864.EmLgFp.rst	
@@ -0,0 +1 @@
+Deprecate ``PyBytesObject.ob_shash``. It will be removed in Python 3.13.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index c6160aad790be..fd1c58c2f233e 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -105,7 +105,10 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
         return PyErr_NoMemory();
     }
     _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
     op->ob_shash = -1;
+_Py_COMP_DIAG_POP
     if (!use_calloc) {
         op->ob_sval[size] = '\0';
     }
@@ -169,7 +172,10 @@ PyBytes_FromString(const char *str)
         return PyErr_NoMemory();
     }
     _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
     op->ob_shash = -1;
+_Py_COMP_DIAG_POP
     memcpy(op->ob_sval, str, size+1);
     return (PyObject *) op;
 }
@@ -1446,7 +1452,10 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
         return PyErr_NoMemory();
     }
     _PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
     op->ob_shash = -1;
+_Py_COMP_DIAG_POP
     op->ob_sval[size] = '\0';
     if (Py_SIZE(a) == 1 && n > 0) {
         memset(op->ob_sval, a->ob_sval[0] , n);
@@ -1562,11 +1571,14 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
 static Py_hash_t
 bytes_hash(PyBytesObject *a)
 {
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
     if (a->ob_shash == -1) {
         /* Can't fail */
         a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a));
     }
     return a->ob_shash;
+_Py_COMP_DIAG_POP
 }
 
 static PyObject*
@@ -2868,8 +2880,11 @@ bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
     if (pnew != NULL) {
         memcpy(PyBytes_AS_STRING(pnew),
                   PyBytes_AS_STRING(tmp), n+1);
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
         ((PyBytesObject *)pnew)->ob_shash =
             ((PyBytesObject *)tmp)->ob_shash;
+_Py_COMP_DIAG_POP
     }
     return pnew;
 }
@@ -3051,7 +3066,10 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
     sv = (PyBytesObject *) *pv;
     Py_SET_SIZE(sv, newsize);
     sv->ob_sval[newsize] = '\0';
+_Py_COMP_DIAG_PUSH
+_Py_COMP_DIAG_IGNORE_DEPR_DECLS
     sv->ob_shash = -1;          /* invalidate cached hash value */
+_Py_COMP_DIAG_POP
     return 0;
 error:
     *pv = 0;



More information about the Python-checkins mailing list