Skip to content

Commit f43b23f

Browse files
committed
Add pathlib.Path support to XML #4455.
Parsing alraedy worked with pathlib.Path but saving didn't.
1 parent 26e267c commit f43b23f

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

atest/robot/standard_libraries/xml/save_xml.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Save Non-ASCII XML
2424
Save Non-ASCII XML Using Custom Encoding
2525
Check Test Case ${TESTNAME}
2626

27+
Save to `pathlib.Path`
28+
Check Test Case ${TESTNAME}
29+
2730
Save to Invalid File
2831
Check Test Case ${TESTNAME}
2932

atest/robot/standard_libraries/xml/save_xml_with_lxml.robot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Save Non-ASCII XML
2525
Save Non-ASCII XML Using Custom Encoding
2626
Check Test Case ${TESTNAME}
2727

28+
Save to `pathlib.Path`
29+
Check Test Case ${TESTNAME}
30+
2831
Save to Invalid File
2932
Check Test Case ${TESTNAME}
3033

atest/testdata/standard_libraries/xml/save_xml.robot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Save Non-ASCII XML Using Custom Encoding
3434
Save XML ${NON-ASCII} ${OUTPUT} ISO-8859-1
3535
XML Content Should Be ${NON-ASCII} ISO-8859-1
3636

37+
Save to `pathlib.Path`
38+
Save XML ${SIMPLE} ${{pathlib.Path($OUTPUT)}}
39+
XML Content Should Be ${SIMPLE}
40+
3741
Save to Invalid File
3842
[Documentation] FAIL REGEXP: (IOError|IsADirectoryError|PermissionError): .*
3943
Save XML ${SIMPLE} %{TEMPDIR}

atest/testdata/standard_libraries/xml/save_xml_with_lxml.robot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Save Non-ASCII XML Using Custom Encoding
3636
Save XML ${NON-ASCII} ${OUTPUT} ISO-8859-1
3737
XML Content Should Be ${NON-ASCII} ISO-8859-1
3838

39+
Save to `pathlib.Path`
40+
Save XML ${SIMPLE} ${{pathlib.Path($OUTPUT)}}
41+
XML Content Should Be ${SIMPLE SAVED}
42+
3943
Save to Invalid File
4044
[Documentation] FAIL REGEXP: (IOError|IsADirectoryError|PermissionError): .*
4145
Save XML ${SIMPLE} %{TEMPDIR}

src/robot/libraries/XML.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import copy
1717
import os
18-
import pathlib
1918
import re
2019

2120
try:
@@ -517,7 +516,7 @@ def parse_xml(self, source, keep_clark_notation=False, strip_namespaces=False):
517516
the whole structure. See `Parsing XML` section for more details and
518517
examples.
519518
"""
520-
if isinstance(source, pathlib.Path):
519+
if isinstance(source, os.PathLike):
521520
source = str(source)
522521
with ETSource(source) as source:
523522
tree = self.etree.parse(source)
@@ -589,7 +588,7 @@ def get_elements(self, source, xpath):
589588
| ${children} = | Get Elements | ${XML} | first/child |
590589
| Should Be Empty | ${children} | | |
591590
"""
592-
if is_string(source) or is_bytes(source) or isinstance(source, pathlib.Path):
591+
if isinstance(source, (str, bytes, os.PathLike)):
593592
source = self.parse_xml(source)
594593
finder = ElementFinder(self.etree, self.modern_etree, self.lxml_etree)
595594
return finder.find_all(source, xpath)
@@ -1349,7 +1348,8 @@ def save_xml(self, source, path, encoding='UTF-8'):
13491348
Use `Element To String` if you just need a string representation of
13501349
the element.
13511350
"""
1352-
path = os.path.abspath(path.replace('/', os.sep))
1351+
path = os.path.abspath(str(path) if isinstance(path, os.PathLike)
1352+
else path.replace('/', os.sep))
13531353
elem = self.get_element(source)
13541354
tree = self.etree.ElementTree(elem)
13551355
config = {'encoding': encoding}

0 commit comments

Comments
 (0)