Skip to content

Commit

Permalink
refactor: move exceptions to their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 2, 2021
1 parent 3f19cd7 commit 6a3d3aa
Show file tree
Hide file tree
Showing 35 changed files with 99 additions and 81 deletions.
2 changes: 1 addition & 1 deletion coverage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from coverage.control import Coverage, process_startup
from coverage.data import CoverageData
from coverage.misc import CoverageException
from coverage.exceptions import CoverageException
from coverage.plugin import CoveragePlugin, FileTracer, FileReporter
from coverage.pytracer import PyTracer

Expand Down
2 changes: 1 addition & 1 deletion coverage/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from coverage.collector import CTracer
from coverage.data import line_counts
from coverage.debug import info_formatter, info_header, short_stack
from coverage.exceptions import BaseCoverageException, ExceptionDuringRun, NoSource
from coverage.execfile import PyRunner
from coverage.misc import BaseCoverageException, ExceptionDuringRun, NoSource
from coverage.results import should_fail_under


Expand Down
3 changes: 2 additions & 1 deletion coverage/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from coverage import env
from coverage.debug import short_stack
from coverage.disposition import FileDisposition
from coverage.misc import CoverageException, isolate_module
from coverage.exceptions import CoverageException
from coverage.misc import isolate_module
from coverage.pytracer import PyTracer

os = isolate_module(os)
Expand Down
4 changes: 2 additions & 2 deletions coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import os.path
import re

from coverage.misc import contract, CoverageException, isolate_module
from coverage.misc import substitute_variables
from coverage.exceptions import CoverageException
from coverage.misc import contract, isolate_module, substitute_variables

from coverage.tomlconfig import TomlConfigParser, TomlDecodeError

Expand Down
3 changes: 2 additions & 1 deletion coverage/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
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.files import PathAliases, abs_file, relative_filename, set_relative_directory
from coverage.html import HtmlReporter
from coverage.inorout import InOrOut
from coverage.jsonreport import JsonReporter
from coverage.misc import CoverageException, bool_or_none, join_regex
from coverage.misc import bool_or_none, join_regex
from coverage.misc import DefaultValue, ensure_dir_for_file, isolate_module
from coverage.plugin import FileReporter
from coverage.plugin_support import Plugins
Expand Down
3 changes: 2 additions & 1 deletion coverage/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import glob
import os.path

from coverage.misc import CoverageException, file_be_gone
from coverage.exceptions import CoverageException
from coverage.misc import file_be_gone
from coverage.sqldata import CoverageData


Expand Down
48 changes: 48 additions & 0 deletions coverage/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Exceptions coverage.py can raise."""


class BaseCoverageException(Exception):
"""The base of all Coverage exceptions."""
pass


class CoverageException(BaseCoverageException):
"""An exception raised by a coverage.py function."""
pass


class NoSource(CoverageException):
"""We couldn't find the source for a module."""
pass


class NoCode(NoSource):
"""We couldn't find any code at all."""
pass


class NotPython(CoverageException):
"""A source file turned out not to be parsable Python."""
pass


class ExceptionDuringRun(CoverageException):
"""An exception happened while running customer code.
Construct it with three arguments, the values from `sys.exc_info`.
"""
pass


class StopEverything(BaseCoverageException):
"""An exception that means everything should stop.
The CoverageTest class converts these to SkipTest, so that when running
tests, raising this exception will automatically skip the test.
"""
pass
3 changes: 2 additions & 1 deletion coverage/execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import types

from coverage import env
from coverage.exceptions import CoverageException, ExceptionDuringRun, NoCode, NoSource
from coverage.files import canonical_filename, python_reported_file
from coverage.misc import CoverageException, ExceptionDuringRun, NoCode, NoSource, isolate_module
from coverage.misc import isolate_module
from coverage.phystokens import compile_unicode
from coverage.python import get_python_source

Expand Down
3 changes: 2 additions & 1 deletion coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import sys

from coverage import env
from coverage.misc import contract, CoverageException, join_regex, isolate_module
from coverage.exceptions import CoverageException
from coverage.misc import contract, join_regex, isolate_module


os = isolate_module(os)
Expand Down
4 changes: 2 additions & 2 deletions coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import coverage
from coverage.data import add_data_to_hash
from coverage.exceptions import CoverageException
from coverage.files import flat_rootname
from coverage.misc import CoverageException, ensure_dir, file_be_gone, Hasher, isolate_module
from coverage.misc import format_local_datetime
from coverage.misc import ensure_dir, file_be_gone, Hasher, isolate_module, format_local_datetime
from coverage.report import get_analysis_to_report
from coverage.results import Numbers
from coverage.templite import Templite
Expand Down
2 changes: 1 addition & 1 deletion coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

from coverage import env
from coverage.disposition import FileDisposition, disposition_init
from coverage.exceptions import CoverageException
from coverage.files import TreeMatcher, FnmatchMatcher, ModuleMatcher
from coverage.files import prep_patterns, find_python_files, canonical_filename
from coverage.misc import CoverageException
from coverage.python import source_for_file, source_for_morf


Expand Down
45 changes: 1 addition & 44 deletions coverage/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import types

from coverage import env
from coverage.exceptions import CoverageException

ISOLATED_MODULES = {}

Expand Down Expand Up @@ -338,47 +339,3 @@ def import_local_file(modname, modfile=None):
spec.loader.exec_module(mod)

return mod


class BaseCoverageException(Exception):
"""The base of all Coverage exceptions."""
pass


class CoverageException(BaseCoverageException):
"""An exception raised by a coverage.py function."""
pass


class NoSource(CoverageException):
"""We couldn't find the source for a module."""
pass


class NoCode(NoSource):
"""We couldn't find any code at all."""
pass


class NotPython(CoverageException):
"""A source file turned out not to be parsable Python."""
pass


class ExceptionDuringRun(CoverageException):
"""An exception happened while running customer code.
Construct it with three arguments, the values from `sys.exc_info`.
"""
pass


class StopEverything(BaseCoverageException):
"""An exception that means everything should stop.
The CoverageTest class converts these to SkipTest, so that when running
tests, raising this exception will automatically skip the test.
"""
pass
2 changes: 1 addition & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from coverage import env
from coverage.bytecode import code_objects
from coverage.debug import short_stack
from coverage.exceptions import NoSource, NotPython, StopEverything
from coverage.misc import contract, join_regex, new_contract, nice_pair, one_of
from coverage.misc import NoSource, NotPython, StopEverything
from coverage.phystokens import compile_unicode, generate_tokens, neuter_encoding_declaration


Expand Down
3 changes: 2 additions & 1 deletion coverage/plugin_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import os.path
import sys

from coverage.misc import CoverageException, isolate_module
from coverage.exceptions import CoverageException
from coverage.misc import isolate_module
from coverage.plugin import CoveragePlugin, FileTracer, FileReporter

os = isolate_module(os)
Expand Down
2 changes: 1 addition & 1 deletion coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import zipimport

from coverage import env, files
from coverage.exceptions import CoverageException, NoSource
from coverage.misc import contract, expensive, isolate_module, join_regex
from coverage.misc import CoverageException, NoSource
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
from coverage.plugin import FileReporter
Expand Down
3 changes: 2 additions & 1 deletion coverage/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"""Reporter foundation for coverage.py."""
import sys

from coverage.exceptions import CoverageException, NoSource, NotPython
from coverage.files import prep_patterns, FnmatchMatcher
from coverage.misc import CoverageException, NoSource, NotPython, ensure_dir_for_file, file_be_gone
from coverage.misc import ensure_dir_for_file, file_be_gone


def render_report(output_path, reporter, morfs):
Expand Down
3 changes: 2 additions & 1 deletion coverage/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import collections

from coverage.debug import SimpleReprMixin
from coverage.misc import contract, CoverageException, nice_pair
from coverage.exceptions import CoverageException
from coverage.misc import contract, nice_pair


class Analysis:
Expand Down
3 changes: 2 additions & 1 deletion coverage/sqldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import zlib

from coverage.debug import NoDebugging, SimpleReprMixin, clipped_repr
from coverage.exceptions import CoverageException
from coverage.files import PathAliases
from coverage.misc import CoverageException, contract, file_be_gone, filename_suffix, isolate_module
from coverage.misc import contract, file_be_gone, filename_suffix, isolate_module
from coverage.numbits import numbits_to_nums, numbits_union, nums_to_numbits
from coverage.version import __version__

Expand Down
2 changes: 1 addition & 1 deletion coverage/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import sys

from coverage.exceptions import CoverageException
from coverage.report import get_analysis_to_report
from coverage.results import Numbers
from coverage.misc import CoverageException


class SummaryReporter:
Expand Down
3 changes: 2 additions & 1 deletion coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import os
import re

from coverage.misc import CoverageException, substitute_variables
from coverage.exceptions import CoverageException
from coverage.misc import substitute_variables

# TOML support is an install-time extra option.
try:
Expand Down
2 changes: 1 addition & 1 deletion lab/parse_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import sys

from coverage.misc import CoverageException
from coverage.exceptions import CoverageException
from coverage.parser import PythonParser

for root, dirnames, filenames in os.walk(sys.argv[1]):
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pytest

from coverage import env
from coverage.misc import StopEverything
from coverage.exceptions import StopEverything


# Pytest will rewrite assertions in test modules, but not elsewhere.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import coverage
from coverage import env
from coverage.data import line_counts
from coverage.exceptions import CoverageException
from coverage.files import abs_file, relative_filename
from coverage.misc import CoverageException, import_local_file
from coverage.misc import import_local_file

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import assert_count_equal, change_dir, nice_file
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from coverage import env
from coverage.config import CoverageConfig
from coverage.data import CoverageData
from coverage.misc import ExceptionDuringRun
from coverage.exceptions import ExceptionDuringRun
from coverage.version import __url__

from tests.coveragetest import CoverageTest, OK, ERR, command_line
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import coverage
from coverage.config import HandyConfigParser
from coverage.misc import CoverageException
from coverage.exceptions import CoverageException

from tests.coveragetest import CoverageTest, UsingModulesMixin
from tests.helpers import without_module
Expand Down
2 changes: 1 addition & 1 deletion tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import coverage
from coverage import env
from coverage.misc import CoverageException
from coverage.exceptions import CoverageException

from tests.coveragetest import CoverageTest

Expand Down
2 changes: 1 addition & 1 deletion tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from coverage.data import CoverageData, combine_parallel_data
from coverage.data import add_data_to_hash, line_counts
from coverage.debug import DebugControlString
from coverage.exceptions import CoverageException
from coverage.files import PathAliases, canonical_filename
from coverage.misc import CoverageException

from tests.coveragetest import CoverageTest
from tests.helpers import assert_count_equal
Expand Down
2 changes: 1 addition & 1 deletion tests/test_execfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import pytest

from coverage import env
from coverage.exceptions import NoCode, NoSource
from coverage.execfile import run_python_file, run_python_module
from coverage.files import python_reported_file
from coverage.misc import NoCode, NoSource

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin

Expand Down
4 changes: 2 additions & 2 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import pytest

from coverage import env
from coverage import files
from coverage.exceptions import CoverageException
from coverage.files import (
TreeMatcher, FnmatchMatcher, ModuleMatcher, PathAliases,
find_python_files, abs_file, actual_path, flat_rootname, fnmatches_to_regex,
)
from coverage.misc import CoverageException
from coverage import env

from tests.coveragetest import CoverageTest

Expand Down
2 changes: 1 addition & 1 deletion tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import coverage
from coverage import env
from coverage.exceptions import CoverageException, NotPython, NoSource
from coverage.files import abs_file, flat_rootname
import coverage.html
from coverage.misc import CoverageException, NotPython, NoSource
from coverage.report import get_analysis_to_report

from tests.coveragetest import CoverageTest, TESTS_DIR
Expand Down

0 comments on commit 6a3d3aa

Please sign in to comment.