summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_xml_etree.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-14 21:15:17 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-14 21:15:17 (GMT)
commitb86680e299d7429532518a496031cb96aa2ea659 (patch)
treec908511dfb569e7a640ed65cc412f1556f0ecc12 /Lib/test/test_xml_etree.py
parentd9f57630fed16bd1e97737f8afc22b5cc6645769 (diff)
downloadcpython-b86680e299d7429532518a496031cb96aa2ea659.zip
cpython-b86680e299d7429532518a496031cb96aa2ea659.tar.gz
cpython-b86680e299d7429532518a496031cb96aa2ea659.tar.bz2
Explicitly close some files (from issue #10093)
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r--Lib/test/test_xml_etree.py23
1 files changed, 16 insertions, 7 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 8079b84..7914d1f 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -602,10 +602,13 @@ def parsefile():
<ns0:empty-element />
</ns0:root>
+ >>> with open(SIMPLE_XMLFILE) as f:
+ ... data = f.read()
+
>>> parser = ET.XMLParser()
>>> parser.version # doctest: +ELLIPSIS
'Expat ...'
- >>> parser.feed(open(SIMPLE_XMLFILE).read())
+ >>> parser.feed(data)
>>> print(serialize(parser.close()))
<root>
<element key="value">text</element>
@@ -614,7 +617,7 @@ def parsefile():
</root>
>>> parser = ET.XMLTreeBuilder() # 1.2 compatibility
- >>> parser.feed(open(SIMPLE_XMLFILE).read())
+ >>> parser.feed(data)
>>> print(serialize(parser.close()))
<root>
<element key="value">text</element>
@@ -624,7 +627,7 @@ def parsefile():
>>> target = ET.TreeBuilder()
>>> parser = ET.XMLParser(target=target)
- >>> parser.feed(open(SIMPLE_XMLFILE).read())
+ >>> parser.feed(data)
>>> print(serialize(parser.close()))
<root>
<element key="value">text</element>
@@ -727,7 +730,8 @@ def iterparse():
end-ns None
>>> events = ("start", "end", "bogus")
- >>> context = iterparse(SIMPLE_XMLFILE, events)
+ >>> with open(SIMPLE_XMLFILE, "rb") as f:
+ ... iterparse(f, events)
Traceback (most recent call last):
ValueError: unknown event 'bogus'
@@ -779,6 +783,8 @@ def custom_builder():
"""
Test parser w. custom builder.
+ >>> with open(SIMPLE_XMLFILE) as f:
+ ... data = f.read()
>>> class Builder:
... def start(self, tag, attrib):
... print("start", tag)
@@ -788,7 +794,7 @@ def custom_builder():
... pass
>>> builder = Builder()
>>> parser = ET.XMLParser(target=builder)
- >>> parser.feed(open(SIMPLE_XMLFILE, "r").read())
+ >>> parser.feed(data)
start root
start element
end element
@@ -798,6 +804,8 @@ def custom_builder():
end empty-element
end root
+ >>> with open(SIMPLE_NS_XMLFILE) as f:
+ ... data = f.read()
>>> class Builder:
... def start(self, tag, attrib):
... print("start", tag)
@@ -811,7 +819,7 @@ def custom_builder():
... print("comment", repr(data))
>>> builder = Builder()
>>> parser = ET.XMLParser(target=builder)
- >>> parser.feed(open(SIMPLE_NS_XMLFILE, "r").read())
+ >>> parser.feed(data)
pi pi 'data'
comment ' comment '
start {namespace}root
@@ -829,7 +837,8 @@ def getchildren():
"""
Test Element.getchildren()
- >>> tree = ET.parse(open(SIMPLE_XMLFILE, "rb"))
+ >>> with open(SIMPLE_XMLFILE, "rb") as f:
+ ... tree = ET.parse(f)
>>> for elem in tree.getroot().iter():
... summarize_list(elem.getchildren())
['element', 'element', 'empty-element']