[Python-checkins] cpython: modernize some modules' code by using with statement around open()

giampaolo.rodola python-checkins at python.org
Tue Feb 12 02:04:38 CET 2013


http://hg.python.org/cpython/rev/cb876235f29d
changeset:   82168:cb876235f29d
user:        Giampaolo Rodola' <g.rodola at gmail.com>
date:        Tue Feb 12 02:04:27 2013 +0100
summary:
  modernize some modules' code by using with statement around open()

files:
  Lib/argparse.py              |   5 +----
  Lib/cProfile.py              |   7 +++----
  Lib/http/cookiejar.py        |  15 +++------------
  Lib/keyword.py               |   5 ++---
  Lib/lib2to3/pgen2/grammar.py |  10 ++++------
  Lib/mailbox.py               |  18 ++++--------------
  Lib/pkgutil.py               |  17 ++++++++---------
  Lib/profile.py               |   7 +++----
  Lib/pstats.py                |  10 +++-------
  Lib/pydoc.py                 |   5 ++---
  Lib/site.py                  |   5 ++---
  Lib/symtable.py              |   3 ++-
  Lib/turtle.py                |  22 +++++++++++-----------
  Lib/xml/dom/expatbuilder.py  |  10 ++--------
  14 files changed, 50 insertions(+), 89 deletions(-)


diff --git a/Lib/argparse.py b/Lib/argparse.py
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -2010,16 +2010,13 @@
             # replace arguments referencing files with the file content
             else:
                 try:
-                    args_file = open(arg_string[1:])
-                    try:
+                    with open(arg_string[1:]) as args_file:
                         arg_strings = []
                         for arg_line in args_file.read().splitlines():
                             for arg in self.convert_arg_line_to_args(arg_line):
                                 arg_strings.append(arg)
                         arg_strings = self._read_args_from_files(arg_strings)
                         new_arg_strings.extend(arg_strings)
-                    finally:
-                        args_file.close()
                 except OSError:
                     err = _sys.exc_info()[1]
                     self.error(str(err))
diff --git a/Lib/cProfile.py b/Lib/cProfile.py
--- a/Lib/cProfile.py
+++ b/Lib/cProfile.py
@@ -77,10 +77,9 @@
 
     def dump_stats(self, file):
         import marshal
-        f = open(file, 'wb')
-        self.create_stats()
-        marshal.dump(self.stats, f)
-        f.close()
+        with open(file, 'wb') as f:
+            self.create_stats()
+            marshal.dump(self.stats, f)
 
     def create_stats(self):
         self.disable()
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -1761,11 +1761,8 @@
             if self.filename is not None: filename = self.filename
             else: raise ValueError(MISSING_FILENAME_TEXT)
 
-        f = open(filename)
-        try:
+        with open(filename) as f:
             self._really_load(f, filename, ignore_discard, ignore_expires)
-        finally:
-            f.close()
 
     def revert(self, filename=None,
                ignore_discard=False, ignore_expires=False):
@@ -1856,15 +1853,12 @@
             if self.filename is not None: filename = self.filename
             else: raise ValueError(MISSING_FILENAME_TEXT)
 
-        f = open(filename, "w")
-        try:
+        with open(filename, "w") as f:
             # There really isn't an LWP Cookies 2.0 format, but this indicates
             # that there is extra information in here (domain_dot and
             # port_spec) while still being compatible with libwww-perl, I hope.
             f.write("#LWP-Cookies-2.0\n")
             f.write(self.as_lwp_str(ignore_discard, ignore_expires))
-        finally:
-            f.close()
 
     def _really_load(self, f, filename, ignore_discard, ignore_expires):
         magic = f.readline()
@@ -2055,8 +2049,7 @@
             if self.filename is not None: filename = self.filename
             else: raise ValueError(MISSING_FILENAME_TEXT)
 
-        f = open(filename, "w")
-        try:
+        with open(filename, "w") as f:
             f.write(self.header)
             now = time.time()
             for cookie in self:
@@ -2085,5 +2078,3 @@
                     "\t".join([cookie.domain, initial_dot, cookie.path,
                                secure, expires, name, value])+
                     "\n")
-        finally:
-            f.close()
diff --git a/Lib/keyword.py b/Lib/keyword.py
--- a/Lib/keyword.py
+++ b/Lib/keyword.py
@@ -85,9 +85,8 @@
         sys.exit(1)
 
     # write the output file
-    fp = open(optfile, 'w')
-    fp.write(''.join(format))
-    fp.close()
+    with open(optfile, 'w') as fp:
+        fp.write(''.join(format))
 
 if __name__ == "__main__":
     main()
diff --git a/Lib/lib2to3/pgen2/grammar.py b/Lib/lib2to3/pgen2/grammar.py
--- a/Lib/lib2to3/pgen2/grammar.py
+++ b/Lib/lib2to3/pgen2/grammar.py
@@ -86,15 +86,13 @@
 
     def dump(self, filename):
         """Dump the grammar tables to a pickle file."""
-        f = open(filename, "wb")
-        pickle.dump(self.__dict__, f, 2)
-        f.close()
+        with open(filename, "wb") as f:
+            pickle.dump(self.__dict__, f, 2)
 
     def load(self, filename):
         """Load the grammar tables from a pickle file."""
-        f = open(filename, "rb")
-        d = pickle.load(f)
-        f.close()
+        with open(filename, "rb") as f:
+            d = pickle.load(f)
         self.__dict__.update(d)
 
     def copy(self):
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -366,14 +366,11 @@
     def get_message(self, key):
         """Return a Message representation or raise a KeyError."""
         subpath = self._lookup(key)
-        f = open(os.path.join(self._path, subpath), 'rb')
-        try:
+        with open(os.path.join(self._path, subpath), 'rb') as f:
             if self._factory:
                 msg = self._factory(f)
             else:
                 msg = MaildirMessage(f)
-        finally:
-            f.close()
         subdir, name = os.path.split(subpath)
         msg.set_subdir(subdir)
         if self.colon in name:
@@ -383,11 +380,8 @@
 
     def get_bytes(self, key):
         """Return a bytes representation or raise a KeyError."""
-        f = open(os.path.join(self._path, self._lookup(key)), 'rb')
-        try:
+        with open(os.path.join(self._path, self._lookup(key)), 'rb') as f:
             return f.read().replace(linesep, b'\n')
-        finally:
-            f.close()
 
     def get_file(self, key):
         """Return a file-like representation or raise a KeyError."""
@@ -1033,7 +1027,7 @@
                 raise KeyError('No message with key: %s' % key)
             else:
                 raise
-        try:
+        with f:
             if self._locked:
                 _lock_file(f)
             try:
@@ -1041,8 +1035,6 @@
             finally:
                 if self._locked:
                     _unlock_file(f)
-        finally:
-            f.close()
         for name, key_list in self.get_sequences().items():
             if key in key_list:
                 msg.add_sequence(name)
@@ -1060,7 +1052,7 @@
                 raise KeyError('No message with key: %s' % key)
             else:
                 raise
-        try:
+        with f:
             if self._locked:
                 _lock_file(f)
             try:
@@ -1068,8 +1060,6 @@
             finally:
                 if self._locked:
                     _unlock_file(f)
-        finally:
-            f.close()
 
     def get_file(self, key):
         """Return a file-like representation or raise a KeyError."""
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -349,9 +349,8 @@
                     self.file.close()
             elif mod_type==imp.PY_COMPILED:
                 if os.path.exists(self.filename[:-1]):
-                    f = open(self.filename[:-1], 'r')
-                    self.source = f.read()
-                    f.close()
+                    with open(self.filename[:-1], 'r') as f:
+                        self.source = f.read()
             elif mod_type==imp.PKG_DIRECTORY:
                 self.source = self._get_delegate().get_source()
         return self.source
@@ -591,12 +590,12 @@
                 sys.stderr.write("Can't open %s: %s\n" %
                                  (pkgfile, msg))
             else:
-                for line in f:
-                    line = line.rstrip('\n')
-                    if not line or line.startswith('#'):
-                        continue
-                    path.append(line) # Don't check for existence!
-                f.close()
+                with f:
+                    for line in f:
+                        line = line.rstrip('\n')
+                        if not line or line.startswith('#'):
+                            continue
+                        path.append(line) # Don't check for existence!
 
     return path
 
diff --git a/Lib/profile.py b/Lib/profile.py
--- a/Lib/profile.py
+++ b/Lib/profile.py
@@ -373,10 +373,9 @@
                   print_stats()
 
     def dump_stats(self, file):
-        f = open(file, 'wb')
-        self.create_stats()
-        marshal.dump(self.stats, f)
-        f.close()
+        with open(file, 'wb') as f:
+            self.create_stats()
+            marshal.dump(self.stats, f)
 
     def create_stats(self):
         self.simulate_cmd_complete()
diff --git a/Lib/pstats.py b/Lib/pstats.py
--- a/Lib/pstats.py
+++ b/Lib/pstats.py
@@ -93,9 +93,8 @@
             self.stats = {}
             return
         elif isinstance(arg, str):
-            f = open(arg, 'rb')
-            self.stats = marshal.load(f)
-            f.close()
+            with open(arg, 'rb') as f:
+                self.stats = marshal.load(f)
             try:
                 file_stats = os.stat(arg)
                 arg = time.ctime(file_stats.st_mtime) + "    " + arg
@@ -149,11 +148,8 @@
 
     def dump_stats(self, filename):
         """Write the profile data to a file we know how to load back."""
-        f = open(filename, 'wb')
-        try:
+        with open(filename, 'wb') as f:
             marshal.dump(self.stats, f)
-        finally:
-            f.close()
 
     # list the tuple indices and directions for sorting,
     # along with some printable description
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1426,9 +1426,8 @@
     """Page through text by invoking a program on a temporary file."""
     import tempfile
     filename = tempfile.mktemp()
-    file = open(filename, 'w')
-    file.write(text)
-    file.close()
+    with open(filename, 'w') as file:
+        file.write(text)
     try:
         os.system(cmd + ' "' + filename + '"')
     finally:
diff --git a/Lib/site.py b/Lib/site.py
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -384,9 +384,8 @@
             for filename in self.__files:
                 filename = os.path.join(dir, filename)
                 try:
-                    fp = open(filename, "r")
-                    data = fp.read()
-                    fp.close()
+                    with open(filename, "r") as fp:
+                        data = fp.read()
                     break
                 except OSError:
                     pass
diff --git a/Lib/symtable.py b/Lib/symtable.py
--- a/Lib/symtable.py
+++ b/Lib/symtable.py
@@ -235,7 +235,8 @@
 
 if __name__ == "__main__":
     import os, sys
-    src = open(sys.argv[0]).read()
+    with open(sys.argv[0]) as f:
+        src = f.read()
     mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
     for ident in mod.get_identifiers():
         info = mod.lookup(ident)
diff --git a/Lib/turtle.py b/Lib/turtle.py
--- a/Lib/turtle.py
+++ b/Lib/turtle.py
@@ -3843,18 +3843,18 @@
         key = "Turtle."+methodname
         docsdict[key] = eval(key).__doc__
 
-    f = open("%s.py" % filename,"w")
-    keys = sorted([x for x in docsdict.keys()
-                        if x.split('.')[1] not in _alias_list])
-    f.write('docsdict = {\n\n')
-    for key in keys[:-1]:
+    with open("%s.py" % filename,"w") as f:
+        keys = sorted([x for x in docsdict.keys()
+                            if x.split('.')[1] not in _alias_list])
+        f.write('docsdict = {\n\n')
+        for key in keys[:-1]:
+            f.write('%s :\n' % repr(key))
+            f.write('        """%s\n""",\n\n' % docsdict[key])
+        key = keys[-1]
         f.write('%s :\n' % repr(key))
-        f.write('        """%s\n""",\n\n' % docsdict[key])
-    key = keys[-1]
-    f.write('%s :\n' % repr(key))
-    f.write('        """%s\n"""\n\n' % docsdict[key])
-    f.write("}\n")
-    f.close()
+        f.write('        """%s\n"""\n\n' % docsdict[key])
+        f.write("}\n")
+        f.close()
 
 def read_docstrings(lang):
     """Read in docstrings from lang-specific docstring dictionary.
diff --git a/Lib/xml/dom/expatbuilder.py b/Lib/xml/dom/expatbuilder.py
--- a/Lib/xml/dom/expatbuilder.py
+++ b/Lib/xml/dom/expatbuilder.py
@@ -905,11 +905,8 @@
         builder = ExpatBuilder()
 
     if isinstance(file, str):
-        fp = open(file, 'rb')
-        try:
+        with open(file, 'rb') as fp:
             result = builder.parseFile(fp)
-        finally:
-            fp.close()
     else:
         result = builder.parseFile(file)
     return result
@@ -939,11 +936,8 @@
         builder = FragmentBuilder(context)
 
     if isinstance(file, str):
-        fp = open(file, 'rb')
-        try:
+        with open(file, 'rb') as fp:
             result = builder.parseFile(fp)
-        finally:
-            fp.close()
     else:
         result = builder.parseFile(file)
     return result

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list