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.
This commit is contained in:
Victor Stinner 2013-11-06 22:41:44 +01:00
parent bb52020d44
commit 090543736f
13 changed files with 72 additions and 49 deletions

View 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 { \

View 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);

View 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(

View 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

View 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;

View 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;

View 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);

View 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) {

View 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;

View 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;

View File

@ -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)

View 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);
}

View 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);