Skip to content

Commit 5f06e7f

Browse files
authored
Merge branch 'python:main' into amit
2 parents 64aa8eb + 8a00c9a commit 5f06e7f

File tree

130 files changed

+1611
-392
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1611
-392
lines changed

Include/cpython/tupleobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
typedef struct {
66
PyObject_VAR_HEAD
7+
/* Cached hash. Initially set to -1. */
8+
Py_hash_t ob_hash;
79
/* ob_item contains space for 'ob_size' elements.
810
Items must normally not be NULL, except during construction when
911
the tuple is not yet visible outside the function that builds it. */

Include/internal/pycore_runtime_init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
#include "pycore_runtime_init_generated.h" // _Py_bytes_characters_INIT
2828
#include "pycore_signal.h" // _signals_RUNTIME_INIT
2929
#include "pycore_tracemalloc.h" // _tracemalloc_runtime_state_INIT
30+
#include "pycore_tuple.h" // _PyTuple_HASH_EMPTY
3031

3132

3233
extern PyTypeObject _PyExc_MemoryError;
@@ -106,6 +107,7 @@ extern PyTypeObject _PyExc_MemoryError;
106107
}, \
107108
.tuple_empty = { \
108109
.ob_base = _PyVarObject_HEAD_INIT(&PyTuple_Type, 0), \
110+
.ob_hash = _PyTuple_HASH_EMPTY, \
109111
}, \
110112
.hamt_bitmap_node_empty = { \
111113
.ob_base = _PyVarObject_HEAD_INIT(&_PyHamt_BitmapNode_Type, 0), \

Include/internal/pycore_stackref.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ extern "C" {
3535
unboxed integers harder in the future.
3636
3737
Steal means that ownership is transferred to something else. The total
38-
number of references to the object stays the same.
38+
number of references to the object stays the same. The old reference is no
39+
longer valid.
3940
4041
New creates a new reference from the old reference. The old reference
4142
is still valid.
4243
43-
With these 3 API, a strict stack discipline must be maintained. All
44-
_PyStackRef must be operated on by the new reference operations:
44+
All _PyStackRef must be operated on by the new reference operations:
4545
4646
1. DUP
4747
2. CLOSE
@@ -668,7 +668,7 @@ static inline int
668668
_Py_TryIncrefCompareStackRef(PyObject **src, PyObject *op, _PyStackRef *out)
669669
{
670670
if (_PyObject_HasDeferredRefcount(op)) {
671-
*out = (_PyStackRef){ .bits = (intptr_t)op | Py_TAG_DEFERRED };
671+
*out = (_PyStackRef){ .bits = (uintptr_t)op | Py_TAG_DEFERRED };
672672
return 1;
673673
}
674674
if (_Py_TryIncrefCompare(src, op)) {

Include/internal/pycore_tuple.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_object.h" // _PyObject_GC_IS_TRACKED
1112
#include "pycore_structs.h" // _PyStackRef
1213

1314
extern void _PyTuple_MaybeUntrack(PyObject *);
@@ -32,6 +33,42 @@ typedef struct {
3233
PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
3334
} _PyTupleIterObject;
3435

36+
#define _PyTuple_RESET_HASH_CACHE(op) \
37+
do { \
38+
assert(op != NULL); \
39+
_PyTuple_CAST(op)->ob_hash = -1; \
40+
} while (0)
41+
42+
/* bpo-42536: If reusing a tuple object, this should be called to re-track it
43+
with the garbage collector and reset its hash cache. */
44+
static inline void
45+
_PyTuple_Recycle(PyObject *op)
46+
{
47+
_PyTuple_RESET_HASH_CACHE(op);
48+
if (!_PyObject_GC_IS_TRACKED(op)) {
49+
_PyObject_GC_TRACK(op);
50+
}
51+
}
52+
53+
/* Below are the official constants from the xxHash specification. Optimizing
54+
compilers should emit a single "rotate" instruction for the
55+
_PyTuple_HASH_XXROTATE() expansion. If that doesn't happen for some important
56+
platform, the macro could be changed to expand to a platform-specific rotate
57+
spelling instead.
58+
*/
59+
#if SIZEOF_PY_UHASH_T > 4
60+
#define _PyTuple_HASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
61+
#define _PyTuple_HASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
62+
#define _PyTuple_HASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
63+
#define _PyTuple_HASH_XXROTATE(x) ((x << 31) | (x >> 33)) /* Rotate left 31 bits */
64+
#else
65+
#define _PyTuple_HASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
66+
#define _PyTuple_HASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
67+
#define _PyTuple_HASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
68+
#define _PyTuple_HASH_XXROTATE(x) ((x << 13) | (x >> 19)) /* Rotate left 13 bits */
69+
#endif
70+
#define _PyTuple_HASH_EMPTY (_PyTuple_HASH_XXPRIME_5 + (_PyTuple_HASH_XXPRIME_5 ^ 3527539UL))
71+
3572
#ifdef __cplusplus
3673
}
3774
#endif

Lib/locale.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import sys
1414
import encodings
1515
import encodings.aliases
16-
import re
1716
import _collections_abc
1817
from builtins import str as _builtin_str
1918
import functools
@@ -177,8 +176,7 @@ def _strip_padding(s, amount):
177176
amount -= 1
178177
return s[lpos:rpos+1]
179178

180-
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
181-
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
179+
_percent_re = None
182180

183181
def _format(percent, value, grouping=False, monetary=False, *additional):
184182
if additional:
@@ -217,6 +215,13 @@ def format_string(f, val, grouping=False, monetary=False):
217215
Grouping is applied if the third parameter is true.
218216
Conversion uses monetary thousands separator and grouping strings if
219217
forth parameter monetary is true."""
218+
global _percent_re
219+
if _percent_re is None:
220+
import re
221+
222+
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?(?P<modifiers'
223+
r'>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
224+
220225
percents = list(_percent_re.finditer(f))
221226
new_f = _percent_re.sub('%s', f)
222227

0 commit comments

Comments
 (0)