This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make the function flush_io accessible in the C-API
Type: enhancement Stage: resolved
Components: C API Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jean-Didier, petr.viktorin, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-11-17 11:52 by Jean-Didier, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg356801 - (view) Author: Jean-Didier (Jean-Didier) Date: 2019-11-17 11:52
Hello, 
when using an embedded python interpreter in a C++ program, which itself uses MPI, the embedded script's error messages are not flushed properly. 
(see the whole discussion in this StackOverflow : https://stackoverflow.com/questions/29352485/python-print-not-working-when-embedded-into-mpi-program/49076389#49076389).

The current preferred solution involves adding a few calls to flush_io in the C++. 
However, flush_io is a `static` function in `pythonrun.c`, and is not visible in the C-API.

Would it be possible to remove its static attribute, and add a reference to it in pythonrun.h?
Or maybe there is another method to flush IO from the C-API?
msg356803 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-11-17 12:20
PyObject_CallMethod(file, "flush", NULL)
msg396341 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2021-06-22 14:22
To follow up on the StackOverflow discussion: A call to PyErr_Print should invoke sys.excepthook. Unless sys.excepthook was changed, this should print the message *and* flush standard output.
If it doesn't, I recommend investigating why.

If that does not work, you can flush stderr with two function calls:

    sys_stderr = PySys_GetObject("stderr");
    PyObject_CallMethodNoArgs(sys_stderr, "flush");

plus the necessary error handling if any returns NULL, of course.

And/or the same thing with stdout. The flush_io function does exactly this (for both stdout and stderr).
I don't think this is general enough to expose as an API -- IMO it's better to be explicit and call the flush method as above.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 83010
2021-06-22 14:22:34petr.viktorinsetstatus: open -> closed

nosy: + petr.viktorin
messages: + msg396341

resolution: not a bug
stage: resolved
2019-12-09 16:12:37vstinnersetcomponents: + C API, - IO
2019-11-17 12:20:40serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg356803
2019-11-17 11:52:05Jean-Didiercreate