Skip to content

Commit 1c37fa1

Browse files
committed
Merge remote-tracking branch 'cpython/main' into pythongh-86682
2 parents 4486fd0 + 51d1035 commit 1c37fa1

File tree

91 files changed

+2225
-1817
lines changed

Some content is hidden

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

91 files changed

+2225
-1817
lines changed

Doc/c-api/init.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,12 +1239,25 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
12391239
The global interpreter lock need not be held, but may be held if it is
12401240
necessary to serialize calls to this function.
12411241
1242+
.. audit-event:: cpython.PyThreadState_New id c.PyThreadState_New
1243+
1244+
Raise an auditing event ``cpython.PyThreadState_New`` with Python's thread
1245+
id as the argument. The event will be raised from the thread creating the new
1246+
``PyThreadState``, which may not be the new thread.
1247+
12421248
12431249
.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)
12441250
12451251
Reset all information in a thread state object. The global interpreter lock
12461252
must be held.
12471253
1254+
.. audit-event:: cpython.PyThreadState_Clear id c.PyThreadState_Clear
1255+
1256+
Raise an auditing event ``cpython.PyThreadState_Clear`` with Python's
1257+
thread id as the argument. The event may be raised from a different thread
1258+
than the one being cleared. Exceptions raised from a hook will be treated
1259+
as unraisable and will not abort the operation.
1260+
12481261
.. versionchanged:: 3.9
12491262
This function now calls the :c:member:`PyThreadState.on_delete` callback.
12501263
Previously, that happened in :c:func:`PyThreadState_Delete`.

Doc/library/_thread.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ This module defines the following constants and functions:
5757
When the function raises a :exc:`SystemExit` exception, it is silently
5858
ignored.
5959

60+
.. audit-event:: _thread.start_new_thread function,args,kwargs start_new_thread
61+
6062
.. versionchanged:: 3.8
6163
:func:`sys.unraisablehook` is now used to handle unhandled exceptions.
6264

Include/cpython/modsupport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,3 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
106106
(minpos), (maxpos), (minkw), (buf)))
107107

108108
PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(PyModuleDef*, int apiver);
109-
110-
PyAPI_DATA(const char *) _Py_PackageContext;

Include/internal/pycore_context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ void _PyContext_Fini(PyInterpreterState *);
1818

1919
/* other API */
2020

21+
typedef struct {
22+
PyObject_HEAD
23+
} _PyContextTokenMissing;
24+
2125
#ifndef WITH_FREELISTS
2226
// without freelists
2327
# define PyContext_MAXFREELIST 0

Include/internal/pycore_dict.h

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ extern "C" {
99
# error "this header requires Py_BUILD_CORE define"
1010
#endif
1111

12+
#include "pycore_dict_state.h"
13+
#include "pycore_runtime.h" // _PyRuntime
14+
1215

1316
/* runtime lifecycle */
1417

@@ -17,25 +20,6 @@ extern void _PyDict_Fini(PyInterpreterState *interp);
1720

1821
/* other API */
1922

20-
#ifndef WITH_FREELISTS
21-
// without freelists
22-
# define PyDict_MAXFREELIST 0
23-
#endif
24-
25-
#ifndef PyDict_MAXFREELIST
26-
# define PyDict_MAXFREELIST 80
27-
#endif
28-
29-
struct _Py_dict_state {
30-
#if PyDict_MAXFREELIST > 0
31-
/* Dictionary reuse scheme to save calls to malloc and free */
32-
PyDictObject *free_list[PyDict_MAXFREELIST];
33-
int numfree;
34-
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
35-
int keys_numfree;
36-
#endif
37-
};
38-
3923
typedef struct {
4024
/* Cached hash code of me_key. */
4125
Py_hash_t me_hash;
@@ -152,13 +136,11 @@ struct _dictvalues {
152136
(PyDictUnicodeEntry*)(&((int8_t*)((dk)->dk_indices))[(size_t)1 << (dk)->dk_log2_index_bytes]))
153137
#define DK_IS_UNICODE(dk) ((dk)->dk_kind != DICT_KEYS_GENERAL)
154138

155-
extern uint64_t _pydict_global_version;
156-
157-
#define DICT_MAX_WATCHERS 8
158139
#define DICT_VERSION_INCREMENT (1 << DICT_MAX_WATCHERS)
159140
#define DICT_VERSION_MASK (DICT_VERSION_INCREMENT - 1)
160141

161-
#define DICT_NEXT_VERSION() (_pydict_global_version += DICT_VERSION_INCREMENT)
142+
#define DICT_NEXT_VERSION() \
143+
(_PyRuntime.dict_state.global_version += DICT_VERSION_INCREMENT)
162144

163145
void
164146
_PyDict_SendEvent(int watcher_bits,

Include/internal/pycore_dict_state.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef Py_INTERNAL_DICT_STATE_H
2+
#define Py_INTERNAL_DICT_STATE_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
struct _Py_dict_runtime_state {
13+
/*Global counter used to set ma_version_tag field of dictionary.
14+
* It is incremented each time that a dictionary is created and each
15+
* time that a dictionary is modified. */
16+
uint64_t global_version;
17+
uint32_t next_keys_version;
18+
};
19+
20+
21+
#ifndef WITH_FREELISTS
22+
// without freelists
23+
# define PyDict_MAXFREELIST 0
24+
#endif
25+
26+
#ifndef PyDict_MAXFREELIST
27+
# define PyDict_MAXFREELIST 80
28+
#endif
29+
30+
#define DICT_MAX_WATCHERS 8
31+
32+
struct _Py_dict_state {
33+
#if PyDict_MAXFREELIST > 0
34+
/* Dictionary reuse scheme to save calls to malloc and free */
35+
PyDictObject *free_list[PyDict_MAXFREELIST];
36+
PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
37+
int numfree;
38+
int keys_numfree;
39+
#endif
40+
PyDict_WatchCallback watchers[DICT_MAX_WATCHERS];
41+
};
42+
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
#endif /* !Py_INTERNAL_DICT_STATE_H */

Include/internal/pycore_fileutils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ extern "C" {
1010

1111
#include <locale.h> /* struct lconv */
1212

13+
14+
struct _fileutils_state {
15+
int force_ascii;
16+
};
17+
1318
typedef enum {
1419
_Py_ERROR_UNKNOWN=0,
1520
_Py_ERROR_STRICT,

Include/internal/pycore_floatobject.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ extern void _PyFloat_FiniType(PyInterpreterState *);
1919

2020
/* other API */
2121

22+
enum _py_float_format_type {
23+
_py_float_format_unknown,
24+
_py_float_format_ieee_big_endian,
25+
_py_float_format_ieee_little_endian,
26+
};
27+
28+
struct _Py_float_runtime_state {
29+
enum _py_float_format_type float_format;
30+
enum _py_float_format_type double_format;
31+
};
32+
33+
2234
#ifndef WITH_FREELISTS
2335
// without freelists
2436
# define PyFloat_MAXFREELIST 0

Include/internal/pycore_function.h

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

11+
struct _py_func_runtime_state {
12+
uint32_t next_version;
13+
};
14+
1115
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
1216

1317
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);

Include/internal/pycore_global_objects.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern "C" {
1010

1111
#include "pycore_gc.h" // PyGC_Head
1212
#include "pycore_global_strings.h" // struct _Py_global_strings
13+
#include "pycore_hamt.h" // PyHamtNode_Bitmap
14+
#include "pycore_context.h" // _PyContextTokenMissing
1315
#include "pycore_typeobject.h" // pytype_slotdef
1416

1517

@@ -52,6 +54,10 @@ struct _Py_global_objects {
5254

5355
_PyGC_Head_UNUSED _tuple_empty_gc_not_used;
5456
PyTupleObject tuple_empty;
57+
58+
_PyGC_Head_UNUSED _hamt_bitmap_node_empty_gc_not_used;
59+
PyHamtNode_Bitmap hamt_bitmap_node_empty;
60+
_PyContextTokenMissing context_token_missing;
5561
} singletons;
5662

5763
PyObject *interned;
@@ -76,6 +82,9 @@ struct _Py_interp_cached_objects {
7682
struct _Py_interp_static_objects {
7783
struct {
7884
int _not_used;
85+
// hamt_empty is here instead of global because of its weakreflist.
86+
_PyGC_Head_UNUSED _hamt_empty_gc_not_used;
87+
PyHamtObject hamt_empty;
7988
} singletons;
8089
};
8190

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_hamt.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ extern PyTypeObject _PyHamtKeys_Type;
2828
extern PyTypeObject _PyHamtValues_Type;
2929
extern PyTypeObject _PyHamtItems_Type;
3030

31-
/* runtime lifecycle */
32-
33-
void _PyHamt_Fini(PyInterpreterState *);
34-
3531

3632
/* other API */
3733

@@ -53,6 +49,13 @@ typedef struct {
5349
} PyHamtObject;
5450

5551

52+
typedef struct {
53+
PyObject_VAR_HEAD
54+
uint32_t b_bitmap;
55+
PyObject *b_array[1];
56+
} PyHamtNode_Bitmap;
57+
58+
5659
/* A struct to hold the state of depth-first traverse of the tree.
5760
5861
HAMT is an immutable collection. Iterators will hold a strong reference

Include/internal/pycore_import.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ struct _import_runtime_state {
3232
_PyTime_t accumulated;
3333
int header;
3434
} find_and_load;
35+
/* Package context -- the full module name for package imports */
36+
const char * pkgcontext;
3537
};
3638

3739

Include/internal/pycore_interp.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414
#include "pycore_ast_state.h" // struct ast_state
1515
#include "pycore_code.h" // struct callable_cache
1616
#include "pycore_context.h" // struct _Py_context_state
17-
#include "pycore_dict.h" // struct _Py_dict_state
17+
#include "pycore_dict_state.h" // struct _Py_dict_state
1818
#include "pycore_exceptions.h" // struct _Py_exc_state
1919
#include "pycore_floatobject.h" // struct _Py_float_state
2020
#include "pycore_genobject.h" // struct _Py_async_gen_state
@@ -28,6 +28,7 @@ extern "C" {
2828

2929

3030
struct _pending_calls {
31+
int busy;
3132
PyThread_type_lock lock;
3233
/* Request for running pending calls. */
3334
_Py_atomic_int calls_to_do;
@@ -170,8 +171,6 @@ struct _is {
170171
// Initialized to _PyEval_EvalFrameDefault().
171172
_PyFrameEvalFunction eval_frame;
172173

173-
PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
174-
175174
Py_ssize_t co_extra_user_count;
176175
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
177176

Include/internal/pycore_pyhash.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
# error "this header requires Py_BUILD_CORE define"
66
#endif
77

8-
uint64_t _Py_KeyedHash(uint64_t, const char *, Py_ssize_t);
98

9+
struct pyhash_runtime_state {
10+
struct {
11+
#ifndef MS_WINDOWS
12+
int fd;
13+
dev_t st_dev;
14+
ino_t st_ino;
15+
#else
16+
// This is a placeholder so the struct isn't empty on Windows.
17+
int _not_used;
18+
#endif
19+
} urandom_cache;
20+
};
21+
22+
#ifndef MS_WINDOWS
23+
# define _py_urandom_cache_INIT \
24+
{ \
25+
.fd = -1, \
26+
}
27+
#else
28+
# define _py_urandom_cache_INIT {0}
1029
#endif
30+
31+
#define pyhash_state_INIT \
32+
{ \
33+
.urandom_cache = _py_urandom_cache_INIT, \
34+
}
35+
36+
37+
uint64_t _Py_KeyedHash(uint64_t, const char *, Py_ssize_t);
38+
39+
40+
#endif // Py_INTERNAL_HASH_H

Include/internal/pycore_pylifecycle.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ extern "C" {
1414
struct _PyArgv;
1515
struct pyruntimestate;
1616

17-
/* True if the main interpreter thread exited due to an unhandled
18-
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
19-
PyAPI_DATA(int) _Py_UnhandledKeyboardInterrupt;
20-
2117
extern int _Py_SetFileSystemEncoding(
2218
const char *encoding,
2319
const char *errors);

Include/internal/pycore_runtime.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_atomic.h" /* _Py_atomic_address */
12+
#include "pycore_dict_state.h" // struct _Py_dict_runtime_state
1213
#include "pycore_dtoa.h" // struct _dtoa_runtime_state
14+
#include "pycore_floatobject.h" // struct _Py_float_runtime_state
15+
#include "pycore_function.h" // struct _func_runtime_state
1316
#include "pycore_gil.h" // struct _gil_runtime_state
1417
#include "pycore_global_objects.h" // struct _Py_global_objects
1518
#include "pycore_import.h" // struct _import_runtime_state
1619
#include "pycore_interp.h" // PyInterpreterState
1720
#include "pycore_pymem.h" // struct _pymem_allocators
21+
#include "pycore_pyhash.h" // struct pyhash_runtime_state
1822
#include "pycore_obmalloc.h" // struct obmalloc_state
1923
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids
2024

@@ -92,6 +96,12 @@ typedef struct pyruntimestate {
9296

9397
struct _pymem_allocators allocators;
9498
struct _obmalloc_state obmalloc;
99+
struct pyhash_runtime_state pyhash_state;
100+
struct {
101+
/* True if the main interpreter thread exited due to an unhandled
102+
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
103+
int unhandled_keyboard_interrupt;
104+
} signals;
95105

96106
struct pyinterpreters {
97107
PyThread_type_lock mutex;
@@ -131,6 +141,7 @@ typedef struct pyruntimestate {
131141
struct _PyTraceMalloc_Config config;
132142
} tracemalloc;
133143
struct _dtoa_runtime_state dtoa;
144+
struct _fileutils_state fileutils;
134145

135146
PyPreConfig preconfig;
136147

@@ -140,7 +151,10 @@ typedef struct pyruntimestate {
140151
void *open_code_userdata;
141152
_Py_AuditHookEntry *audit_hook_head;
142153

143-
struct _Py_unicode_runtime_ids unicode_ids;
154+
struct _Py_float_runtime_state float_state;
155+
struct _Py_unicode_runtime_state unicode_state;
156+
struct _Py_dict_runtime_state dict_state;
157+
struct _py_func_runtime_state func_state;
144158

145159
struct {
146160
/* Used to set PyTypeObject.tp_version_tag */

0 commit comments

Comments
 (0)