diff --git a/src/libyt_python_shell.cpp b/src/libyt_python_shell.cpp index d3ed426b..d5e03e74 100644 --- a/src/libyt_python_shell.cpp +++ b/src/libyt_python_shell.cpp @@ -469,8 +469,12 @@ CodeValidity LibytPythonShell::check_code_validity(const std::string& code, bool CodeValidity code_validity; - PyRun_SimpleString("import sys, io\n"); - PyRun_SimpleString("sys.OUTPUT_STDERR=''\nstderr_buf=io.StringIO()\nsys.stderr=stderr_buf\n"); + // clear error buffer before redirecting stderr + PyErr_Clear(); + PyRun_SimpleString("import sys, io"); + PyRun_SimpleString("sys.OUTPUT_STDERR=''"); + PyRun_SimpleString("stderr_buf=io.StringIO()"); + PyRun_SimpleString("sys.stderr=stderr_buf"); PyObject* py_test_compile; if (prompt_env) {