diff options
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r-- | Python/pythonrun.c | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ade5efe..ece709c 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -137,6 +137,20 @@ add_flag(int flag, const char *envs) return flag; } +static int +isatty_no_error(PyObject *sys_stream) +{ + PyObject *sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); + if (sys_isatty) { + int isatty = PyObject_IsTrue(sys_isatty); + Py_DECREF(sys_isatty); + if (isatty >= 0) + return isatty; + } + PyErr_Clear(); + return 0; +} + void Py_InitializeEx(int install_sigs) { @@ -150,7 +164,7 @@ Py_InitializeEx(int install_sigs) char *errors = NULL; int free_codeset = 0; int overridden = 0; - PyObject *sys_stream, *sys_isatty; + PyObject *sys_stream; #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET) char *saved_locale, *loc_codeset; #endif @@ -336,40 +350,25 @@ Py_InitializeEx(int install_sigs) if (codeset) { sys_stream = PySys_GetObject("stdin"); - sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); - if (!sys_isatty) - PyErr_Clear(); - if ((overridden || - (sys_isatty && PyObject_IsTrue(sys_isatty))) && - PyFile_Check(sys_stream)) { + if ((overridden || isatty_no_error(sys_stream)) && + PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors)) Py_FatalError("Cannot set codeset of stdin"); } - Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stdout"); - sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); - if (!sys_isatty) - PyErr_Clear(); - if ((overridden || - (sys_isatty && PyObject_IsTrue(sys_isatty))) && - PyFile_Check(sys_stream)) { + if ((overridden || isatty_no_error(sys_stream)) && + PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) Py_FatalError("Cannot set codeset of stdout"); } - Py_XDECREF(sys_isatty); sys_stream = PySys_GetObject("stderr"); - sys_isatty = PyObject_CallMethod(sys_stream, "isatty", ""); - if (!sys_isatty) - PyErr_Clear(); - if((overridden || - (sys_isatty && PyObject_IsTrue(sys_isatty))) && - PyFile_Check(sys_stream)) { + if ((overridden || isatty_no_error(sys_stream)) && + PyFile_Check(sys_stream)) { if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors)) Py_FatalError("Cannot set codeset of stderr"); } - Py_XDECREF(sys_isatty); if (free_codeset) free(codeset); |