[Python-checkins] distutils2: Wrap I/O operations in try/except blocks to ensure handles are closed

tarek.ziade python-checkins at python.org
Sat Jul 3 16:19:06 CEST 2010


tarek.ziade pushed c61f4eff8a25 to distutils2:

http://hg.python.org/distutils2/rev/c61f4eff8a25
changeset:   244:c61f4eff8a25
user:        ?ric Araujo <merwok at netwok.org>
date:        Sun Jun 27 02:42:50 2010 +0200
summary:     Wrap I/O operations in try/except blocks to ensure handles are closed
files:       src/distutils2/_backport/sysconfig.py, src/distutils2/_backport/tarfile.py, src/distutils2/_backport/tests/test_pkgutil.py, src/distutils2/compiler/ccompiler.py, src/distutils2/compiler/emxccompiler.py, src/distutils2/mkpkg.py, src/distutils2/tests/test_build_py.py, src/distutils2/tests/test_build_scripts.py, src/distutils2/tests/test_dist.py, src/distutils2/tests/test_install_scripts.py, src/distutils2/tests/test_metadata.py, src/distutils2/tests/test_msvc9compiler.py, src/distutils2/util.py

diff --git a/src/distutils2/_backport/sysconfig.py b/src/distutils2/_backport/sysconfig.py
--- a/src/distutils2/_backport/sysconfig.py
+++ b/src/distutils2/_backport/sysconfig.py
@@ -580,10 +580,11 @@
                 # behaviour.
                 pass
             else:
-                m = re.search(
-                        r'<key>ProductUserVisibleVersion</key>\s*' +
-                        r'<string>(.*?)</string>', f.read())
-                f.close()
+                try:
+                    m = re.search(r'<key>ProductUserVisibleVersion</key>\s*'
+                                  r'<string>(.*?)</string>', f.read())
+                finally:
+                    f.close()
                 if m is not None:
                     macrelease = '.'.join(m.group(1).split('.')[:2])
                 # else: fall back to the default behaviour
diff --git a/src/distutils2/_backport/tarfile.py b/src/distutils2/_backport/tarfile.py
--- a/src/distutils2/_backport/tarfile.py
+++ b/src/distutils2/_backport/tarfile.py
@@ -2003,8 +2003,10 @@
         # Append the tar header and data to the archive.
         if tarinfo.isreg():
             f = bltn_open(name, "rb")
-            self.addfile(tarinfo, f)
-            f.close()
+            try:
+                self.addfile(tarinfo, f)
+            finally:
+                f.close()
 
         elif tarinfo.isdir():
             self.addfile(tarinfo)
@@ -2214,9 +2216,11 @@
         """
         source = self.extractfile(tarinfo)
         target = bltn_open(targetpath, "wb")
-        copyfileobj(source, target)
-        source.close()
-        target.close()
+        try:
+            copyfileobj(source, target)
+        finally:
+            source.close()
+            target.close()
 
     def makeunknown(self, tarinfo, targetpath):
         """Make a file from a TarInfo object with an unknown type
@@ -2564,8 +2568,10 @@
        are able to handle, else return False.
     """
     try:
-        t = open(name)
-        t.close()
+        try:
+            t = open(name)
+        finally:
+            t.close()
         return True
     except TarError:
         return False
diff --git a/src/distutils2/_backport/tests/test_pkgutil.py b/src/distutils2/_backport/tests/test_pkgutil.py
--- a/src/distutils2/_backport/tests/test_pkgutil.py
+++ b/src/distutils2/_backport/tests/test_pkgutil.py
@@ -46,15 +46,22 @@
         os.mkdir(package_dir)
         # Empty init.py
         f = open(os.path.join(package_dir, '__init__.py'), "wb")
-        f.close()
+        try:
+            pass
+        finally:
+            f.close()
         # Resource files, res.txt, sub/res.txt
         f = open(os.path.join(package_dir, 'res.txt'), "wb")
-        f.write(RESOURCE_DATA)
-        f.close()
+        try:
+            f.write(RESOURCE_DATA)
+        finally:
+            f.close()
         os.mkdir(os.path.join(package_dir, 'sub'))
         f = open(os.path.join(package_dir, 'sub', 'res.txt'), "wb")
-        f.write(RESOURCE_DATA)
-        f.close()
+        try:
+            f.write(RESOURCE_DATA)
+        finally:
+            f.close()
 
         # Check we can read the resources
         res1 = pkgutil.get_data(pkg, 'res.txt')
@@ -74,13 +81,14 @@
         # Make a package with some resources
         zip_file = os.path.join(self.dirname, zip)
         z = zipfile.ZipFile(zip_file, 'w')
-
-        # Empty init.py
-        z.writestr(pkg + '/__init__.py', "")
-        # Resource files, res.txt, sub/res.txt
-        z.writestr(pkg + '/res.txt', RESOURCE_DATA)
-        z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
-        z.close()
+        try:
+            # Empty init.py
+            z.writestr(pkg + '/__init__.py', "")
+            # Resource files, res.txt, sub/res.txt
+            z.writestr(pkg + '/res.txt', RESOURCE_DATA)
+            z.writestr(pkg + '/sub/res.txt', RESOURCE_DATA)
+        finally:
+            z.close()
 
         # Check we can read the resources
         sys.path.insert(0, zip_file)
diff --git a/src/distutils2/compiler/ccompiler.py b/src/distutils2/compiler/ccompiler.py
--- a/src/distutils2/compiler/ccompiler.py
+++ b/src/distutils2/compiler/ccompiler.py
@@ -800,14 +800,16 @@
             library_dirs = []
         fd, fname = tempfile.mkstemp(".c", funcname, text=True)
         f = os.fdopen(fd, "w")
-        for incl in includes:
-            f.write("""#include "%s"\n""" % incl)
-        f.write("""\
+        try:
+            for incl in includes:
+                f.write("""#include "%s"\n""" % incl)
+            f.write("""\
 main (int argc, char **argv) {
     %s();
 }
 """ % funcname)
-        f.close()
+        finally:
+            f.close()
         try:
             objects = self.compile([fname], include_dirs=include_dirs)
         except CompileError:
diff --git a/src/distutils2/compiler/emxccompiler.py b/src/distutils2/compiler/emxccompiler.py
--- a/src/distutils2/compiler/emxccompiler.py
+++ b/src/distutils2/compiler/emxccompiler.py
@@ -272,8 +272,10 @@
         # It would probably better to read single lines to search.
         # But we do this only once, and it is fast enough
         f = open(fn)
-        s = f.read()
-        f.close()
+        try:
+            s = f.read()
+        finally:
+            f.close()
 
     except IOError, exc:
         # if we can't read this file, we cannot say it is wrong
diff --git a/src/distutils2/mkpkg.py b/src/distutils2/mkpkg.py
--- a/src/distutils2/mkpkg.py
+++ b/src/distutils2/mkpkg.py
@@ -717,14 +717,16 @@
 
     def inspectFile(self, path):
         fp = open(path, 'r')
-        for line in [ fp.readline() for x in range(10) ]:
-            m = re.match(r'^#!.*python((?P<major>\d)(\.\d+)?)?$', line)
-            if m:
-                if m.group('major') == '3':
-                    self.classifierDict['Programming Language :: Python :: 3'] = 1
-                else:
-                    self.classifierDict['Programming Language :: Python :: 2'] = 1
-        fp.close()
+        try:
+            for line in [ fp.readline() for x in range(10) ]:
+                m = re.match(r'^#!.*python((?P<major>\d)(\.\d+)?)?$', line)
+                if m:
+                    if m.group('major') == '3':
+                        self.classifierDict['Programming Language :: Python :: 3'] = 1
+                    else:
+                        self.classifierDict['Programming Language :: Python :: 2'] = 1
+        finally:
+            fp.close()
 
 
     def inspectDirectory(self):
@@ -885,38 +887,33 @@
         if os.path.exists('setup.py'): shutil.move('setup.py', 'setup.py.old')
 
         fp = open('setup.py', 'w')
-        fp.write('#!/usr/bin/env python\n\n')
-        fp.write('from distutils2.core import setup\n\n')
+        try:
+            fp.write('#!/usr/bin/env python\n\n')
+            fp.write('from distutils2.core import setup\n\n')
+            fp.write('setup(name=%s,\n' % repr(self.setupData['name']))
+            fp.write('      version=%s,\n' % repr(self.setupData['version']))
+            fp.write('      description=%s,\n'
+                    % repr(self.setupData['description']))
+            fp.write('      author=%s,\n' % repr(self.setupData['author']))
+            fp.write('      author_email=%s,\n'
+                    % repr(self.setupData['author_email']))
+            if self.setupData['url']:
+                fp.write('      url=%s,\n' % repr(self.setupData['url']))
+            if self.setupData['classifier']:
+                fp.write('      classifier=[\n')
+                for classifier in sorted(self.setupData['classifier'].keys()):
+                    fp.write('            %s,\n' % repr(classifier))
+                fp.write('         ],\n')
+            if self.setupData['packages']:
+                fp.write('      packages=%s,\n'
+                        % repr(self._dotted_packages(self.setupData['packages'])))
+                fp.write('      package_dir=%s,\n'
+                        % repr(self.setupData['packages']))
+            fp.write('      #scripts=[\'path/to/script\']\n')
 
-        fp.write('from sys import version\n')
-        fp.write('if version < \'2.2.3\':\n')
-        fp.write('    from distutils2.dist import DistributionMetadata\n')
-        fp.write('    DistributionMetadata.classifier = None\n')
-        fp.write('    DistributionMetadata.download_url = None\n')
-
-        fp.write('setup(name = %s,\n' % repr(self.setupData['name']))
-        fp.write('        version = %s,\n' % repr(self.setupData['version']))
-        fp.write('        description = %s,\n'
-                % repr(self.setupData['description']))
-        fp.write('        author = %s,\n' % repr(self.setupData['author']))
-        fp.write('        author_email = %s,\n'
-                % repr(self.setupData['author_email']))
-        if self.setupData['url']:
-            fp.write('        url = %s,\n' % repr(self.setupData['url']))
-        if self.setupData['classifier']:
-            fp.write('        classifier = [\n')
-            for classifier in sorted(self.setupData['classifier'].keys()):
-                fp.write('              %s,\n' % repr(classifier))
-            fp.write('           ],\n')
-        if self.setupData['packages']:
-            fp.write('        packages = %s,\n'
-                    % repr(self._dotted_packages(self.setupData['packages'])))
-            fp.write('        package_dir = %s,\n'
-                    % repr(self.setupData['packages']))
-        fp.write('        #scripts = [\'path/to/script\']\n')
-
-        fp.write('        )\n')
-        fp.close()
+            fp.write('      )\n')
+        finally:
+            fp.close()
         os.chmod('setup.py', 0755)
 
         print 'Wrote "setup.py".'
diff --git a/src/distutils2/tests/test_build_py.py b/src/distutils2/tests/test_build_py.py
--- a/src/distutils2/tests/test_build_py.py
+++ b/src/distutils2/tests/test_build_py.py
@@ -19,11 +19,15 @@
     def test_package_data(self):
         sources = self.mkdtemp()
         f = open(os.path.join(sources, "__init__.py"), "w")
-        f.write("# Pretend this is a package.")
-        f.close()
+        try:
+            f.write("# Pretend this is a package.")
+        finally:
+            f.close()
         f = open(os.path.join(sources, "README.txt"), "w")
-        f.write("Info about this package")
-        f.close()
+        try:
+            f.write("Info about this package")
+        finally:
+            f.close()
 
         destination = self.mkdtemp()
 
diff --git a/src/distutils2/tests/test_build_scripts.py b/src/distutils2/tests/test_build_scripts.py
--- a/src/distutils2/tests/test_build_scripts.py
+++ b/src/distutils2/tests/test_build_scripts.py
@@ -74,8 +74,10 @@
 
     def write_script(self, dir, name, text):
         f = open(os.path.join(dir, name), "w")
-        f.write(text)
-        f.close()
+        try:
+            f.write(text)
+        finally:
+            f.close()
 
     def test_version_int(self):
         source = self.mkdtemp()
diff --git a/src/distutils2/tests/test_dist.py b/src/distutils2/tests/test_dist.py
--- a/src/distutils2/tests/test_dist.py
+++ b/src/distutils2/tests/test_dist.py
@@ -340,8 +340,10 @@
         temp_dir = self.mkdtemp()
         user_filename = os.path.join(temp_dir, user_filename)
         f = open(user_filename, 'w')
-        f.write('.')
-        f.close()
+        try:
+            f.write('.')
+        finally:
+            f.close()
 
         try:
             dist = Distribution()
diff --git a/src/distutils2/tests/test_install_scripts.py b/src/distutils2/tests/test_install_scripts.py
--- a/src/distutils2/tests/test_install_scripts.py
+++ b/src/distutils2/tests/test_install_scripts.py
@@ -42,8 +42,10 @@
         def write_script(name, text):
             expected.append(name)
             f = open(os.path.join(source, name), "w")
-            f.write(text)
-            f.close()
+            try:
+                f.write(text)
+            finally:
+                f.close()
 
         write_script("script1.py", ("#! /usr/bin/env python2.3\n"
                                     "# bogus script w/ Python sh-bang\n"
diff --git a/src/distutils2/tests/test_metadata.py b/src/distutils2/tests/test_metadata.py
--- a/src/distutils2/tests/test_metadata.py
+++ b/src/distutils2/tests/test_metadata.py
@@ -64,9 +64,12 @@
         res.seek(0)
         res = res.read()
         f = open(PKG_INFO)
-        wanted = f.read()
+        try:
+            # XXX this is not used
+            wanted = f.read()
+        finally:
+            f.close()
         self.assertTrue('Keywords: keyring,password,crypt' in res)
-        f.close()
 
     def test_metadata_markers(self):
         # see if we can be platform-aware
diff --git a/src/distutils2/tests/test_msvc9compiler.py b/src/distutils2/tests/test_msvc9compiler.py
--- a/src/distutils2/tests/test_msvc9compiler.py
+++ b/src/distutils2/tests/test_msvc9compiler.py
@@ -116,17 +116,21 @@
         tempdir = self.mkdtemp()
         manifest = os.path.join(tempdir, 'manifest')
         f = open(manifest, 'w')
-        f.write(_MANIFEST)
-        f.close()
+        try:
+            f.write(_MANIFEST)
+        finally:
+            f.close()
 
         compiler = MSVCCompiler()
         compiler._remove_visual_c_ref(manifest)
 
         # see what we got
         f = open(manifest)
-        # removing trailing spaces
-        content = '\n'.join([line.rstrip() for line in f.readlines()])
-        f.close()
+        try:
+            # removing trailing spaces
+            content = '\n'.join([line.rstrip() for line in f.readlines()])
+        finally:
+            f.close()
 
         # makes sure the manifest was properly cleaned
         self.assertEqual(content, _CLEANED_MANIFEST)
diff --git a/src/distutils2/util.py b/src/distutils2/util.py
--- a/src/distutils2/util.py
+++ b/src/distutils2/util.py
@@ -358,34 +358,36 @@
             else:
                 script = open(script_name, "w")
 
-            script.write("""\
+            try:
+                script.write("""\
 from distutils2.util import byte_compile
 files = [
 """)
 
-            # XXX would be nice to write absolute filenames, just for
-            # safety's sake (script should be more robust in the face of
-            # chdir'ing before running it).  But this requires abspath'ing
-            # 'prefix' as well, and that breaks the hack in build_lib's
-            # 'byte_compile()' method that carefully tacks on a trailing
-            # slash (os.sep really) to make sure the prefix here is "just
-            # right".  This whole prefix business is rather delicate -- the
-            # problem is that it's really a directory, but I'm treating it
-            # as a dumb string, so trailing slashes and so forth matter.
+                # XXX would be nice to write absolute filenames, just for
+                # safety's sake (script should be more robust in the face of
+                # chdir'ing before running it).  But this requires abspath'ing
+                # 'prefix' as well, and that breaks the hack in build_lib's
+                # 'byte_compile()' method that carefully tacks on a trailing
+                # slash (os.sep really) to make sure the prefix here is "just
+                # right".  This whole prefix business is rather delicate -- the
+                # problem is that it's really a directory, but I'm treating it
+                # as a dumb string, so trailing slashes and so forth matter.
 
-            #py_files = map(os.path.abspath, py_files)
-            #if prefix:
-            #    prefix = os.path.abspath(prefix)
+                #py_files = map(os.path.abspath, py_files)
+                #if prefix:
+                #    prefix = os.path.abspath(prefix)
 
-            script.write(",\n".join(map(repr, py_files)) + "]\n")
-            script.write("""
+                script.write(",\n".join(map(repr, py_files)) + "]\n")
+                script.write("""
 byte_compile(files, optimize=%r, force=%r,
              prefix=%r, base_dir=%r,
              verbose=%r, dry_run=0,
              direct=1)
 """ % (optimize, force, prefix, base_dir, verbose))
 
-            script.close()
+            finally:
+                script.close()
 
         cmd = [sys.executable, script_name]
         if optimize == 1:
@@ -534,10 +536,12 @@
     """Create a file with the specified name and write 'contents' (a
     sequence of strings without line terminators) to it.
     """
-    f = open(filename, "w")
-    for line in contents:
-        f.write(line + "\n")
-    f.close()
+    try:
+        f = open(filename, "w")
+        for line in contents:
+            f.write(line + "\n")
+    finally:
+        f.close()
 
 def _is_package(path):
     """Returns True if path is a package (a dir with an __init__ file."""

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


More information about the Python-checkins mailing list