From 79753a1476a407b4fe1fad69081395c497d53d21 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 13 Nov 2021 16:05:37 -0500 Subject: [PATCH] fix: suppress exceptions when finding source= modules. #1203 --- CHANGES.rst | 8 ++++++++ coverage/inorout.py | 2 +- doc/source.rst | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 65f0c21af..b811114d4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -22,6 +22,12 @@ This list is detailed and covers changes in each pre-release version. Unreleased ---------- +- Fix: A module specified as the ``source`` setting is imported during startup, + before the user program imports it. This could cause problems if the rest of + the program isn't ready yet. For example, `issue 1203`_ describes a Django + setting that is accessed before settings have been configured. Now that + early import is wrapped in a try/except so the error doesn't stop execution. + - Fix: A colon in a decorator expression would cause an exclusion to end too early, preventing the exclusion of the decorated function. This is now fixed. @@ -31,6 +37,8 @@ Unreleased - Debug: The `coverage debug data` command will now sniff out combinable data files, and report on all of them. +.. _issue 1203: https://github.com/nedbat/coveragepy/issues/1203 + .. _changes_612: diff --git a/coverage/inorout.py b/coverage/inorout.py index 174a19dcf..87afdaafe 100644 --- a/coverage/inorout.py +++ b/coverage/inorout.py @@ -120,7 +120,7 @@ def file_and_path_for_module(modulename): path = [] try: spec = importlib.util.find_spec(modulename) - except ImportError: + except Exception: pass else: if spec is not None: diff --git a/doc/source.rst b/doc/source.rst index 18eb706c9..85a46a339 100644 --- a/doc/source.rst +++ b/doc/source.rst @@ -46,6 +46,8 @@ scratch files written by text editors). Files that do not end with ``.py``, isn't a problem, but could cause trouble if a module has side-effects at import time. + Exceptions during the early import are suppressed and ignored. + You can further fine-tune coverage.py's attention with the ``--include`` and ``--omit`` switches (or ``[run] include`` and ``[run] omit`` configuration values). ``--include`` is a list of file name patterns. If specified, only