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

This commit is contained in:
Éric Araujo 2010-11-05 23:51:56 +00:00
parent afb078dd26
commit bee5cef7db
24 changed files with 261 additions and 171 deletions

View File

@ -779,6 +779,7 @@ class CCompiler:
library_dirs = []
fd, fname = tempfile.mkstemp(".c", funcname, text=True)
f = os.fdopen(fd, "w")
try:
for incl in includes:
f.write("""#include "%s"\n""" % incl)
f.write("""\
@ -786,6 +787,7 @@ main (int argc, char **argv) {
%s();
}
""" % funcname)
finally:
f.close()
try:
objects = self.compile([fname], include_dirs=include_dirs)

View File

@ -340,4 +340,8 @@ 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()

View File

@ -76,7 +76,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

View File

@ -215,7 +215,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).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

View File

@ -350,11 +350,14 @@ def check_config_h():
# 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))

View File

@ -1013,7 +1013,9 @@ class DistributionMetadata:
"""Write the PKG-INFO file into the release tree.
"""
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):

View File

@ -270,7 +270,9 @@ 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)
try:
s = f.read()
finally:
f.close()
except IOError as exc:
@ -298,7 +300,9 @@ def get_versions():
gcc_exe = find_executable('gcc')
if gcc_exe:
out = os.popen(gcc_exe + ' -dumpversion','r')
try:
out_string = out.read()
finally:
out.close()
result = re.search('(\d+\.\d+\.\d+)', out_string, re.ASCII)
if result:

View File

@ -149,6 +149,7 @@ def read_setup_file(filename):
file = TextFile(filename,
strip_comments=1, skip_blanks=1, join_lines=1,
lstrip_ws=1, rstrip_ws=1)
try:
extensions = []
while True:
@ -228,5 +229,7 @@ def read_setup_file(filename):
file.warn("unrecognized argument '%s'" % word)
extensions.append(ext)
finally:
file.close()
return extensions

View File

@ -234,6 +234,8 @@ def write_file (filename, contents):
sequence of strings without line terminators) to it.
"""
f = open(filename, "w")
try:
for line in contents:
f.write(line + "\n")
finally:
f.close()

View File

@ -19,10 +19,14 @@ class BuildPyTestCase(support.TempdirManager,
def test_package_data(self):
sources = self.mkdtemp()
f = open(os.path.join(sources, "__init__.py"), "w")
try:
f.write("# Pretend this is a package.")
finally:
f.close()
f = open(os.path.join(sources, "README.txt"), "w")
try:
f.write("Info about this package")
finally:
f.close()
destination = self.mkdtemp()

View File

@ -71,7 +71,9 @@ class BuildScriptsTestCase(support.TempdirManager,
def write_script(self, dir, name, text):
f = open(os.path.join(dir, name), "w")
try:
f.write(text)
finally:
f.close()
def test_version_int(self):

View File

@ -105,8 +105,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()
f = open(rc)
try:
content = f.read()
self.assertEquals(content, WANTED)
finally:
f.close()
def test_suite():
return unittest.makeSuite(PyPIRCCommandTestCase)

View File

@ -52,7 +52,11 @@ class CoreTestCase(support.EnvironGuard, unittest.TestCase):
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):

View File

@ -88,7 +88,9 @@ 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')
try:
f.write('some content')
finally:
f.close()
wanted = ['copying %s -> %s' % (a_file, self.target2)]

View File

@ -79,11 +79,14 @@ class DistributionTestCase(support.LoggingSilencer,
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"])
@ -100,9 +103,6 @@ class DistributionTestCase(support.LoggingSilencer,
d = self.create_distribution([TESTFN])
self.assertEqual(d.get_command_packages(), ["distutils.command"])
finally:
os.unlink(TESTFN)
def test_empty_options(self):
# an empty options dictionary should not stay in the
# list of attributes
@ -260,7 +260,9 @@ class MetadataTestCase(support.TempdirManager, support.EnvironGuard,
temp_dir = self.mkdtemp()
user_filename = os.path.join(temp_dir, user_filename)
f = open(user_filename, 'w')
try:
f.write('.')
finally:
f.close()
try:

View File

@ -31,7 +31,9 @@ class FileUtilTestCase(support.TempdirManager, unittest.TestCase):
def test_move_file_verbosity(self):
f = open(self.source, 'w')
try:
f.write('some content')
finally:
f.close()
move_file(self.source, self.target, verbose=0)

View File

@ -182,8 +182,11 @@ class InstallTestCase(support.TempdirManager,
# 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

View File

@ -113,7 +113,9 @@ class msvc9compilerTestCase(support.TempdirManager,
tempdir = self.mkdtemp()
manifest = os.path.join(tempdir, 'manifest')
f = open(manifest, 'w')
try:
f.write(_MANIFEST)
finally:
f.close()
compiler = MSVCCompiler()
@ -121,8 +123,10 @@ class msvc9compilerTestCase(support.TempdirManager,
# see what we got
f = open(manifest)
try:
# removing trailing spaces
content = '\n'.join([line.rstrip() for line in f.readlines()])
finally:
f.close()
# makes sure the manifest was properly cleaned

View File

@ -118,8 +118,12 @@ class RegisterTestCase(PyPIRCCommandTestCase):
self.assertTrue(os.path.exists(self.rc))
# with the content similar to WANTED_PYPIRC
content = open(self.rc).read()
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

View File

@ -215,8 +215,12 @@ class SDistTestCase(PyPIRCCommandTestCase):
self.assertEquals(len(content), 11)
# checking the MANIFEST
manifest = open(join(self.tmp_dir, 'MANIFEST')).read()
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

View File

@ -75,8 +75,10 @@ class SysconfigTestCase(support.EnvironGuard,
def test_parse_makefile_base(self):
self.makefile = TESTFN
fd = open(self.makefile, 'w')
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'",
@ -85,8 +87,10 @@ class SysconfigTestCase(support.EnvironGuard,
def test_parse_makefile_literal_dollar(self):
self.makefile = TESTFN
fd = open(self.makefile, 'w')
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'",

View File

@ -60,26 +60,44 @@ class TextFileTestCase(support.TempdirManager, unittest.TestCase):
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)

View File

@ -115,13 +115,15 @@ def get_platform ():
# behaviour.
pass
else:
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

View File

@ -680,6 +680,7 @@ def get_platform():
# behaviour.
pass
else:
try:
m = re.search(
r'<key>ProductUserVisibleVersion</key>\s*' +
r'<string>(.*?)</string>', f.read())
@ -687,6 +688,8 @@ def get_platform():
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