1 # borrowed from python 2.5.2c1
2 # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
3 # Licensed to PSF under a Contributor Agreement.
6 mswindows
= (sys
.platform
== "win32")
13 class CalledProcessError(Exception):
14 def __init__(self
, returncode
, cmd
):
15 self
.returncode
= returncode
18 return "Command '%s' returned non-zero exit status %d" % (self
.cmd
, self
.returncode
)
25 from win32api
import GetStdHandle
, STD_INPUT_HANDLE
, \
26 STD_OUTPUT_HANDLE
, STD_ERROR_HANDLE
27 from win32api
import GetCurrentProcess
, DuplicateHandle
, \
28 GetModuleFileName
, GetVersion
29 from win32con
import DUPLICATE_SAME_ACCESS
, SW_HIDE
30 from win32pipe
import CreatePipe
31 from win32process
import CreateProcess
, STARTUPINFO
, \
32 GetExitCodeProcess
, STARTF_USESTDHANDLES
, \
33 STARTF_USESHOWWINDOW
, CREATE_NEW_CONSOLE
34 from win32event
import WaitForSingleObject
, INFINITE
, WAIT_OBJECT_0
36 from _subprocess
import *
51 __all__
= ["Popen", "PIPE", "STDOUT", "call", "check_call", "CalledProcessError"]
54 MAXFD
= os
.sysconf("SC_OPEN_MAX")
67 for inst
in _active
[:]:
68 if inst
.poll(_deadstate
=sys
.maxint
) >= 0:
78 def call(*popenargs
, **kwargs
):
79 return Popen(*popenargs
, **kwargs
).wait()
81 def check_call(*popenargs
, **kwargs
):
82 retcode
= call(*popenargs
, **kwargs
)
83 cmd
= kwargs
.get("args")
87 raise CalledProcessError(retcode
, cmd
)
91 def list2cmdline(seq
):
100 needquote
= (" " in arg
) or ("\t" in arg
) or arg
== ""
108 result
.append('\\' * len(bs_buf
)*2)
113 result
.extend(bs_buf
)
118 result
.extend(bs_buf
)
121 result
.extend(bs_buf
)
124 return ''.join(result
)
127 def __init__(self
, args
, bufsize
=0, executable
=None,
128 stdin
=None, stdout
=None, stderr
=None,
129 preexec_fn
=None, close_fds
=False, shell
=False,
130 cwd
=None, env
=None, universal_newlines
=False,
131 startupinfo
=None, creationflags
=0):
134 self
._child
_created
= False
135 if not isinstance(bufsize
, (int, long)):
136 raise TypeError("bufsize must be an integer")
139 if preexec_fn
is not None:
140 raise ValueError("preexec_fn is not supported on Windows platforms")
142 raise ValueError("close_fds is not supported on Windows platforms")
144 if startupinfo
is not None:
145 raise ValueError("startupinfo is only supported on Windows platforms")
146 if creationflags
!= 0:
147 raise ValueError("creationflags is only supported on Windows platforms")
153 self
.returncode
= None
154 self
.universal_newlines
= universal_newlines
158 errread
, errwrite
) = self
._get
_handles
(stdin
, stdout
, stderr
)
160 self
._execute
_child
(args
, executable
, preexec_fn
, close_fds
,
161 cwd
, env
, universal_newlines
,
162 startupinfo
, creationflags
, shell
,
168 if stdin
is None and p2cwrite
is not None:
171 if stdout
is None and c2pread
is not None:
174 if stderr
is None and errread
is not None:
179 self
.stdin
= os
.fdopen(p2cwrite
, 'wb', bufsize
)
181 if universal_newlines
:
182 self
.stdout
= os
.fdopen(c2pread
, 'rU', bufsize
)
184 self
.stdout
= os
.fdopen(c2pread
, 'rb', bufsize
)
186 if universal_newlines
:
187 self
.stderr
= os
.fdopen(errread
, 'rU', bufsize
)
189 self
.stderr
= os
.fdopen(errread
, 'rb', bufsize
)
192 def _translate_newlines(self
, data
):
193 data
= data
.replace("\r\n", "\n")
194 data
= data
.replace("\r", "\n")
198 def __del__(self
, sys
=sys
):
199 if not self
._child
_created
:
201 self
.poll(_deadstate
=sys
.maxint
)
202 if self
.returncode
is None and _active
is not None:
206 def communicate(self
, input=None):
207 if [self
.stdin
, self
.stdout
, self
.stderr
].count(None) >= 2:
212 self
.stdin
.write(input)
215 stdout
= self
.stdout
.read()
217 stderr
= self
.stderr
.read()
219 return (stdout
, stderr
)
221 return self
._communicate
(input)
225 def _get_handles(self
, stdin
, stdout
, stderr
):
226 if stdin
is None and stdout
is None and stderr
is None:
227 return (None, None, None, None, None, None)
229 p2cread
, p2cwrite
= None, None
230 c2pread
, c2pwrite
= None, None
231 errread
, errwrite
= None, None
234 p2cread
= GetStdHandle(STD_INPUT_HANDLE
)
235 if p2cread
is not None:
237 elif stdin
is None or stdin
== PIPE
:
238 p2cread
, p2cwrite
= CreatePipe(None, 0)
239 p2cwrite
= p2cwrite
.Detach()
240 p2cwrite
= msvcrt
.open_osfhandle(p2cwrite
, 0)
241 elif isinstance(stdin
, int):
242 p2cread
= msvcrt
.get_osfhandle(stdin
)
244 p2cread
= msvcrt
.get_osfhandle(stdin
.fileno())
245 p2cread
= self
._make
_inheritable
(p2cread
)
248 c2pwrite
= GetStdHandle(STD_OUTPUT_HANDLE
)
249 if c2pwrite
is not None:
251 elif stdout
is None or stdout
== PIPE
:
252 c2pread
, c2pwrite
= CreatePipe(None, 0)
253 c2pread
= c2pread
.Detach()
254 c2pread
= msvcrt
.open_osfhandle(c2pread
, 0)
255 elif isinstance(stdout
, int):
256 c2pwrite
= msvcrt
.get_osfhandle(stdout
)
258 c2pwrite
= msvcrt
.get_osfhandle(stdout
.fileno())
259 c2pwrite
= self
._make
_inheritable
(c2pwrite
)
262 errwrite
= GetStdHandle(STD_ERROR_HANDLE
)
263 if errwrite
is not None:
265 elif stderr
is None or stderr
== PIPE
:
266 errread
, errwrite
= CreatePipe(None, 0)
267 errread
= errread
.Detach()
268 errread
= msvcrt
.open_osfhandle(errread
, 0)
269 elif stderr
== STDOUT
:
271 elif isinstance(stderr
, int):
272 errwrite
= msvcrt
.get_osfhandle(stderr
)
274 errwrite
= msvcrt
.get_osfhandle(stderr
.fileno())
275 errwrite
= self
._make
_inheritable
(errwrite
)
277 return (p2cread
, p2cwrite
,
280 def _make_inheritable(self
, handle
):
281 return DuplicateHandle(GetCurrentProcess(), handle
, GetCurrentProcess(), 0, 1, DUPLICATE_SAME_ACCESS
)
283 def _find_w9xpopen(self
):
284 w9xpopen
= os
.path
.join(os
.path
.dirname(GetModuleFileName(0)), "w9xpopen.exe")
285 if not os
.path
.exists(w9xpopen
):
286 w9xpopen
= os
.path
.join(os
.path
.dirname(sys
.exec_prefix
), "w9xpopen.exe")
287 if not os
.path
.exists(w9xpopen
):
288 raise RuntimeError("Cannot locate w9xpopen.exe, which is needed for Popen to work with your shell or platform.")
291 def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
,
292 cwd
, env
, universal_newlines
,
293 startupinfo
, creationflags
, shell
,
298 if not isinstance(args
, types
.StringTypes
):
299 args
= list2cmdline(args
)
301 if startupinfo
is None:
302 startupinfo
= STARTUPINFO()
303 if None not in (p2cread
, c2pwrite
, errwrite
):
304 startupinfo
.dwFlags |
= STARTF_USESTDHANDLES
305 startupinfo
.hStdInput
= p2cread
306 startupinfo
.hStdOutput
= c2pwrite
307 startupinfo
.hStdError
= errwrite
310 startupinfo
.dwFlags |
= STARTF_USESHOWWINDOW
311 startupinfo
.wShowWindow
= SW_HIDE
312 comspec
= os
.environ
.get("COMSPEC", "cmd.exe")
313 args
= comspec
+ " /c " + args
314 if (GetVersion() >= 0x80000000L
or
315 os
.path
.basename(comspec
).lower() == "command.com"):
316 w9xpopen
= self
._find
_w
9xpopen
()
317 args
= '"%s" %s' % (w9xpopen
, args
)
318 creationflags |
= CREATE_NEW_CONSOLE
321 hp
, ht
, pid
, tid
= CreateProcess(executable
, args
, None, None, 1, creationflags
, env
, cwd
, startupinfo
)
322 except pywintypes
.error
, e
:
323 raise WindowsError(*e
.args
)
325 self
._child
_created
= True
330 if p2cread
is not None:
332 if c2pwrite
is not None:
334 if errwrite
is not None:
338 def poll(self
, _deadstate
=None):
339 if self
.returncode
is None:
340 if WaitForSingleObject(self
._handle
, 0) == WAIT_OBJECT_0
:
341 self
.returncode
= GetExitCodeProcess(self
._handle
)
342 return self
.returncode
346 if self
.returncode
is None:
347 obj
= WaitForSingleObject(self
._handle
, INFINITE
)
348 self
.returncode
= GetExitCodeProcess(self
._handle
)
349 return self
.returncode
351 def _readerthread(self
, fh
, buffer):
352 buffer.append(fh
.read())
354 def _communicate(self
, input):
360 stdout_thread
= threading
.Thread(target
=self
._readerthread
, args
=(self
.stdout
, stdout
))
361 stdout_thread
.setDaemon(True)
362 stdout_thread
.start()
365 stderr_thread
= threading
.Thread(target
=self
._readerthread
, args
=(self
.stderr
, stderr
))
366 stderr_thread
.setDaemon(True)
367 stderr_thread
.start()
370 if input is not None:
371 self
.stdin
.write(input)
379 if stdout
is not None:
381 if stderr
is not None:
384 if self
.universal_newlines
and hasattr(file, 'newlines'):
386 stdout
= self
._translate
_newlines
(stdout
)
388 stderr
= self
._translate
_newlines
(stderr
)
391 return (stdout
, stderr
)
394 def _get_handles(self
, stdin
, stdout
, stderr
):
395 p2cread
, p2cwrite
= None, None
396 c2pread
, c2pwrite
= None, None
397 errread
, errwrite
= None, None
402 p2cread
, p2cwrite
= os
.pipe()
403 elif isinstance(stdin
, int):
406 p2cread
= stdin
.fileno()
411 c2pread
, c2pwrite
= os
.pipe()
412 elif isinstance(stdout
, int):
415 c2pwrite
= stdout
.fileno()
420 errread
, errwrite
= os
.pipe()
421 elif stderr
== STDOUT
:
423 elif isinstance(stderr
, int):
426 errwrite
= stderr
.fileno()
428 return (p2cread
, p2cwrite
, c2pread
, c2pwrite
, errread
, errwrite
)
430 def _set_cloexec_flag(self
, fd
):
432 cloexec_flag
= fcntl
.FD_CLOEXEC
433 except AttributeError:
436 old
= fcntl
.fcntl(fd
, fcntl
.F_GETFD
)
437 fcntl
.fcntl(fd
, fcntl
.F_SETFD
, old | cloexec_flag
)
439 def _close_fds(self
, but
):
440 for i
in xrange(3, MAXFD
):
448 def _execute_child(self
, args
, executable
, preexec_fn
, close_fds
,
449 cwd
, env
, universal_newlines
, startupinfo
, creationflags
, shell
,
450 p2cread
, p2cwrite
, c2pread
, c2pwrite
, errread
, errwrite
):
452 if isinstance(args
, types
.StringTypes
):
458 args
= ["/bin/sh", "-c"] + args
460 if executable
is None:
463 errpipe_read
, errpipe_write
= os
.pipe()
464 self
._set
_cloexec
_flag
(errpipe_write
)
466 gc_was_enabled
= gc
.isenabled()
474 self
._child
_created
= True
483 os
.close(errpipe_read
)
492 if p2cread
and p2cread
not in (0,):
494 if c2pwrite
and c2pwrite
not in (p2cread
, 1):
496 if errwrite
and errwrite
not in (p2cread
, c2pwrite
, 2):
500 self
._close
_fds
(but
=errpipe_write
)
509 os
.execvp(executable
, args
)
511 os
.execvpe(executable
, args
, env
)
514 exc_type
, exc_value
, tb
= sys
.exc_info()
515 exc_lines
= traceback
.format_exception(exc_type
, exc_value
, tb
)
516 exc_value
.child_traceback
= ''.join(exc_lines
)
517 os
.write(errpipe_write
, pickle
.dumps(exc_value
))
523 os
.close(errpipe_write
)
524 if p2cread
and p2cwrite
:
526 if c2pwrite
and c2pread
:
528 if errwrite
and errread
:
531 data
= os
.read(errpipe_read
, 1048576)
532 os
.close(errpipe_read
)
534 os
.waitpid(self
.pid
, 0)
535 child_exception
= pickle
.loads(data
)
536 raise child_exception
538 def _handle_exitstatus(self
, sts
):
539 if os
.WIFSIGNALED(sts
):
540 self
.returncode
= -os
.WTERMSIG(sts
)
541 elif os
.WIFEXITED(sts
):
542 self
.returncode
= os
.WEXITSTATUS(sts
)
544 raise RuntimeError("Unknown child exit status!")
546 def poll(self
, _deadstate
=None):
547 if self
.returncode
is None:
549 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
551 self
._handle
_exitstatus
(sts
)
553 if _deadstate
is not None:
554 self
.returncode
= _deadstate
555 return self
.returncode
558 if self
.returncode
is None:
559 pid
, sts
= os
.waitpid(self
.pid
, 0)
560 self
._handle
_exitstatus
(sts
)
561 return self
.returncode
563 def _communicate(self
, input):
572 write_set
.append(self
.stdin
)
576 read_set
.append(self
.stdout
)
579 read_set
.append(self
.stderr
)
583 while read_set
or write_set
:
584 rlist
, wlist
, xlist
= select
.select(read_set
, write_set
, [])
586 if self
.stdin
in wlist
:
587 bytes_written
= os
.write(self
.stdin
.fileno(), buffer(input, input_offset
, 512))
588 input_offset
+= bytes_written
589 if input_offset
>= len(input):
591 write_set
.remove(self
.stdin
)
593 if self
.stdout
in rlist
:
594 data
= os
.read(self
.stdout
.fileno(), 1024)
597 read_set
.remove(self
.stdout
)
600 if self
.stderr
in rlist
:
601 data
= os
.read(self
.stderr
.fileno(), 1024)
604 read_set
.remove(self
.stderr
)
607 if stdout
is not None:
608 stdout
= ''.join(stdout
)
609 if stderr
is not None:
610 stderr
= ''.join(stderr
)
612 if self
.universal_newlines
and hasattr(file, 'newlines'):
614 stdout
= self
._translate
_newlines
(stdout
)
616 stderr
= self
._translate
_newlines
(stderr
)
619 return (stdout
, stderr
)