[Python-checkins] r86223 - in python/branches/py3k/Lib: distutils/ccompiler.py distutils/command/bdist_wininst.py distutils/command/upload.py distutils/core.py distutils/cygwinccompiler.py distutils/dist.py distutils/emxccompiler.py distutils/extension.py distutils/file_util.py distutils/tests/test_build_py.py distutils/tests/test_build_scripts.py distutils/tests/test_config.py distutils/tests/test_core.py distutils/tests/test_dir_util.py distutils/tests/test_dist.py distutils/tests/test_file_util.py distutils/tests/test_install.py distutils/tests/test_msvc9compiler.py distutils/tests/test_register.py distutils/tests/test_sdist.py distutils/tests/test_sysconfig.py distutils/tests/test_text_file.py distutils/util.py sysconfig.py

eric.araujo python-checkins at python.org
Sat Nov 6 00:51:57 CET 2010


Author: eric.araujo
Date: Sat Nov  6 00:51:56 2010
New Revision: 86223

Log:
Always close files in distutils code and tests (#10252).


Modified:
   python/branches/py3k/Lib/distutils/ccompiler.py
   python/branches/py3k/Lib/distutils/command/bdist_wininst.py
   python/branches/py3k/Lib/distutils/command/upload.py
   python/branches/py3k/Lib/distutils/core.py
   python/branches/py3k/Lib/distutils/cygwinccompiler.py
   python/branches/py3k/Lib/distutils/dist.py
   python/branches/py3k/Lib/distutils/emxccompiler.py
   python/branches/py3k/Lib/distutils/extension.py
   python/branches/py3k/Lib/distutils/file_util.py
   python/branches/py3k/Lib/distutils/tests/test_build_py.py
   python/branches/py3k/Lib/distutils/tests/test_build_scripts.py
   python/branches/py3k/Lib/distutils/tests/test_config.py
   python/branches/py3k/Lib/distutils/tests/test_core.py
   python/branches/py3k/Lib/distutils/tests/test_dir_util.py
   python/branches/py3k/Lib/distutils/tests/test_dist.py
   python/branches/py3k/Lib/distutils/tests/test_file_util.py
   python/branches/py3k/Lib/distutils/tests/test_install.py
   python/branches/py3k/Lib/distutils/tests/test_msvc9compiler.py
   python/branches/py3k/Lib/distutils/tests/test_register.py
   python/branches/py3k/Lib/distutils/tests/test_sdist.py
   python/branches/py3k/Lib/distutils/tests/test_sysconfig.py
   python/branches/py3k/Lib/distutils/tests/test_text_file.py
   python/branches/py3k/Lib/distutils/util.py
   python/branches/py3k/Lib/sysconfig.py

Modified: python/branches/py3k/Lib/distutils/ccompiler.py
==============================================================================
--- python/branches/py3k/Lib/distutils/ccompiler.py	(original)
+++ python/branches/py3k/Lib/distutils/ccompiler.py	Sat Nov  6 00:51:56 2010
@@ -779,14 +779,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:

Modified: python/branches/py3k/Lib/distutils/command/bdist_wininst.py
==============================================================================
--- python/branches/py3k/Lib/distutils/command/bdist_wininst.py	(original)
+++ python/branches/py3k/Lib/distutils/command/bdist_wininst.py	Sat Nov  6 00:51:56 2010
@@ -340,4 +340,8 @@
             sfix = ''
 
         filename = os.path.join(directory, "wininst-%.1f%s.exe" % (bv, sfix))
-        return open(filename, "rb").read()
+        f = open(filename, "rb")
+        try:
+            return f.read()
+        finally:
+            f.close()

Modified: python/branches/py3k/Lib/distutils/command/upload.py
==============================================================================
--- python/branches/py3k/Lib/distutils/command/upload.py	(original)
+++ python/branches/py3k/Lib/distutils/command/upload.py	Sat Nov  6 00:51:56 2010
@@ -76,7 +76,11 @@
 
         # Fill in the data - send all the meta-data in case we need to
         # register a new release
-        content = open(filename,'rb').read()
+        f = open(filename,'rb')
+        try:
+            content = f.read()
+        finally:
+            f.close()
         meta = self.distribution.metadata
         data = {
             # action

Modified: python/branches/py3k/Lib/distutils/core.py
==============================================================================
--- python/branches/py3k/Lib/distutils/core.py	(original)
+++ python/branches/py3k/Lib/distutils/core.py	Sat Nov  6 00:51:56 2010
@@ -215,7 +215,11 @@
             sys.argv[0] = script_name
             if script_args is not None:
                 sys.argv[1:] = script_args
-            exec(open(script_name).read(), g, l)
+            f = open(script_name)
+            try:
+                exec(f.read(), g, l)
+            finally:
+                f.close()
         finally:
             sys.argv = save_argv
             _setup_stop_after = None

Modified: python/branches/py3k/Lib/distutils/cygwinccompiler.py
==============================================================================
--- python/branches/py3k/Lib/distutils/cygwinccompiler.py	(original)
+++ python/branches/py3k/Lib/distutils/cygwinccompiler.py	Sat Nov  6 00:51:56 2010
@@ -350,11 +350,14 @@
     # let's see if __GNUC__ is mentioned in python.h
     fn = sysconfig.get_config_h_filename()
     try:
-        with open(fn) as config_h:
+        config_h = open(fn)
+        try:
             if "__GNUC__" in config_h.read():
                 return CONFIG_H_OK, "'%s' mentions '__GNUC__'" % fn
             else:
                 return CONFIG_H_NOTOK, "'%s' does not mention '__GNUC__'" % fn
+        finally:
+            config_h.close()
     except IOError as exc:
         return (CONFIG_H_UNCERTAIN,
                 "couldn't read '%s': %s" % (fn, exc.strerror))

Modified: python/branches/py3k/Lib/distutils/dist.py
==============================================================================
--- python/branches/py3k/Lib/distutils/dist.py	(original)
+++ python/branches/py3k/Lib/distutils/dist.py	Sat Nov  6 00:51:56 2010
@@ -1012,9 +1012,11 @@
     def write_pkg_info(self, base_dir):
         """Write the PKG-INFO file into the release tree.
         """
-        pkg_info = open( os.path.join(base_dir, 'PKG-INFO'), 'w')
-        self.write_pkg_file(pkg_info)
-        pkg_info.close()
+        pkg_info = open(os.path.join(base_dir, 'PKG-INFO'), 'w')
+        try:
+            self.write_pkg_file(pkg_info)
+        finally:
+            pkg_info.close()
 
     def write_pkg_file(self, file):
         """Write the PKG-INFO format data to a file object.

Modified: python/branches/py3k/Lib/distutils/emxccompiler.py
==============================================================================
--- python/branches/py3k/Lib/distutils/emxccompiler.py	(original)
+++ python/branches/py3k/Lib/distutils/emxccompiler.py	Sat Nov  6 00:51:56 2010
@@ -270,8 +270,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 as exc:
         # if we can't read this file, we cannot say it is wrong
@@ -298,8 +300,10 @@
     gcc_exe = find_executable('gcc')
     if gcc_exe:
         out = os.popen(gcc_exe + ' -dumpversion','r')
-        out_string = out.read()
-        out.close()
+        try:
+            out_string = out.read()
+        finally:
+            out.close()
         result = re.search('(\d+\.\d+\.\d+)', out_string, re.ASCII)
         if result:
             gcc_version = StrictVersion(result.group(1))

Modified: python/branches/py3k/Lib/distutils/extension.py
==============================================================================
--- python/branches/py3k/Lib/distutils/extension.py	(original)
+++ python/branches/py3k/Lib/distutils/extension.py	Sat Nov  6 00:51:56 2010
@@ -149,84 +149,87 @@
     file = TextFile(filename,
                     strip_comments=1, skip_blanks=1, join_lines=1,
                     lstrip_ws=1, rstrip_ws=1)
-    extensions = []
+    try:
+        extensions = []
 
-    while True:
-        line = file.readline()
-        if line is None:                # eof
-            break
-        if _variable_rx.match(line):    # VAR=VALUE, handled in first pass
-            continue
-
-        if line[0] == line[-1] == "*":
-            file.warn("'%s' lines not handled yet" % line)
-            continue
-
-        line = expand_makefile_vars(line, vars)
-        words = split_quoted(line)
-
-        # NB. this parses a slightly different syntax than the old
-        # makesetup script: here, there must be exactly one extension per
-        # line, and it must be the first word of the line.  I have no idea
-        # why the old syntax supported multiple extensions per line, as
-        # they all wind up being the same.
-
-        module = words[0]
-        ext = Extension(module, [])
-        append_next_word = None
-
-        for word in words[1:]:
-            if append_next_word is not None:
-                append_next_word.append(word)
-                append_next_word = None
+        while True:
+            line = file.readline()
+            if line is None:                # eof
+                break
+            if _variable_rx.match(line):    # VAR=VALUE, handled in first pass
                 continue
 
-            suffix = os.path.splitext(word)[1]
-            switch = word[0:2] ; value = word[2:]
+            if line[0] == line[-1] == "*":
+                file.warn("'%s' lines not handled yet" % line)
+                continue
 
-            if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
-                # hmm, should we do something about C vs. C++ sources?
-                # or leave it up to the CCompiler implementation to
-                # worry about?
-                ext.sources.append(word)
-            elif switch == "-I":
-                ext.include_dirs.append(value)
-            elif switch == "-D":
-                equals = value.find("=")
-                if equals == -1:        # bare "-DFOO" -- no value
-                    ext.define_macros.append((value, None))
-                else:                   # "-DFOO=blah"
-                    ext.define_macros.append((value[0:equals],
-                                              value[equals+2:]))
-            elif switch == "-U":
-                ext.undef_macros.append(value)
-            elif switch == "-C":        # only here 'cause makesetup has it!
-                ext.extra_compile_args.append(word)
-            elif switch == "-l":
-                ext.libraries.append(value)
-            elif switch == "-L":
-                ext.library_dirs.append(value)
-            elif switch == "-R":
-                ext.runtime_library_dirs.append(value)
-            elif word == "-rpath":
-                append_next_word = ext.runtime_library_dirs
-            elif word == "-Xlinker":
-                append_next_word = ext.extra_link_args
-            elif word == "-Xcompiler":
-                append_next_word = ext.extra_compile_args
-            elif switch == "-u":
-                ext.extra_link_args.append(word)
-                if not value:
-                    append_next_word = ext.extra_link_args
-            elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
-                # NB. a really faithful emulation of makesetup would
-                # append a .o file to extra_objects only if it
-                # had a slash in it; otherwise, it would s/.o/.c/
-                # and append it to sources.  Hmmmm.
-                ext.extra_objects.append(word)
-            else:
-                file.warn("unrecognized argument '%s'" % word)
+            line = expand_makefile_vars(line, vars)
+            words = split_quoted(line)
 
-        extensions.append(ext)
+            # NB. this parses a slightly different syntax than the old
+            # makesetup script: here, there must be exactly one extension per
+            # line, and it must be the first word of the line.  I have no idea
+            # why the old syntax supported multiple extensions per line, as
+            # they all wind up being the same.
+
+            module = words[0]
+            ext = Extension(module, [])
+            append_next_word = None
+
+            for word in words[1:]:
+                if append_next_word is not None:
+                    append_next_word.append(word)
+                    append_next_word = None
+                    continue
+
+                suffix = os.path.splitext(word)[1]
+                switch = word[0:2] ; value = word[2:]
+
+                if suffix in (".c", ".cc", ".cpp", ".cxx", ".c++", ".m", ".mm"):
+                    # hmm, should we do something about C vs. C++ sources?
+                    # or leave it up to the CCompiler implementation to
+                    # worry about?
+                    ext.sources.append(word)
+                elif switch == "-I":
+                    ext.include_dirs.append(value)
+                elif switch == "-D":
+                    equals = value.find("=")
+                    if equals == -1:        # bare "-DFOO" -- no value
+                        ext.define_macros.append((value, None))
+                    else:                   # "-DFOO=blah"
+                        ext.define_macros.append((value[0:equals],
+                                                  value[equals+2:]))
+                elif switch == "-U":
+                    ext.undef_macros.append(value)
+                elif switch == "-C":        # only here 'cause makesetup has it!
+                    ext.extra_compile_args.append(word)
+                elif switch == "-l":
+                    ext.libraries.append(value)
+                elif switch == "-L":
+                    ext.library_dirs.append(value)
+                elif switch == "-R":
+                    ext.runtime_library_dirs.append(value)
+                elif word == "-rpath":
+                    append_next_word = ext.runtime_library_dirs
+                elif word == "-Xlinker":
+                    append_next_word = ext.extra_link_args
+                elif word == "-Xcompiler":
+                    append_next_word = ext.extra_compile_args
+                elif switch == "-u":
+                    ext.extra_link_args.append(word)
+                    if not value:
+                        append_next_word = ext.extra_link_args
+                elif suffix in (".a", ".so", ".sl", ".o", ".dylib"):
+                    # NB. a really faithful emulation of makesetup would
+                    # append a .o file to extra_objects only if it
+                    # had a slash in it; otherwise, it would s/.o/.c/
+                    # and append it to sources.  Hmmmm.
+                    ext.extra_objects.append(word)
+                else:
+                    file.warn("unrecognized argument '%s'" % word)
+
+            extensions.append(ext)
+    finally:
+        file.close()
 
     return extensions

Modified: python/branches/py3k/Lib/distutils/file_util.py
==============================================================================
--- python/branches/py3k/Lib/distutils/file_util.py	(original)
+++ python/branches/py3k/Lib/distutils/file_util.py	Sat Nov  6 00:51:56 2010
@@ -234,6 +234,8 @@
     sequence of strings without line terminators) to it.
     """
     f = open(filename, "w")
-    for line in contents:
-        f.write(line + "\n")
-    f.close()
+    try:
+        for line in contents:
+            f.write(line + "\n")
+    finally:
+        f.close()

Modified: python/branches/py3k/Lib/distutils/tests/test_build_py.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_build_py.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_build_py.py	Sat Nov  6 00:51:56 2010
@@ -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()
 

Modified: python/branches/py3k/Lib/distutils/tests/test_build_scripts.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_build_scripts.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_build_scripts.py	Sat Nov  6 00:51:56 2010
@@ -71,8 +71,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()

Modified: python/branches/py3k/Lib/distutils/tests/test_config.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_config.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_config.py	Sat Nov  6 00:51:56 2010
@@ -105,8 +105,12 @@
         self.assertTrue(not os.path.exists(rc))
         cmd._store_pypirc('tarek', 'xxx')
         self.assertTrue(os.path.exists(rc))
-        content = open(rc).read()
-        self.assertEquals(content, WANTED)
+        f = open(rc)
+        try:
+            content = f.read()
+            self.assertEquals(content, WANTED)
+        finally:
+            f.close()
 
 def test_suite():
     return unittest.makeSuite(PyPIRCCommandTestCase)

Modified: python/branches/py3k/Lib/distutils/tests/test_core.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_core.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_core.py	Sat Nov  6 00:51:56 2010
@@ -52,7 +52,11 @@
             shutil.rmtree(path)
 
     def write_setup(self, text, path=test.support.TESTFN):
-        open(path, "w").write(text)
+        f = open(path, "w")
+        try:
+            f.write(text)
+        finally:
+            f.close()
         return path
 
     def test_run_setup_provides_file(self):

Modified: python/branches/py3k/Lib/distutils/tests/test_dir_util.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_dir_util.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_dir_util.py	Sat Nov  6 00:51:56 2010
@@ -88,8 +88,10 @@
         mkpath(self.target, verbose=0)
         a_file = os.path.join(self.target, 'ok.txt')
         f = open(a_file, 'w')
-        f.write('some content')
-        f.close()
+        try:
+            f.write('some content')
+        finally:
+            f.close()
 
         wanted = ['copying %s -> %s' % (a_file, self.target2)]
         copy_tree(self.target, self.target2, verbose=1)

Modified: python/branches/py3k/Lib/distutils/tests/test_dist.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_dist.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_dist.py	Sat Nov  6 00:51:56 2010
@@ -79,29 +79,29 @@
 
     def test_command_packages_configfile(self):
         sys.argv.append("build")
+        self.addCleanup(os.unlink, TESTFN)
         f = open(TESTFN, "w")
         try:
             print("[global]", file=f)
             print("command_packages = foo.bar, splat", file=f)
+        finally:
             f.close()
-            d = self.create_distribution([TESTFN])
-            self.assertEqual(d.get_command_packages(),
-                             ["distutils.command", "foo.bar", "splat"])
-
-            # ensure command line overrides config:
-            sys.argv[1:] = ["--command-packages", "spork", "build"]
-            d = self.create_distribution([TESTFN])
-            self.assertEqual(d.get_command_packages(),
-                             ["distutils.command", "spork"])
-
-            # Setting --command-packages to '' should cause the default to
-            # be used even if a config file specified something else:
-            sys.argv[1:] = ["--command-packages", "", "build"]
-            d = self.create_distribution([TESTFN])
-            self.assertEqual(d.get_command_packages(), ["distutils.command"])
 
-        finally:
-            os.unlink(TESTFN)
+        d = self.create_distribution([TESTFN])
+        self.assertEqual(d.get_command_packages(),
+                         ["distutils.command", "foo.bar", "splat"])
+
+        # ensure command line overrides config:
+        sys.argv[1:] = ["--command-packages", "spork", "build"]
+        d = self.create_distribution([TESTFN])
+        self.assertEqual(d.get_command_packages(),
+                         ["distutils.command", "spork"])
+
+        # Setting --command-packages to '' should cause the default to
+        # be used even if a config file specified something else:
+        sys.argv[1:] = ["--command-packages", "", "build"]
+        d = self.create_distribution([TESTFN])
+        self.assertEqual(d.get_command_packages(), ["distutils.command"])
 
     def test_empty_options(self):
         # an empty options dictionary should not stay in the
@@ -260,8 +260,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()

Modified: python/branches/py3k/Lib/distutils/tests/test_file_util.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_file_util.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_file_util.py	Sat Nov  6 00:51:56 2010
@@ -31,8 +31,10 @@
 
     def test_move_file_verbosity(self):
         f = open(self.source, 'w')
-        f.write('some content')
-        f.close()
+        try:
+            f.write('some content')
+        finally:
+            f.close()
 
         move_file(self.source, self.target, verbose=0)
         wanted = []

Modified: python/branches/py3k/Lib/distutils/tests/test_install.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_install.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_install.py	Sat Nov  6 00:51:56 2010
@@ -182,8 +182,11 @@
 
         # let's check the RECORD file was created with one
         # line (the egg info file)
-        with open(cmd.record) as f:
+        f = open(cmd.record)
+        try:
             self.assertEquals(len(f.readlines()), 1)
+        finally:
+            f.close()
 
     def test_debug_mode(self):
         # this covers the code called when DEBUG is set

Modified: python/branches/py3k/Lib/distutils/tests/test_msvc9compiler.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_msvc9compiler.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_msvc9compiler.py	Sat Nov  6 00:51:56 2010
@@ -113,17 +113,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.assertEquals(content, _CLEANED_MANIFEST)

Modified: python/branches/py3k/Lib/distutils/tests/test_register.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_register.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_register.py	Sat Nov  6 00:51:56 2010
@@ -118,8 +118,12 @@
         self.assertTrue(os.path.exists(self.rc))
 
         # with the content similar to WANTED_PYPIRC
-        content = open(self.rc).read()
-        self.assertEquals(content, WANTED_PYPIRC)
+        f = open(self.rc)
+        try:
+            content = f.read()
+            self.assertEquals(content, WANTED_PYPIRC)
+        finally:
+            f.close()
 
         # now let's make sure the .pypirc file generated
         # really works : we shouldn't be asked anything

Modified: python/branches/py3k/Lib/distutils/tests/test_sdist.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_sdist.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_sdist.py	Sat Nov  6 00:51:56 2010
@@ -215,8 +215,12 @@
         self.assertEquals(len(content), 11)
 
         # checking the MANIFEST
-        manifest = open(join(self.tmp_dir, 'MANIFEST')).read()
-        self.assertEquals(manifest, MANIFEST % {'sep': os.sep})
+        f = open(join(self.tmp_dir, 'MANIFEST'))
+        try:
+            manifest = f.read()
+            self.assertEquals(manifest, MANIFEST % {'sep': os.sep})
+        finally:
+            f.close()
 
     def test_metadata_check_option(self):
         # testing the `medata-check` option

Modified: python/branches/py3k/Lib/distutils/tests/test_sysconfig.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_sysconfig.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_sysconfig.py	Sat Nov  6 00:51:56 2010
@@ -75,9 +75,11 @@
     def test_parse_makefile_base(self):
         self.makefile = TESTFN
         fd = open(self.makefile, 'w')
-        fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=LIB'" '\n')
-        fd.write('VAR=$OTHER\nOTHER=foo')
-        fd.close()
+        try:
+            fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=LIB'" '\n')
+            fd.write('VAR=$OTHER\nOTHER=foo')
+        finally:
+            fd.close()
         d = sysconfig.parse_makefile(self.makefile)
         self.assertEquals(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
                               'OTHER': 'foo'})
@@ -85,9 +87,11 @@
     def test_parse_makefile_literal_dollar(self):
         self.makefile = TESTFN
         fd = open(self.makefile, 'w')
-        fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
-        fd.write('VAR=$OTHER\nOTHER=foo')
-        fd.close()
+        try:
+            fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
+            fd.write('VAR=$OTHER\nOTHER=foo')
+        finally:
+            fd.close()
         d = sysconfig.parse_makefile(self.makefile)
         self.assertEquals(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
                               'OTHER': 'foo'})

Modified: python/branches/py3k/Lib/distutils/tests/test_text_file.py
==============================================================================
--- python/branches/py3k/Lib/distutils/tests/test_text_file.py	(original)
+++ python/branches/py3k/Lib/distutils/tests/test_text_file.py	Sat Nov  6 00:51:56 2010
@@ -58,28 +58,46 @@
         finally:
             out_file.close()
 
-        in_file = TextFile (filename, strip_comments=0, skip_blanks=0,
-                            lstrip_ws=0, rstrip_ws=0)
-        test_input (1, "no processing", in_file, result1)
-
-        in_file = TextFile (filename, strip_comments=1, skip_blanks=0,
-                            lstrip_ws=0, rstrip_ws=0)
-        test_input (2, "strip comments", in_file, result2)
-
-        in_file = TextFile (filename, strip_comments=0, skip_blanks=1,
-                            lstrip_ws=0, rstrip_ws=0)
-        test_input (3, "strip blanks", in_file, result3)
-
-        in_file = TextFile (filename)
-        test_input (4, "default processing", in_file, result4)
-
-        in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
-                            join_lines=1, rstrip_ws=1)
-        test_input (5, "join lines without collapsing", in_file, result5)
-
-        in_file = TextFile (filename, strip_comments=1, skip_blanks=1,
-                            join_lines=1, rstrip_ws=1, collapse_join=1)
-        test_input (6, "join lines with collapsing", in_file, result6)
+        in_file = TextFile(filename, strip_comments=0, skip_blanks=0,
+                           lstrip_ws=0, rstrip_ws=0)
+        try:
+            test_input(1, "no processing", in_file, result1)
+        finally:
+            in_file.close()
+
+        in_file = TextFile(filename, strip_comments=1, skip_blanks=0,
+                           lstrip_ws=0, rstrip_ws=0)
+        try:
+            test_input(2, "strip comments", in_file, result2)
+        finally:
+            in_file.close()
+
+        in_file = TextFile(filename, strip_comments=0, skip_blanks=1,
+                           lstrip_ws=0, rstrip_ws=0)
+        try:
+            test_input(3, "strip blanks", in_file, result3)
+        finally:
+            in_file.close()
+
+        in_file = TextFile(filename)
+        try:
+            test_input(4, "default processing", in_file, result4)
+        finally:
+            in_file.close()
+
+        in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
+                           join_lines=1, rstrip_ws=1)
+        try:
+            test_input(5, "join lines without collapsing", in_file, result5)
+        finally:
+            in_file.close()
+
+        in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
+                           join_lines=1, rstrip_ws=1, collapse_join=1)
+        try:
+            test_input(6, "join lines with collapsing", in_file, result6)
+        finally:
+            in_file.close()
 
 def test_suite():
     return unittest.makeSuite(TextFileTestCase)

Modified: python/branches/py3k/Lib/distutils/util.py
==============================================================================
--- python/branches/py3k/Lib/distutils/util.py	(original)
+++ python/branches/py3k/Lib/distutils/util.py	Sat Nov  6 00:51:56 2010
@@ -115,13 +115,15 @@
                 # behaviour.
                 pass
             else:
-                m = re.search(
-                        r'<key>ProductUserVisibleVersion</key>\s*' +
-                        r'<string>(.*?)</string>', f.read())
-                f.close()
-                if m is not None:
-                    macrelease = '.'.join(m.group(1).split('.')[:2])
-                # else: fall back to the default behaviour
+                try:
+                    m = re.search(
+                            r'<key>ProductUserVisibleVersion</key>\s*' +
+                            r'<string>(.*?)</string>', f.read())
+                    if m is not None:
+                        macrelease = '.'.join(m.group(1).split('.')[:2])
+                    # else: fall back to the default behaviour
+                finally:
+                    f.close()
 
         if not macver:
             macver = macrelease

Modified: python/branches/py3k/Lib/sysconfig.py
==============================================================================
--- python/branches/py3k/Lib/sysconfig.py	(original)
+++ python/branches/py3k/Lib/sysconfig.py	Sat Nov  6 00:51:56 2010
@@ -680,13 +680,16 @@
                 # behaviour.
                 pass
             else:
-                m = re.search(
-                        r'<key>ProductUserVisibleVersion</key>\s*' +
-                        r'<string>(.*?)</string>', f.read())
-                f.close()
-                if m is not None:
-                    macrelease = '.'.join(m.group(1).split('.')[:2])
-                # else: fall back to the default behaviour
+                try:
+                    m = re.search(
+                            r'<key>ProductUserVisibleVersion</key>\s*' +
+                            r'<string>(.*?)</string>', f.read())
+                    f.close()
+                    if m is not None:
+                        macrelease = '.'.join(m.group(1).split('.')[:2])
+                    # else: fall back to the default behaviour
+                finally:
+                    f.close()
 
         if not macver:
             macver = macrelease


More information about the Python-checkins mailing list