]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #19512: add some common identifiers to only create common strings once,
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 6 Nov 2013 21:41:44 +0000 (22:41 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Wed, 6 Nov 2013 21:41:44 +0000 (22:41 +0100)
instead of creating temporary Unicode string objects

Add also more identifiers in pythonrun.c to avoid temporary Unicode string
objets for the interactive interpreter.

13 files changed:
Include/object.h
Modules/_ctypes/callbacks.c
Modules/_cursesmodule.c
Modules/_threadmodule.c
Modules/faulthandler.c
Modules/main.c
Modules/syslogmodule.c
Python/_warnings.c
Python/bltinmodule.c
Python/errors.c
Python/pythonrun.c
Python/sysmodule.c
Python/traceback.c

index ef4583834e7374915c0073ba9847f77fe3bf209c..a36c5be52f50065ba0848b23c36ddaf4d97ad195 100644 (file)
@@ -143,9 +143,17 @@ typedef struct _Py_Identifier {
     PyObject *object;
 } _Py_Identifier;
 
-#define _Py_static_string(varname, value)  static _Py_Identifier varname = { 0, value, 0 }
+#define _Py_static_string_init(value) { 0, value, 0 }
+#define _Py_static_string(varname, value)  static _Py_Identifier varname = _Py_static_string_init(value)
 #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
 
+/* Common identifiers */
+PyAPI_DATA(_Py_Identifier) _PyId_path;
+PyAPI_DATA(_Py_Identifier) _PyId_argv;
+PyAPI_DATA(_Py_Identifier) _PyId_stdin;
+PyAPI_DATA(_Py_Identifier) _PyId_stdout;
+PyAPI_DATA(_Py_Identifier) _PyId_stderr;
+
 /*
 Type objects contain a string containing the type name (to help somewhat
 in debugging), the allocation parameters (see PyObject_New() and
@@ -829,7 +837,7 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
         PyObject *_py_xincref_tmp = (PyObject *)(op); \
         if (_py_xincref_tmp != NULL)                  \
             Py_INCREF(_py_xincref_tmp);               \
-    } while (0)                                    
+    } while (0)
 
 #define Py_XDECREF(op)                                \
     do {                                              \
index ec8fd12a8969587d792a7d13dd7b7378119f3675..756f4efbc9dfc961da156b4da74eec975a7a9fc6 100644 (file)
@@ -80,7 +80,7 @@ static void
 PrintError(char *msg, ...)
 {
     char buf[512];
-    PyObject *f = PySys_GetObject("stderr");
+    PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
     va_list marker;
 
     va_start(marker, msg);
index 1f192dc73c8b0611b6e520a213232d80844f6186..5cdd9260af4ac06d8aa58d9083cce94e33c67a48 100644 (file)
@@ -2578,7 +2578,7 @@ PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
     if (fd == -1) {
         PyObject* sys_stdout;
 
-        sys_stdout = PySys_GetObject("stdout");
+        sys_stdout = _PySys_GetObjectId(&_PyId_stdout);
 
         if (sys_stdout == NULL || sys_stdout == Py_None) {
             PyErr_SetString(
index 32656c9ac588b46706bdda4eadbe22cf145cc960..a7123b7ee5669cad38314e148928418f722227d9 100644 (file)
@@ -1005,7 +1005,7 @@ t_bootstrap(void *boot_raw)
             PySys_WriteStderr(
                 "Unhandled exception in thread started by ");
             PyErr_Fetch(&exc, &value, &tb);
-            file = PySys_GetObject("stderr");
+            file = _PySys_GetObjectId(&_PyId_stderr);
             if (file != NULL && file != Py_None)
                 PyFile_WriteObject(boot->func, file, 0);
             else
index 47bc9e8d3b3ba9458c2ccc5716e243899558cb81..315b32da38f43aae835db11d3eb9b9876bf0b4bd 100644 (file)
@@ -136,7 +136,7 @@ faulthandler_get_fileno(PyObject *file, int *p_fd)
     int fd;
 
     if (file == NULL || file == Py_None) {
-        file = PySys_GetObject("stderr");
+        file = _PySys_GetObjectId(&_PyId_stderr);
         if (file == NULL) {
             PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
             return NULL;
index 9171070ab5dd2f4ee731981c1b17f91edc5e1edc..4b1a9681b9b55a93d781a3c5c4b98bb470255906 100644 (file)
@@ -261,7 +261,7 @@ RunMainFromImporter(wchar_t *filename)
 
     /* argv0 is usable as an import source, so put it in sys.path[0]
        and import __main__ */
-    sys_path = PySys_GetObject("path");
+    sys_path = _PySys_GetObjectId(&_PyId_path);
     if (sys_path == NULL) {
         PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
         goto error;
index 9d79eec52a38a5dab6e8bc40b59c2e074e7e045e..edd12abb98f8c2efc79721c81312c50b243642f6 100644 (file)
@@ -71,7 +71,7 @@ syslog_get_argv(void)
     Py_ssize_t argv_len, scriptlen;
     PyObject *scriptobj;
     Py_ssize_t slash;
-    PyObject *argv = PySys_GetObject("argv");
+    PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
 
     if (argv == NULL) {
         return(NULL);
index e88f6460895ff1016a810f9f02fe1f629ab85fdd..de601281be4c03ad395adff46efc0b65a8f6dfe1 100644 (file)
@@ -265,7 +265,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
     if (name == NULL)  /* XXX Can an object lack a '__name__' attribute? */
         goto error;
 
-    f_stderr = PySys_GetObject("stderr");
+    f_stderr = _PySys_GetObjectId(&_PyId_stderr);
     if (f_stderr == NULL) {
         fprintf(stderr, "lost sys.stderr\n");
         goto error;
@@ -562,7 +562,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
     else {
         *filename = NULL;
         if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
-            PyObject *argv = PySys_GetObject("argv");
+            PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
             /* PyList_Check() is needed because sys.argv is set to None during
                Python finalization */
             if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
index 6069e41b48538aa7734716ed2dbe967eb0d7d703..6eb0ee7ddff7b0f171b786c09330fad806147a2c 100644 (file)
@@ -1553,7 +1553,7 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
                                      kwlist, &sep, &end, &file, &flush))
         return NULL;
     if (file == NULL || file == Py_None) {
-        file = PySys_GetObject("stdout");
+        file = _PySys_GetObjectId(&_PyId_stdout);
         if (file == NULL) {
             PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
             return NULL;
@@ -1638,9 +1638,9 @@ static PyObject *
 builtin_input(PyObject *self, PyObject *args)
 {
     PyObject *promptarg = NULL;
-    PyObject *fin = PySys_GetObject("stdin");
-    PyObject *fout = PySys_GetObject("stdout");
-    PyObject *ferr = PySys_GetObject("stderr");
+    PyObject *fin = _PySys_GetObjectId(&_PyId_stdin);
+    PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
+    PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
     PyObject *tmp;
     long fd;
     int tty;
index 1832b5b60224c52be43e8af379d51dd0d911b470..b92911a034433bf0a6ccfd19b887d6e1a33ea07a 100644 (file)
@@ -844,7 +844,7 @@ PyErr_WriteUnraisable(PyObject *obj)
 
     PyErr_Fetch(&t, &v, &tb);
 
-    f = PySys_GetObject("stderr");
+    f = _PySys_GetObjectId(&_PyId_stderr);
     if (f == NULL || f == Py_None)
         goto done;
 
index 61d03521e8ed3885465a2e38d9742d6893e52a63..e510e6f78abba98bc91ced8bbcbe7bfd325553bb 100644 (file)
 #define PATH_MAX MAXPATHLEN
 #endif
 
+/* Common identifiers */
+_Py_Identifier _PyId_argv = _Py_static_string_init("argv");
+_Py_Identifier _PyId_path = _Py_static_string_init("path");
+_Py_Identifier _PyId_stdin = _Py_static_string_init("stdin");
+_Py_Identifier _PyId_stdout = _Py_static_string_init("stdout");
+_Py_Identifier _PyId_stderr = _Py_static_string_init("stderr");
+
+/* local identifiers */
+_Py_IDENTIFIER(excepthook);
+_Py_IDENTIFIER(ps1);
+_Py_IDENTIFIER(ps2);
+_Py_IDENTIFIER(last_type);
+_Py_IDENTIFIER(last_value);
+_Py_IDENTIFIER(last_traceback);
+
 #ifdef Py_REF_DEBUG
 static
 void _print_total_refs(void) {
@@ -412,7 +427,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
     pstderr = PyFile_NewStdPrinter(fileno(stderr));
     if (pstderr == NULL)
         Py_FatalError("Py_Initialize: can't set preliminary stderr");
-    PySys_SetObject("stderr", pstderr);
+    _PySys_SetObjectId(&_PyId_stderr, pstderr);
     PySys_SetObject("__stderr__", pstderr);
     Py_DECREF(pstderr);
 
@@ -497,8 +512,8 @@ file_is_closed(PyObject *fobj)
 static void
 flush_std_files(void)
 {
-    PyObject *fout = PySys_GetObject("stdout");
-    PyObject *ferr = PySys_GetObject("stderr");
+    PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
+    PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
     PyObject *tmp;
     _Py_IDENTIFIER(flush);
 
@@ -776,7 +791,7 @@ Py_NewInterpreter(void)
         pstderr = PyFile_NewStdPrinter(fileno(stderr));
         if (pstderr == NULL)
             Py_FatalError("Py_Initialize: can't set preliminary stderr");
-        PySys_SetObject("stderr", pstderr);
+        _PySys_SetObjectId(&_PyId_stderr, pstderr);
         PySys_SetObject("__stderr__", pstderr);
         Py_DECREF(pstderr);
 
@@ -1170,7 +1185,7 @@ initstdio(void)
             goto error;
     } /* if (fd < 0) */
     PySys_SetObject("__stdin__", std);
-    PySys_SetObject("stdin", std);
+    _PySys_SetObjectId(&_PyId_stdin, std);
     Py_DECREF(std);
 
     /* Set sys.stdout */
@@ -1185,7 +1200,7 @@ initstdio(void)
             goto error;
     } /* if (fd < 0) */
     PySys_SetObject("__stdout__", std);
-    PySys_SetObject("stdout", std);
+    _PySys_SetObjectId(&_PyId_stdout, std);
     Py_DECREF(std);
 
 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
@@ -1219,7 +1234,7 @@ initstdio(void)
         Py_DECREF(std);
         goto error;
     }
-    if (PySys_SetObject("stderr", std) < 0) {
+    if (_PySys_SetObjectId(&_PyId_stderr, std) < 0) {
         Py_DECREF(std);
         goto error;
     }
@@ -1281,14 +1296,14 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
         flags = &local_flags;
         local_flags.cf_flags = 0;
     }
-    v = PySys_GetObject("ps1");
+    v = _PySys_GetObjectId(&PyId_ps1);
     if (v == NULL) {
-        PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
+        _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
         Py_XDECREF(v);
     }
-    v = PySys_GetObject("ps2");
+    v = _PySys_GetObjectId(&PyId_ps2);
     if (v == NULL) {
-        PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
+        _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
         Py_XDECREF(v);
     }
     err = -1;
@@ -1351,7 +1366,7 @@ PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
 
     if (fp == stdin) {
         /* Fetch encoding from sys.stdin if possible. */
-        v = PySys_GetObject("stdin");
+        v = _PySys_GetObjectId(&_PyId_stdin);
         if (v && v != Py_None) {
             oenc = _PyObject_GetAttrId(v, &PyId_encoding);
             if (oenc)
@@ -1360,7 +1375,7 @@ PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
                 PyErr_Clear();
         }
     }
-    v = PySys_GetObject("ps1");
+    v = _PySys_GetObjectId(&PyId_ps1);
     if (v != NULL) {
         v = PyObject_Str(v);
         if (v == NULL)
@@ -1373,7 +1388,7 @@ PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
             }
         }
     }
-    w = PySys_GetObject("ps2");
+    w = _PySys_GetObjectId(&PyId_ps2);
     if (w != NULL) {
         w = PyObject_Str(w);
         if (w == NULL)
@@ -1752,7 +1767,7 @@ handle_system_exit(void)
     if (PyLong_Check(value))
         exitcode = (int)PyLong_AsLong(value);
     else {
-        PyObject *sys_stderr = PySys_GetObject("stderr");
+        PyObject *sys_stderr = _PySys_GetObjectId(&_PyId_stderr);
         if (sys_stderr != NULL && sys_stderr != Py_None) {
             PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
         } else {
@@ -1795,11 +1810,11 @@ PyErr_PrintEx(int set_sys_last_vars)
         return;
     /* Now we know v != NULL too */
     if (set_sys_last_vars) {
-        PySys_SetObject("last_type", exception);
-        PySys_SetObject("last_value", v);
-        PySys_SetObject("last_traceback", tb);
+        _PySys_SetObjectId(&PyId_last_type, exception);
+        _PySys_SetObjectId(&PyId_last_value, v);
+        _PySys_SetObjectId(&PyId_last_traceback, tb);
     }
-    hook = PySys_GetObject("excepthook");
+    hook = _PySys_GetObjectId(&PyId_excepthook);
     if (hook) {
         PyObject *args = PyTuple_Pack(3, exception, v, tb);
         PyObject *result = PyEval_CallObject(hook, args);
@@ -2009,7 +2024,7 @@ void
 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
 {
     PyObject *seen;
-    PyObject *f = PySys_GetObject("stderr");
+    PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
     if (PyExceptionInstance_Check(value)
         && tb != NULL && PyTraceBack_Check(tb)) {
         /* Put the traceback on the exception, otherwise it won't get
@@ -2107,7 +2122,7 @@ flush_io(void)
     /* Save the current exception */
     PyErr_Fetch(&type, &value, &traceback);
 
-    f = PySys_GetObject("stderr");
+    f = _PySys_GetObjectId(&_PyId_stderr);
     if (f != NULL) {
         r = _PyObject_CallMethodId(f, &PyId_flush, "");
         if (r)
@@ -2115,7 +2130,7 @@ flush_io(void)
         else
             PyErr_Clear();
     }
-    f = PySys_GetObject("stdout");
+    f = _PySys_GetObjectId(&_PyId_stdout);
     if (f != NULL) {
         r = _PyObject_CallMethodId(f, &PyId_flush, "");
         if (r)
index 32136e844f412eb437cf343d917c634979186e97..de33d558358a147a603db88d0ca31465f88a2138 100644 (file)
@@ -183,7 +183,7 @@ sys_displayhook(PyObject *self, PyObject *o)
     }
     if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0)
         return NULL;
-    outf = PySys_GetObject("stdout");
+    outf = _PySys_GetObjectId(&_PyId_stdout);
     if (outf == NULL || outf == Py_None) {
         PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
         return NULL;
@@ -1825,7 +1825,7 @@ PySys_SetPath(const wchar_t *path)
     PyObject *v;
     if ((v = makepathobject(path, DELIM)) == NULL)
         Py_FatalError("can't create sys.path");
-    if (PySys_SetObject("path", v) != 0)
+    if (_PySys_SetObjectId(&_PyId_path, v) != 0)
         Py_FatalError("can't assign sys.path");
     Py_DECREF(v);
 }
@@ -1894,7 +1894,7 @@ sys_update_path(int argc, wchar_t **argv)
     wchar_t fullpath[MAX_PATH];
 #endif
 
-    path = PySys_GetObject("path");
+    path = _PySys_GetObjectId(&_PyId_path);
     if (path == NULL)
         return;
 
@@ -2081,7 +2081,7 @@ sys_pyfile_write(const char *text, PyObject *file)
  */
 
 static void
-sys_write(char *name, FILE *fp, const char *format, va_list va)
+sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
 {
     PyObject *file;
     PyObject *error_type, *error_value, *error_traceback;
@@ -2089,7 +2089,7 @@ sys_write(char *name, FILE *fp, const char *format, va_list va)
     int written;
 
     PyErr_Fetch(&error_type, &error_value, &error_traceback);
-    file = PySys_GetObject(name);
+    file = _PySys_GetObjectId(key);
     written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
     if (sys_pyfile_write(buffer, file) != 0) {
         PyErr_Clear();
@@ -2109,7 +2109,7 @@ PySys_WriteStdout(const char *format, ...)
     va_list va;
 
     va_start(va, format);
-    sys_write("stdout", stdout, format, va);
+    sys_write(&_PyId_stdout, stdout, format, va);
     va_end(va);
 }
 
@@ -2119,19 +2119,19 @@ PySys_WriteStderr(const char *format, ...)
     va_list va;
 
     va_start(va, format);
-    sys_write("stderr", stderr, format, va);
+    sys_write(&_PyId_stderr, stderr, format, va);
     va_end(va);
 }
 
 static void
-sys_format(char *name, FILE *fp, const char *format, va_list va)
+sys_format(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
 {
     PyObject *file, *message;
     PyObject *error_type, *error_value, *error_traceback;
     char *utf8;
 
     PyErr_Fetch(&error_type, &error_value, &error_traceback);
-    file = PySys_GetObject(name);
+    file = _PySys_GetObjectId(key);
     message = PyUnicode_FromFormatV(format, va);
     if (message != NULL) {
         if (sys_pyfile_write_unicode(message, file) != 0) {
@@ -2151,7 +2151,7 @@ PySys_FormatStdout(const char *format, ...)
     va_list va;
 
     va_start(va, format);
-    sys_format("stdout", stdout, format, va);
+    sys_format(&_PyId_stdout, stdout, format, va);
     va_end(va);
 }
 
@@ -2161,6 +2161,6 @@ PySys_FormatStderr(const char *format, ...)
     va_list va;
 
     va_start(va, format);
-    sys_format("stderr", stderr, format, va);
+    sys_format(&_PyId_stderr, stderr, format, va);
     va_end(va);
 }
index 01e947315a89e33b4162cb5b1547ef8c82db5901..b0bed5f7800a66619f7d44036f472dfc8157075c 100644 (file)
@@ -169,7 +169,7 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *
         tail++;
     taillen = strlen(tail);
 
-    syspath = PySys_GetObject("path");
+    syspath = _PySys_GetObjectId(&_PyId_path);
     if (syspath == NULL || !PyList_Check(syspath))
         goto error;
     npath = PyList_Size(syspath);