Mercurial > hg
comparison mercurial/ui.py @ 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 | be8552f25cab |
children | e63b27fb0595 |
comparison
equal
deleted
inserted
replaced
43875:8376d5d62d36 | 43876:e5f69e3bb3f6 |
---|---|
1764 rdir = repopath | 1764 rdir = repopath |
1765 (fd, name) = pycompat.mkstemp( | 1765 (fd, name) = pycompat.mkstemp( |
1766 prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir | 1766 prefix=b'hg-' + extra[b'prefix'] + b'-', suffix=suffix, dir=rdir |
1767 ) | 1767 ) |
1768 try: | 1768 try: |
1769 f = os.fdopen(fd, 'wb') | 1769 with os.fdopen(fd, 'wb') as f: |
1770 f.write(util.tonativeeol(text)) | 1770 f.write(util.tonativeeol(text)) |
1771 f.close() | |
1772 | 1771 |
1773 environ = {b'HGUSER': user} | 1772 environ = {b'HGUSER': user} |
1774 if b'transplant_source' in extra: | 1773 if b'transplant_source' in extra: |
1775 environ.update( | 1774 environ.update( |
1776 {b'HGREVISION': hex(extra[b'transplant_source'])} | 1775 {b'HGREVISION': hex(extra[b'transplant_source'])} |
1792 onerr=error.Abort, | 1791 onerr=error.Abort, |
1793 errprefix=_(b"edit failed"), | 1792 errprefix=_(b"edit failed"), |
1794 blockedtag=b'editor', | 1793 blockedtag=b'editor', |
1795 ) | 1794 ) |
1796 | 1795 |
1797 f = open(name, 'rb') | 1796 with open(name, 'rb') as f: |
1798 t = util.fromnativeeol(f.read()) | 1797 t = util.fromnativeeol(f.read()) |
1799 f.close() | |
1800 finally: | 1798 finally: |
1801 os.unlink(name) | 1799 os.unlink(name) |
1802 | 1800 |
1803 return t | 1801 return t |
1804 | 1802 |