Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/mail.py @ 46982:d467bae86b2d
mail: use a context manager when writing to mbox
Differential Revision: https://phab.mercurial-scm.org/D10484
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 20 Apr 2021 00:23:56 -0400 |
parents | ffd3e823a7e5 |
children | 83c0d144ef8d |
comparison
equal
deleted
inserted
replaced
46981:abd18d6306f1 | 46982:d467bae86b2d |
---|---|
206 ) | 206 ) |
207 | 207 |
208 | 208 |
209 def _mbox(mbox, sender, recipients, msg): | 209 def _mbox(mbox, sender, recipients, msg): |
210 '''write mails to mbox''' | 210 '''write mails to mbox''' |
211 fp = open(mbox, b'ab+') | 211 with open(mbox, b'ab+') as fp: |
212 # Should be time.asctime(), but Windows prints 2-characters day | 212 # Should be time.asctime(), but Windows prints 2-characters day |
213 # of month instead of one. Make them print the same thing. | 213 # of month instead of one. Make them print the same thing. |
214 date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime()) | 214 date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime()) |
215 fp.write( | 215 fp.write( |
216 b'From %s %s\n' | 216 b'From %s %s\n' |
217 % (encoding.strtolocal(sender), encoding.strtolocal(date)) | 217 % (encoding.strtolocal(sender), encoding.strtolocal(date)) |
218 ) | 218 ) |
219 fp.write(msg) | 219 fp.write(msg) |
220 fp.write(b'\n\n') | 220 fp.write(b'\n\n') |
221 fp.close() | |
222 | 221 |
223 | 222 |
224 def connect(ui, mbox=None): | 223 def connect(ui, mbox=None): |
225 """make a mail connection. return a function to send mail. | 224 """make a mail connection. return a function to send mail. |
226 call as sendmail(sender, list-of-recipients, msg).""" | 225 call as sendmail(sender, list-of-recipients, msg).""" |