Skip to content

Commit 7d7ee41

Browse files
authored
chore: use temp_file instead of ensure_clean in test (#62529)
1 parent 5cc3240 commit 7d7ee41

File tree

2 files changed

+60
-55
lines changed

2 files changed

+60
-55
lines changed

pandas/tests/io/parser/test_encoding.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
)
1010
import os
1111
import tempfile
12-
import uuid
1312

1413
import numpy as np
1514
import pytest
@@ -51,28 +50,26 @@ def test_read_csv_unicode(all_parsers):
5150
@skip_pyarrow
5251
@pytest.mark.parametrize("sep", [",", "\t"])
5352
@pytest.mark.parametrize("encoding", ["utf-16", "utf-16le", "utf-16be"])
54-
def test_utf16_bom_skiprows(all_parsers, sep, encoding):
53+
def test_utf16_bom_skiprows(all_parsers, sep, encoding, temp_file):
5554
# see gh-2298
5655
parser = all_parsers
5756
data = """skip this
5857
skip this too
5958
A,B,C
6059
1,2,3
6160
4,5,6""".replace(",", sep)
62-
path = f"__{uuid.uuid4()}__.csv"
6361
kwargs = {"sep": sep, "skiprows": 2}
6462
utf8 = "utf-8"
6563

66-
with tm.ensure_clean(path) as path:
67-
bytes_data = data.encode(encoding)
64+
bytes_data = data.encode(encoding)
6865

69-
with open(path, "wb") as f:
70-
f.write(bytes_data)
66+
with open(temp_file, "wb") as f:
67+
f.write(bytes_data)
7168

72-
with TextIOWrapper(BytesIO(data.encode(utf8)), encoding=utf8) as bytes_buffer:
73-
result = parser.read_csv(path, encoding=encoding, **kwargs)
74-
expected = parser.read_csv(bytes_buffer, encoding=utf8, **kwargs)
75-
tm.assert_frame_equal(result, expected)
69+
with TextIOWrapper(BytesIO(data.encode(utf8)), encoding=utf8) as bytes_buffer:
70+
result = parser.read_csv(temp_file, encoding=encoding, **kwargs)
71+
expected = parser.read_csv(bytes_buffer, encoding=utf8, **kwargs)
72+
tm.assert_frame_equal(result, expected)
7673

7774

7875
def test_utf16_example(all_parsers, csv_dir_path):
@@ -240,7 +237,7 @@ def test_parse_encoded_special_characters(encoding):
240237

241238

242239
@pytest.mark.parametrize("encoding", ["utf-8", None, "utf-16", "cp1255", "latin-1"])
243-
def test_encoding_memory_map(all_parsers, encoding):
240+
def test_encoding_memory_map(all_parsers, encoding, temp_file):
244241
# GH40986
245242
parser = all_parsers
246243
expected = DataFrame(
@@ -250,20 +247,19 @@ def test_encoding_memory_map(all_parsers, encoding):
250247
"weapon": ["sai", "bo staff", "nunchunk", "katana"],
251248
}
252249
)
253-
with tm.ensure_clean() as file:
254-
expected.to_csv(file, index=False, encoding=encoding)
250+
expected.to_csv(temp_file, index=False, encoding=encoding)
255251

256-
if parser.engine == "pyarrow":
257-
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
258-
with pytest.raises(ValueError, match=msg):
259-
parser.read_csv(file, encoding=encoding, memory_map=True)
260-
return
252+
if parser.engine == "pyarrow":
253+
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
254+
with pytest.raises(ValueError, match=msg):
255+
parser.read_csv(temp_file, encoding=encoding, memory_map=True)
256+
return
261257

262-
df = parser.read_csv(file, encoding=encoding, memory_map=True)
258+
df = parser.read_csv(temp_file, encoding=encoding, memory_map=True)
263259
tm.assert_frame_equal(df, expected)
264260

265261

266-
def test_chunk_splits_multibyte_char(all_parsers):
262+
def test_chunk_splits_multibyte_char(all_parsers, temp_file):
267263
"""
268264
Chunk splits a multibyte character with memory_map=True
269265
@@ -276,20 +272,19 @@ def test_chunk_splits_multibyte_char(all_parsers):
276272
# Put two-bytes utf-8 encoded character "ą" at the end of chunk
277273
# utf-8 encoding of "ą" is b'\xc4\x85'
278274
df.iloc[2047] = "a" * 127 + "ą"
279-
with tm.ensure_clean("bug-gh43540.csv") as fname:
280-
df.to_csv(fname, index=False, header=False, encoding="utf-8")
275+
df.to_csv(temp_file, index=False, header=False, encoding="utf-8")
281276

282-
if parser.engine == "pyarrow":
283-
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
284-
with pytest.raises(ValueError, match=msg):
285-
parser.read_csv(fname, header=None, memory_map=True)
286-
return
277+
if parser.engine == "pyarrow":
278+
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
279+
with pytest.raises(ValueError, match=msg):
280+
parser.read_csv(temp_file, header=None, memory_map=True)
281+
return
287282

288-
dfr = parser.read_csv(fname, header=None, memory_map=True)
283+
dfr = parser.read_csv(temp_file, header=None, memory_map=True)
289284
tm.assert_frame_equal(dfr, df)
290285

291286

292-
def test_readcsv_memmap_utf8(all_parsers):
287+
def test_readcsv_memmap_utf8(all_parsers, temp_file):
293288
"""
294289
GH 43787
295290
@@ -310,16 +305,15 @@ def test_readcsv_memmap_utf8(all_parsers):
310305
lines.append(line)
311306
parser = all_parsers
312307
df = DataFrame(lines)
313-
with tm.ensure_clean("utf8test.csv") as fname:
314-
df.to_csv(fname, index=False, header=False, encoding="utf-8")
308+
df.to_csv(temp_file, index=False, header=False, encoding="utf-8")
315309

316-
if parser.engine == "pyarrow":
317-
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
318-
with pytest.raises(ValueError, match=msg):
319-
parser.read_csv(fname, header=None, memory_map=True, encoding="utf-8")
320-
return
310+
if parser.engine == "pyarrow":
311+
msg = "The 'memory_map' option is not supported with the 'pyarrow' engine"
312+
with pytest.raises(ValueError, match=msg):
313+
parser.read_csv(temp_file, header=None, memory_map=True, encoding="utf-8")
314+
return
321315

322-
dfr = parser.read_csv(fname, header=None, memory_map=True, encoding="utf-8")
316+
dfr = parser.read_csv(temp_file, header=None, memory_map=True, encoding="utf-8")
323317
tm.assert_frame_equal(df, dfr)
324318

325319

pandas/tests/io/xml/test_xml_dtypes.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ def iterparse(request):
3131
return request.param
3232

3333

34-
def read_xml_iterparse(data, **kwargs):
35-
with tm.ensure_clean() as path:
36-
with open(path, "w", encoding="utf-8") as f:
37-
f.write(data)
38-
return read_xml(path, **kwargs)
34+
def read_xml_iterparse(data, temp_file, **kwargs):
35+
with open(temp_file, "w", encoding="utf-8") as f:
36+
f.write(data)
37+
return read_xml(temp_file, **kwargs)
3938

4039

4140
xml_types = """\
@@ -84,13 +83,14 @@ def read_xml_iterparse(data, **kwargs):
8483
# DTYPE
8584

8685

87-
def test_dtype_single_str(parser):
86+
def test_dtype_single_str(parser, temp_file):
8887
df_result = read_xml(StringIO(xml_types), dtype={"degrees": "str"}, parser=parser)
8988
df_iter = read_xml_iterparse(
9089
xml_types,
9190
parser=parser,
9291
dtype={"degrees": "str"},
9392
iterparse={"row": ["shape", "degrees", "sides"]},
93+
temp_file=temp_file,
9494
)
9595

9696
df_expected = DataFrame(
@@ -105,13 +105,14 @@ def test_dtype_single_str(parser):
105105
tm.assert_frame_equal(df_iter, df_expected)
106106

107107

108-
def test_dtypes_all_str(parser):
108+
def test_dtypes_all_str(parser, temp_file):
109109
df_result = read_xml(StringIO(xml_dates), dtype="string", parser=parser)
110110
df_iter = read_xml_iterparse(
111111
xml_dates,
112112
parser=parser,
113113
dtype="string",
114114
iterparse={"row": ["shape", "degrees", "sides", "date"]},
115+
temp_file=temp_file,
115116
)
116117

117118
df_expected = DataFrame(
@@ -128,7 +129,7 @@ def test_dtypes_all_str(parser):
128129
tm.assert_frame_equal(df_iter, df_expected)
129130

130131

131-
def test_dtypes_with_names(parser):
132+
def test_dtypes_with_names(parser, temp_file):
132133
df_result = read_xml(
133134
StringIO(xml_dates),
134135
names=["Col1", "Col2", "Col3", "Col4"],
@@ -141,6 +142,7 @@ def test_dtypes_with_names(parser):
141142
names=["Col1", "Col2", "Col3", "Col4"],
142143
dtype={"Col2": "string", "Col3": "Int64", "Col4": "datetime64[ns]"},
143144
iterparse={"row": ["shape", "degrees", "sides", "date"]},
145+
temp_file=temp_file,
144146
)
145147

146148
df_expected = DataFrame(
@@ -158,13 +160,14 @@ def test_dtypes_with_names(parser):
158160
tm.assert_frame_equal(df_iter, df_expected)
159161

160162

161-
def test_dtype_nullable_int(parser):
163+
def test_dtype_nullable_int(parser, temp_file):
162164
df_result = read_xml(StringIO(xml_types), dtype={"sides": "Int64"}, parser=parser)
163165
df_iter = read_xml_iterparse(
164166
xml_types,
165167
parser=parser,
166168
dtype={"sides": "Int64"},
167169
iterparse={"row": ["shape", "degrees", "sides"]},
170+
temp_file=temp_file,
168171
)
169172

170173
df_expected = DataFrame(
@@ -179,13 +182,14 @@ def test_dtype_nullable_int(parser):
179182
tm.assert_frame_equal(df_iter, df_expected)
180183

181184

182-
def test_dtype_float(parser):
185+
def test_dtype_float(parser, temp_file):
183186
df_result = read_xml(StringIO(xml_types), dtype={"degrees": "float"}, parser=parser)
184187
df_iter = read_xml_iterparse(
185188
xml_types,
186189
parser=parser,
187190
dtype={"degrees": "float"},
188191
iterparse={"row": ["shape", "degrees", "sides"]},
192+
temp_file=temp_file,
189193
)
190194

191195
df_expected = DataFrame(
@@ -209,7 +213,7 @@ def test_wrong_dtype(xml_books, parser, iterparse):
209213
)
210214

211215

212-
def test_both_dtype_converters(parser):
216+
def test_both_dtype_converters(parser, temp_file):
213217
df_expected = DataFrame(
214218
{
215219
"shape": ["square", "circle", "triangle"],
@@ -231,6 +235,7 @@ def test_both_dtype_converters(parser):
231235
converters={"degrees": str},
232236
parser=parser,
233237
iterparse={"row": ["shape", "degrees", "sides"]},
238+
temp_file=temp_file,
234239
)
235240

236241
tm.assert_frame_equal(df_result, df_expected)
@@ -240,7 +245,7 @@ def test_both_dtype_converters(parser):
240245
# CONVERTERS
241246

242247

243-
def test_converters_str(parser):
248+
def test_converters_str(parser, temp_file):
244249
df_result = read_xml(
245250
StringIO(xml_types), converters={"degrees": str}, parser=parser
246251
)
@@ -249,6 +254,7 @@ def test_converters_str(parser):
249254
parser=parser,
250255
converters={"degrees": str},
251256
iterparse={"row": ["shape", "degrees", "sides"]},
257+
temp_file=temp_file,
252258
)
253259

254260
df_expected = DataFrame(
@@ -263,7 +269,7 @@ def test_converters_str(parser):
263269
tm.assert_frame_equal(df_iter, df_expected)
264270

265271

266-
def test_converters_date(parser):
272+
def test_converters_date(parser, temp_file):
267273
convert_to_datetime = lambda x: to_datetime(x)
268274
df_result = read_xml(
269275
StringIO(xml_dates), converters={"date": convert_to_datetime}, parser=parser
@@ -273,6 +279,7 @@ def test_converters_date(parser):
273279
parser=parser,
274280
converters={"date": convert_to_datetime},
275281
iterparse={"row": ["shape", "degrees", "sides", "date"]},
282+
temp_file=temp_file,
276283
)
277284

278285
df_expected = DataFrame(
@@ -312,13 +319,14 @@ def test_callable_str_converters(xml_books, parser, iterparse):
312319
# PARSE DATES
313320

314321

315-
def test_parse_dates_column_name(parser):
322+
def test_parse_dates_column_name(parser, temp_file):
316323
df_result = read_xml(StringIO(xml_dates), parse_dates=["date"], parser=parser)
317324
df_iter = read_xml_iterparse(
318325
xml_dates,
319326
parser=parser,
320327
parse_dates=["date"],
321328
iterparse={"row": ["shape", "degrees", "sides", "date"]},
329+
temp_file=temp_file,
322330
)
323331

324332
df_expected = DataFrame(
@@ -334,13 +342,14 @@ def test_parse_dates_column_name(parser):
334342
tm.assert_frame_equal(df_iter, df_expected)
335343

336344

337-
def test_parse_dates_column_index(parser):
345+
def test_parse_dates_column_index(parser, temp_file):
338346
df_result = read_xml(StringIO(xml_dates), parse_dates=[3], parser=parser)
339347
df_iter = read_xml_iterparse(
340348
xml_dates,
341349
parser=parser,
342350
parse_dates=[3],
343351
iterparse={"row": ["shape", "degrees", "sides", "date"]},
352+
temp_file=temp_file,
344353
)
345354

346355
df_expected = DataFrame(
@@ -356,14 +365,15 @@ def test_parse_dates_column_index(parser):
356365
tm.assert_frame_equal(df_iter, df_expected)
357366

358367

359-
def test_parse_dates_true(parser):
368+
def test_parse_dates_true(parser, temp_file):
360369
df_result = read_xml(StringIO(xml_dates), parse_dates=True, parser=parser)
361370

362371
df_iter = read_xml_iterparse(
363372
xml_dates,
364373
parser=parser,
365374
parse_dates=True,
366375
iterparse={"row": ["shape", "degrees", "sides", "date"]},
376+
temp_file=temp_file,
367377
)
368378

369379
df_expected = DataFrame(
@@ -379,7 +389,7 @@ def test_parse_dates_true(parser):
379389
tm.assert_frame_equal(df_iter, df_expected)
380390

381391

382-
def test_day_first_parse_dates(parser):
392+
def test_day_first_parse_dates(parser, temp_file):
383393
xml = """\
384394
<?xml version='1.0' encoding='utf-8'?>
385395
<data>
@@ -421,6 +431,7 @@ def test_day_first_parse_dates(parser):
421431
parse_dates=["date"],
422432
parser=parser,
423433
iterparse={"row": ["shape", "degrees", "sides", "date"]},
434+
temp_file=temp_file,
424435
)
425436

426437
tm.assert_frame_equal(df_result, df_expected)

0 commit comments

Comments
 (0)