Skip to content

Commit 0905437

Browse files
committed
Issue #19512: add some common identifiers to only create common strings once,
instead of creating temporary Unicode string objects Add also more identifiers in pythonrun.c to avoid temporary Unicode string objets for the interactive interpreter.
1 parent bb52020 commit 0905437

File tree

13 files changed

+72
-49
lines changed

13 files changed

+72
-49
lines changed

Include/object.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,17 @@ typedef struct _Py_Identifier {
143143
PyObject *object;
144144
} _Py_Identifier;
145145

146-
#define _Py_static_string(varname, value) static _Py_Identifier varname = { 0, value, 0 }
146+
#define _Py_static_string_init(value) { 0, value, 0 }
147+
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
147148
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
148149

150+
/* Common identifiers */
151+
PyAPI_DATA(_Py_Identifier) _PyId_path;
152+
PyAPI_DATA(_Py_Identifier) _PyId_argv;
153+
PyAPI_DATA(_Py_Identifier) _PyId_stdin;
154+
PyAPI_DATA(_Py_Identifier) _PyId_stdout;
155+
PyAPI_DATA(_Py_Identifier) _PyId_stderr;
156+
149157
/*
150158
Type objects contain a string containing the type name (to help somewhat
151159
in debugging), the allocation parameters (see PyObject_New() and
@@ -829,7 +837,7 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
829837
PyObject *_py_xincref_tmp = (PyObject *)(op); \
830838
if (_py_xincref_tmp != NULL) \
831839
Py_INCREF(_py_xincref_tmp); \
832-
} while (0)
840+
} while (0)
833841

834842
#define Py_XDECREF(op) \
835843
do { \

Modules/_ctypes/callbacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void
8080
PrintError(char *msg, ...)
8181
{
8282
char buf[512];
83-
PyObject *f = PySys_GetObject("stderr");
83+
PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
8484
va_list marker;
8585

8686
va_start(marker, msg);

Modules/_cursesmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,7 @@ PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
25782578
if (fd == -1) {
25792579
PyObject* sys_stdout;
25802580

2581-
sys_stdout = PySys_GetObject("stdout");
2581+
sys_stdout = _PySys_GetObjectId(&_PyId_stdout);
25822582

25832583
if (sys_stdout == NULL || sys_stdout == Py_None) {
25842584
PyErr_SetString(

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ t_bootstrap(void *boot_raw)
10051005
PySys_WriteStderr(
10061006
"Unhandled exception in thread started by ");
10071007
PyErr_Fetch(&exc, &value, &tb);
1008-
file = PySys_GetObject("stderr");
1008+
file = _PySys_GetObjectId(&_PyId_stderr);
10091009
if (file != NULL && file != Py_None)
10101010
PyFile_WriteObject(boot->func, file, 0);
10111011
else

Modules/faulthandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ faulthandler_get_fileno(PyObject *file, int *p_fd)
136136
int fd;
137137

138138
if (file == NULL || file == Py_None) {
139-
file = PySys_GetObject("stderr");
139+
file = _PySys_GetObjectId(&_PyId_stderr);
140140
if (file == NULL) {
141141
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
142142
return NULL;

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ RunMainFromImporter(wchar_t *filename)
261261

262262
/* argv0 is usable as an import source, so put it in sys.path[0]
263263
and import __main__ */
264-
sys_path = PySys_GetObject("path");
264+
sys_path = _PySys_GetObjectId(&_PyId_path);
265265
if (sys_path == NULL) {
266266
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
267267
goto error;

Modules/syslogmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ syslog_get_argv(void)
7171
Py_ssize_t argv_len, scriptlen;
7272
PyObject *scriptobj;
7373
Py_ssize_t slash;
74-
PyObject *argv = PySys_GetObject("argv");
74+
PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
7575

7676
if (argv == NULL) {
7777
return(NULL);

Python/_warnings.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
265265
if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
266266
goto error;
267267

268-
f_stderr = PySys_GetObject("stderr");
268+
f_stderr = _PySys_GetObjectId(&_PyId_stderr);
269269
if (f_stderr == NULL) {
270270
fprintf(stderr, "lost sys.stderr\n");
271271
goto error;
@@ -562,7 +562,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
562562
else {
563563
*filename = NULL;
564564
if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
565-
PyObject *argv = PySys_GetObject("argv");
565+
PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
566566
/* PyList_Check() is needed because sys.argv is set to None during
567567
Python finalization */
568568
if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {

Python/bltinmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
15531553
kwlist, &sep, &end, &file, &flush))
15541554
return NULL;
15551555
if (file == NULL || file == Py_None) {
1556-
file = PySys_GetObject("stdout");
1556+
file = _PySys_GetObjectId(&_PyId_stdout);
15571557
if (file == NULL) {
15581558
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
15591559
return NULL;
@@ -1638,9 +1638,9 @@ static PyObject *
16381638
builtin_input(PyObject *self, PyObject *args)
16391639
{
16401640
PyObject *promptarg = NULL;
1641-
PyObject *fin = PySys_GetObject("stdin");
1642-
PyObject *fout = PySys_GetObject("stdout");
1643-
PyObject *ferr = PySys_GetObject("stderr");
1641+
PyObject *fin = _PySys_GetObjectId(&_PyId_stdin);
1642+
PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
1643+
PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
16441644
PyObject *tmp;
16451645
long fd;
16461646
int tty;

Python/errors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ PyErr_WriteUnraisable(PyObject *obj)
844844

845845
PyErr_Fetch(&t, &v, &tb);
846846

847-
f = PySys_GetObject("stderr");
847+
f = _PySys_GetObjectId(&_PyId_stderr);
848848
if (f == NULL || f == Py_None)
849849
goto done;
850850

0 commit comments

Comments
 (0)