changeset 43876:e5f69e3bb3f6

ui: use a context manager to handle file streams in edit() Differential Revision: https://phab.mercurial-scm.org/D7669
author Matt Harbison <matt_harbison@yahoo.com>
date Sat, 14 Dec 2019 18:23:37 -0500
parents 8376d5d62d36
children e63b27fb0595
files mercurial/ui.py
diffstat 1 files changed, 4 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ui.py	Thu Dec 12 15:30:16 2019 -0800
+++ b/mercurial/ui.py	Sat Dec 14 18:23:37 2019 -0500
@@ -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)