@@ -51,6 +51,33 @@ static inline PyObject* bytes_get_empty(void)
51
51
}
52
52
53
53
54
+ static inline void
55
+ set_ob_shash (PyBytesObject * a , Py_hash_t hash )
56
+ {
57
+ _Py_COMP_DIAG_PUSH
58
+ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
59
+ #ifdef Py_GIL_DISABLED
60
+ _Py_atomic_store_ssize_relaxed (& a -> ob_shash , hash );
61
+ #else
62
+ a -> ob_shash = hash ;
63
+ #endif
64
+ _Py_COMP_DIAG_POP
65
+ }
66
+
67
+ static inline Py_hash_t
68
+ get_ob_shash (PyBytesObject * a )
69
+ {
70
+ _Py_COMP_DIAG_PUSH
71
+ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
72
+ #ifdef Py_GIL_DISABLED
73
+ return _Py_atomic_load_ssize_relaxed (& a -> ob_shash );
74
+ #else
75
+ return a -> ob_shash ;
76
+ #endif
77
+ _Py_COMP_DIAG_POP
78
+ }
79
+
80
+
54
81
/*
55
82
For PyBytes_FromString(), the parameter 'str' points to a null-terminated
56
83
string containing exactly 'size' bytes.
@@ -98,10 +125,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
98
125
return PyErr_NoMemory ();
99
126
}
100
127
_PyObject_InitVar ((PyVarObject * )op , & PyBytes_Type , size );
101
- _Py_COMP_DIAG_PUSH
102
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
103
- op -> ob_shash = -1 ;
104
- _Py_COMP_DIAG_POP
128
+ set_ob_shash (op , -1 );
105
129
if (!use_calloc ) {
106
130
op -> ob_sval [size ] = '\0' ;
107
131
}
@@ -165,10 +189,7 @@ PyBytes_FromString(const char *str)
165
189
return PyErr_NoMemory ();
166
190
}
167
191
_PyObject_InitVar ((PyVarObject * )op , & PyBytes_Type , size );
168
- _Py_COMP_DIAG_PUSH
169
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
170
- op -> ob_shash = -1 ;
171
- _Py_COMP_DIAG_POP
192
+ set_ob_shash (op , -1 );
172
193
memcpy (op -> ob_sval , str , size + 1 );
173
194
return (PyObject * ) op ;
174
195
}
@@ -1485,10 +1506,7 @@ bytes_repeat(PyObject *self, Py_ssize_t n)
1485
1506
return PyErr_NoMemory ();
1486
1507
}
1487
1508
_PyObject_InitVar ((PyVarObject * )op , & PyBytes_Type , size );
1488
- _Py_COMP_DIAG_PUSH
1489
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
1490
- op -> ob_shash = -1 ;
1491
- _Py_COMP_DIAG_POP
1509
+ set_ob_shash (op , -1 );
1492
1510
op -> ob_sval [size ] = '\0' ;
1493
1511
1494
1512
_PyBytes_Repeat (op -> ob_sval , size , a -> ob_sval , Py_SIZE (a ));
@@ -1597,14 +1615,13 @@ static Py_hash_t
1597
1615
bytes_hash (PyObject * self )
1598
1616
{
1599
1617
PyBytesObject * a = _PyBytes_CAST (self );
1600
- _Py_COMP_DIAG_PUSH
1601
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
1602
- if (a -> ob_shash == -1 ) {
1618
+ Py_hash_t hash = get_ob_shash (a );
1619
+ if (hash == -1 ) {
1603
1620
/* Can't fail */
1604
- a -> ob_shash = Py_HashBuffer (a -> ob_sval , Py_SIZE (a ));
1621
+ hash = Py_HashBuffer (a -> ob_sval , Py_SIZE (a ));
1622
+ set_ob_shash (a , hash );
1605
1623
}
1606
- return a -> ob_shash ;
1607
- _Py_COMP_DIAG_POP
1624
+ return hash ;
1608
1625
}
1609
1626
1610
1627
static PyObject *
@@ -3004,10 +3021,7 @@ bytes_alloc(PyTypeObject *self, Py_ssize_t nitems)
3004
3021
if (obj == NULL ) {
3005
3022
return NULL ;
3006
3023
}
3007
- _Py_COMP_DIAG_PUSH
3008
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
3009
- obj -> ob_shash = -1 ;
3010
- _Py_COMP_DIAG_POP
3024
+ set_ob_shash (obj , -1 );
3011
3025
return (PyObject * )obj ;
3012
3026
}
3013
3027
@@ -3024,11 +3038,8 @@ bytes_subtype_new(PyTypeObject *type, PyObject *tmp)
3024
3038
if (pnew != NULL ) {
3025
3039
memcpy (PyBytes_AS_STRING (pnew ),
3026
3040
PyBytes_AS_STRING (tmp ), n + 1 );
3027
- _Py_COMP_DIAG_PUSH
3028
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
3029
- ( (PyBytesObject * )pnew )-> ob_shash =
3030
- ((PyBytesObject * )tmp )-> ob_shash ;
3031
- _Py_COMP_DIAG_POP
3041
+ set_ob_shash ((PyBytesObject * )pnew ,
3042
+ get_ob_shash ((PyBytesObject * )tmp ));
3032
3043
}
3033
3044
return pnew ;
3034
3045
}
@@ -3221,10 +3232,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
3221
3232
sv = (PyBytesObject * ) * pv ;
3222
3233
Py_SET_SIZE (sv , newsize );
3223
3234
sv -> ob_sval [newsize ] = '\0' ;
3224
- _Py_COMP_DIAG_PUSH
3225
- _Py_COMP_DIAG_IGNORE_DEPR_DECLS
3226
- sv -> ob_shash = -1 ; /* invalidate cached hash value */
3227
- _Py_COMP_DIAG_POP
3235
+ set_ob_shash (sv , -1 ); /* invalidate cached hash value */
3228
3236
return 0 ;
3229
3237
}
3230
3238
0 commit comments