Skip to content

Commit 45991bc

Browse files
committed
Use explicit default value for TempDirectory delete flag
Now we can opt-in to globally-managed + globally-configured file deletion for pre-existing directories by passing an explicit `None`.
1 parent ace7d09 commit 45991bc

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/pip/_internal/utils/temp_dir.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1414

1515
if MYPY_CHECK_RUNNING:
16-
from typing import Any, Dict, Iterator, Optional, TypeVar
16+
from typing import Any, Dict, Iterator, Optional, TypeVar, Union
1717

1818
_T = TypeVar('_T', bound='TempDirectory')
1919

@@ -77,6 +77,13 @@ def tempdir_registry():
7777
_tempdir_registry = old_tempdir_registry
7878

7979

80+
class _Default(object):
81+
pass
82+
83+
84+
_default = _Default()
85+
86+
8087
class TempDirectory(object):
8188
"""Helper class that owns and cleans up a temporary directory.
8289
@@ -101,16 +108,21 @@ class TempDirectory(object):
101108
def __init__(
102109
self,
103110
path=None, # type: Optional[str]
104-
delete=None, # type: Optional[bool]
111+
delete=_default, # type: Union[bool, None, _Default]
105112
kind="temp", # type: str
106113
globally_managed=False, # type: bool
107114
):
108115
super(TempDirectory, self).__init__()
109116

110-
# If we were given an explicit directory, resolve delete option now.
111-
# Otherwise we wait until cleanup and see what tempdir_registry says.
112-
if path is not None and delete is None:
113-
delete = False
117+
if delete is _default:
118+
if path is not None:
119+
# If we were given an explicit directory, resolve delete option
120+
# now.
121+
delete = False
122+
else:
123+
# Otherwise, we wait until cleanup and see what
124+
# tempdir_registry says.
125+
delete = None
114126

115127
if path is None:
116128
path = self._create(kind)

tests/unit/test_utils_temp_dir.py

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pip._internal.utils.temp_dir import (
1111
AdjacentTempDirectory,
1212
TempDirectory,
13+
_default,
1314
global_tempdir_manager,
1415
tempdir_registry,
1516
)
@@ -216,12 +217,15 @@ def test_tempdirectory_asserts_global_tempdir(monkeypatch):
216217

217218
@pytest.mark.parametrize("delete,kind,exists", [
218219
(None, deleted_kind, False),
220+
(_default, deleted_kind, False),
219221
(True, deleted_kind, False),
220222
(False, deleted_kind, True),
221223
(None, not_deleted_kind, True),
224+
(_default, not_deleted_kind, True),
222225
(True, not_deleted_kind, False),
223226
(False, not_deleted_kind, True),
224227
(None, "unspecified", False),
228+
(_default, "unspecified", False),
225229
(True, "unspecified", False),
226230
(False, "unspecified", True),
227231
])
@@ -236,6 +240,24 @@ def test_tempdir_registry(kind, delete, exists):
236240
assert os.path.exists(path) == exists
237241

238242

243+
@pytest.mark.parametrize("delete,exists", [
244+
(_default, True), (None, False)
245+
])
246+
def test_temp_dir_does_not_delete_explicit_paths_by_default(
247+
tmpdir, delete, exists
248+
):
249+
path = tmpdir / "example"
250+
path.mkdir()
251+
252+
with tempdir_registry() as registry:
253+
registry.set_delete(deleted_kind, True)
254+
255+
with TempDirectory(path=path, delete=delete, kind=deleted_kind) as d:
256+
assert str(d.path) == path
257+
assert os.path.exists(path)
258+
assert os.path.exists(path) == exists
259+
260+
239261
@pytest.mark.parametrize("should_delete", [True, False])
240262
def test_tempdir_registry_lazy(should_delete):
241263
"""

0 commit comments

Comments
 (0)