Python: flush stdout and stderr after running Python script

When debugging with gdb in vscode, the stuff I print when executing a script in
the text editor does not show up in the terminal. It does work when I flush
explicitly though using `print(..., flush=True)`. This is quite annoying.

The solution is to always flush `stdout` and `stderr` automatically when running
a script. This is done using the CPython API, as just using
`fflush(stdout/stderr)` did not solve the issue.

Pull Request: https://projects.blender.org/blender/blender/pulls/136632
This commit is contained in:
Jacques Lucke
2025-04-01 12:41:38 +02:00
parent e6b89924a0
commit 46fc5cb2cf
3 changed files with 49 additions and 0 deletions

View File

@@ -217,6 +217,10 @@ static bool python_script_exec(
PyC_MainModule_Restore(main_mod);
/* Flush stdout/stderr to ensure the script output is visible.
* Using fflush(stdout) does not solve it. */
PyC_StdFilesFlush();
bpy_context_clear(C, &gilstate);
return (py_result != nullptr);