Skip to content

Commit 2d8b764

Browse files
authored
bpo-46864: Deprecate PyBytesObject.ob_shash. (GH-31598)
1 parent 6927632 commit 2d8b764

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,9 @@ Deprecated
985985
<init-config>` instead (:pep:`587`).
986986
(Contributed by Victor Stinner in :issue:`44113`.)
987987

988+
* Deprecate the ``ob_shash`` member of the :c:type:`PyBytesObject`. Use :c:func:`PyObject_Hash` instead.
989+
(Contributed by Inada Naoki in :issue:`46864`.)
990+
988991
Removed
989992
-------
990993

Include/cpython/bytesobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
typedef struct {
66
PyObject_VAR_HEAD
7-
Py_hash_t ob_shash;
7+
Py_DEPRECATED(3.11) Py_hash_t ob_shash;
88
char ob_sval[1];
99

1010
/* Invariants:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate ``PyBytesObject.ob_shash``. It will be removed in Python 3.13.

Objects/bytesobject.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
105105
return PyErr_NoMemory();
106106
}
107107
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
108+
_Py_COMP_DIAG_PUSH
109+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
108110
op->ob_shash = -1;
111+
_Py_COMP_DIAG_POP
109112
if (!use_calloc) {
110113
op->ob_sval[size] = '\0';
111114
}
@@ -169,7 +172,10 @@ PyBytes_FromString(const char *str)
169172
return PyErr_NoMemory();
170173
}
171174
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
175+
_Py_COMP_DIAG_PUSH
176+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
172177
op->ob_shash = -1;
178+
_Py_COMP_DIAG_POP
173179
memcpy(op->ob_sval, str, size+1);
174180
return (PyObject *) op;
175181
}
@@ -1446,7 +1452,10 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
14461452
return PyErr_NoMemory();
14471453
}
14481454
_PyObject_InitVar((PyVarObject*)op, &PyBytes_Type, size);
1455+
_Py_COMP_DIAG_PUSH
1456+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
14491457
op->ob_shash = -1;
1458+
_Py_COMP_DIAG_POP
14501459
op->ob_sval[size] = '\0';
14511460
if (Py_SIZE(a) == 1 && n > 0) {
14521461
memset(op->ob_sval, a->ob_sval[0] , n);
@@ -1562,11 +1571,14 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
15621571
static Py_hash_t
15631572
bytes_hash(PyBytesObject *a)
15641573
{
1574+
_Py_COMP_DIAG_PUSH
1575+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
15651576
if (a->ob_shash == -1) {
15661577
/* Can't fail */
15671578
a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a));
15681579
}
15691580
return a->ob_shash;
1581+
_Py_COMP_DIAG_POP
15701582
}
15711583

15721584
static PyObject*
@@ -2868,8 +2880,11 @@ bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
28682880
if (pnew != NULL) {
28692881
memcpy(PyBytes_AS_STRING(pnew),
28702882
PyBytes_AS_STRING(tmp), n+1);
2883+
_Py_COMP_DIAG_PUSH
2884+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
28712885
((PyBytesObject *)pnew)->ob_shash =
28722886
((PyBytesObject *)tmp)->ob_shash;
2887+
_Py_COMP_DIAG_POP
28732888
}
28742889
return pnew;
28752890
}
@@ -3051,7 +3066,10 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
30513066
sv = (PyBytesObject *) *pv;
30523067
Py_SET_SIZE(sv, newsize);
30533068
sv->ob_sval[newsize] = '\0';
3069+
_Py_COMP_DIAG_PUSH
3070+
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
30543071
sv->ob_shash = -1; /* invalidate cached hash value */
3072+
_Py_COMP_DIAG_POP
30553073
return 0;
30563074
error:
30573075
*pv = 0;

0 commit comments

Comments
 (0)