From 90317aac59534a3bfd40e0ca2f26a84cb3d99ad0 2024-12-17 19:24:37 From: M Bussonnier Date: 2024-12-17 19:24:37 Subject: [PATCH] fix types --- diff --git a/IPython/utils/_process_common.py b/IPython/utils/_process_common.py index 93396f6..3a55c9f 100644 --- a/IPython/utils/_process_common.py +++ b/IPython/utils/_process_common.py @@ -14,18 +14,19 @@ of subprocess utilities, and it contains tools that are common to all of them. #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- -import subprocess +import os import shlex +import subprocess import sys -import os -from typing import Callable, Optional, Union, List +from typing import IO, Any, Callable, List, Union + from IPython.utils import py3compat #----------------------------------------------------------------------------- # Function definitions #----------------------------------------------------------------------------- -def read_no_interrupt(p: subprocess.Popen) -> str: +def read_no_interrupt(stream: IO[Any]) -> bytes: """Read from a pipe ignoring EINTR errors. This is necessary because when reading from pipes with GUI event loops @@ -34,7 +35,7 @@ def read_no_interrupt(p: subprocess.Popen) -> str: import errno try: - return p.read() + return stream.read() except IOError as err: if err.errno != errno.EINTR: raise diff --git a/IPython/utils/_process_win32.py b/IPython/utils/_process_win32.py index f251d23..58ef6b4 100644 --- a/IPython/utils/_process_win32.py +++ b/IPython/utils/_process_win32.py @@ -15,23 +15,23 @@ This file is only meant to be imported by process.py, not by end-users. #----------------------------------------------------------------------------- # stdlib +import ctypes import os +import subprocess import sys -import ctypes import time - -from ctypes import c_int, POINTER -from ctypes.wintypes import LPCWSTR, HLOCAL -from subprocess import STDOUT, TimeoutExpired +from ctypes import POINTER, c_int +from ctypes.wintypes import HLOCAL, LPCWSTR +from subprocess import STDOUT from threading import Thread -import subprocess +from types import TracebackType +from typing import IO, Any, List, Optional -from typing import Optional, List -import traceback +from . import py3compat +from ._process_common import arg_split as py_arg_split # our own imports -from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split -from . import py3compat +from ._process_common import process_handler, read_no_interrupt from .encoding import DEFAULT_ENCODING #----------------------------------------------------------------------------- @@ -72,7 +72,7 @@ class AvoidUNCPath: return None def __exit__( - self, exc_type: Optional[type], exc_value: Optional[BaseException], traceback + self, exc_type: Optional[type[BaseException]], exc_value: Optional[BaseException], traceback:TracebackType ) -> None: if self.is_unc_path: os.chdir(self.path) @@ -82,18 +82,23 @@ def _system_body(p: subprocess.Popen) -> int: """Callback for _system.""" enc = DEFAULT_ENCODING + # Dec 2024: in both of these functions, I'm not sure why we .splitlines() + # the bytes and then decode each line individually instead of just decoding + # the whole thing at once. def stdout_read() -> None: try: - for line in read_no_interrupt(p.stdout).splitlines(): - line = line.decode(enc, "replace") + assert p.stdout is not None + for byte_line in read_no_interrupt(p.stdout).splitlines(): + line = byte_line.decode(enc, "replace") print(line, file=sys.stdout) except Exception as e: print(f"Error reading stdout: {e}", file=sys.stderr) def stderr_read() -> None: try: - for line in read_no_interrupt(p.stderr).splitlines(): - line = line.decode(enc, "replace") + assert p.stderr is not None + for byte_line in read_no_interrupt(p.stderr).splitlines(): + line = byte_line.decode(enc, "replace") print(line, file=sys.stderr) except Exception as e: print(f"Error reading stderr: {e}", file=sys.stderr) @@ -204,7 +209,7 @@ try: ) if arg is not None ] - retval = LocalFree(result_pointer) + LocalFree(result_pointer) return result except AttributeError: arg_split = py_arg_split