Skip to content

Commit 84b3812

Browse files
committed
Show a single warning on temp directory cleanup
Log individual errors at debug logging level.
1 parent 92bff79 commit 84b3812

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/pip/_internal/utils/temp_dir.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55
import tempfile
66
import traceback
77
from contextlib import ExitStack, contextmanager
8-
from typing import Any, Callable, Dict, Generator, Optional, Tuple, Type, TypeVar, Union
8+
from typing import (
9+
Any,
10+
Callable,
11+
Dict,
12+
Generator,
13+
List,
14+
Optional,
15+
Tuple,
16+
Type,
17+
TypeVar,
18+
Union,
19+
)
920

1021
from pip._internal.utils.misc import enum, rmtree
1122

@@ -174,6 +185,8 @@ def cleanup(self) -> None:
174185
if not os.path.exists(self._path):
175186
return
176187

188+
errors: List[BaseException] = []
189+
177190
def onerror(
178191
func: Callable[[str], Any],
179192
path: str,
@@ -183,14 +196,14 @@ def onerror(
183196
exc_val = "\n".join(traceback.format_exception_only(*exc_info[:2]))
184197
exc_val = exc_val.rstrip() # remove trailing new line
185198
if func in (os.unlink, os.remove, os.rmdir):
186-
logging.warning(
187-
"Failed to remove a temporary file '%s' due to %s.\n"
188-
"You can safely remove it manually.",
199+
logger.debug(
200+
"Failed to remove a temporary file '%s' due to %s.\n",
189201
path,
190202
exc_val,
191203
)
192204
else:
193-
logging.warning("%s failed with %s.", func.__qualname__, exc_val)
205+
logger.debug("%s failed with %s.", func.__qualname__, exc_val)
206+
errors.append(exc_info[1])
194207

195208
if self.ignore_cleanup_errors:
196209
try:
@@ -199,6 +212,12 @@ def onerror(
199212
except OSError:
200213
# last pass ignore/log all errors
201214
rmtree(self._path, onerror=onerror)
215+
if errors:
216+
logger.warning(
217+
"Failed to remove contents of a temporary directory '%s'.\n"
218+
"You can safely remove it manually.",
219+
self._path,
220+
)
202221
else:
203222
rmtree(self._path)
204223

0 commit comments

Comments
 (0)