Skip to content

Commit bb4ca59

Browse files
committed
Fixing broken tests and linting
1 parent cb1bbfa commit bb4ca59

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/pipx/animate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from contextlib import contextmanager
44
from multiprocessing import Queue
55
from threading import Event, Thread
6-
from typing import Generator, List
6+
from typing import Generator, List, Optional
77

88
from pipx.constants import WINDOWS
99
from pipx.emojis import EMOJI_SUPPORT
@@ -31,7 +31,9 @@ def _env_supports_animation() -> bool:
3131

3232

3333
@contextmanager
34-
def animate(message: str, do_animation: bool, *, delay: float = 0, stream: Queue = None) -> Generator[None, None, None]:
34+
def animate(
35+
message: str, do_animation: bool, *, delay: float = 0, stream: Optional[Queue] = None
36+
) -> Generator[None, None, None]:
3537
if not do_animation or not _env_supports_animation():
3638
# No animation, just a single print of message
3739
sys.stderr.write(f"{message}...\n")

src/pipx/util.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import logging
23
import os
34
import random
@@ -10,6 +11,7 @@
1011
from dataclasses import dataclass
1112
from multiprocessing import Queue
1213
from pathlib import Path
14+
from subprocess import TimeoutExpired
1315
from typing import (
1416
Any,
1517
Dict,
@@ -166,7 +168,7 @@ def run_subprocess(
166168
log_stdout: bool = True,
167169
log_stderr: bool = True,
168170
run_dir: Optional[str] = None,
169-
stream: Queue = None,
171+
stream: Optional[Queue] = None,
170172
) -> "subprocess.CompletedProcess[str]":
171173
"""Run arbitrary command as subprocess, capturing stderr and stout"""
172174
env = dict(os.environ)
@@ -193,6 +195,13 @@ def run_subprocess(
193195
text=True,
194196
cwd=run_dir,
195197
) as process:
198+
try:
199+
importlib.util.find_spec("msvcrt")
200+
except ModuleNotFoundError:
201+
_mswindows = False
202+
else:
203+
_mswindows = True
204+
196205
try:
197206
stdout, stderr = "", ""
198207
while True:
@@ -204,10 +213,10 @@ def run_subprocess(
204213
stream.put_nowait(out)
205214
if not out and not err:
206215
break
207-
except subprocess.TimeoutExpired as exc:
216+
except TimeoutExpired:
208217
process.kill()
209-
if process._mswindows:
210-
exc.stdout, exc.stderr = process.communicate()
218+
if _mswindows:
219+
process.communicate()
211220
else:
212221
process.wait()
213222
raise
@@ -216,7 +225,8 @@ def run_subprocess(
216225
raise
217226
retcode = process.poll()
218227

219-
completed_process = subprocess.CompletedProcess(process.args, retcode, stdout, stderr)
228+
if type(retcode) is int:
229+
completed_process = subprocess.CompletedProcess(process.args, retcode, stdout, stderr)
220230

221231
if capture_stdout and log_stdout:
222232
logger.debug(f"stdout: {completed_process.stdout}".rstrip())

src/pipx/venv.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def install_package(
245245
# check syntax and clean up spec and pip_args
246246
(package_or_url, pip_args) = parse_specifier_for_install(package_or_url, pip_args)
247247

248-
pip_out_stream = Queue()
248+
pip_out_stream: Queue = Queue()
249249

250250
logger.info("Installing %s", package_descr := full_package_description(package_name, package_or_url))
251251
with animate(f"installing {package_descr}", self.do_animation, stream=pip_out_stream):
@@ -293,7 +293,7 @@ def install_package(
293293
def install_unmanaged_packages(self, requirements: List[str], pip_args: List[str]) -> None:
294294
"""Install packages in the venv, but do not record them in the metadata."""
295295

296-
pip_out_stream = Queue()
296+
pip_out_stream: Queue = Queue()
297297

298298
# Note: We want to install everything at once, as that lets
299299
# pip resolve conflicts correctly.
@@ -312,7 +312,9 @@ def install_unmanaged_packages(self, requirements: List[str], pip_args: List[str
312312
]
313313
# no logging because any errors will be specially logged by
314314
# subprocess_post_check_handle_pip_error()
315-
pip_process = run_subprocess(cmd, log_stdout=False, log_stderr=False, run_dir=str(self.root), stream=pip_out_stream)
315+
pip_process = run_subprocess(
316+
cmd, log_stdout=False, log_stderr=False, run_dir=str(self.root), stream=pip_out_stream
317+
)
316318
subprocess_post_check_handle_pip_error(pip_process)
317319
if pip_process.returncode:
318320
raise PipxError(f"Error installing {', '.join(requirements)}.")

0 commit comments

Comments
 (0)