summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-30 14:45:12 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-30 14:45:12 (GMT)
commit5127ed727c014e320f3f449f47d31724679f9529 (patch)
treed0d1a8574691f746f2e5f1fc0a15156abc5cfd1d /Python/pythonrun.c
parent1e95340bc378d883609d41da7176e24d1f3e9025 (diff)
downloadcpython-5127ed727c014e320f3f449f47d31724679f9529.zip
cpython-5127ed727c014e320f3f449f47d31724679f9529.tar.gz
cpython-5127ed727c014e320f3f449f47d31724679f9529.tar.bz2
Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),
PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains() to check for and handle errors correctly.
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c43
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);