5

Essentially I want to do something similar as described here: redirecting the python output in an application embedding python. The best answer seems to be this one, because it is written only in C / C++ (mixing Python and C / C++ seems not clean to me).

Still I'm not happy with the answers. First of all when looking at https://docs.python.org/3/c-api/file.html, it seems that I should not use PyFile_FromFile(..) anymore, and I'm not sure whether PyFile_FromFd(..) is the Python 3 substitute, looking at the warning in red. Second I only want to redirect the output generated by my calls to Python functions, e.g. PyRun_SimpleString(..), not the whole program output. Essentially my application is compiled as a shared library and when loaded should only redirect it's own output.

An application of library would be a custom Python GUI: A GUI which loads the library, the user can enter a string which will be sent to the library, which gives it to PyRun_SimpleString(..) and returns the output. The GUI will then will then display the output.

dev15
  • 665
  • 3
  • 14
  • Do you want an answer in C or C++ or both? – JeremyP Jan 15 '18 at 14:51
  • C++ would be preferred, for completeness I've added C. Also I'm not sure whether redirecting to a file is the preferred solution, redirecting to a variable might be better (see https://stackoverflow.com/a/4307737/5586720, but the answer is again Python 2 and mixing C / C++ and Python). – dev15 Jan 15 '18 at 15:01
  • An example of an application of the code would be my own Python GUI, the user can enter a string which will be sent to the shared library, which gives it to PyRun_SimpleString(..) and returns the output. I'm currently not interested in intermediate values, so PyRun_SimpleString(..) is sufficient – dev15 Jan 15 '18 at 15:03
  • As a Python-side solution you can still just replace `sys.stdin`, `sys.stdout`, `sys.stderr`. It might be *easier* to open the corresponding files using a piece of *Python code* instead of C++. – Antti Haapala -- Слава Україні Jan 15 '18 at 15:10

0 Answers0