Skip to content

Commit b0725ff

Browse files
committed
TST: Replace ensure_clean utility function with the temp_file pytest fixture in \_testing
1 parent 607e489 commit b0725ff

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

pandas/_testing/_io.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io
55
import pathlib
66
import tarfile
7+
import tempfile
78
from typing import (
89
TYPE_CHECKING,
910
Any,
@@ -14,7 +15,6 @@
1415
from pandas.compat._optional import import_optional_dependency
1516

1617
import pandas as pd
17-
from pandas._testing.contexts import ensure_clean
1818

1919
if TYPE_CHECKING:
2020
from collections.abc import Callable
@@ -54,7 +54,11 @@ def round_trip_pickle(
5454
_path = path
5555
if _path is None:
5656
_path = f"__{uuid.uuid4()}__.pickle"
57-
with ensure_clean(_path) as temp_path:
57+
# use a temporary directory and create a path inside it. This avoids
58+
# keeping an open file handle (important on Windows) while still
59+
# ensuring automatic cleanup.
60+
with tempfile.TemporaryDirectory() as tmpdir:
61+
temp_path = pathlib.Path(tmpdir) / _path
5862
pd.to_pickle(obj, temp_path)
5963
return pd.read_pickle(temp_path)
6064

@@ -80,9 +84,12 @@ def round_trip_pathlib(writer, reader, path: str | None = None):
8084
Path = pathlib.Path
8185
if path is None:
8286
path = "___pathlib___"
83-
with ensure_clean(path) as path:
84-
writer(Path(path))
85-
obj = reader(Path(path))
87+
# Use a temporary directory to host the file so we don't hold an open
88+
# file handle while allowing callers to use pathlib.Path semantics.
89+
with tempfile.TemporaryDirectory() as tmpdir:
90+
p = pathlib.Path(tmpdir) / path
91+
writer(Path(p))
92+
obj = reader(Path(p))
8693
return obj
8794

8895

0 commit comments

Comments
 (0)