diff -r 7f4435078a8f -r 7bfa1b199972 mercurial/cmdutil.py --- a/mercurial/cmdutil.py Thu May 25 21:43:09 2017 +0900 +++ b/mercurial/cmdutil.py Sat May 27 17:58:36 2017 +0900 @@ -2642,11 +2642,11 @@ fp = open(filename, 'wb') else: fp = _unclosablefile(ui.fout) - data = ctx[path].data() - if opts.get('decode'): - data = repo.wwritedata(path, data) - fp.write(data) - fp.close() + with fp: + data = ctx[path].data() + if opts.get('decode'): + data = repo.wwritedata(path, data) + fp.write(data) # Automation often uses hg cat on single files, so special case it # for performance to avoid the cost of parsing the manifest.