[PATCH 03 of 10] commands: use a context manager for file I/O in debugdiscovery

Bryan O'Sullivan bos at serpentine.com
Tue Jan 12 16:50:06 CST 2016


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1452637723 28800
#      Tue Jan 12 14:28:43 2016 -0800
# Node ID 05b64240c26293abba48e2795c63eb5633ac3629
# Parent  9f45e2dd68b429b1c2f14074b2b49a48281e71eb
commands: use a context manager for file I/O in debugdiscovery

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2311,8 +2311,7 @@ def debugdiscovery(ui, repo, remoteurl="
     serverlogs = opts.get('serverlog')
     if serverlogs:
         for filename in serverlogs:
-            logfile = open(filename, 'r')
-            try:
+            with open(filename, 'r') as logfile:
                 line = logfile.readline()
                 while line:
                     parts = line.strip().split(';')
@@ -2324,9 +2323,6 @@ def debugdiscovery(ui, repo, remoteurl="
                     elif op == 'unb':
                         doit(parts[3].split(' '), parts[2].split(' '))
                     line = logfile.readline()
-            finally:
-                logfile.close()
-
     else:
         remoterevs, _checkout = hg.addbranchrevs(repo, remote, branches,
                                                  opts.get('remote_head'))


More information about the Mercurial-devel mailing list