This is on windows with python 3.6. The goal is to have all stdout from python and c extensions logged to the same file. Because this is a windows application with no console sys.stdout.fileno() is -2 so the normal method of file descriptor duplication doesn't work. It could work in py2 by passing down sys.stdout from python, and in C using PyFile_AsFile() and freopen() but in python 3 PyFile_AsFile doesn't exist. Is there a way this can be achieved in this situation?
Asked
Active
Viewed 192 times
0
-
Does this answer your question? [How To catch python stdout in c++ code](https://stackoverflow.com/questions/4307187/how-to-catch-python-stdout-in-c-code) – DavidW Apr 06 '20 at 18:54
-
I suspect [the second answer](https://stackoverflow.com/a/8335297/4657412) is more helpful - write a custom object in C/C++/whatever that has a `write` method, then adapt that to direct to your file. See also https://stackoverflow.com/questions/1956407/how-to-redirect-stderr-in-python-via-python-c-api for a related question – DavidW Apr 06 '20 at 18:57