Skip to content

Commit 450a962

Browse files
committed
Reraise sensible errors from auto_chmod
1 parent 8e96382 commit 450a962

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

newsfragments/4593.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reraise sensible errors from ``setuptools.command.easy_install.auto_chmod`` instead of nonsensical ``TypeError: 'Exception' object is not subscriptable`` -- by :user:`Avasam`

setuptools/command/easy_install.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from collections.abc import Iterable
3535
from glob import glob
3636
from sysconfig import get_path
37-
from typing import TYPE_CHECKING
37+
from typing import TYPE_CHECKING, Callable, TypeVar
3838

3939
from jaraco.text import yield_lines
4040

@@ -89,6 +89,8 @@
8989
'get_exe_prefixes',
9090
]
9191

92+
_T = TypeVar("_T")
93+
9294

9395
def is_64bit():
9496
return struct.calcsize("P") == 8
@@ -1786,13 +1788,18 @@ def _first_line_re():
17861788
return re.compile(first_line_re.pattern.decode())
17871789

17881790

1789-
def auto_chmod(func, arg, exc):
1790-
if func in [os.unlink, os.remove] and os.name == 'nt':
1791-
chmod(arg, stat.S_IWRITE)
1792-
return func(arg)
1793-
et, ev, _ = sys.exc_info()
1794-
# TODO: This code doesn't make sense. What is it trying to do?
1795-
raise (ev[0], ev[1] + (" %s %s" % (func, arg)))
1791+
# Must match shutil._OnExcCallback
1792+
def auto_chmod(func: Callable[..., _T], arg: str, exc: BaseException) -> _T:
1793+
"""shutils onexc callback to automatically call chmod for certain functions."""
1794+
if os.name != 'nt':
1795+
raise OSError(f"Can't call auto_chmod on non-nt os {os.name!r}") from exc
1796+
supported_methods = {os.unlink, os.remove}
1797+
if func not in supported_methods:
1798+
raise ValueError(
1799+
f"Argument func is not one of {[method.__name__ for method in supported_methods]} (got: {func.__name__!r})"
1800+
) from exc
1801+
chmod(arg, stat.S_IWRITE)
1802+
return func(arg)
17961803

17971804

17981805
def update_dist_caches(dist_path, fix_zipimporter_caches):

0 commit comments

Comments
 (0)