summaryrefslogtreecommitdiffstats
path: root/Lib
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
parentd9f57630fed16bd1e97737f8afc22b5cc6645769 (diff)
downloadcpython-b86680e299d7429532518a496031cb96aa2ea659.zip
cpython-b86680e299d7429532518a496031cb96aa2ea659.tar.gz
cpython-b86680e299d7429532518a496031cb96aa2ea659.tar.bz2
Explicitly close some files (from issue #10093)
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/base64.py3
-rw-r--r--Lib/mimetypes.py7
-rw-r--r--Lib/sysconfig.py3
-rw-r--r--Lib/test/test_argparse.py3
-rw-r--r--Lib/test/test_xml_etree.py23
5 files changed, 25 insertions, 14 deletions
diff --git a/Lib/base64.py b/Lib/base64.py
index faa3836..af7cf64 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -383,7 +383,8 @@ def main():
if o == '-u': func = decode
if o == '-t': test(); return
if args and args[0] != '-':
- func(open(args[0], 'rb'), sys.stdout.buffer)
+ with open(args[0], 'rb') as f:
+ func(f, sys.stdout.buffer)
else:
func(sys.stdin.buffer, sys.stdout.buffer)
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index 3c9883c..d5d0fc3 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -199,9 +199,8 @@ class MimeTypes:
list of standard types, else to the list of non-standard
types.
"""
- fp = open(filename)
- self.readfp(fp, strict)
- fp.close()
+ with open(filename) as fp:
+ self.readfp(fp, strict)
def readfp(self, fp, strict=True):
"""
@@ -348,7 +347,7 @@ def init(files=None):
files = knownfiles
for file in files:
if os.path.isfile(file):
- db.readfp(open(file))
+ db.read(file)
encodings_map = db.encodings_map
suffix_map = db.suffix_map
types_map = db.types_map[True]
diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py
index f7c647c..9b804e6 100644
--- a/Lib/sysconfig.py
+++ b/Lib/sysconfig.py
@@ -333,7 +333,8 @@ def _init_posix(vars):
# load the installed pyconfig.h:
config_h = get_config_h_filename()
try:
- parse_config_h(open(config_h), vars)
+ with open(config_h) as f:
+ parse_config_h(f, vars)
except IOError as e:
msg = "invalid Python installation: unable to open %s" % config_h
if hasattr(e, "strerror"):
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index cf0f8e4..f01c65f 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4160,7 +4160,8 @@ class TestEncoding(TestCase):
def _test_module_encoding(self, path):
path, _ = os.path.splitext(path)
path += ".py"
- codecs.open(path, 'r', 'utf8').read()
+ with codecs.open(path, 'r', 'utf8') as f:
+ f.read()
def test_argparse_module_encoding(self):
self._test_module_encoding(argparse.__file__)
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']