1
1
import contextlib
2
2
import errno
3
- import functools
4
3
import getpass
5
4
import hashlib
6
5
import io
@@ -125,26 +124,6 @@ def get_prog() -> str:
125
124
return "pip"
126
125
127
126
128
- def bare_exc_to_onexc (exc_val : BaseException ) -> ExcInfo :
129
- exc_ty = type (exc_val )
130
- tb = exc_val .__traceback__
131
- if tb is None :
132
- import inspect
133
-
134
- frame = inspect .currentframe ()
135
- assert frame is not None
136
- tb = TracebackType (None , frame , frame .f_lasti , frame .f_lineno )
137
- return (exc_ty , exc_val , tb )
138
-
139
-
140
- def extract_exc_info_arg (f : OnErr ) -> OnExc :
141
- def g (fn : FunctionType , p : Path , e : BaseException ) -> Any :
142
- info = bare_exc_to_onexc (e )
143
- return f (fn , p , info )
144
-
145
- return functools .update_wrapper (g , f )
146
-
147
-
148
127
# Retry every half second for up to 3 seconds
149
128
# Tenacity raises RetryError by default, explicitly raise the original exception
150
129
@retry (reraise = True , stop = stop_after_delay (3 ), wait = wait_fixed (0.5 ))
@@ -157,10 +136,15 @@ def rmtree(
157
136
onexc = _onerror_ignore
158
137
if onexc is None :
159
138
onexc = _onerror_reraise
160
- handler : OnErr = partial (rmtree_errorhandler , onexc = onexc )
139
+ handler : OnErr = partial (
140
+ # `[func, path, Union[ExcInfo, BaseException]] -> Any` is equivalent to
141
+ # `Union[([func, path, ExcInfo] -> Any), ([func, path, BaseException] -> Any)]`.
142
+ cast (Union [OnExc , OnErr ], rmtree_errorhandler ),
143
+ onexc = onexc ,
144
+ )
161
145
if sys .version_info >= (3 , 12 ):
162
- exc_handler = extract_exc_info_arg ( handler )
163
- shutil .rmtree (dir , onexc = exc_handler )
146
+ # See https://docs.python.org/3.12/whatsnew/3.12.html#shutil.
147
+ shutil .rmtree (dir , onexc = handler )
164
148
else :
165
149
shutil .rmtree (dir , onerror = handler )
166
150
@@ -178,7 +162,7 @@ def rmtree_errorhandler(
178
162
path : str ,
179
163
exc_info : Union [ExcInfo , BaseException ],
180
164
* ,
181
- onexc : Callable [..., Any ] = _onerror_reraise ,
165
+ onexc : OnExc = _onerror_reraise ,
182
166
) -> None :
183
167
"""
184
168
`rmtree` error handler to 'force' a file remove (i.e. like `rm -f`).
0 commit comments