Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a57c15d

Browse files
committed
reformat
1 parent 4e1b8d3 commit a57c15d

3 files changed

Lines changed: 36 additions & 19 deletions

File tree

IPython/utils/_process_common.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Function definitions
2626
#-----------------------------------------------------------------------------
2727

28-
def read_no_interrupt(p: subprocess.Popen):
28+
def read_no_interrupt(p: subprocess.Popen) -> str:
2929
"""Read from a pipe ignoring EINTR errors.
3030
3131
This is necessary because when reading from pipes with GUI event loops
@@ -40,7 +40,11 @@ def read_no_interrupt(p: subprocess.Popen):
4040
raise
4141

4242

43-
def process_handler(cmd:Union[str, List[str]], callback:Callable[[subprocess.Popen], int], stderr=subprocess.PIPE) -> Optional[int]:
43+
def process_handler(
44+
cmd: Union[str, List[str]],
45+
callback: Callable[[subprocess.Popen], int],
46+
stderr=subprocess.PIPE,
47+
) -> int:
4448
"""Open a command in a shell subprocess and execute a callback.
4549
4650
This function provides common scaffolding for creating subprocess.Popen()

IPython/utils/_process_win32.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
from threading import Thread
2727
import subprocess
2828

29+
from typing import Optional, List
30+
import traceback
31+
2932
# our own imports
3033
from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split
3134
from . 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

166172
try:
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
194209
except 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))

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ exclude = [
131131
'IPython/lib/deepreload.py',
132132
'IPython/sphinxext/ipython_directive.py',
133133
'IPython/terminal/ipapp.py',
134-
'IPython/utils/_process_win32.py',
135134
'IPython/utils/path.py',
136135
]
137136
# check_untyped_defs = true
@@ -334,7 +333,6 @@ addopts = [
334333
"--ignore=IPython/terminal/console.py",
335334
"--ignore=IPython/utils/_process_cli.py",
336335
"--ignore=IPython/utils/_process_posix.py",
337-
"--ignore=IPython/utils/_process_win32.py",
338336
"--ignore=IPython/utils/_process_win32_controller.py",
339337
"--ignore=IPython/utils/daemonize.py",
340338
"--ignore=IPython/utils/eventful.py",

0 commit comments

Comments
 (0)