Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c193fa

Browse files
committed
Solve issue 1400 at least in part -- whenever we run Python code, at the end
we also flush stderr and stdout. (XXX this may override errors if there's a problem flushing.)
1 parent 48dc27c commit 6c193fa

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Python/pythonrun.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,28 @@ PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
14421442
return ret;
14431443
}
14441444

1445+
static void
1446+
flush_io(void)
1447+
{
1448+
PyObject *f, *r;
1449+
f = PySys_GetObject("stderr");
1450+
if (f != NULL) {
1451+
r = PyObject_CallMethod(f, "flush", "");
1452+
if (r)
1453+
Py_DECREF(r);
1454+
else
1455+
PyErr_Clear();
1456+
}
1457+
f = PySys_GetObject("stdout");
1458+
if (f != NULL) {
1459+
r = PyObject_CallMethod(f, "flush", "");
1460+
if (r)
1461+
Py_DECREF(r);
1462+
else
1463+
PyErr_Clear();
1464+
}
1465+
}
1466+
14451467
static PyObject *
14461468
run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
14471469
PyCompilerFlags *flags, PyArena *arena)
@@ -1453,6 +1475,7 @@ run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
14531475
return NULL;
14541476
v = PyEval_EvalCode(co, globals, locals);
14551477
Py_DECREF(co);
1478+
flush_io();
14561479
return v;
14571480
}
14581481

@@ -1485,6 +1508,7 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
14851508
if (v && flags)
14861509
flags->cf_flags |= (co->co_flags & PyCF_MASK);
14871510
Py_DECREF(co);
1511+
flush_io();
14881512
return v;
14891513
}
14901514

0 commit comments

Comments
 (0)