Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

synthesize a traceback to get a normal exc_info from an exception #12187

Merged
merged 5 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/12187.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix improper handling of the new onexc argument of shutil.rmtree() in python 3.12.
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
36 changes: 31 additions & 5 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import errno
import functools
import getpass
import hashlib
import io
Expand All @@ -14,7 +15,8 @@
from functools import partial
from io import StringIO
from itertools import filterfalse, tee, zip_longest
from types import TracebackType
from pathlib import Path
from types import FunctionType, TracebackType
from typing import (
Any,
BinaryIO,
Expand Down Expand Up @@ -67,6 +69,8 @@
ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
VersionInfo = Tuple[int, int, int]
NetlocTuple = Tuple[str, Tuple[Optional[str], Optional[str]]]
OnExc = Callable[[FunctionType, Path, BaseException], Any]
OnErr = Callable[[FunctionType, Path, ExcInfo], Any]


def get_pip_version() -> str:
Expand Down Expand Up @@ -121,22 +125,44 @@ def get_prog() -> str:
return "pip"


def bare_exc_to_onexc(exc_val: BaseException) -> ExcInfo:
exc_ty = type(exc_val)
tb = exc_val.__traceback__
if tb is None:
import inspect

frame = inspect.currentframe()
assert frame is not None
tb = TracebackType(None, frame, frame.f_lasti, frame.f_lineno)
cosmicexplorer marked this conversation as resolved.
Show resolved Hide resolved
return (exc_ty, exc_val, tb)


def extract_exc_info_arg(f: OnErr) -> OnExc:
def g(fn: FunctionType, p: Path, e: BaseException) -> Any:
info = bare_exc_to_onexc(e)
return f(fn, p, info)

return functools.update_wrapper(g, f)


# Retry every half second for up to 3 seconds
# Tenacity raises RetryError by default, explicitly raise the original exception
@retry(reraise=True, stop=stop_after_delay(3), wait=wait_fixed(0.5))
def rmtree(
dir: str,
ignore_errors: bool = False,
onexc: Optional[Callable[[Any, Any, Any], Any]] = None,
onexc: Optional[OnErr] = None,
) -> None:
if ignore_errors:
onexc = _onerror_ignore
elif onexc is None:
if onexc is None:
onexc = _onerror_reraise
handler: OnErr = partial(rmtree_errorhandler, onexc=onexc)
if sys.version_info >= (3, 12):
shutil.rmtree(dir, onexc=partial(rmtree_errorhandler, onexc=onexc))
exc_handler = extract_exc_info_arg(handler)
shutil.rmtree(dir, onexc=exc_handler)
else:
shutil.rmtree(dir, onerror=partial(rmtree_errorhandler, onexc=onexc))
shutil.rmtree(dir, onerror=handler)


def _onerror_ignore(*_args: Any) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/pip/_internal/utils/temp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
import traceback
from contextlib import ExitStack, contextmanager
from pathlib import Path
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -189,7 +190,7 @@ def cleanup(self) -> None:

def onerror(
func: Callable[[str], Any],
path: str,
path: Path,
exc_info: Tuple[Type[BaseException], BaseException, Any],
) -> None:
"""Log a warning for a `rmtree` error and continue"""
Expand Down