Patchwork D10484: mail: use a context manager when writing to mbox

login
register
mail settings
Submitter phabricator
Date April 20, 2021, 5:11 a.m.
Message ID <differential-rev-PHID-DREV-jzpktkstzyivq2ljh4l7-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48801/
State Superseded
Headers show

Comments

phabricator - April 20, 2021, 5:11 a.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/mail.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -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):