Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-81057: Move More Globals in Core Code to _PyRuntimeState #99516

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Include/cpython/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,3 @@ PyAPI_FUNC(PyObject * const *) _PyArg_UnpackKeywordsWithVararg(
(minpos), (maxpos), (minkw), (buf)))

PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(PyModuleDef*, int apiver);

PyAPI_DATA(const char *) _Py_PackageContext;
5 changes: 5 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ extern "C" {

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


struct _fileutils_state {
int force_ascii;
};

typedef enum {
_Py_ERROR_UNKNOWN=0,
_Py_ERROR_STRICT,
Expand Down
12 changes: 12 additions & 0 deletions Include/internal/pycore_floatobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ extern void _PyFloat_FiniType(PyInterpreterState *);

/* other API */

enum _py_float_format_type {
_py_float_format_unknown,
_py_float_format_ieee_big_endian,
_py_float_format_ieee_little_endian,
};

struct _Py_float_runtime_state {
enum _py_float_format_type float_format;
enum _py_float_format_type double_format;
};


#ifndef WITH_FREELISTS
// without freelists
# define PyFloat_MAXFREELIST 0
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct _import_runtime_state {
_PyTime_t accumulated;
int header;
} find_and_load;
/* Package context -- the full module name for package imports */
const char * pkgcontext;
};


Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {


struct _pending_calls {
int busy;
PyThread_type_lock lock;
/* Request for running pending calls. */
_Py_atomic_int calls_to_do;
Expand Down
32 changes: 31 additions & 1 deletion Include/internal/pycore_pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@
# error "this header requires Py_BUILD_CORE define"
#endif

uint64_t _Py_KeyedHash(uint64_t, const char *, Py_ssize_t);

struct pyhash_runtime_state {
struct {
#ifndef MS_WINDOWS
int fd;
dev_t st_dev;
ino_t st_ino;
#else
// This is a placeholder so the struct isn't empty on Windows.
int _not_used;
#endif
} urandom_cache;
};

#ifndef MS_WINDOWS
# define _py_urandom_cache_INIT \
{ \
.fd = -1, \
}
#else
# define _py_urandom_cache_INIT {0}
#endif

#define pyhash_state_INIT \
{ \
.urandom_cache = _py_urandom_cache_INIT, \
}


uint64_t _Py_KeyedHash(uint64_t, const char *, Py_ssize_t);


#endif // Py_INTERNAL_HASH_H
4 changes: 0 additions & 4 deletions Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ extern "C" {
struct _PyArgv;
struct pyruntimestate;

/* True if the main interpreter thread exited due to an unhandled
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
PyAPI_DATA(int) _Py_UnhandledKeyboardInterrupt;

extern int _Py_SetFileSystemEncoding(
const char *encoding,
const char *errors);
Expand Down
12 changes: 11 additions & 1 deletion Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ extern "C" {

#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_dtoa.h" // struct _dtoa_runtime_state
#include "pycore_floatobject.h" // struct _Py_float_runtime_state
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
#include "pycore_import.h" // struct _import_runtime_state
#include "pycore_interp.h" // PyInterpreterState
#include "pycore_pymem.h" // struct _pymem_allocators
#include "pycore_pyhash.h" // struct pyhash_runtime_state
#include "pycore_obmalloc.h" // struct obmalloc_state
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids

Expand Down Expand Up @@ -92,6 +94,12 @@ typedef struct pyruntimestate {

struct _pymem_allocators allocators;
struct _obmalloc_state obmalloc;
struct pyhash_runtime_state pyhash_state;
struct {
/* True if the main interpreter thread exited due to an unhandled
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
int unhandled_keyboard_interrupt;
} signals;

struct pyinterpreters {
PyThread_type_lock mutex;
Expand Down Expand Up @@ -131,6 +139,7 @@ typedef struct pyruntimestate {
struct _PyTraceMalloc_Config config;
} tracemalloc;
struct _dtoa_runtime_state dtoa;
struct _fileutils_state fileutils;

PyPreConfig preconfig;

Expand All @@ -140,7 +149,8 @@ typedef struct pyruntimestate {
void *open_code_userdata;
_Py_AuditHookEntry *audit_hook_head;

struct _Py_unicode_runtime_ids unicode_ids;
struct _Py_float_runtime_state float_state;
struct _Py_unicode_runtime_state unicode_state;

struct {
/* Used to set PyTypeObject.tp_version_tag */
Expand Down
34 changes: 21 additions & 13 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,18 @@ extern "C" {

#define _PyRuntimeState_INIT(runtime) \
{ \
.gilstate = { \
.check_enabled = 1, \
/* A TSS key must be initialized with Py_tss_NEEDS_INIT \
in accordance with the specification. */ \
.autoTSSkey = Py_tss_NEEDS_INIT, \
}, \
.allocators = { \
_pymem_allocators_standard_INIT(runtime), \
_pymem_allocators_debug_INIT, \
_pymem_allocators_obj_arena_INIT, \
}, \
.obmalloc = _obmalloc_state_INIT(runtime.obmalloc), \
.pyhash_state = pyhash_state_INIT, \
.interpreters = { \
/* This prevents interpreters from getting created \
until _PyInterpreterState_Enable() is called. */ \
.next_id = -1, \
}, \
.tracemalloc = { \
.config = _PyTraceMalloc_Config_INIT, \
}, \
.dtoa = _dtoa_runtime_state_INIT(runtime), \
.types = { \
.next_version_tag = 1, \
}, \
.imports = { \
.lock = { \
.mutex = NULL, \
Expand All @@ -53,6 +41,26 @@ extern "C" {
.header = 1, \
}, \
}, \
.gilstate = { \
.check_enabled = 1, \
/* A TSS key must be initialized with Py_tss_NEEDS_INIT \
in accordance with the specification. */ \
.autoTSSkey = Py_tss_NEEDS_INIT, \
}, \
.tracemalloc = { \
.config = _PyTraceMalloc_Config_INIT, \
}, \
.dtoa = _dtoa_runtime_state_INIT(runtime), \
.fileutils = { \
.force_ascii = -1, \
}, \
.float_state = { \
.float_format = _py_float_format_unknown, \
.double_format = _py_float_format_unknown, \
}, \
.types = { \
.next_version_tag = 1, \
}, \
.global_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct _Py_unicode_runtime_ids {
Py_ssize_t next_index;
};

struct _Py_unicode_runtime_state {
struct _Py_unicode_runtime_ids ids;
};

/* fs_codec.encoding is initialized to NULL.
Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
struct _Py_unicode_fs_codec {
Expand Down
6 changes: 3 additions & 3 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
Py_DECREF(module);
return pymain_exit_err_print();
}
_Py_UnhandledKeyboardInterrupt = 0;
_PyRuntime.signals.unhandled_keyboard_interrupt = 0;
result = PyObject_Call(runmodule, runargs, NULL);
if (!result && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
_Py_UnhandledKeyboardInterrupt = 1;
_PyRuntime.signals.unhandled_keyboard_interrupt = 1;
}
Py_DECREF(runpy);
Py_DECREF(runmodule);
Expand Down Expand Up @@ -696,7 +696,7 @@ Py_RunMain(void)

pymain_free();

if (_Py_UnhandledKeyboardInterrupt) {
if (_PyRuntime.signals.unhandled_keyboard_interrupt) {
exitcode = exit_sigint();
}

Expand Down
27 changes: 17 additions & 10 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1723,12 +1723,14 @@ float___getnewargs___impl(PyObject *self)
}

/* this is for the benefit of the pack/unpack routines below */
typedef enum _py_float_format_type float_format_type;
#define unknown_format _py_float_format_unknown
#define ieee_big_endian_format _py_float_format_ieee_big_endian
#define ieee_little_endian_format _py_float_format_ieee_little_endian

typedef enum {
unknown_format, ieee_big_endian_format, ieee_little_endian_format
} float_format_type;
#define float_format (_PyRuntime.float_state.float_format)
#define double_format (_PyRuntime.float_state.double_format)

static float_format_type double_format, float_format;

/*[clinic input]
@classmethod
Expand Down Expand Up @@ -1929,13 +1931,9 @@ PyTypeObject PyFloat_Type = {
.tp_vectorcall = (vectorcallfunc)float_vectorcall,
};

void
_PyFloat_InitState(PyInterpreterState *interp)
static void
_init_global_state(void)
{
if (!_Py_IsMainInterpreter(interp)) {
return;
}

float_format_type detected_double_format, detected_float_format;

/* We attempt to determine if this machine is using IEEE
Expand Down Expand Up @@ -1985,6 +1983,15 @@ _PyFloat_InitState(PyInterpreterState *interp)
float_format = detected_float_format;
}

void
_PyFloat_InitState(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return;
}
_init_global_state();
}

PyStatus
_PyFloat_InitTypes(PyInterpreterState *interp)
{
Expand Down
2 changes: 2 additions & 0 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,15 @@ _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
_Py_PackageContext, and PyModule_Create*() will substitute this
(if the name actually matches).
*/
#define _Py_PackageContext (_PyRuntime.imports.pkgcontext)
if (_Py_PackageContext != NULL) {
const char *p = strrchr(_Py_PackageContext, '.');
if (p != NULL && strcmp(module->m_name, p+1) == 0) {
name = _Py_PackageContext;
_Py_PackageContext = NULL;
}
}
#undef _Py_PackageContext
if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
return NULL;

Expand Down
19 changes: 15 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1856,7 +1856,7 @@ _PyUnicode_FromId(_Py_Identifier *id)

Py_ssize_t index = _Py_atomic_size_get(&id->index);
if (index < 0) {
struct _Py_unicode_runtime_ids *rt_ids = &interp->runtime->unicode_ids;
struct _Py_unicode_runtime_ids *rt_ids = &interp->runtime->unicode_state.ids;

PyThread_acquire_lock(rt_ids->lock, WAIT_LOCK);
// Check again to detect concurrent access. Another thread can have
Expand Down Expand Up @@ -14491,12 +14491,14 @@ PyTypeObject PyUnicode_Type = {

/* Initialize the Unicode implementation */

void
_PyUnicode_InitState(PyInterpreterState *interp)
static void
_init_global_state(void)
{
if (!_Py_IsMainInterpreter(interp)) {
static int initialized = 0;
if (initialized) {
return;
}
initialized = 1;

/* initialize the linebreak bloom filter */
const Py_UCS2 linebreak[] = {
Expand All @@ -14514,6 +14516,15 @@ _PyUnicode_InitState(PyInterpreterState *interp)
Py_ARRAY_LENGTH(linebreak));
}

void
_PyUnicode_InitState(PyInterpreterState *interp)
{
if (!_Py_IsMainInterpreter(interp)) {
return;
}
_init_global_state();
}


PyStatus
_PyUnicode_InitGlobalObjects(PyInterpreterState *interp)
Expand Down
10 changes: 5 additions & 5 deletions Python/bootstrap_hash.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include "pycore_initconfig.h"
#include "pycore_fileutils.h" // _Py_fstat_noraise()
#include "pycore_runtime.h" // _PyRuntime

#ifdef MS_WINDOWS
# include <windows.h>
Expand Down Expand Up @@ -263,11 +264,7 @@ py_getentropy(char *buffer, Py_ssize_t size, int raise)
#endif /* defined(HAVE_GETENTROPY) && !(defined(__sun) && defined(__SVR4)) */


static struct {
int fd;
dev_t st_dev;
ino_t st_ino;
} urandom_cache = { -1 };
#define urandom_cache (_PyRuntime.pyhash_state.urandom_cache)

/* Read random bytes from the /dev/urandom device:

Expand Down Expand Up @@ -402,6 +399,9 @@ dev_urandom_close(void)
urandom_cache.fd = -1;
}
}

#undef urandom_cache

#endif /* !MS_WINDOWS */


Expand Down
Loading