@@ -360,41 +360,47 @@ class Popen(args, bufsize=0, executable=None,
360
360
import signal
361
361
362
362
# Exception classes used by this module.
363
+
364
+
363
365
class CalledProcessError (Exception ):
364
366
"""This exception is raised when a process run by check_call() returns
365
367
a non-zero exit status. The exit status will be stored in the
366
368
returncode attribute."""
369
+
367
370
def __init__ (self , returncode , cmd ):
368
371
self .returncode = returncode
369
372
self .cmd = cmd
373
+
370
374
def __str__ (self ):
371
375
return "Command '%s' returned non-zero exit status %d" % (self .cmd , self .returncode )
372
376
373
377
374
378
if mswindows :
375
379
import threading
376
380
import msvcrt
377
- if 0 : # <-- change this to use pywin32 instead of the _subprocess driver
381
+ if 0 : # <-- change this to use pywin32 instead of the _subprocess driver
378
382
import pywintypes
379
383
from win32api import GetStdHandle , STD_INPUT_HANDLE , \
380
- STD_OUTPUT_HANDLE , STD_ERROR_HANDLE
384
+ STD_OUTPUT_HANDLE , STD_ERROR_HANDLE
381
385
from win32api import GetCurrentProcess , DuplicateHandle , \
382
- GetModuleFileName , GetVersion
386
+ GetModuleFileName , GetVersion
383
387
from win32con import DUPLICATE_SAME_ACCESS , SW_HIDE
384
388
from win32pipe import CreatePipe
385
389
from win32process import CreateProcess , STARTUPINFO , \
386
- GetExitCodeProcess , STARTF_USESTDHANDLES , \
387
- STARTF_USESHOWWINDOW , CREATE_NEW_CONSOLE
390
+ GetExitCodeProcess , STARTF_USESTDHANDLES , \
391
+ STARTF_USESHOWWINDOW , CREATE_NEW_CONSOLE
388
392
from win32process import TerminateProcess
389
393
from win32event import WaitForSingleObject , INFINITE , WAIT_OBJECT_0
390
394
else :
391
395
from _subprocess import *
396
+
392
397
class STARTUPINFO :
393
398
dwFlags = 0
394
399
hStdInput = None
395
400
hStdOutput = None
396
401
hStdError = None
397
402
wShowWindow = 0
403
+
398
404
class pywintypes :
399
405
error = IOError
400
406
else :
@@ -403,22 +409,24 @@ class pywintypes:
403
409
import fcntl
404
410
import pickle
405
411
406
- __all__ = ["Popen" , "PIPE" , "STDOUT" , "call" , "check_call" , "CalledProcessError" ]
412
+ __all__ = ["Popen" , "PIPE" , "STDOUT" , "call" ,
413
+ "check_call" , "CalledProcessError" ]
407
414
408
415
try :
409
416
MAXFD = os .sysconf ("SC_OPEN_MAX" )
410
417
except :
411
418
MAXFD = 256
412
419
413
420
# True/False does not exist on 2.2.0
414
- #try:
421
+ # try:
415
422
# False
416
- #except NameError:
423
+ # except NameError:
417
424
# False = 0
418
425
# True = 1
419
426
420
427
_active = []
421
428
429
+
422
430
def _cleanup ():
423
431
for inst in _active [:]:
424
432
if inst ._internal_poll (_deadstate = sys .maxint ) >= 0 :
@@ -510,7 +518,7 @@ def list2cmdline(seq):
510
518
bs_buf .append (c )
511
519
elif c == '"' :
512
520
# Double backslashes.
513
- result .append ('\\ ' * len (bs_buf )* 2 )
521
+ result .append ('\\ ' * len (bs_buf ) * 2 )
514
522
bs_buf = []
515
523
result .append ('\\ "' )
516
524
else :
@@ -543,6 +551,7 @@ def _closerange(start, max):
543
551
544
552
545
553
class Popen (object ):
554
+
546
555
def __init__ (self , args , bufsize = 0 , executable = None ,
547
556
stdin = None , stdout = None , stderr = None ,
548
557
preexec_fn = None , close_fds = False , shell = False ,
@@ -634,13 +643,11 @@ def __init__(self, args, bufsize=0, executable=None,
634
643
else :
635
644
self .stderr = os .fdopen (errread , 'rb' , bufsize )
636
645
637
-
638
646
def _translate_newlines (self , data ):
639
647
data = data .replace ("\r \n " , "\n " )
640
648
data = data .replace ("\r " , "\n " )
641
649
return data
642
650
643
-
644
651
def __del__ (self , sys = sys ):
645
652
if not self ._child_created :
646
653
# We didn't get to successfully create a child process.
@@ -651,7 +658,6 @@ def __del__(self, sys=sys):
651
658
# Child is still running, keep us alive until we can wait on it.
652
659
_active .append (self )
653
660
654
-
655
661
def communicate (self , input = None ):
656
662
"""Interact with process: Send data to stdin. Read data from
657
663
stdout and stderr, until end-of-file is reached. Wait for
@@ -681,11 +687,9 @@ def communicate(self, input=None):
681
687
682
688
return self ._communicate (input )
683
689
684
-
685
690
def poll (self ):
686
691
return self ._internal_poll ()
687
692
688
-
689
693
if mswindows :
690
694
#
691
695
# Windows methods
@@ -755,14 +759,12 @@ def _get_handles(self, stdin, stdout, stderr):
755
759
c2pread , c2pwrite ,
756
760
errread , errwrite )
757
761
758
-
759
762
def _make_inheritable (self , handle ):
760
763
"""Return a duplicate of handle, which is inheritable"""
761
764
return DuplicateHandle (GetCurrentProcess (), handle ,
762
765
GetCurrentProcess (), 0 , 1 ,
763
766
DUPLICATE_SAME_ACCESS )
764
767
765
-
766
768
def _find_w9xpopen (self ):
767
769
"""Find and return absolut path to w9xpopen.exe"""
768
770
w9xpopen = os .path .join (os .path .dirname (GetModuleFileName (0 )),
@@ -778,7 +780,6 @@ def _find_w9xpopen(self):
778
780
"shell or platform." )
779
781
return w9xpopen
780
782
781
-
782
783
def _execute_child (self , args , executable , preexec_fn , close_fds ,
783
784
cwd , env , universal_newlines ,
784
785
startupinfo , creationflags , shell ,
@@ -823,13 +824,13 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
823
824
# Start the process
824
825
try :
825
826
hp , ht , pid , tid = CreateProcess (executable , args ,
826
- # no special security
827
- None , None ,
828
- int (not close_fds ),
829
- creationflags ,
830
- env ,
831
- cwd ,
832
- startupinfo )
827
+ # no special security
828
+ None , None ,
829
+ int (not close_fds ),
830
+ creationflags ,
831
+ env ,
832
+ cwd ,
833
+ startupinfo )
833
834
except pywintypes .error , e :
834
835
# Translate pywintypes.error to WindowsError, which is
835
836
# a subclass of OSError. FIXME: We should really
@@ -856,7 +857,6 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
856
857
if errwrite is not None :
857
858
errwrite .Close ()
858
859
859
-
860
860
def _internal_poll (self , _deadstate = None ):
861
861
"""Check if child process has terminated. Returns returncode
862
862
attribute."""
@@ -865,7 +865,6 @@ def _internal_poll(self, _deadstate=None):
865
865
self .returncode = GetExitCodeProcess (self ._handle )
866
866
return self .returncode
867
867
868
-
869
868
def wait (self ):
870
869
"""Wait for child process to terminate. Returns returncode
871
870
attribute."""
@@ -874,14 +873,12 @@ def wait(self):
874
873
self .returncode = GetExitCodeProcess (self ._handle )
875
874
return self .returncode
876
875
877
-
878
876
def _readerthread (self , fh , buffer ):
879
877
buffer .append (fh .read ())
880
878
881
-
882
879
def _communicate (self , input ):
883
- stdout = None # Return
884
- stderr = None # Return
880
+ stdout = None # Return
881
+ stderr = None # Return
885
882
886
883
if self .stdout :
887
884
stdout = []
@@ -988,7 +985,6 @@ def _get_handles(self, stdin, stdout, stderr):
988
985
c2pread , c2pwrite ,
989
986
errread , errwrite )
990
987
991
-
992
988
def _set_cloexec_flag (self , fd ):
993
989
try :
994
990
cloexec_flag = fcntl .FD_CLOEXEC
@@ -998,12 +994,10 @@ def _set_cloexec_flag(self, fd):
998
994
old = fcntl .fcntl (fd , fcntl .F_GETFD )
999
995
fcntl .fcntl (fd , fcntl .F_SETFD , old | cloexec_flag )
1000
996
1001
-
1002
997
def _close_fds (self , but ):
1003
998
_closerange (3 , but )
1004
999
_closerange (but + 1 , MAXFD )
1005
1000
1006
-
1007
1001
def _execute_child (self , args , executable , preexec_fn , close_fds ,
1008
1002
cwd , env , universal_newlines ,
1009
1003
startupinfo , creationflags , shell ,
@@ -1109,14 +1103,13 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
1109
1103
os .close (errwrite )
1110
1104
1111
1105
# Wait for exec to fail or succeed; possibly raising exception
1112
- data = os .read (errpipe_read , 1048576 ) # Exceptions limited to 1 MB
1106
+ data = os .read (errpipe_read , 1048576 ) # Exceptions limited to 1 MB
1113
1107
os .close (errpipe_read )
1114
1108
if data != "" :
1115
1109
os .waitpid (self .pid , 0 )
1116
1110
child_exception = pickle .loads (data )
1117
1111
raise child_exception
1118
1112
1119
-
1120
1113
def _handle_exitstatus (self , sts ):
1121
1114
if os .WIFSIGNALED (sts ):
1122
1115
self .returncode = - os .WTERMSIG (sts )
@@ -1126,7 +1119,6 @@ def _handle_exitstatus(self, sts):
1126
1119
# Should never happen
1127
1120
raise RuntimeError ("Unknown child exit status!" )
1128
1121
1129
-
1130
1122
def _internal_poll (self , _deadstate = None ):
1131
1123
"""Check if child process has terminated. Returns returncode
1132
1124
attribute."""
@@ -1140,7 +1132,6 @@ def _internal_poll(self, _deadstate=None):
1140
1132
self .returncode = _deadstate
1141
1133
return self .returncode
1142
1134
1143
-
1144
1135
def wait (self ):
1145
1136
"""Wait for child process to terminate. Returns returncode
1146
1137
attribute."""
@@ -1149,12 +1140,11 @@ def wait(self):
1149
1140
self ._handle_exitstatus (sts )
1150
1141
return self .returncode
1151
1142
1152
-
1153
1143
def _communicate (self , input ):
1154
1144
read_set = []
1155
1145
write_set = []
1156
- stdout = None # Return
1157
- stderr = None # Return
1146
+ stdout = None # Return
1147
+ stderr = None # Return
1158
1148
1159
1149
if self .stdin :
1160
1150
# Flush stdio buffer. This might block, if the user has
@@ -1174,7 +1164,8 @@ def _communicate(self, input):
1174
1164
input_offset = 0
1175
1165
while read_set or write_set :
1176
1166
try :
1177
- rlist , wlist , xlist = select .select (read_set , write_set , [])
1167
+ rlist , wlist , xlist = select .select (
1168
+ read_set , write_set , [])
1178
1169
except select .error , e :
1179
1170
if e .args [0 ] == errno .EINTR :
1180
1171
continue
@@ -1184,7 +1175,7 @@ def _communicate(self, input):
1184
1175
# When select has indicated that the file is writable,
1185
1176
# we can write up to PIPE_BUF bytes without risk
1186
1177
# blocking. POSIX defines PIPE_BUF >= 512
1187
- chunk = input [input_offset : input_offset + 512 ]
1178
+ chunk = input [input_offset : input_offset + 512 ]
1188
1179
bytes_written = os .write (self .stdin .fileno (), chunk )
1189
1180
input_offset += bytes_written
1190
1181
if input_offset >= len (input ):
0 commit comments