Skip to content

Conversation

davep
Copy link
Contributor

@davep davep commented Sep 13, 2023

Fixes Textualize/textual#3237

I'm undecided about this. It doesn't feel quite right to special-case this, but on the other hand print is a form of logging that seems to work differently in Textual vs the normal logging facility (which seems to do its own dance to handle the call location, before it makes it to devtools).

This does correct the issue, this does ensure that print reports the location the same as any self.log call. So this is a commit made to solicit more detail on the design decisions in the core of Textual.


Sets us up to fix Textualize/textual#3237

This PR allows the caller to pass in a frame that should be considered to be the current frame, for the purposes of working out the location of the original log/print. This change alone won't fix Textualize/textual#3237 but provides what's needed for a change to Textual that will allow fixing it.

IMPORTANT: If this is an acceptable approach, this code should be merged into textual-dev and published first. There is a corresponding PR for Textual itself that should only be merged and used after this version of textual-dev is available and able to be used. This PR is backward-compatible so should not cause problems for any existing developer-installations of Textual.

Fixes Textualize/textual#3237 I'm undecided about this. It doesn't feel quite right to special-case this, but on the other hand `print` is a form of logging that seems to work differently in Textual vs the normal logging facility (which seems to do its own dance to handle the call location, before it makes it to devtools). This does correct the issue, this does ensure that `print` reports the location the same as any `self.log` call. So this is a commit made to solicit more detail on the design decisions in the core of Textual.
@davep davep added the bug Something isn't working label Sep 13, 2023
@davep davep self-assigned this Sep 13, 2023
@davep
Copy link
Contributor Author

davep commented Sep 13, 2023

Throwing out a review request rather than just charging ahead with merging as this is a slightly hacky fix.

Looks like someone may have added black to CI without first running black over the code; so this commit is here just to feed the gatekeeping monster.
@willmcgugan
Copy link
Member

I think the way I've dealt with similar issues in the past, is to add a parameter for the offset in to the callstack. This would allow the caller to handle the logic.

@davep
Copy link
Contributor Author

davep commented Sep 13, 2023

I think the way I've dealt with similar issues in the past, is to add a parameter for the offset in to the callstack.

That was going to be my initial approach too, but that meant changing the signature of StdoutRediector.write (albeit in a backward-compatible way), which felt a little heavy-handed for this one particular case. If we're happy with that I'll look to taking that approach instead; it should end up being easier to maintain.

This time, don't special-case the print down in the StdoutRedirector, but instead allow the caller to pass in the frame we're supposed to work from. Note that with this change it *won't* fix the problem that this seeks to fix, but it will allow for a change in Textual that will fix the problem. As such this code should be merged and released *before* such code is merged into Textual. Meanwhile, this code should be safe to release first as it's designed to be backward-compatible. See Textualize/textual#3237
@davep
Copy link
Contributor Author

davep commented Sep 13, 2023

Right, done. Note the change to the description of this PR. Also note that this does not fix Textualize/textual#3237 but it does provide what's needed to fix it within Textual. Once this is live the fix will be as simple as:

diff --git a/src/textual/app.py b/src/textual/app.py index 2991f51e..5634d8f6 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -1161,7 +1161,8 @@ class App(Generic[ReturnType], DOMNode): stderr: True if the print was to stderr, or False for stdout. """ if self._devtools_redirector is not None: - self._devtools_redirector.write(text) + current_frame = inspect.currentframe() + self._devtools_redirector.write(text, current_frame.f_back if current_frame is not None else None) for target, (_stdout, _stderr) in self._capture_print.items(): if (_stderr and stderr) or (_stdout and not stderr): target.post_message(events.Print(text, stderr=stderr))
@davep
Copy link
Contributor Author

davep commented Sep 21, 2023

Tagging @darrenburns as just suggested by @willmcgugan

@davep davep merged commit be63409 into main Sep 25, 2023
@davep davep deleted the report-print-location branch September 25, 2023 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

4 participants