Skip to content

Commit

Permalink
fix: when stashing the singleton to sys.modules, use an actual module…
Browse files Browse the repository at this point in the history
… object. (#1399)

At least this won't trip anyone iterating through sys.modules and expects the values are actual modules.
  • Loading branch information
yilei committed Jun 11, 2022
1 parent f4273f8 commit d0fc548
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions coverage/debug.py
Expand Up @@ -12,6 +12,7 @@
import pprint
import reprlib
import sys
import types
import _thread

from coverage.misc import isolate_module
Expand Down Expand Up @@ -282,6 +283,7 @@ def __init__(self, outfile, show_process, filters):
self.write(f"New process: pid: {os.getpid()!r}, parent pid: {os.getppid()!r}\n")

SYS_MOD_NAME = '$coverage.debug.DebugOutputFile.the_one'
SINGLETON_ATTR = 'the_one_and_is_interim'

@classmethod
def get_one(cls, fileobj=None, show_process=True, filters=(), interim=False):
Expand Down Expand Up @@ -310,7 +312,8 @@ def get_one(cls, fileobj=None, show_process=True, filters=(), interim=False):
# this class can be defined more than once. But we really want
# a process-wide singleton. So stash it in sys.modules instead of
# on a class attribute. Yes, this is aggressively gross.
the_one, is_interim = sys.modules.get(cls.SYS_MOD_NAME, (None, True))
singleton_module = sys.modules.get(cls.SYS_MOD_NAME)
the_one, is_interim = getattr(singleton_module, cls.SINGLETON_ATTR, (None, True))
if the_one is None or is_interim:
if fileobj is None:
debug_file_name = os.environ.get("COVERAGE_DEBUG_FILE", FORCED_DEBUG_FILE)
Expand All @@ -321,7 +324,9 @@ def get_one(cls, fileobj=None, show_process=True, filters=(), interim=False):
else:
fileobj = sys.stderr
the_one = cls(fileobj, show_process, filters)
sys.modules[cls.SYS_MOD_NAME] = (the_one, interim)
singleton_module = types.ModuleType(cls.SYS_MOD_NAME)
setattr(singleton_module, cls.SINGLETON_ATTR, (the_one, interim))
sys.modules[cls.SYS_MOD_NAME] = singleton_module
return the_one

def write(self, text):
Expand Down

0 comments on commit d0fc548

Please sign in to comment.