[diffoscope] 02/02: main, logging: restore old logger settings to avoid pytest vomiting in certain situations
Ximin Luo
infinity0 at debian.org
Mon Jul 17 12:03:26 CEST 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository diffoscope.
commit fe57c99828060ae77af2784961d47beb44eb74f5
Author: Ximin Luo <infinity0 at debian.org>
Date: Mon Jul 17 12:02:43 2017 +0200
main, logging: restore old logger settings to avoid pytest vomiting in certain situations
---
diffoscope/logging.py | 11 +++++++++++
diffoscope/main.py | 11 +++++------
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/diffoscope/logging.py b/diffoscope/logging.py
index 38bb9bf..3668aa3 100644
--- a/diffoscope/logging.py
+++ b/diffoscope/logging.py
@@ -17,11 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
+import contextlib
import logging
+ at contextlib.contextmanager
def setup_logging(debug, log_handler):
logger = logging.getLogger()
+ oldLevel = logger.getEffectiveLevel()
logger.setLevel(logging.DEBUG if debug else logging.WARNING)
ch = log_handler or logging.StreamHandler()
@@ -33,3 +36,11 @@ def setup_logging(debug, log_handler):
'%Y-%m-%d %H:%M:%S',
)
ch.setFormatter(formatter)
+ try:
+ yield logger
+ finally:
+ # restore old logging settings. this helps pytest not spew out errors
+ # like "ValueError: I/O operation on closed file", see
+ # https://github.com/pytest-dev/pytest/issues/14#issuecomment-272243656
+ logger.removeHandler(ch)
+ logger.setLevel(oldLevel)
diff --git a/diffoscope/main.py b/diffoscope/main.py
index b91f8b5..60e6600 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -322,11 +322,7 @@ def maybe_set_limit(config, parsed_args, key):
elif parsed_args.no_default_limits:
setattr(config, key, float("inf"))
-
-def run_diffoscope(parsed_args, post_parse):
- log_handler = ProgressManager().setup(parsed_args)
- setup_logging(parsed_args.debug, log_handler)
- post_parse(parsed_args)
+def run_diffoscope(parsed_args):
ProfileManager().setup(parsed_args)
PresenterManager().configure(parsed_args)
logger.debug("Starting diffoscope %s", VERSION)
@@ -394,7 +390,10 @@ def main(args=None):
with profile('main', 'parse_args'):
parser, post_parse = create_parser()
parsed_args = parser.parse_args(args)
- sys.exit(run_diffoscope(parsed_args, post_parse))
+ log_handler = ProgressManager().setup(parsed_args)
+ with setup_logging(parsed_args.debug, log_handler) as logger:
+ post_parse(parsed_args)
+ sys.exit(run_diffoscope(parsed_args))
except KeyboardInterrupt:
logger.info('Keyboard Interrupt')
sys.exit(2)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list