mail: use a context manager when writing to mbox
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 20 Apr 2021 00:23:56 -0400
changeset 46982 d467bae86b2d
parent 46981 abd18d6306f1
child 46983 e38718838808
mail: use a context manager when writing to mbox Differential Revision: https://phab.mercurial-scm.org/D10484
mercurial/mail.py
--- a/mercurial/mail.py	Mon Apr 19 21:31:24 2021 -0700
+++ b/mercurial/mail.py	Tue Apr 20 00:23:56 2021 -0400
@@ -208,17 +208,16 @@
 
 def _mbox(mbox, sender, recipients, msg):
     '''write mails to mbox'''
-    fp = open(mbox, b'ab+')
-    # Should be time.asctime(), but Windows prints 2-characters day
-    # of month instead of one. Make them print the same thing.
-    date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
-    fp.write(
-        b'From %s %s\n'
-        % (encoding.strtolocal(sender), encoding.strtolocal(date))
-    )
-    fp.write(msg)
-    fp.write(b'\n\n')
-    fp.close()
+    with open(mbox, b'ab+') as fp:
+        # Should be time.asctime(), but Windows prints 2-characters day
+        # of month instead of one. Make them print the same thing.
+        date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
+        fp.write(
+            b'From %s %s\n'
+            % (encoding.strtolocal(sender), encoding.strtolocal(date))
+        )
+        fp.write(msg)
+        fp.write(b'\n\n')
 
 
 def connect(ui, mbox=None):