|
34 | 34 | from collections.abc import Iterable
|
35 | 35 | from glob import glob
|
36 | 36 | from sysconfig import get_path
|
37 |
| -from typing import TYPE_CHECKING |
| 37 | +from typing import TYPE_CHECKING, Callable, TypeVar |
38 | 38 |
|
39 | 39 | from jaraco.text import yield_lines
|
40 | 40 |
|
|
89 | 89 | 'get_exe_prefixes',
|
90 | 90 | ]
|
91 | 91 |
|
| 92 | +_T = TypeVar("_T") |
| 93 | + |
92 | 94 |
|
93 | 95 | def is_64bit():
|
94 | 96 | return struct.calcsize("P") == 8
|
@@ -1786,13 +1788,18 @@ def _first_line_re():
|
1786 | 1788 | return re.compile(first_line_re.pattern.decode())
|
1787 | 1789 |
|
1788 | 1790 |
|
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) |
1796 | 1803 |
|
1797 | 1804 |
|
1798 | 1805 | def update_dist_caches(dist_path, fix_zipimporter_caches):
|
|
0 commit comments