How to inherit stdin and stdout in python by using os.execv()

放肆的年华 提交于 2019-11-28 14:04:48

In your python code, you've opened new IO streams and set the sys.stdin and sys.stdout Python names to point to these. They will have their own file descriptors (most likely 3, and 4). However the standard file descriptors, 0, and 1 will remain unchanged, and continue to point at their original locations. These are the descriptors inherited when you exec into stdin and stdout in your C++ program.

You need to make sure that the actual file descriptors are adjusted before execing.

os.dup2(sys.stdin.fileno(), 0)
os.dup2(sys.stdout.fileno(), 1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!