2626from threading import Thread
2727import subprocess
2828
29+ from typing import Optional , List
30+ import traceback
31+
2932# our own imports
3033from ._process_common import read_no_interrupt , process_handler , arg_split as py_arg_split
3134from . import py3compat
@@ -55,7 +58,8 @@ class AvoidUNCPath:
5558 cmd = '"pushd %s &&"%s' % (path, cmd)
5659 os.system(cmd)
5760 """
58- def __enter__ (self ):
61+
62+ def __enter__ (self ) -> Optional [str ]:
5963 self .path = os .getcwd ()
6064 self .is_unc_path = self .path .startswith (r"\\" )
6165 if self .is_unc_path :
@@ -67,7 +71,9 @@ def __enter__(self):
6771 # directory
6872 return None
6973
70- def __exit__ (self , exc_type , exc_value , traceback ):
74+ def __exit__ (
75+ self , exc_type : Optional [type ], exc_value : Optional [BaseException ], traceback
76+ ) -> None :
7177 if self .is_unc_path :
7278 os .chdir (self .path )
7379
@@ -76,18 +82,18 @@ def _system_body(p: subprocess.Popen) -> int:
7682 """Callback for _system."""
7783 enc = DEFAULT_ENCODING
7884
79- def stdout_read ():
85+ def stdout_read () -> None :
8086 try :
8187 for line in read_no_interrupt (p .stdout ).splitlines ():
82- line = line .decode (enc , ' replace' )
88+ line = line .decode (enc , " replace" )
8389 print (line , file = sys .stdout )
8490 except Exception as e :
8591 print (f"Error reading stdout: { e } " , file = sys .stderr )
8692
87- def stderr_read ():
93+ def stderr_read () -> None :
8894 try :
8995 for line in read_no_interrupt (p .stderr ).splitlines ():
90- line = line .decode (enc , ' replace' )
96+ line = line .decode (enc , " replace" )
9197 print (line , file = sys .stderr )
9298 except Exception as e :
9399 print (f"Error reading stderr: { e } " , file = sys .stderr )
@@ -115,7 +121,7 @@ def stderr_read():
115121 return result
116122
117123
118- def system (cmd : str ):
124+ def system (cmd : str ) -> Optional [ int ] :
119125 """Win32 version of os.system() that works with network shares.
120126
121127 Note that this implementation returns None, as meant for use in IPython.
@@ -139,7 +145,7 @@ def system(cmd: str):
139145 cmd = '"pushd %s &&"%s' % (path , cmd )
140146 return process_handler (cmd , _system_body )
141147
142- def getoutput (cmd ) :
148+ def getoutput (cmd : str ) -> str :
143149 """Return standard output of executing cmd in a shell.
144150
145151 Accepts the same arguments as os.system().
@@ -164,14 +170,17 @@ def getoutput(cmd):
164170 return py3compat .decode (out )
165171
166172try :
167- CommandLineToArgvW = ctypes .windll .shell32 .CommandLineToArgvW
173+ windll = ctypes .windll # type: ignore [attr-defined]
174+ CommandLineToArgvW = windll .shell32 .CommandLineToArgvW
168175 CommandLineToArgvW .arg_types = [LPCWSTR , POINTER (c_int )]
169176 CommandLineToArgvW .restype = POINTER (LPCWSTR )
170- LocalFree = ctypes . windll .kernel32 .LocalFree
177+ LocalFree = windll .kernel32 .LocalFree
171178 LocalFree .res_type = HLOCAL
172179 LocalFree .arg_types = [HLOCAL ]
173-
174- def arg_split (commandline , posix = False , strict = True ):
180+
181+ def arg_split (
182+ commandline : str , posix : bool = False , strict : bool = True
183+ ) -> List [str ]:
175184 """Split a command line's arguments in a shell-like manner.
176185
177186 This is a special version for windows that use a ctypes call to CommandLineToArgvW
@@ -188,13 +197,19 @@ def arg_split(commandline, posix=False, strict=True):
188197 argvn = c_int ()
189198 result_pointer = CommandLineToArgvW (commandline .lstrip (), ctypes .byref (argvn ))
190199 result_array_type = LPCWSTR * argvn .value
191- result = [arg for arg in result_array_type .from_address (ctypes .addressof (result_pointer .contents ))]
200+ result = [
201+ arg
202+ for arg in result_array_type .from_address (
203+ ctypes .addressof (result_pointer .contents )
204+ )
205+ if arg is not None
206+ ]
192207 retval = LocalFree (result_pointer )
193208 return result
194209except AttributeError :
195210 arg_split = py_arg_split
196211
197- def check_pid (pid ) :
212+ def check_pid (pid : int ) -> bool :
198213 # OpenProcess returns 0 if no such process (of ours) exists
199214 # positive int otherwise
200- return bool (ctypes . windll .kernel32 .OpenProcess (1 ,0 , pid ))
215+ return bool (windll .kernel32 .OpenProcess (1 , 0 , pid ))
0 commit comments