]> granicus.if.org Git - python/commitdiff
Merged revisions 86223-86224,86226,86234 via svnmerge from
authorÉric Araujo <merwok@netwok.org>
Sat, 6 Nov 2010 04:06:18 +0000 (04:06 +0000)
committerÉric Araujo <merwok@netwok.org>
Sat, 6 Nov 2010 04:06:18 +0000 (04:06 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86223 | eric.araujo | 2010-11-06 00:51:56 +0100 (sam., 06 nov. 2010) | 2 lines

  Always close files in distutils code and tests (#10252).
........
  r86224 | eric.araujo | 2010-11-06 00:58:34 +0100 (sam., 06 nov. 2010) | 2 lines

  Add missing entry for r86223.
........
  r86226 | eric.araujo | 2010-11-06 00:59:32 +0100 (sam., 06 nov. 2010) | 2 lines

  Of course, I forgot one file in r86223.
........
  r86234 | eric.araujo | 2010-11-06 03:10:32 +0100 (sam., 06 nov. 2010) | 2 lines

  Also close file descriptors from os.popen and subprocess.Popen
........

27 files changed:
Lib/distutils/ccompiler.py
Lib/distutils/command/bdist_rpm.py
Lib/distutils/command/bdist_wininst.py
Lib/distutils/command/upload.py
Lib/distutils/core.py
Lib/distutils/cygwinccompiler.py
Lib/distutils/dist.py
Lib/distutils/emxccompiler.py
Lib/distutils/extension.py
Lib/distutils/file_util.py
Lib/distutils/msvc9compiler.py
Lib/distutils/tests/test_build_py.py
Lib/distutils/tests/test_build_scripts.py
Lib/distutils/tests/test_config.py
Lib/distutils/tests/test_core.py
Lib/distutils/tests/test_dir_util.py
Lib/distutils/tests/test_dist.py
Lib/distutils/tests/test_file_util.py
Lib/distutils/tests/test_install_scripts.py
Lib/distutils/tests/test_msvc9compiler.py
Lib/distutils/tests/test_register.py
Lib/distutils/tests/test_sdist.py
Lib/distutils/tests/test_sysconfig.py
Lib/distutils/tests/test_text_file.py
Lib/distutils/util.py
Lib/sysconfig.py
Misc/NEWS

index a34177e71f9f08190183a5862df229afcd4602fd..c2b1f6fbe9d7226ad1bf58a20c21458cbd8ae791 100644 (file)
@@ -794,14 +794,16 @@ class CCompiler:
             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:
index 6d9d47d2eb36739d365470f145273df7071e2292..0bba36355740b3fa686b7b4d144ec14f3c1a1cf4 100644 (file)
@@ -355,22 +355,26 @@ class bdist_rpm (Command):
             src_rpm, non_src_rpm, spec_path)
 
         out = os.popen(q_cmd)
-        binary_rpms = []
-        source_rpm = None
-        while 1:
-            line = out.readline()
-            if not line:
-                break
-            l = string.split(string.strip(line))
-            assert(len(l) == 2)
-            binary_rpms.append(l[1])
-            # The source rpm is named after the first entry in the spec file
-            if source_rpm is None:
-                source_rpm = l[0]
-
-        status = out.close()
-        if status:
-            raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd))
+        try:
+            binary_rpms = []
+            source_rpm = None
+            while 1:
+                line = out.readline()
+                if not line:
+                    break
+                l = string.split(string.strip(line))
+                assert(len(l) == 2)
+                binary_rpms.append(l[1])
+                # The source rpm is named after the first entry in the spec file
+                if source_rpm is None:
+                    source_rpm = l[0]
+
+            status = out.close()
+            if status:
+                raise DistutilsExecError("Failed to execute: %s" % repr(q_cmd))
+
+        finally:
+            out.close()
 
         self.spawn(rpm_cmd)
 
index a31a5f7bacb160c3526e7a4e126b7b64ff73d41c..36d46bd627bf670b0604855c12ab3f56a193b1a0 100644 (file)
@@ -356,5 +356,9 @@ class bdist_wininst (Command):
             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()
 # class bdist_wininst
index c3f19d207f672ff6d887ab6220d8b601be8d6ad3..980cf68d06dd4a2c1ceab5659743576725c81a23 100644 (file)
@@ -79,7 +79,11 @@ class upload(PyPIRCCommand):
 
         # 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
index 99ccf44fad9538ea60098e187d131780e0e8591f..b89557d7679ce9e7f8ce8df8957e5d7175332695 100644 (file)
@@ -216,7 +216,11 @@ def run_setup(script_name, script_args=None, stop_after="run"):
             sys.argv[0] = script_name
             if script_args is not None:
                 sys.argv[1:] = script_args
-            exec open(script_name, 'r').read() in g, l
+            f = open(script_name)
+            try:
+                exec f.read() in g, l
+            finally:
+                f.close()
         finally:
             sys.argv = save_argv
             _setup_stop_after = None
index 2dabc0f0feb5033560ea682ee692f4edff36e4ba..a1ee815c6cc782354a1ee1735a88d31679e1e4db 100644 (file)
@@ -382,8 +382,10 @@ def check_config_h():
         # 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
index 5dbdaef19c0edf5e70ad9ec66dc5da3cb4ff8748..597909ea1a801a2ca8aecbd9eead6ef26e762386 100644 (file)
@@ -1101,9 +1101,11 @@ class DistributionMetadata:
     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.
index f52e63232db1afa9b14ee4a074d57c2a004b2b3d..a0172058a38175d00bc2da288447c2336be6c15c 100644 (file)
@@ -272,8 +272,10 @@ def check_config_h():
         # 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
@@ -300,8 +302,10 @@ def get_versions():
     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)
         if result:
             gcc_version = StrictVersion(result.group(1))
index 440d128cdc6e658221c7ae3b2d944ea0693cb85d..9a67ca8b3ea9b4690e69a6e47a01794b01106d39 100644 (file)
@@ -150,87 +150,96 @@ def read_setup_file (filename):
     file = TextFile(filename,
                     strip_comments=1, skip_blanks=1, join_lines=1,
                     lstrip_ws=1, rstrip_ws=1)
-    extensions = []
-
-    while 1:
-        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
-
-        #print "original line: " + line
-        line = expand_makefile_vars(line, vars)
-        words = split_quoted(line)
-        #print "expanded line: " + 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
+    try:
+        extensions = []
+
+        while 1:
+            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 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 = string.find(value, "=")
-                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:
+                if line[0] == line[-1] == "*":
+                    file.warn("'%s' lines not handled yet" % line)
+                    continue
+
+            #print "original line: " + line
+            line = expand_makefile_vars(line, vars)
+            words = split_quoted(line)
+            #print "expanded line: " + 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
+                    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 = string.find(value, "=")
+                    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 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)
+                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 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()
 
         #print "module:", module
         #print "source files:", source_files
index b3d9d54ec025691f0f2232dcfa2348b1309fd2a4..b9f07861335c911497e05f38b6559e7e1a8f6fd2 100644 (file)
@@ -224,6 +224,8 @@ def write_file (filename, contents):
     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()
index d5d7f66528b930ae5d317227fb41573c1f63a5b8..55a4db18805bb05f471de4d7835239246ce0ca6e 100644 (file)
@@ -273,10 +273,12 @@ def query_vcvarsall(version, arch="x86"):
     popen = subprocess.Popen('"%s" %s & set' % (vcvarsall, arch),
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE)
-
-    stdout, stderr = popen.communicate()
-    if popen.wait() != 0:
-        raise DistutilsPlatformError(stderr.decode("mbcs"))
+    try:
+        stdout, stderr = popen.communicate()
+        if popen.wait() != 0:
+            raise DistutilsPlatformError(stderr.decode("mbcs"))
+    finally:
+        popen.close()
 
     stdout = stdout.decode("mbcs")
     for line in stdout.split("\n"):
index 3c8bc41bae9577702c3410eda6e3319560e53e26..03517392fbe90f59da31c332fb28015294298015 100644 (file)
@@ -19,11 +19,15 @@ class BuildPyTestCase(support.TempdirManager,
     def _setup_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()
 
index 72e8915c00e5512daa8e9686e401a87f6feb677a..df89cdec25e3c14eb29aeab4db41382d658fa17a 100644 (file)
@@ -71,8 +71,10 @@ class BuildScriptsTestCase(support.TempdirManager,
 
     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()
index 0a1bb961ff0cc06c78d56250dc99ee2c74d64929..6c85efad242418c74ac0d0ad6c1306011380c328 100644 (file)
@@ -108,8 +108,12 @@ class PyPIRCCommandTestCase(support.TempdirManager,
         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)
index 48ec1b436e83025b86d2734fe0ac0700092495bc..63f6b31da69f5e41b0cf36c6d1e733574516d5ff 100644 (file)
@@ -52,7 +52,11 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase):
             shutil.rmtree(path)
 
     def write_setup(self, text, path=test.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):
index a1647fbcf5394d987815b2f8006a48b2251f9731..aa9f9ebb9fd63056bc7431c11bc07667b8691eaa 100644 (file)
@@ -88,8 +88,10 @@ class DirUtilTestCase(support.TempdirManager, unittest.TestCase):
         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)
index 1eddf6d25c0289943ffe24a436a9553e080181b3..d9b413f954e53382ddf6876aa81fdd7d00499813 100644 (file)
@@ -102,29 +102,29 @@ class DistributionTestCase(support.TempdirManager,
 
     def test_command_packages_configfile(self):
         sys.argv.append("build")
+        self.addCleanup(os.unlink, TESTFN)
         f = open(TESTFN, "w")
         try:
             print >>f, "[global]"
             print >>f, "command_packages = foo.bar, splat"
+        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_write_pkg_file(self):
         # Check DistributionMetadata handling of Unicode fields
@@ -341,8 +341,10 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
         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()
index 99b421f73f20c2acccb8c850cea87c96c0f52358..823f2110897c2f4a10aee47873105ad9f2ce6f10 100644 (file)
@@ -31,8 +31,10 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
 
     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 = []
index b7eb625ac50c00d87bab0c9ef8dbf5a35658e871..08360d297b8ee58bb42c851ba18be29a56d84f64 100644 (file)
@@ -42,8 +42,10 @@ class InstallScriptsTestCase(support.TempdirManager,
         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"
index c95795448236568208b2a17ffe2420464ed2da8b..567505ddf16cc28a1b7ca5f3a6731a73b7d2d9d0 100644 (file)
@@ -113,17 +113,21 @@ class msvc9compilerTestCase(support.TempdirManager,
         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)
index 370d65989359936d1b9f8f1b10e1c7db9b63fbbf..6da479b1398e0eb90fe5bb1a1aa3fc3f78187ad3 100644 (file)
@@ -119,8 +119,12 @@ class RegisterTestCase(PyPIRCCommandTestCase):
         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
index 87adb458943450842e9a75f4bbb191b6b6ba8b37..7cebbf53adb310ace8a11213063ca49c8cab0b7c 100644 (file)
@@ -234,8 +234,12 @@ class SDistTestCase(PyPIRCCommandTestCase):
         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()
 
     @unittest.skipUnless(zlib, "requires zlib")
     def test_metadata_check_option(self):
index 30664860664da827c660bee69b66cb1450394e29..8449ddca786358986c69e0fa14798bd2896d5e04 100644 (file)
@@ -50,9 +50,11 @@ class SysconfigTestCase(support.EnvironGuard,
     def test_parse_makefile_base(self):
         self.makefile = test.test_support.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'})
@@ -60,9 +62,11 @@ class SysconfigTestCase(support.EnvironGuard,
     def test_parse_makefile_literal_dollar(self):
         self.makefile = test.test_support.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'})
index 00f083a1308c980858ac02d3b1ed765286426ca2..3093097dbaca69c1e303b3019105feb0cf12485d 100644 (file)
@@ -58,28 +58,46 @@ class TextFileTestCase(support.TempdirManager, unittest.TestCase):
         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=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)
-        test_input (2, "strip comments", in_file, result2)
+        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)
-        test_input (3, "strip blanks", in_file, result3)
+        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)
-        test_input (4, "default processing", in_file, result4)
+        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)
-        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)
+        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)
-        test_input (6, "join lines with collapsing", in_file, result6)
+        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)
index 4dcfeb550525d021faa0321fd7e67bd2cd44443b..f06e4fdf88114890a085a127c9cee2a9f3a2e466 100644 (file)
@@ -116,13 +116,15 @@ def get_platform ():
                 # 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
index c40bb032e0b4fa8bdcfff24774d1f1446e0536c6..33b27915360e7731ca59fa08ace9e6255443f6a8 100644 (file)
@@ -635,13 +635,16 @@ def get_platform():
                 # 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
index 698c971414f7aa7e4c78f12c96d512064e9429a7..a578b8a642dd20c422e3d7c3e2c373db178f5016 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -69,6 +69,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10252: Close file objects in a timely manner in distutils code and
+  tests.  Patch by Brian Brazil, completed by Éric Araujo.
+
 - Issue #10311: The signal module now restores errno before returning from
   its low-level signal handler.  Patch by Hallvard B Furuseth.