Skip to content

Commit

Permalink
fix: add more isolation from os-module mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 14, 2024
1 parent 9a36d33 commit 85aa5fc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ Unreleased
statements changed how they traced. This affected whether people saw the
fix for `issue 1880`_.

- fix: isolate our code more from mocking in the os module that in rare cases
can cause `bizarre behavior <pytest-cov-666_>`_.

- refactor: some code unreachable code paths in parser.py were changed to
asserts. If you encounter any of these, please let me know!

.. _pytest-cov-666: https://github.com/pytest-dev/pytest-cov/issues/666


.. start-releases
Expand Down
3 changes: 3 additions & 0 deletions coverage/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from coverage import env
from coverage.disposition import FileDisposition
from coverage.exceptions import ConfigError
from coverage.misc import isolate_module
from coverage.pytracer import PyTracer
from coverage.sysmon import SysMonitor
from coverage.types import (
Expand All @@ -21,6 +22,8 @@
)


os = isolate_module(os)

try:
# Use the C extension code when we can, for speed.
from coverage.tracer import CTracer, CFileDisposition
Expand Down
5 changes: 4 additions & 1 deletion coverage/inorout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import inspect
import itertools
import os
import os.path
import platform
import re
import sys
Expand All @@ -26,7 +27,7 @@
from coverage.exceptions import CoverageException, PluginError
from coverage.files import TreeMatcher, GlobMatcher, ModuleMatcher
from coverage.files import prep_patterns, find_python_files, canonical_filename
from coverage.misc import sys_modules_saved
from coverage.misc import isolate_module, sys_modules_saved
from coverage.python import source_for_file, source_for_morf
from coverage.types import TFileDisposition, TMorf, TWarnFn, TDebugCtl

Expand Down Expand Up @@ -57,6 +58,8 @@
pass


os = isolate_module(os)

def canonical_path(morf: TMorf, directory: bool = False) -> str:
"""Return the canonical path of the module or file `morf`.
Expand Down
4 changes: 3 additions & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
from coverage.bytecode import code_objects
from coverage.debug import short_stack
from coverage.exceptions import NoSource, NotPython
from coverage.misc import nice_pair
from coverage.misc import isolate_module, nice_pair
from coverage.phystokens import generate_tokens
from coverage.types import TArc, TLineNo

os = isolate_module(os)


class PythonParser:
"""Parse code to find executable lines, excluded lines, etc.
Expand Down
3 changes: 3 additions & 0 deletions coverage/sysmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)

from coverage.debug import short_filename, short_stack
from coverage.misc import isolate_module
from coverage.types import (
AnyCallable,
TArc,
Expand All @@ -36,6 +37,8 @@
TWarnFn,
)

os = isolate_module(os)

# pylint: disable=unused-argument

LOG = False
Expand Down
3 changes: 2 additions & 1 deletion coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

from coverage import env
from coverage.exceptions import ConfigError
from coverage.misc import import_third_party, substitute_variables
from coverage.misc import import_third_party, isolate_module, substitute_variables
from coverage.types import TConfigSectionOut, TConfigValueOut

os = isolate_module(os)

if env.PYVERSION >= (3, 11, 0, "alpha", 7):
import tomllib # pylint: disable=import-error
Expand Down

0 comments on commit 85aa5fc

Please sign in to comment.