From 30c023b5b74f9c798645cbb3f35362ae046a4c25 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 30 May 2021 17:39:20 -0400 Subject: feat: warnings are now real warnings This makes coverage warnings visible when running test suites under pytest. But it also means some uninteresting warnings would show up in our own test suite, so we had to catch or suppress those. --- coverage/control.py | 5 +++-- coverage/exceptions.py | 5 +++++ coverage/inorout.py | 13 +++++-------- coverage/pytracer.py | 6 ++---- 4 files changed, 15 insertions(+), 14 deletions(-) (limited to 'coverage') diff --git a/coverage/control.py b/coverage/control.py index bf91d447..b13acf45 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -11,6 +11,7 @@ import os.path import platform import sys import time +import warnings from coverage import env from coverage.annotate import AnnotateReporter @@ -20,7 +21,7 @@ from coverage.context import should_start_context_test_function, combine_context from coverage.data import CoverageData, combine_parallel_data from coverage.debug import DebugControl, short_stack, write_formatted_info from coverage.disposition import disposition_debug_msg -from coverage.exceptions import CoverageException +from coverage.exceptions import CoverageException, CoverageWarning from coverage.files import PathAliases, abs_file, relative_filename, set_relative_directory from coverage.html import HtmlReporter from coverage.inorout import InOrOut @@ -362,7 +363,7 @@ class Coverage: msg = f"{msg} ({slug})" if self._debug.should('pid'): msg = f"[{os.getpid()}] {msg}" - sys.stderr.write(f"Coverage.py warning: {msg}\n") + warnings.warn(msg, category=CoverageWarning, stacklevel=2) if once: self._no_warn_slugs.append(slug) diff --git a/coverage/exceptions.py b/coverage/exceptions.py index ed96fb21..6631e1ad 100644 --- a/coverage/exceptions.py +++ b/coverage/exceptions.py @@ -46,3 +46,8 @@ class StopEverything(BaseCoverageException): """ pass + + +class CoverageWarning(Warning): + """A warning from Coverage.py.""" + pass diff --git a/coverage/inorout.py b/coverage/inorout.py index fae9ef18..32eb9079 100644 --- a/coverage/inorout.py +++ b/coverage/inorout.py @@ -356,10 +356,9 @@ class InOrOut: ) break except Exception: - self.warn( - "Disabling plug-in %r due to an exception:" % (plugin._coverage_plugin_name) - ) - traceback.print_exc() + plugin_name = plugin._coverage_plugin_name + tb = traceback.format_exc() + self.warn(f"Disabling plug-in {plugin_name!r} due to an exception:\n{tb}") plugin._coverage_enabled = False continue else: @@ -503,10 +502,8 @@ class InOrOut: # The module was in sys.modules, and seems like a module with code, but # we never measured it. I guess that means it was imported before # coverage even started. - self.warn( - "Module %s was previously imported, but not measured" % pkg, - slug="module-not-measured", - ) + msg = f"Module {pkg} was previously imported, but not measured" + self.warn(msg, slug="module-not-measured") def find_possibly_unexecuted_files(self): """Find files in the areas of interest that might be untraced. diff --git a/coverage/pytracer.py b/coverage/pytracer.py index 51f08a1b..540df68c 100644 --- a/coverage/pytracer.py +++ b/coverage/pytracer.py @@ -254,10 +254,8 @@ class PyTracer: # has changed to None. dont_warn = (env.PYPY and env.PYPYVERSION >= (5, 4) and self.in_atexit and tf is None) if (not dont_warn) and tf != self._trace: # pylint: disable=comparison-with-callable - self.warn( - f"Trace function changed, measurement is likely wrong: {tf!r}", - slug="trace-changed", - ) + msg = f"Trace function changed, measurement is likely wrong: {tf!r}" + self.warn(msg, slug="trace-changed") def activity(self): """Has there been any activity?""" -- cgit v1.2.1