Patchwork [5,of,6] cat: use with statement to close output file

login
register
mail settings
Submitter Yuya Nishihara
Date May 30, 2017, 4:07 p.m.
Message ID <88dd1416922eaf34fd90.1496160433@mimosa>
Download mbox | patch
Permalink /patch/21072/
State Accepted
Headers show

Comments

Yuya Nishihara - May 30, 2017, 4:07 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1495875516 -32400
#      Sat May 27 17:58:36 2017 +0900
# Node ID 88dd1416922eaf34fd90e2e76a3d941f9a950ba3
# Parent  d403731c94f481cb5349580f374e8a8564fed2f4
cat: use with statement to close output file

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2642,11 +2642,11 @@  def cat(ui, repo, ctx, matcher, fntempla
             fp = open(filename, 'wb')
         else:
             fp = _unclosablefile(ui.fout)
-        data = ctx[path].data()
-        if opts.get('decode'):
-            data = repo.wwritedata(path, data)
-        fp.write(data)
-        fp.close()
+        with fp:
+            data = ctx[path].data()
+            if opts.get('decode'):
+                data = repo.wwritedata(path, data)
+            fp.write(data)
 
     # Automation often uses hg cat on single files, so special case it
     # for performance to avoid the cost of parsing the manifest.