From c038b02c3925eded48db6e21d59487482e44d978 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 12 Apr 2022 14:59:45 +0100 Subject: [PATCH 1/3] Ensure filename is passed to devtools from __init__ --- src/textual/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/textual/__init__.py b/src/textual/__init__.py index 29bf8f0185..2dd1db7ca3 100644 --- a/src/textual/__init__.py +++ b/src/textual/__init__.py @@ -1,3 +1,4 @@ +import inspect from typing import Any from rich.console import RenderableType @@ -9,7 +10,9 @@ def log(*args: object, verbosity: int = 0, **kwargs) -> None: from ._context import active_app app = active_app.get() - app.log(*args, verbosity=verbosity, **kwargs) + + caller = inspect.stack()[1] + app.log(*args, verbosity=verbosity, caller=caller, **kwargs) def panic(*args: RenderableType) -> None: From 7e15822bf9dd6691e8452e709dd3d2125bfbf4b8 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 12 Apr 2022 15:05:26 +0100 Subject: [PATCH 2/3] Use a name less likely to conflict --- src/textual/__init__.py | 2 +- src/textual/app.py | 10 +++++----- src/textual/message_pump.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/textual/__init__.py b/src/textual/__init__.py index 2dd1db7ca3..4958b9a7d1 100644 --- a/src/textual/__init__.py +++ b/src/textual/__init__.py @@ -12,7 +12,7 @@ def log(*args: object, verbosity: int = 0, **kwargs) -> None: app = active_app.get() caller = inspect.stack()[1] - app.log(*args, verbosity=verbosity, caller=caller, **kwargs) + app.log(*args, verbosity=verbosity, _textual_calling_frame=caller, **kwargs) def panic(*args: RenderableType) -> None: diff --git a/src/textual/app.py b/src/textual/app.py index 9cdd719469..212fe3bda9 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -208,7 +208,7 @@ def log( self, *objects: Any, verbosity: int = 1, - caller: inspect.FrameInfo | None = None, + _textual_calling_frame: inspect.FrameInfo | None = None, **kwargs, ) -> None: """Write to logs. @@ -224,11 +224,11 @@ def log( key_values = " ".join(f"{key}={value}" for key, value in kwargs.items()) output = " ".join((output, key_values)) - if not caller: - caller = inspect.stack()[1] + if not _textual_calling_frame: + _textual_calling_frame = inspect.stack()[1] - calling_path = caller.filename - calling_lineno = caller.lineno + calling_path = _textual_calling_frame.filename + calling_lineno = _textual_calling_frame.lineno if self.devtools.is_connected and verbosity <= self.log_verbosity: if len(objects) > 1 or len(kwargs) >= 1 and output: diff --git a/src/textual/message_pump.py b/src/textual/message_pump.py index e50d0e6465..420ffe3b60 100644 --- a/src/textual/message_pump.py +++ b/src/textual/message_pump.py @@ -87,7 +87,7 @@ def is_running(self) -> bool: return self._running def log(self, *args, **kwargs) -> None: - return self.app.log(*args, **kwargs, caller=inspect.stack()[1]) + return self.app.log(*args, **kwargs, _textual_calling_frame=inspect.stack()[1]) def set_parent(self, parent: MessagePump) -> None: self._parent = parent From 6ca51bf727af446fb1c478b58455264550cef811 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Tue, 12 Apr 2022 15:07:11 +0100 Subject: [PATCH 3/3] Add docstring --- src/textual/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/textual/app.py b/src/textual/app.py index 212fe3bda9..02040bae4b 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -216,6 +216,8 @@ def log( Args: *objects (Any): Positional arguments are converted to string and written to logs. verbosity (int, optional): Verbosity level 0-3. Defaults to 1. + _textual_calling_frame (inspect.FrameInfo | None): The frame info to include in + the log message sent to the devtools server. """ output = "" try: