Patchwork [6,of,7] profile: close 'fp' in all cases

login
register
mail settings
Submitter Pierre-Yves David
Date June 12, 2017, 4:17 p.m.
Message ID <b398563a8ca6167ceb94.1497284270@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/21346/
State Accepted
Headers show

Comments

Pierre-Yves David - June 12, 2017, 4:17 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@octobus.net>
# Date 1497281137 -7200
#      Mon Jun 12 17:25:37 2017 +0200
# Node ID b398563a8ca6167ceb94a2bdc19bdcdb92162636
# Parent  6db777ef801754d7f97006f865c41408f3bfc300
# EXP-Topic profile
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r b398563a8ca6
profile: close 'fp' in all cases

There are no way for this to happen today, but better be safe than sorry, no
one know how the code will evolve. We now make sure the file pointer is closed
even is profiler is None.

Patch

diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -214,15 +214,14 @@  class profile(object):
             raise
 
     def __exit__(self, exception_type, exception_value, traceback):
-        if self._profiler is None:
-            return
-        self._profiler.__exit__(exception_type, exception_value, traceback)
-        if self._output == 'blackbox':
-            val = 'Profile:\n%s' % self._fp.getvalue()
-            # ui.log treats the input as a format string,
-            # so we need to escape any % signs.
-            val = val.replace('%', '%%')
-            self._ui.log('profile', val)
+        if self._profiler is not None:
+            self._profiler.__exit__(exception_type, exception_value, traceback)
+            if self._output == 'blackbox':
+                val = 'Profile:\n%s' % self._fp.getvalue()
+                # ui.log treats the input as a format string,
+                # so we need to escape any % signs.
+                val = val.replace('%', '%%')
+                self._ui.log('profile', val)
         self._closefp()
 
     def _closefp(self):