Skip to content

Commit ac6c83b

Browse files
authored
TST: Replace ensure_clean utility function with the temp_file pytest fixture in test_xml.py (#63075)
1 parent 3157d07 commit ac6c83b

File tree

1 file changed

+82
-75
lines changed

1 file changed

+82
-75
lines changed

pandas/tests/io/xml/test_xml.py

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,19 @@ def parser(request):
263263
return request.param
264264

265265

266-
def read_xml_iterparse(data, **kwargs):
267-
with tm.ensure_clean() as path:
268-
with open(path, "w", encoding="utf-8") as f:
269-
f.write(data)
270-
return read_xml(path, **kwargs)
266+
def read_xml_iterparse(data, tmp_path, **kwargs):
267+
path = tmp_path / "temp.xml"
268+
with open(path, "w", encoding="utf-8") as f:
269+
f.write(data)
270+
return read_xml(path, **kwargs)
271271

272272

273-
def read_xml_iterparse_comp(comp_path, compression_only, **kwargs):
273+
def read_xml_iterparse_comp(comp_path, compression_only, tmp_path, **kwargs):
274+
path = tmp_path / "temp.xml"
274275
with get_handle(comp_path, "r", compression=compression_only) as handles:
275-
with tm.ensure_clean() as path:
276-
with open(path, "w", encoding="utf-8") as f:
277-
f.write(handles.handle.read())
278-
return read_xml(path, **kwargs)
276+
with open(path, "w", encoding="utf-8") as f:
277+
f.write(handles.handle.read())
278+
return read_xml(path, **kwargs)
279279

280280

281281
# FILE / URL
@@ -524,7 +524,7 @@ def test_wrong_url(parser, httpserver):
524524
# CONTENT
525525

526526

527-
def test_whitespace(parser):
527+
def test_whitespace(parser, tmp_path):
528528
xml = """
529529
<data>
530530
<row sides=" 4 ">
@@ -551,6 +551,7 @@ def test_whitespace(parser):
551551

552552
df_iter = read_xml_iterparse(
553553
xml,
554+
tmp_path,
554555
parser=parser,
555556
iterparse={"row": ["sides", "shape", "degrees"]},
556557
dtype="string",
@@ -599,7 +600,7 @@ def test_bad_xpath_lxml(xml_books):
599600
# NAMESPACE
600601

601602

602-
def test_default_namespace(parser):
603+
def test_default_namespace(parser, tmp_path):
603604
df_nmsp = read_xml(
604605
StringIO(xml_default_nmsp),
605606
xpath=".//ns:row",
@@ -609,6 +610,7 @@ def test_default_namespace(parser):
609610

610611
df_iter = read_xml_iterparse(
611612
xml_default_nmsp,
613+
tmp_path,
612614
parser=parser,
613615
iterparse={"row": ["shape", "degrees", "sides"]},
614616
)
@@ -625,15 +627,18 @@ def test_default_namespace(parser):
625627
tm.assert_frame_equal(df_iter, df_expected)
626628

627629

628-
def test_prefix_namespace(parser):
630+
def test_prefix_namespace(parser, tmp_path):
629631
df_nmsp = read_xml(
630632
StringIO(xml_prefix_nmsp),
631633
xpath=".//doc:row",
632634
namespaces={"doc": "http://example.com"},
633635
parser=parser,
634636
)
635637
df_iter = read_xml_iterparse(
636-
xml_prefix_nmsp, parser=parser, iterparse={"row": ["shape", "degrees", "sides"]}
638+
xml_prefix_nmsp,
639+
tmp_path,
640+
parser=parser,
641+
iterparse={"row": ["shape", "degrees", "sides"]},
637642
)
638643

639644
df_expected = DataFrame(
@@ -820,7 +825,7 @@ def test_empty_elems_only(parser):
820825
read_xml(StringIO(xml), xpath="./row", elems_only=True, parser=parser)
821826

822827

823-
def test_attribute_centric_xml():
828+
def test_attribute_centric_xml(tmp_path):
824829
pytest.importorskip("lxml")
825830
xml = """\
826831
<?xml version="1.0" encoding="UTF-8"?>
@@ -845,9 +850,11 @@ def test_attribute_centric_xml():
845850
df_lxml = read_xml(StringIO(xml), xpath=".//station")
846851
df_etree = read_xml(StringIO(xml), xpath=".//station", parser="etree")
847852

848-
df_iter_lx = read_xml_iterparse(xml, iterparse={"station": ["Name", "coords"]})
853+
df_iter_lx = read_xml_iterparse(
854+
xml, tmp_path, iterparse={"station": ["Name", "coords"]}
855+
)
849856
df_iter_et = read_xml_iterparse(
850-
xml, parser="etree", iterparse={"station": ["Name", "coords"]}
857+
xml, tmp_path, parser="etree", iterparse={"station": ["Name", "coords"]}
851858
)
852859

853860
tm.assert_frame_equal(df_lxml, df_etree)
@@ -882,7 +889,7 @@ def test_names_option_output(xml_books, parser):
882889
tm.assert_frame_equal(df_iter, df_expected)
883890

884891

885-
def test_repeat_names(parser):
892+
def test_repeat_names(parser, tmp_path):
886893
xml = """\
887894
<shapes>
888895
<shape type="2D">
@@ -903,6 +910,7 @@ def test_repeat_names(parser):
903910

904911
df_iter = read_xml_iterparse(
905912
xml,
913+
tmp_path,
906914
parser=parser,
907915
iterparse={"shape": ["type", "name", "type"]},
908916
names=["type_dim", "shape", "type_edge"],
@@ -920,7 +928,7 @@ def test_repeat_names(parser):
920928
tm.assert_frame_equal(df_iter, df_expected)
921929

922930

923-
def test_repeat_values_new_names(parser):
931+
def test_repeat_values_new_names(parser, tmp_path):
924932
xml = """\
925933
<shapes>
926934
<shape>
@@ -946,6 +954,7 @@ def test_repeat_values_new_names(parser):
946954

947955
df_iter = read_xml_iterparse(
948956
xml,
957+
tmp_path,
949958
parser=parser,
950959
iterparse={"shape": ["name", "family"]},
951960
names=["name", "group"],
@@ -962,7 +971,7 @@ def test_repeat_values_new_names(parser):
962971
tm.assert_frame_equal(df_iter, df_expected)
963972

964973

965-
def test_repeat_elements(parser):
974+
def test_repeat_elements(parser, tmp_path):
966975
xml = """\
967976
<shapes>
968977
<shape>
@@ -993,6 +1002,7 @@ def test_repeat_elements(parser):
9931002

9941003
df_iter = read_xml_iterparse(
9951004
xml,
1005+
tmp_path,
9961006
parser=parser,
9971007
iterparse={"shape": ["value", "value", "value", "value"]},
9981008
names=["name", "family", "degrees", "sides"],
@@ -1443,19 +1453,19 @@ def test_url_path_error(parser, httpserver, xml_file):
14431453
)
14441454

14451455

1446-
def test_compression_error(parser, compression_only):
1447-
with tm.ensure_clean(filename="geom_xml.zip") as path:
1448-
geom_df.to_xml(path, parser=parser, compression=compression_only)
1456+
def test_compression_error(parser, compression_only, tmp_path):
1457+
path = tmp_path / "geom_xml.zip"
1458+
geom_df.to_xml(path, parser=parser, compression=compression_only)
14491459

1450-
with pytest.raises(
1451-
ParserError, match=("iterparse is designed for large XML files")
1452-
):
1453-
read_xml(
1454-
path,
1455-
parser=parser,
1456-
iterparse={"row": ["shape", "degrees", "sides", "date"]},
1457-
compression=compression_only,
1458-
)
1460+
with pytest.raises(
1461+
ParserError, match=("iterparse is designed for large XML files")
1462+
):
1463+
read_xml(
1464+
path,
1465+
parser=parser,
1466+
iterparse={"row": ["shape", "degrees", "sides", "date"]},
1467+
compression=compression_only,
1468+
)
14591469

14601470

14611471
def test_wrong_dict_type(xml_books, parser):
@@ -1474,7 +1484,7 @@ def test_wrong_dict_value(xml_books, parser):
14741484
read_xml(xml_books, parser=parser, iterparse={"book": "category"})
14751485

14761486

1477-
def test_bad_xml(parser):
1487+
def test_bad_xml(parser, tmp_path):
14781488
bad_xml = """\
14791489
<?xml version='1.0' encoding='utf-8'?>
14801490
<row>
@@ -1496,25 +1506,23 @@ def test_bad_xml(parser):
14961506
<date>2022-01-01</date>
14971507
</row>
14981508
"""
1499-
with tm.ensure_clean(filename="bad.xml") as path:
1500-
with open(path, "w", encoding="utf-8") as f:
1501-
f.write(bad_xml)
1509+
path = tmp_path / "bad.xml"
1510+
with open(path, "w", encoding="utf-8") as f:
1511+
f.write(bad_xml)
15021512

1503-
with pytest.raises(
1504-
SyntaxError,
1505-
match=(
1506-
"Extra content at the end of the document|junk after document element"
1507-
),
1508-
):
1509-
read_xml(
1510-
path,
1511-
parser=parser,
1512-
parse_dates=["date"],
1513-
iterparse={"row": ["shape", "degrees", "sides", "date"]},
1514-
)
1513+
with pytest.raises(
1514+
SyntaxError,
1515+
match=("Extra content at the end of the document|junk after document element"),
1516+
):
1517+
read_xml(
1518+
path,
1519+
parser=parser,
1520+
parse_dates=["date"],
1521+
iterparse={"row": ["shape", "degrees", "sides", "date"]},
1522+
)
15151523

15161524

1517-
def test_comment(parser):
1525+
def test_comment(parser, tmp_path):
15181526
xml = """\
15191527
<!-- comment before root -->
15201528
<shapes>
@@ -1535,7 +1543,7 @@ def test_comment(parser):
15351543
df_xpath = read_xml(StringIO(xml), xpath=".//shape", parser=parser)
15361544

15371545
df_iter = read_xml_iterparse(
1538-
xml, parser=parser, iterparse={"shape": ["name", "type"]}
1546+
xml, tmp_path, parser=parser, iterparse={"shape": ["name", "type"]}
15391547
)
15401548

15411549
df_expected = DataFrame(
@@ -1549,7 +1557,7 @@ def test_comment(parser):
15491557
tm.assert_frame_equal(df_iter, df_expected)
15501558

15511559

1552-
def test_dtd(parser):
1560+
def test_dtd(parser, tmp_path):
15531561
xml = """\
15541562
<?xml version="1.0" encoding="UTF-8"?>
15551563
<!DOCTYPE non-profits [
@@ -1571,7 +1579,7 @@ def test_dtd(parser):
15711579
df_xpath = read_xml(StringIO(xml), xpath=".//shape", parser=parser)
15721580

15731581
df_iter = read_xml_iterparse(
1574-
xml, parser=parser, iterparse={"shape": ["name", "type"]}
1582+
xml, tmp_path, parser=parser, iterparse={"shape": ["name", "type"]}
15751583
)
15761584

15771585
df_expected = DataFrame(
@@ -1585,7 +1593,7 @@ def test_dtd(parser):
15851593
tm.assert_frame_equal(df_iter, df_expected)
15861594

15871595

1588-
def test_processing_instruction(parser):
1596+
def test_processing_instruction(parser, tmp_path):
15891597
xml = """\
15901598
<?xml version="1.0" encoding="UTF-8"?>
15911599
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
@@ -1607,7 +1615,7 @@ def test_processing_instruction(parser):
16071615
df_xpath = read_xml(StringIO(xml), xpath=".//shape", parser=parser)
16081616

16091617
df_iter = read_xml_iterparse(
1610-
xml, parser=parser, iterparse={"shape": ["name", "type"]}
1618+
xml, tmp_path, parser=parser, iterparse={"shape": ["name", "type"]}
16111619
)
16121620

16131621
df_expected = DataFrame(
@@ -1913,27 +1921,26 @@ def test_online_stylesheet():
19131921
# COMPRESSION
19141922

19151923

1916-
def test_compression_read(parser, compression_only):
1917-
with tm.ensure_clean() as comp_path:
1918-
geom_df.to_xml(
1919-
comp_path, index=False, parser=parser, compression=compression_only
1920-
)
1924+
def test_compression_read(parser, compression_only, tmp_path):
1925+
comp_path = tmp_path / "test.xml"
1926+
geom_df.to_xml(comp_path, index=False, parser=parser, compression=compression_only)
19211927

1922-
df_xpath = read_xml(comp_path, parser=parser, compression=compression_only)
1928+
df_xpath = read_xml(comp_path, parser=parser, compression=compression_only)
19231929

1924-
df_iter = read_xml_iterparse_comp(
1925-
comp_path,
1926-
compression_only,
1927-
parser=parser,
1928-
iterparse={"row": ["shape", "degrees", "sides"]},
1929-
compression=compression_only,
1930-
)
1930+
df_iter = read_xml_iterparse_comp(
1931+
comp_path,
1932+
compression_only,
1933+
tmp_path,
1934+
parser=parser,
1935+
iterparse={"row": ["shape", "degrees", "sides"]},
1936+
compression=compression_only,
1937+
)
19311938

19321939
tm.assert_frame_equal(df_xpath, geom_df)
19331940
tm.assert_frame_equal(df_iter, geom_df)
19341941

19351942

1936-
def test_wrong_compression(parser, compression, compression_only):
1943+
def test_wrong_compression(parser, compression, compression_only, tmp_path):
19371944
actual_compression = compression
19381945
attempted_compression = compression_only
19391946

@@ -1954,17 +1961,17 @@ def test_wrong_compression(parser, compression, compression_only):
19541961
errors["xz"] = (LZMAError, "Input format not supported by decoder")
19551962
error_cls, error_str = errors[attempted_compression]
19561963

1957-
with tm.ensure_clean() as path:
1958-
geom_df.to_xml(path, parser=parser, compression=actual_compression)
1964+
path = tmp_path / "test.xml"
1965+
geom_df.to_xml(path, parser=parser, compression=actual_compression)
19591966

1960-
with pytest.raises(error_cls, match=error_str):
1961-
read_xml(path, parser=parser, compression=attempted_compression)
1967+
with pytest.raises(error_cls, match=error_str):
1968+
read_xml(path, parser=parser, compression=attempted_compression)
19621969

19631970

1964-
def test_unsupported_compression(parser):
1971+
def test_unsupported_compression(parser, tmp_path):
1972+
path = tmp_path / "test.xml"
19651973
with pytest.raises(ValueError, match="Unrecognized compression type"):
1966-
with tm.ensure_clean() as path:
1967-
read_xml(path, parser=parser, compression="7z")
1974+
read_xml(path, parser=parser, compression="7z")
19681975

19691976

19701977
# STORAGE OPTIONS

0 commit comments

Comments
 (0)