# HG changeset patch # User timeless # Date 2016-03-09 15:35:57 # Node ID 121d25719e9217512fdce0d3d7188fbef5b98b70 # Parent 588874c33b4df7e464bd7fd0a1063a7e41b32b24 setup: switch to with open as We're leaving the modulepolicy bit alone, because it's being rewritten in the next commit. diff --git a/setup.py b/setup.py --- a/setup.py +++ b/setup.py @@ -207,11 +207,9 @@ elif os.path.exists('.hg_archival.txt'): version = kw.get('node', '')[:12] if version: - f = open("mercurial/__version__.py", "w") - f.write('# this file is autogenerated by setup.py\n') - f.write('version = "%s"\n' % version) - f.close() - + with open("mercurial/__version__.py", "w") as f: + f.write('# this file is autogenerated by setup.py\n') + f.write('version = "%s"\n' % version) try: from mercurial import __version__ @@ -345,9 +343,8 @@ class buildhgextindex(Command): def run(self): if os.path.exists(self._indexfilename): - f = open(self._indexfilename, 'w') - f.write('# empty\n') - f.close() + with open(self._indexfilename, 'w') as f: + f.write('# empty\n') # here no extension enabled, disabled() lists up everything code = ('import pprint; from mercurial import extensions; ' @@ -356,11 +353,10 @@ class buildhgextindex(Command): if err: raise DistutilsExecError(err) - f = open(self._indexfilename, 'w') - f.write('# this file is autogenerated by setup.py\n') - f.write('docs = ') - f.write(out) - f.close() + with open(self._indexfilename, 'w') as f: + f.write('# this file is autogenerated by setup.py\n') + f.write('docs = ') + f.write(out) class buildhgexe(build_ext): description = 'compile hg.exe from mercurial/exewrapper.c' @@ -373,10 +369,9 @@ class buildhgexe(build_ext): self.compiler.dll_libraries = [] # no -lmsrvc90 hv = sys.hexversion pythonlib = 'python%d%d' % (hv >> 24, (hv >> 16) & 0xff) - f = open('mercurial/hgpythonlib.h', 'wb') - f.write('/* this file is autogenerated by setup.py */\n') - f.write('#define HGPYTHONLIB "%s"\n' % pythonlib) - f.close() + with open('mercurial/hgpythonlib.h', 'wb') as f: + f.write('/* this file is autogenerated by setup.py */\n') + f.write('#define HGPYTHONLIB "%s"\n' % pythonlib) objects = self.compiler.compile(['mercurial/exewrapper.c'], output_dir=self.build_temp) dir = os.path.dirname(self.get_ext_fullpath('dummy')) @@ -476,9 +471,8 @@ class hginstallscripts(install_scripts): libdir = uplevel * ('..' + os.sep) + self.install_lib[len(common):] for outfile in self.outfiles: - fp = open(outfile, 'rb') - data = fp.read() - fp.close() + with open(outfile, 'rb') as fp: + data = fp.read() # skip binary files if b'\0' in data: @@ -493,9 +487,8 @@ class hginstallscripts(install_scripts): continue data = data.replace(b'@LIBDIR@', libdir.encode(libdir_escape)) - fp = open(outfile, 'wb') - fp.write(data) - fp.close() + with open(outfile, 'wb') as fp: + fp.write(data) cmdclass = {'build': hgbuild, 'build_mo': hgbuildmo,