Patchwork [5,of,7] profile: close 'fp' on error within '__enter__'

login
register
mail settings
Submitter Pierre-Yves David
Date June 12, 2017, 4:17 p.m.
Message ID <6db777ef801754d7f970.1497284269@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/21340/
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 1497280901 -7200
#      Mon Jun 12 17:21:41 2017 +0200
# Node ID 6db777ef801754d7f97006f865c41408f3bfc300
# Parent  0f3d18d09dd1c744a1158b1d2b6b881f87fac391
# 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 6db777ef8017
profile: close 'fp' on error within '__enter__'

Previously, error when initialying the profiler would forgot to explicitly
close the file. Thank goes to Yuya Nishihara for spotting this.
Sean Farley - June 13, 2017, 2:10 a.m.
Pierre-Yves David <pierre-yves.david@ens-lyon.org> writes:

> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@octobus.net>
> # Date 1497280901 -7200
> #      Mon Jun 12 17:21:41 2017 +0200
> # Node ID 6db777ef801754d7f97006f865c41408f3bfc300
> # Parent  0f3d18d09dd1c744a1158b1d2b6b881f87fac391
> # 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 6db777ef8017
> profile: close 'fp' on error within '__enter__'
>
> Previously, error when initialying the profiler would forgot to explicitly
> close the file. Thank goes to Yuya Nishihara for spotting this.
>
> diff --git a/mercurial/profiling.py b/mercurial/profiling.py
> --- a/mercurial/profiling.py
> +++ b/mercurial/profiling.py
> @@ -188,7 +188,7 @@ class profile(object):
>  
>          self._output = self._ui.config('profiling', 'output')
>  
> -        if True:
> +        try:
>              if self._output == 'blackbox':
>                  self._fp = util.stringio()
>              elif self._output:
> @@ -209,6 +209,9 @@ class profile(object):
>  
>              self._profiler = proffn(self._ui, self._fp)
>              self._profiler.__enter__()
> +        except: # re-raises
> +            self._closefp

I think you mean self._closefp() here; will fix in-flight.

Patch

diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -188,7 +188,7 @@  class profile(object):
 
         self._output = self._ui.config('profiling', 'output')
 
-        if True:
+        try:
             if self._output == 'blackbox':
                 self._fp = util.stringio()
             elif self._output:
@@ -209,6 +209,9 @@  class profile(object):
 
             self._profiler = proffn(self._ui, self._fp)
             self._profiler.__enter__()
+        except: # re-raises
+            self._closefp
+            raise
 
     def __exit__(self, exception_type, exception_value, traceback):
         if self._profiler is None: