setup: switch to with open as
authortimeless <timeless@mozdev.org>
Wed, 09 Mar 2016 15:35:57 +0000
changeset 28418 121d25719e92
parent 28417 588874c33b4d
child 28419 eb9d0e828c30
setup: switch to with open as We're leaving the modulepolicy bit alone, because it's being rewritten in the next commit.
setup.py
--- a/setup.py	Wed Mar 02 21:50:35 2016 +0000
+++ b/setup.py	Wed Mar 09 15:35:57 2016 +0000
@@ -207,11 +207,9 @@
         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 @@
 
     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 @@
         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 @@
             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 @@
             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 @@
                 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,