Imported Upstream version 12.1.0
[contrib/python-twisted.git] / twisted / internet / test / process_helper.py
1
2 # A program which exits after starting a child which inherits its
3 # stdin/stdout/stderr and keeps them open until stdin is closed.
4
5 import sys, os
6
7 def grandchild():
8     sys.stdout.write('grandchild started')
9     sys.stdout.flush()
10     sys.stdin.read()
11
12 def main():
13     if sys.argv[1] == 'child':
14         if sys.argv[2] == 'windows':
15             import win32api as api, win32process as proc
16             info = proc.STARTUPINFO()
17             info.hStdInput = api.GetStdHandle(api.STD_INPUT_HANDLE)
18             info.hStdOutput = api.GetStdHandle(api.STD_OUTPUT_HANDLE)
19             info.hStdError = api.GetStdHandle(api.STD_ERROR_HANDLE)
20             python = sys.executable
21             scriptDir = os.path.dirname(__file__)
22             scriptName = os.path.basename(__file__)
23             proc.CreateProcess(
24                 None, " ".join((python, scriptName, "grandchild")), None,
25                 None, 1, 0, os.environ, scriptDir, info)
26         else:
27             if os.fork() == 0:
28                 grandchild()
29     else:
30         grandchild()
31
32 if __name__ == '__main__':
33     main()