Patchwork [2,of,7] profile: use explicit logic to control file closing

login
register
mail settings
Submitter Pierre-Yves David
Date June 12, 2017, 4:17 p.m.
Message ID <a9f13910ea2972e82c08.1497284266@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/21343/
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 1497280496 -7200
#      Mon Jun 12 17:14:56 2017 +0200
# Node ID a9f13910ea2972e82c0809fb1f46d42baa1f44d3
# Parent  ffbf377689412a98a606c7576c3a40977f6d34f2
# 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 a9f13910ea29
profile: use explicit logic to control file closing

We make the decision to close 'fp' more explicit instead of relying on the
implication of other variable. This makes the overall logic more robust.

Patch

diff --git a/mercurial/profiling.py b/mercurial/profiling.py
--- a/mercurial/profiling.py
+++ b/mercurial/profiling.py
@@ -151,6 +151,7 @@  class profile(object):
         self._ui = ui
         self._output = None
         self._fp = None
+        self._fpdoclose = True
         self._profiler = None
         self._enabled = enabled
         self._entered = False
@@ -193,6 +194,7 @@  class profile(object):
             path = self._ui.expandpath(self._output)
             self._fp = open(path, 'wb')
         else:
+            self._fpdoclose = False
             self._fp = self._ui.ferr
 
         if proffn is not None:
@@ -221,4 +223,5 @@  class profile(object):
             self._closefp()
 
     def _closefp(self):
-        self._fp.close()
+        if self._fpdoclose and self._fp is not None:
+            self._fp.close()