diff options
Diffstat (limited to 'coverage/debug.py')
-rw-r--r-- | coverage/debug.py | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/coverage/debug.py b/coverage/debug.py index 29dd1e7f..8ca548e9 100644 --- a/coverage/debug.py +++ b/coverage/debug.py @@ -39,7 +39,12 @@ class DebugControl: show_repr_attr = False # For AutoReprMixin - def __init__(self, options: Iterable[str], output: Optional[IO[str]]) -> None: + def __init__( + self, + options: Iterable[str], + output: Optional[IO[str]], + file_name: Optional[str] = None, + ) -> None: """Configure the options and output file for debugging.""" self.options = list(options) + FORCED_DEBUG self.suppress_callers = False @@ -49,6 +54,7 @@ class DebugControl: filters.append(add_pid_and_tid) self.output = DebugOutputFile.get_one( output, + file_name=file_name, show_process=self.should('process'), filters=filters, ) @@ -313,6 +319,7 @@ class DebugOutputFile: # pragma: debugging def get_one( cls, fileobj: Optional[IO[str]] = None, + file_name: Optional[str] = None, show_process: bool = True, filters: Iterable[Callable[[str], str]] = (), interim: bool = False, @@ -321,9 +328,9 @@ class DebugOutputFile: # pragma: debugging If `fileobj` is provided, then a new DebugOutputFile is made with it. - If `fileobj` isn't provided, then a file is chosen - (COVERAGE_DEBUG_FILE, or stderr), and a process-wide singleton - DebugOutputFile is made. + If `fileobj` isn't provided, then a file is chosen (`file_name` if + provided, or COVERAGE_DEBUG_FILE, or stderr), and a process-wide + singleton DebugOutputFile is made. `show_process` controls whether the debug file adds process-level information, and filters is a list of other message filters to apply. @@ -345,12 +352,14 @@ class DebugOutputFile: # pragma: debugging 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) - if debug_file_name in ("stdout", "stderr"): - fileobj = getattr(sys, debug_file_name) - elif debug_file_name: - fileobj = open(debug_file_name, "a") + if file_name is not None: + fileobj = open(file_name, "a", encoding="utf-8") + else: + file_name = os.environ.get("COVERAGE_DEBUG_FILE", FORCED_DEBUG_FILE) + if file_name in ("stdout", "stderr"): + fileobj = getattr(sys, file_name) + elif file_name: + fileobj = open(file_name, "a", encoding="utf-8") else: fileobj = sys.stderr the_one = cls(fileobj, show_process, filters) |