summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-02-08 20:39:07 (GMT)
committerGitHub <noreply@github.com>2022-02-08 20:39:07 (GMT)
commit81c72044a181dbbfbf689d7a977d0d99090f26a8 (patch)
tree14329746bd6f179cf2ae7c9818e1ae881eb46360 /Python/pylifecycle.c
parentc018d3037b5b62e6d48d5985d1a37b91762fbffb (diff)
downloadcpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.zip
cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.gz
cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.bz2
bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized global objects. (gh-30928)
We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c68
1 files changed, 25 insertions, 43 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index ba7bcd2..4a3a1ab 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -21,9 +21,9 @@
#include "pycore_pylifecycle.h" // _PyErr_Print()
#include "pycore_pymem.h" // _PyObject_DebugMallocStats()
#include "pycore_pystate.h" // _PyThreadState_GET()
+#include "pycore_runtime.h" // _Py_ID()
#include "pycore_runtime_init.h" // _PyRuntimeState_INIT
#include "pycore_sliceobject.h" // _PySlice_Fini()
-#include "pycore_structseq.h" // _PyStructSequence_InitState()
#include "pycore_symtable.h" // _PySymtable_Fini()
#include "pycore_sysmodule.h" // _PySys_ClearAuditHooks()
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
@@ -64,13 +64,6 @@ extern void _PyIO_Fini(void);
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
-_Py_IDENTIFIER(flush);
-_Py_IDENTIFIER(name);
-_Py_IDENTIFIER(stdin);
-_Py_IDENTIFIER(stdout);
-_Py_IDENTIFIER(stderr);
-_Py_IDENTIFIER(threading);
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -704,11 +697,6 @@ pycore_init_types(PyInterpreterState *interp)
{
PyStatus status;
- status = _PyStructSequence_InitState(interp);
- if (_PyStatus_EXCEPTION(status)) {
- return status;
- }
-
status = _PyTypes_InitState(interp);
if (_PyStatus_EXCEPTION(status)) {
return status;
@@ -1450,8 +1438,7 @@ finalize_clear_modules_dict(PyObject *modules)
PyDict_Clear(modules);
}
else {
- _Py_IDENTIFIER(clear);
- if (_PyObject_CallMethodIdNoArgs(modules, &PyId_clear) == NULL) {
+ if (PyObject_CallMethodNoArgs(modules, &_Py_ID(clear)) == NULL) {
PyErr_WriteUnraisable(NULL);
}
}
@@ -1622,13 +1609,14 @@ file_is_closed(PyObject *fobj)
static int
flush_std_files(void)
{
- PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
- PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
+ PyThreadState *tstate = _PyThreadState_GET();
+ PyObject *fout = _PySys_GetAttr(tstate, &_Py_ID(stdout));
+ PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
PyObject *tmp;
int status = 0;
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
- tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
+ tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
if (tmp == NULL) {
PyErr_WriteUnraisable(fout);
status = -1;
@@ -1638,7 +1626,7 @@ flush_std_files(void)
}
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
- tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
+ tmp = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
if (tmp == NULL) {
PyErr_Clear();
status = -1;
@@ -2227,10 +2215,6 @@ create_stdio(const PyConfig *config, PyObject* io,
const char* newline;
PyObject *line_buffering, *write_through;
int buffering, isatty;
- _Py_IDENTIFIER(open);
- _Py_IDENTIFIER(isatty);
- _Py_IDENTIFIER(TextIOWrapper);
- _Py_IDENTIFIER(mode);
const int buffered_stdio = config->buffered_stdio;
if (!is_valid_fd(fd))
@@ -2249,16 +2233,15 @@ create_stdio(const PyConfig *config, PyObject* io,
mode = "wb";
else
mode = "rb";
- buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOO",
- fd, mode, buffering,
- Py_None, Py_None, /* encoding, errors */
- Py_None, Py_False); /* newline, closefd */
+ buf = _PyObject_CallMethod(io, &_Py_ID(open), "isiOOOO",
+ fd, mode, buffering,
+ Py_None, Py_None, /* encoding, errors */
+ Py_None, Py_False); /* newline, closefd */
if (buf == NULL)
goto error;
if (buffering) {
- _Py_IDENTIFIER(raw);
- raw = _PyObject_GetAttrId(buf, &PyId_raw);
+ raw = PyObject_GetAttr(buf, &_Py_ID(raw));
if (raw == NULL)
goto error;
}
@@ -2274,9 +2257,9 @@ create_stdio(const PyConfig *config, PyObject* io,
#endif
text = PyUnicode_FromString(name);
- if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
+ if (text == NULL || PyObject_SetAttr(raw, &_Py_ID(name), text) < 0)
goto error;
- res = _PyObject_CallMethodIdNoArgs(raw, &PyId_isatty);
+ res = PyObject_CallMethodNoArgs(raw, &_Py_ID(isatty));
if (res == NULL)
goto error;
isatty = PyObject_IsTrue(res);
@@ -2319,9 +2302,9 @@ create_stdio(const PyConfig *config, PyObject* io,
goto error;
}
- stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
- buf, encoding_str, errors_str,
- newline, line_buffering, write_through);
+ stream = _PyObject_CallMethod(io, &_Py_ID(TextIOWrapper), "OOOsOO",
+ buf, encoding_str, errors_str,
+ newline, line_buffering, write_through);
Py_CLEAR(buf);
Py_CLEAR(encoding_str);
Py_CLEAR(errors_str);
@@ -2333,7 +2316,7 @@ create_stdio(const PyConfig *config, PyObject* io,
else
mode = "r";
text = PyUnicode_FromString(mode);
- if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
+ if (!text || PyObject_SetAttr(stream, &_Py_ID(mode), text) < 0)
goto error;
Py_CLEAR(text);
return stream;
@@ -2432,7 +2415,7 @@ init_sys_streams(PyThreadState *tstate)
if (std == NULL)
goto error;
PySys_SetObject("__stdin__", std);
- _PySys_SetObjectId(&PyId_stdin, std);
+ _PySys_SetAttr(&_Py_ID(stdin), std);
Py_DECREF(std);
/* Set sys.stdout */
@@ -2443,7 +2426,7 @@ init_sys_streams(PyThreadState *tstate)
if (std == NULL)
goto error;
PySys_SetObject("__stdout__", std);
- _PySys_SetObjectId(&PyId_stdout, std);
+ _PySys_SetAttr(&_Py_ID(stdout), std);
Py_DECREF(std);
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
@@ -2472,7 +2455,7 @@ init_sys_streams(PyThreadState *tstate)
Py_DECREF(std);
goto error;
}
- if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
+ if (_PySys_SetAttr(&_Py_ID(stderr), std) < 0) {
Py_DECREF(std);
goto error;
}
@@ -2522,7 +2505,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
return 0;
}
- ferr = _PySys_GetObjectId(&PyId_stderr);
+ ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
if (ferr == NULL || ferr == Py_None) {
/* sys.stderr is not set yet or set to None,
no need to try to display the exception */
@@ -2547,7 +2530,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
Py_XDECREF(tb);
/* sys.stderr may be buffered: call sys.stderr.flush() */
- res = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
+ res = PyObject_CallMethodNoArgs(ferr, &_Py_ID(flush));
if (res == NULL) {
_PyErr_Clear(tstate);
}
@@ -2899,9 +2882,8 @@ Py_ExitStatusException(PyStatus status)
static void
wait_for_thread_shutdown(PyThreadState *tstate)
{
- _Py_IDENTIFIER(_shutdown);
PyObject *result;
- PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
+ PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
if (threading == NULL) {
if (_PyErr_Occurred(tstate)) {
PyErr_WriteUnraisable(NULL);
@@ -2909,7 +2891,7 @@ wait_for_thread_shutdown(PyThreadState *tstate)
/* else: threading not imported */
return;
}
- result = _PyObject_CallMethodIdNoArgs(threading, &PyId__shutdown);
+ result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
if (result == NULL) {
PyErr_WriteUnraisable(threading);
}