Commit 6c97510b authored by Jason Madden's avatar Jason Madden

Close various files when finished writing to them. This avoids...

Close various files when finished writing to them. This avoids ResourceWarnings on Python 3, and better supports doctests under PyPy.
parent dac64121
...@@ -4,12 +4,15 @@ Change History ...@@ -4,12 +4,15 @@ Change History
Unreleased Unreleased
========== ==========
- Open files for ``exec()`` in universal newlines mode. See - Open files for ``exec()`` in universal newlines mode. See
https://github.com/buildout/buildout/issues/130 https://github.com/buildout/buildout/issues/130
- Add ``BUILDOUT_HOME`` as an alternate way to control how the user default - Add ``BUILDOUT_HOME`` as an alternate way to control how the user default
configuration is found. configuration is found.
- Close various files when finished writing to them. This avoids
ResourceWarnings on Python 3, and better supports doctests under PyPy.
2.2.1 (2013-09-05) 2.2.1 (2013-09-05)
================== ==================
......
...@@ -868,7 +868,8 @@ def develop(setup, dest, ...@@ -868,7 +868,8 @@ def develop(setup, dest,
os.rename(setup_cfg+'-develop-aside', setup_cfg) os.rename(setup_cfg+'-develop-aside', setup_cfg)
undo.append(restore_old_setup) undo.append(restore_old_setup)
else: else:
open(setup_cfg, 'w') f = open(setup_cfg, 'w')
f.close()
undo.append(lambda: os.remove(setup_cfg)) undo.append(lambda: os.remove(setup_cfg))
setuptools.command.setopt.edit_config( setuptools.command.setopt.edit_config(
setup_cfg, dict(build_ext=build_ext)) setup_cfg, dict(build_ext=build_ext))
...@@ -1134,7 +1135,10 @@ def _create_script(contents, dest): ...@@ -1134,7 +1135,10 @@ def _create_script(contents, dest):
generated = [] generated = []
script = dest script = dest
changed = not (os.path.exists(dest) and open(dest).read() == contents) changed = True
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != contents
if is_win32: if is_win32:
# generate exe file and give the script a magic name: # generate exe file and give the script a magic name:
...@@ -1147,15 +1151,18 @@ def _create_script(contents, dest): ...@@ -1147,15 +1151,18 @@ def _create_script(contents, dest):
except AttributeError: except AttributeError:
# fall back for compatibility with older Distribute versions # fall back for compatibility with older Distribute versions
new_data = pkg_resources.resource_string('setuptools', 'cli.exe') new_data = pkg_resources.resource_string('setuptools', 'cli.exe')
if (not os.path.exists(win32_exe) or win32_changed = True
(open(win32_exe, 'rb').read() != new_data) if os.path.exists(win32_exe):
): with open(win32_exe, 'rb') as f:
win32_changed = f.reod() != new_data
if win32_changed:
# Only write it if it's different. # Only write it if it's different.
open(win32_exe, 'wb').write(new_data) open(win32_exe, 'wb').write(new_data)
generated.append(win32_exe) generated.append(win32_exe)
if changed: if changed:
open(dest, 'w').write(contents) with open(dest, 'w') as f:
f.write(contents)
logger.info( logger.info(
"Generated script %r.", "Generated script %r.",
# Normalize for windows # Normalize for windows
...@@ -1218,18 +1225,23 @@ def _pyscript(path, dest, rsetup, initialization=''): ...@@ -1218,18 +1225,23 @@ def _pyscript(path, dest, rsetup, initialization=''):
relative_paths_setup = rsetup, relative_paths_setup = rsetup,
initialization=initialization, initialization=initialization,
) )
changed = not (os.path.exists(dest) and open(dest).read() == contents) changed = True
if os.path.exists(dest):
with open(dest) as f:
changed = f.read() != contents
if is_win32: if is_win32:
# generate exe file and give the script a magic name: # generate exe file and give the script a magic name:
exe = script + '.exe' exe = script + '.exe'
open(exe, 'wb').write( with open(exe, 'wb') as f:
pkg_resources.resource_string('setuptools', 'cli.exe') f.write(
pkg_resources.resource_string('setuptools', 'cli.exe')
) )
generated.append(exe) generated.append(exe)
if changed: if changed:
open(dest, 'w').write(contents) with open(dest, 'w') as f:
f.write(contents)
try: try:
os.chmod(dest, _execute_permission()) os.chmod(dest, _execute_permission())
except (AttributeError, os.error): except (AttributeError, os.error):
......
...@@ -56,7 +56,8 @@ def cat(dir, *names): ...@@ -56,7 +56,8 @@ def cat(dir, *names):
and os.path.exists(path+'-script.py') and os.path.exists(path+'-script.py')
): ):
path = path+'-script.py' path = path+'-script.py'
print_(open(path).read(), end='') with open(path) as f:
print_(f.read(), end='')
def ls(dir, *subs): def ls(dir, *subs):
if subs: if subs:
...@@ -240,9 +241,8 @@ def buildoutSetUp(test): ...@@ -240,9 +241,8 @@ def buildoutSetUp(test):
os.chdir(sample) os.chdir(sample)
# Create a basic buildout.cfg to avoid a warning from buildout: # Create a basic buildout.cfg to avoid a warning from buildout:
open('buildout.cfg', 'w').write( with open('buildout.cfg', 'w') as f:
"[buildout]\nparts =\n" f.write("[buildout]\nparts =\n")
)
# Use the buildout bootstrap command to create a buildout # Use the buildout bootstrap command to create a buildout
zc.buildout.buildout.Buildout( zc.buildout.buildout.Buildout(
...@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler): ...@@ -375,7 +375,8 @@ class Handler(BaseHTTPRequestHandler):
self.send_header('Content-Length', str(len(out))) self.send_header('Content-Length', str(len(out)))
self.send_header('Content-Type', 'text/html') self.send_header('Content-Type', 'text/html')
else: else:
out = open(path, 'rb').read() with open(path, 'rb') as f:
out = f.read()
self.send_header('Content-Length', len(out)) self.send_header('Content-Length', len(out))
if path.endswith('.egg'): if path.endswith('.egg'):
self.send_header('Content-Type', 'application/zip') self.send_header('Content-Type', 'application/zip')
...@@ -471,8 +472,8 @@ def install(project, destination): ...@@ -471,8 +472,8 @@ def install(project, destination):
shutil.copyfile(dist.location, destination) shutil.copyfile(dist.location, destination)
else: else:
# copy link # copy link
open(os.path.join(destination, project+'.egg-link'), 'w' with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
).write(dist.location) f.write(dist.location)
def install_develop(project, destination): def install_develop(project, destination):
if not isinstance(destination, str): if not isinstance(destination, str):
...@@ -481,8 +482,8 @@ def install_develop(project, destination): ...@@ -481,8 +482,8 @@ def install_develop(project, destination):
dist = pkg_resources.working_set.find( dist = pkg_resources.working_set.find(
pkg_resources.Requirement.parse(project)) pkg_resources.Requirement.parse(project))
open(os.path.join(destination, project+'.egg-link'), 'w' with open(os.path.join(destination, project+'.egg-link'), 'w') as f:
).write(dist.location) f.write(dist.location)
def _normalize_path(match): def _normalize_path(match):
path = match.group(1) path = match.group(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment