D7669: ui: use a context manager to handle file streams in edit()

mharbison72 (Matt Harbison) phabricator at mercurial-scm.org
Mon Dec 16 04:12:28 UTC 2019


mharbison72 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7669

AFFECTED FILES
  mercurial/ui.py

CHANGE DETAILS

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1766,9 +1766,8 @@
             prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir
         )
         try:
-            f = os.fdopen(fd, 'wb')
-            f.write(util.tonativeeol(text))
-            f.close()
+            with os.fdopen(fd, 'wb') as f:
+                f.write(util.tonativeeol(text))
 
             environ = {b'HGUSER': user}
             if b'transplant_source' in extra:
@@ -1794,9 +1793,8 @@
                 blockedtag=b'editor',
             )
 
-            f = open(name, 'rb')
-            t = util.fromnativeeol(f.read())
-            f.close()
+            with open(name, 'rb') as f:
+                t = util.fromnativeeol(f.read())
         finally:
             os.unlink(name)
 



To: mharbison72, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list