summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2013-11-06 21:41:44 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2013-11-06 21:41:44 (GMT)
commit090543736fa86c676252de7a44e6a7c6f347164e (patch)
treeca82d3bf4857aa474ba91d81c64cc946a22d31a0 /Python
parentbb52020d44b6f3f1ebd16ec6ce38f1f8a6330728 (diff)
downloadcpython-090543736fa86c676252de7a44e6a7c6f347164e.zip
cpython-090543736fa86c676252de7a44e6a7c6f347164e.tar.gz
cpython-090543736fa86c676252de7a44e6a7c6f347164e.tar.bz2
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/_warnings.c4
-rw-r--r--Python/bltinmodule.c8
-rw-r--r--Python/errors.c2
-rw-r--r--Python/pythonrun.c59
-rw-r--r--Python/sysmodule.c22
-rw-r--r--Python/traceback.c2
6 files changed, 56 insertions, 41 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index e88f646..de60128 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -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) {
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 6069e41..6eb0ee7 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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;
diff --git a/Python/errors.c b/Python/errors.c
index 1832b5b..b92911a 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -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;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 61d0352..e510e6f 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -35,6 +35,21 @@
#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)
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 32136e8..de33d55 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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);
}
diff --git a/Python/traceback.c b/Python/traceback.c
index 01e9473..b0bed5f 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -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);