summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-11-15 16:45:11 (GMT)
committerGitHub <noreply@github.com>2022-11-15 16:45:11 (GMT)
commit3c57971a2d3b6d2c6fd1f525ba2108fccb35add2 (patch)
tree697c28707c62e43c9a444af3069304b8fad2e54b /Python
parent73943cbc4c5e97f71b76150c549d07e8ed00066b (diff)
downloadcpython-3c57971a2d3b6d2c6fd1f525ba2108fccb35add2.zip
cpython-3c57971a2d3b6d2c6fd1f525ba2108fccb35add2.tar.gz
cpython-3c57971a2d3b6d2c6fd1f525ba2108fccb35add2.tar.bz2
gh-81057: Move Globals in Core Code to _PyRuntimeState (gh-99496)
This is the first of several changes to consolidate non-object globals in core code. https://github.com/python/cpython/issues/81057
Diffstat (limited to 'Python')
-rw-r--r--Python/dtoa.c35
-rw-r--r--Python/getargs.c11
-rw-r--r--Python/getversion.c17
-rw-r--r--Python/pylifecycle.c2
4 files changed, 35 insertions, 30 deletions
diff --git a/Python/dtoa.c b/Python/dtoa.c
index 733e70b..1b47d83 100644
--- a/Python/dtoa.c
+++ b/Python/dtoa.c
@@ -119,6 +119,7 @@
#include "Python.h"
#include "pycore_dtoa.h" // _PY_SHORT_FLOAT_REPR
+#include "pycore_runtime.h" // _PyRuntime
#include <stdlib.h> // exit()
/* if _PY_SHORT_FLOAT_REPR == 0, then don't even try to compile
@@ -156,7 +157,7 @@
#endif
-typedef uint32_t ULong;
+// ULong is defined in pycore_dtoa.h.
typedef int32_t Long;
typedef uint64_t ULLong;
@@ -171,12 +172,6 @@ typedef uint64_t ULLong;
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
#endif
-#ifndef PRIVATE_MEM
-#define PRIVATE_MEM 2304
-#endif
-#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
-static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -298,8 +293,6 @@ BCinfo {
#define FFFFFFFF 0xffffffffUL
-#define Kmax 7
-
/* struct Bigint is used to represent arbitrary-precision integers. These
integers are stored in sign-magnitude format, with the magnitude stored as
an array of base 2**32 digits. Bigints are always normalized: if x is a
@@ -322,13 +315,7 @@ BCinfo {
significant (x[0]) to most significant (x[wds-1]).
*/
-struct
-Bigint {
- struct Bigint *next;
- int k, maxwds, sign, wds;
- ULong x[1];
-};
-
+// struct Bigint is defined in pycore_dtoa.h.
typedef struct Bigint Bigint;
#ifndef Py_USING_MEMORY_DEBUGGER
@@ -352,7 +339,9 @@ typedef struct Bigint Bigint;
Bfree to PyMem_Free. Investigate whether this has any significant
performance on impact. */
-static Bigint *freelist[Kmax+1];
+#define freelist _PyRuntime.dtoa.freelist
+#define private_mem _PyRuntime.dtoa.preallocated
+#define pmem_next _PyRuntime.dtoa.preallocated_next
/* Allocate space for a Bigint with up to 1<<k digits */
@@ -363,13 +352,15 @@ Balloc(int k)
Bigint *rv;
unsigned int len;
- if (k <= Kmax && (rv = freelist[k]))
+ if (k <= Bigint_Kmax && (rv = freelist[k]))
freelist[k] = rv->next;
else {
x = 1 << k;
len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
/sizeof(double);
- if (k <= Kmax && pmem_next - private_mem + len <= (Py_ssize_t)PRIVATE_mem) {
+ if (k <= Bigint_Kmax &&
+ pmem_next - private_mem + len <= (Py_ssize_t)Bigint_PREALLOC_SIZE
+ ) {
rv = (Bigint*)pmem_next;
pmem_next += len;
}
@@ -391,7 +382,7 @@ static void
Bfree(Bigint *v)
{
if (v) {
- if (v->k > Kmax)
+ if (v->k > Bigint_Kmax)
FREE((void*)v);
else {
v->next = freelist[v->k];
@@ -400,6 +391,10 @@ Bfree(Bigint *v)
}
}
+#undef pmem_next
+#undef private_mem
+#undef freelist
+
#else
/* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
diff --git a/Python/getargs.c b/Python/getargs.c
index 7034622..748209d 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1846,9 +1846,6 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
}
-/* List of static parsers. */
-static struct _PyArg_Parser *static_arg_parsers = NULL;
-
static int
scan_keywords(const char * const *keywords, int *ptotal, int *pposonly)
{
@@ -2024,8 +2021,8 @@ _parser_init(struct _PyArg_Parser *parser)
parser->initialized = owned ? 1 : -1;
assert(parser->next == NULL);
- parser->next = static_arg_parsers;
- static_arg_parsers = parser;
+ parser->next = _PyRuntime.getargs.static_parsers;
+ _PyRuntime.getargs.static_parsers = parser;
return 1;
}
@@ -2930,14 +2927,14 @@ _PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
void
_PyArg_Fini(void)
{
- struct _PyArg_Parser *tmp, *s = static_arg_parsers;
+ struct _PyArg_Parser *tmp, *s = _PyRuntime.getargs.static_parsers;
while (s) {
tmp = s->next;
s->next = NULL;
parser_clear(s);
s = tmp;
}
- static_arg_parsers = NULL;
+ _PyRuntime.getargs.static_parsers = NULL;
}
#ifdef __cplusplus
diff --git a/Python/getversion.c b/Python/getversion.c
index 4691045..5db836a 100644
--- a/Python/getversion.c
+++ b/Python/getversion.c
@@ -5,12 +5,23 @@
#include "patchlevel.h"
-const char *
-Py_GetVersion(void)
+static int initialized = 0;
+static char version[250];
+
+void _Py_InitVersion(void)
{
- static char version[250];
+ if (initialized) {
+ return;
+ }
+ initialized = 1;
PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
+}
+
+const char *
+Py_GetVersion(void)
+{
+ _Py_InitVersion();
return version;
}
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 44f8442..7ca284e 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -600,6 +600,8 @@ pycore_init_runtime(_PyRuntimeState *runtime,
*/
_PyRuntimeState_SetFinalizing(runtime, NULL);
+ _Py_InitVersion();
+
status = _Py_HashRandomization_Init(config);
if (_PyStatus_EXCEPTION(status)) {
return status;