[PATCH 1 of 3] setup: switch to with open as

timeless timeless at mozdev.org
Wed Mar 9 11:19:09 EST 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1457537757 0
#      Wed Mar 09 15:35:57 2016 +0000
# Node ID f9363da1d708516fbf365d41c9075018b2ce020c
# Parent  1c658391b22fb4d98ccfb60c0e57315b55634117
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
@@ -220,11 +220,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__
@@ -372,9 +370,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; '
@@ -383,11 +380,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'
@@ -400,10 +396,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'))
@@ -503,9 +498,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:
@@ -520,9 +514,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,


More information about the Mercurial-devel mailing list