From 34cd7e781bdb567710f20a9905d4360e4cf207e4 Mon Sep 17 00:00:00 2001 From: mk688ba Date: Fri, 3 Oct 2025 14:15:43 +0200 Subject: [PATCH 1/2] Replace ensure_clean with temp_file --- pandas/tests/io/parser/test_parse_dates.py | 10 ++--- pandas/tests/io/parser/test_read_fwf.py | 49 +++++++++++----------- pandas/tests/io/parser/test_unsupported.py | 16 +++---- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py index 4ced43b6463e1..c708f77b8e966 100644 --- a/pandas/tests/io/parser/test_parse_dates.py +++ b/pandas/tests/io/parser/test_parse_dates.py @@ -86,7 +86,7 @@ def test_date_col_as_index_col(all_parsers): @xfail_pyarrow -def test_nat_parse(all_parsers): +def test_nat_parse(all_parsers, temp_file): # see gh-3062 parser = all_parsers df = DataFrame( @@ -97,11 +97,11 @@ def test_nat_parse(all_parsers): ) df.iloc[3:6, :] = np.nan - with tm.ensure_clean("__nat_parse_.csv") as path: - df.to_csv(path) + path = temp_file.parent / "__nat_parse_.csv" + df.to_csv(path) - result = parser.read_csv(path, index_col=0, parse_dates=["B"]) - tm.assert_frame_equal(result, df) + result = parser.read_csv(path, index_col=0, parse_dates=["B"]) + tm.assert_frame_equal(result, df) @skip_pyarrow diff --git a/pandas/tests/io/parser/test_read_fwf.py b/pandas/tests/io/parser/test_read_fwf.py index 6243185294894..4b78048a3a073 100644 --- a/pandas/tests/io/parser/test_read_fwf.py +++ b/pandas/tests/io/parser/test_read_fwf.py @@ -8,7 +8,6 @@ BytesIO, StringIO, ) -from pathlib import Path import numpy as np import pytest @@ -642,7 +641,7 @@ def test_default_delimiter(): @pytest.mark.parametrize("infer", [True, False]) -def test_fwf_compression(compression_only, infer, compression_to_extension): +def test_fwf_compression(compression_only, infer, compression_to_extension, temp_file): data = """1111111111 2222222222 3333333333""".strip() @@ -655,17 +654,17 @@ def test_fwf_compression(compression_only, infer, compression_to_extension): data = bytes(data, encoding="utf-8") - with tm.ensure_clean(filename="tmp." + extension) as path: - tm.write_to_compressed(compression, path, data) + path = temp_file.parent / f"tmp.{extension}" + tm.write_to_compressed(compression, path, data) - if infer is not None: - kwargs["compression"] = "infer" if infer else compression + if infer is not None: + kwargs["compression"] = "infer" if infer else compression - result = read_fwf(path, **kwargs) - tm.assert_frame_equal(result, expected) + result = read_fwf(path, **kwargs) + tm.assert_frame_equal(result, expected) -def test_binary_mode(): +def test_binary_mode(temp_file): """ read_fwf supports opening files in binary mode. @@ -676,31 +675,31 @@ def test_binary_mode(): df_reference = DataFrame( [["bba", "bab", "b a"]], columns=["aaa", "aaa.1", "aaa.2"], index=[0] ) - with tm.ensure_clean() as path: - Path(path).write_text(data, encoding="utf-8") - with open(path, "rb") as file: - df = read_fwf(file) - file.seek(0) - tm.assert_frame_equal(df, df_reference) + path = temp_file + path.write_text(data, encoding="utf-8") + with open(path, "rb") as file: + df = read_fwf(file) + file.seek(0) + tm.assert_frame_equal(df, df_reference) @pytest.mark.parametrize("memory_map", [True, False]) -def test_encoding_mmap(memory_map): +def test_encoding_mmap(memory_map, temp_file): """ encoding should be working, even when using a memory-mapped file. GH 23254. """ encoding = "iso8859_1" - with tm.ensure_clean() as path: - Path(path).write_bytes(" 1 A Ä 2\n".encode(encoding)) - df = read_fwf( - path, - header=None, - widths=[2, 2, 2, 2], - encoding=encoding, - memory_map=memory_map, - ) + path = temp_file + path.write_bytes(" 1 A Ä 2\n".encode(encoding)) + df = read_fwf( + path, + header=None, + widths=[2, 2, 2, 2], + encoding=encoding, + memory_map=memory_map, + ) df_reference = DataFrame([[1, "A", "Ä", 2]]) tm.assert_frame_equal(df, df_reference) diff --git a/pandas/tests/io/parser/test_unsupported.py b/pandas/tests/io/parser/test_unsupported.py index 07f84466e3ac2..07be638b77e8d 100644 --- a/pandas/tests/io/parser/test_unsupported.py +++ b/pandas/tests/io/parser/test_unsupported.py @@ -167,7 +167,7 @@ def test_on_bad_lines_callable_python_or_pyarrow(self, all_parsers): parser.read_csv(sio, on_bad_lines=bad_lines_func) -def test_close_file_handle_on_invalid_usecols(all_parsers): +def test_close_file_handle_on_invalid_usecols(all_parsers, temp_file): # GH 45384 parser = all_parsers @@ -176,13 +176,13 @@ def test_close_file_handle_on_invalid_usecols(all_parsers): # Raises pyarrow.lib.ArrowKeyError pytest.skip(reason="https://github.com/apache/arrow/issues/38676") - with tm.ensure_clean("test.csv") as fname: - Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8") - with tm.assert_produces_warning(False): - with pytest.raises(error, match="col3"): - parser.read_csv(fname, usecols=["col1", "col2", "col3"]) - # unlink fails on windows if file handles still point to it - os.unlink(fname) + fname = temp_file.parent / "test.csv" + Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8") + with tm.assert_produces_warning(False): + with pytest.raises(error, match="col3"): + parser.read_csv(fname, usecols=["col1", "col2", "col3"]) + # unlink fails on windows if file handles still point to it + os.unlink(fname) def test_invalid_file_inputs(request, all_parsers): From f815da93675029122e1245f29d6ba06ccc8714b7 Mon Sep 17 00:00:00 2001 From: mk688ba Date: Sat, 4 Oct 2025 11:46:18 +0200 Subject: [PATCH 2/2] Directly using temp_file instead of constructing path --- pandas/tests/io/parser/test_parse_dates.py | 2 +- pandas/tests/io/parser/test_unsupported.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py index c708f77b8e966..beb5e8d9d996c 100644 --- a/pandas/tests/io/parser/test_parse_dates.py +++ b/pandas/tests/io/parser/test_parse_dates.py @@ -97,7 +97,7 @@ def test_nat_parse(all_parsers, temp_file): ) df.iloc[3:6, :] = np.nan - path = temp_file.parent / "__nat_parse_.csv" + path = temp_file df.to_csv(path) result = parser.read_csv(path, index_col=0, parse_dates=["B"]) diff --git a/pandas/tests/io/parser/test_unsupported.py b/pandas/tests/io/parser/test_unsupported.py index 07be638b77e8d..8812a65ee4e7d 100644 --- a/pandas/tests/io/parser/test_unsupported.py +++ b/pandas/tests/io/parser/test_unsupported.py @@ -176,7 +176,7 @@ def test_close_file_handle_on_invalid_usecols(all_parsers, temp_file): # Raises pyarrow.lib.ArrowKeyError pytest.skip(reason="https://github.com/apache/arrow/issues/38676") - fname = temp_file.parent / "test.csv" + fname = temp_file Path(fname).write_text("col1,col2\na,b\n1,2", encoding="utf-8") with tm.assert_produces_warning(False): with pytest.raises(error, match="col3"):