diff options
| author | ?ric Araujo <merwok@netwok.org> | 2010-06-27 02:42:50 +0200 |
|---|---|---|
| committer | ?ric Araujo <merwok@netwok.org> | 2010-06-27 02:42:50 +0200 |
| commit | a7522208f7672cf71181d17336f6c515f5d832fe (patch) | |
| tree | cfee76034736ea5a7ac14d33d53e01f807b331e1 /src | |
| parent | c495465ba7449005a62d85ff826420b0a56c18ac (diff) | |
| download | disutils2-a7522208f7672cf71181d17336f6c515f5d832fe.tar.gz | |
Wrap I/O operations in try/except blocks to ensure handles are closed
Diffstat (limited to 'src')
| -rw-r--r-- | src/distutils2/_backport/sysconfig.py | 9 | ||||
| -rw-r--r-- | src/distutils2/_backport/tarfile.py | 20 | ||||
| -rw-r--r-- | src/distutils2/_backport/tests/test_pkgutil.py | 32 | ||||
| -rw-r--r-- | src/distutils2/compiler/ccompiler.py | 10 | ||||
| -rw-r--r-- | src/distutils2/compiler/emxccompiler.py | 6 | ||||
| -rwxr-xr-x | src/distutils2/mkpkg.py | 77 | ||||
| -rw-r--r-- | src/distutils2/tests/test_build_py.py | 12 | ||||
| -rw-r--r-- | src/distutils2/tests/test_build_scripts.py | 6 | ||||
| -rw-r--r-- | src/distutils2/tests/test_dist.py | 6 | ||||
| -rw-r--r-- | src/distutils2/tests/test_install_scripts.py | 6 | ||||
| -rw-r--r-- | src/distutils2/tests/test_metadata.py | 7 | ||||
| -rw-r--r-- | src/distutils2/tests/test_msvc9compiler.py | 14 | ||||
| -rw-r--r-- | src/distutils2/util.py | 48 |
13 files changed, 145 insertions, 108 deletions
diff --git a/src/distutils2/_backport/sysconfig.py b/src/distutils2/_backport/sysconfig.py index e2448f8..a240fc4 100644 --- a/src/distutils2/_backport/sysconfig.py +++ b/src/distutils2/_backport/sysconfig.py @@ -580,10 +580,11 @@ def get_platform(): # 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 index ab0a14b..0d2c49b 100644 --- a/src/distutils2/_backport/tarfile.py +++ b/src/distutils2/_backport/tarfile.py @@ -2003,8 +2003,10 @@ class TarFile(object): # 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 @@ class TarFile(object): """ 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 @@ def is_tarfile(name): 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 index 8b289ff..07b9a5c 100644 --- a/src/distutils2/_backport/tests/test_pkgutil.py +++ b/src/distutils2/_backport/tests/test_pkgutil.py @@ -46,15 +46,22 @@ class TestPkgUtilData(unittest.TestCase): 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 @@ class TestPkgUtilData(unittest.TestCase): # 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 index f62be34..c9b69b9 100644 --- a/src/distutils2/compiler/ccompiler.py +++ b/src/distutils2/compiler/ccompiler.py @@ -800,14 +800,16 @@ class CCompiler(object): 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 index a1073da..6247c00 100644 --- a/src/distutils2/compiler/emxccompiler.py +++ b/src/distutils2/compiler/emxccompiler.py @@ -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 diff --git a/src/distutils2/mkpkg.py b/src/distutils2/mkpkg.py index 7fc3b2e..3fe82e3 100755 --- a/src/distutils2/mkpkg.py +++ b/src/distutils2/mkpkg.py @@ -717,14 +717,16 @@ class SetupClass(object): 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 @@ Status''', required = False) 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') - - 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() + 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(' )\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 index 7f7e1ec..4b46da9 100644 --- a/src/distutils2/tests/test_build_py.py +++ b/src/distutils2/tests/test_build_py.py @@ -19,11 +19,15 @@ class BuildPyTestCase(support.TempdirManager, 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 index 2167ac1..6a724bd 100644 --- a/src/distutils2/tests/test_build_scripts.py +++ b/src/distutils2/tests/test_build_scripts.py @@ -74,8 +74,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() diff --git a/src/distutils2/tests/test_dist.py b/src/distutils2/tests/test_dist.py index 20dd588..f528684 100644 --- a/src/distutils2/tests/test_dist.py +++ b/src/distutils2/tests/test_dist.py @@ -340,8 +340,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() diff --git a/src/distutils2/tests/test_install_scripts.py b/src/distutils2/tests/test_install_scripts.py index 32c800d..be65d65 100644 --- a/src/distutils2/tests/test_install_scripts.py +++ b/src/distutils2/tests/test_install_scripts.py @@ -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" diff --git a/src/distutils2/tests/test_metadata.py b/src/distutils2/tests/test_metadata.py index 8c15d58..56d1a45 100644 --- a/src/distutils2/tests/test_metadata.py +++ b/src/distutils2/tests/test_metadata.py @@ -64,9 +64,12 @@ class DistributionMetadataTestCase(unittest.TestCase): 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 index 42dea21..452c3f5 100644 --- a/src/distutils2/tests/test_msvc9compiler.py +++ b/src/distutils2/tests/test_msvc9compiler.py @@ -116,17 +116,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.assertEqual(content, _CLEANED_MANIFEST) diff --git a/src/distutils2/util.py b/src/distutils2/util.py index 33a0696..0750d31 100644 --- a/src/distutils2/util.py +++ b/src/distutils2/util.py @@ -358,34 +358,36 @@ def byte_compile(py_files, optimize=0, force=0, prefix=None, base_dir=None, 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. - - #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(""" + # 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) + + 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 @@ def write_file(filename, contents): """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.""" |
