[Python-checkins] r85499 - in python/branches/release27-maint: Lib/base64.py Lib/mimetypes.py Lib/sysconfig.py Lib/test/test_argparse.py Lib/test/test_xml_etree.py

antoine.pitrou python-checkins at python.org
Thu Oct 14 23:22:52 CEST 2010


Author: antoine.pitrou
Date: Thu Oct 14 23:22:52 2010
New Revision: 85499

Log:
Merged revisions 85497 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85497 | antoine.pitrou | 2010-10-14 23:15:17 +0200 (jeu., 14 oct. 2010) | 3 lines
  
  Explicitly close some files (from issue #10093)
........


Modified:
   python/branches/release27-maint/   (props changed)
   python/branches/release27-maint/Lib/base64.py
   python/branches/release27-maint/Lib/mimetypes.py
   python/branches/release27-maint/Lib/sysconfig.py
   python/branches/release27-maint/Lib/test/test_argparse.py
   python/branches/release27-maint/Lib/test/test_xml_etree.py

Modified: python/branches/release27-maint/Lib/base64.py
==============================================================================
--- python/branches/release27-maint/Lib/base64.py	(original)
+++ python/branches/release27-maint/Lib/base64.py	Thu Oct 14 23:22:52 2010
@@ -343,7 +343,8 @@
         if o == '-u': func = decode
         if o == '-t': test1(); return
     if args and args[0] != '-':
-        func(open(args[0], 'rb'), sys.stdout)
+        with open(args[0], 'rb') as f:
+            func(f, sys.stdout)
     else:
         func(sys.stdin, sys.stdout)
 

Modified: python/branches/release27-maint/Lib/mimetypes.py
==============================================================================
--- python/branches/release27-maint/Lib/mimetypes.py	(original)
+++ python/branches/release27-maint/Lib/mimetypes.py	Thu Oct 14 23:22:52 2010
@@ -199,9 +199,8 @@
         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):
         """
@@ -356,7 +355,7 @@
         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]

Modified: python/branches/release27-maint/Lib/sysconfig.py
==============================================================================
--- python/branches/release27-maint/Lib/sysconfig.py	(original)
+++ python/branches/release27-maint/Lib/sysconfig.py	Thu Oct 14 23:22:52 2010
@@ -289,7 +289,8 @@
     # 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, e:
         msg = "invalid Python installation: unable to open %s" % config_h
         if hasattr(e, "strerror"):

Modified: python/branches/release27-maint/Lib/test/test_argparse.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_argparse.py	(original)
+++ python/branches/release27-maint/Lib/test/test_argparse.py	Thu Oct 14 23:22:52 2010
@@ -4171,7 +4171,8 @@
     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__)

Modified: python/branches/release27-maint/Lib/test/test_xml_etree.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_xml_etree.py	(original)
+++ python/branches/release27-maint/Lib/test/test_xml_etree.py	Thu Oct 14 23:22:52 2010
@@ -586,10 +586,13 @@
        <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>
@@ -598,7 +601,7 @@
     </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>
@@ -608,7 +611,7 @@
 
     >>> 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>
@@ -711,7 +714,8 @@
     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'
 
@@ -763,6 +767,8 @@
     """
     Test parser w. custom builder.
 
+    >>> with open(SIMPLE_XMLFILE) as f:
+    ...     data = f.read()
     >>> class Builder:
     ...     def start(self, tag, attrib):
     ...         print "start", tag
@@ -772,7 +778,7 @@
     ...         pass
     >>> builder = Builder()
     >>> parser = ET.XMLParser(target=builder)
-    >>> parser.feed(open(SIMPLE_XMLFILE, "r").read())
+    >>> parser.feed(data)
     start root
     start element
     end element
@@ -782,6 +788,8 @@
     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
@@ -795,7 +803,7 @@
     ...         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
@@ -813,7 +821,8 @@
     """
     Test Element.getchildren()
 
-    >>> tree = ET.parse(open(SIMPLE_XMLFILE, "r"))
+    >>> with open(SIMPLE_XMLFILE, "r") as f:
+    ...     tree = ET.parse(f)
     >>> for elem in tree.getroot().iter():
     ...     summarize_list(elem.getchildren())
     ['element', 'element', 'empty-element']


More information about the Python-checkins mailing list