Ticket #5391: child_error.py

File child_error.py, 1.3 KB (added by philmayers, 8 years ago)

Test case showing powershell child processes failing

Line 
1import os
2import os.path
3import sys
4import tempfile
5import time
6
7from twisted.internet import utils, reactor, defer, protocol
8from twisted.python import log, util
9
10def success(result):
11    print "success"
12    print result
13    reactor.callLater(0, reactor.stop)
14
15def failure(error):
16    print "failure"
17    print error
18    reactor.callLater(0, reactor.stop)
19
20def timeout():
21    print "timeout"
22    reactor.callLater(0, reactor.stop)
23
24def search_path(exe):
25    if '\\' in exe:
26        return exe
27    for path in os.environ['PATH'].split(os.pathsep):
28        fullname = os.path.join(path, exe)
29        if os.path.exists(fullname):
30            return fullname
31    raise Exception('not found')   
32       
33def work():
34    d = utils.getProcessOutputAndValue(
35        search_path('powershell.exe'),
36        ('-Command', 'echo hello'),
37        )
38    d.addCallbacks(success, failure)
39    reactor.callLater(10, timeout)
40
41def main():
42    log.startLogging(sys.stderr)
43    reactor.callWhenRunning(work)
44    reactor.run()
45
46def monkeypatch():
47    # Make the Twisted code close stdin when process is connected
48    utils._EverythingGetter.connectionMade = lambda x: x.transport.closeStdin()
49
50if __name__=='__main__':
51    args = sys.argv[1:]
52    if args and args[0]=='--fixed':
53        monkeypatch()
54    main()