Show source code in tracebacks sent by Dev cog commands #5843

Merged
Jackenmen merged 14 commits from show_source_in_dev_commands_tracebacks into V3/develop 2023-04-15 11:30:19 +02:00
Jackenmen commented 2022-09-04 20:54:29 +02:00 (Migrated from github.com)

Description of the changes

Fixes an incredibly annoying thing with Dev cog's commands - the tracebacks returned by it do not include the source lines!
image

With this PR, you'll finally be able to tell where in your (sometimes maybe a little too long) eval lies the problem:
image

This PR features:

  • source code in the traceback
  • fixed line numbers (no more trying to figure out what that line number is trying to refer to exactly...)
  • stripping of frames that are actually part of Red's code, not the code passed to the command

Have the changes in this PR been tested?

Yes

### Description of the changes Fixes an incredibly annoying thing with Dev cog's commands - the tracebacks returned by it do not include the source lines! ![image](https://user-images.githubusercontent.com/6032823/188328831-ea50a9bf-611b-4dee-ba2e-9cce011174f1.png) With this PR, you'll finally be able to tell where in your (sometimes maybe a little too long) eval lies the problem: ![image](https://user-images.githubusercontent.com/6032823/188328874-d64bdba0-08c6-4096-9755-34f088c30695.png) This PR features: - source code in the traceback - fixed line numbers (no more trying to figure out what that line number is trying to refer to exactly...) - stripping of frames that are actually part of Red's code, not the code passed to the command ### Have the changes in this PR been tested? Yes
Jackenmen commented 2023-01-03 08:06:50 +01:00 (Migrated from github.com)

I reorganized the dev cog to improve testability, added a bunch of tests, and fixed a couple of issues based on them:

  • source code of syntax errors sometimes didn't show up (may have been dependent on the complexity of the parsed expression)
  • fine-grained error locations in tracebacks were off-by-one if the error was in the last line and that line didn't end with \n
  • the result of [p]debug command was not shown if it was None (regression in comparison to current behavior)
  • start code block regex didn't account for how Discord renders a code block with leading empty lines (it doesn't render them so line numbers need to account for that)
I reorganized the dev cog to improve testability, added a bunch of tests, and fixed a couple of issues based on them: - source code of syntax errors sometimes didn't show up (may have been dependent on the complexity of the parsed expression) - fine-grained error locations in tracebacks were off-by-one if the error was in the last line and that line didn't end with `\n` - the result of `[p]debug` command was not shown if it was `None` (regression in comparison to current behavior) - start code block regex didn't account for how Discord renders a code block with leading empty lines (it doesn't render them so line numbers need to account for that)
Zephyrkul (Migrated from github.com) reviewed 2023-01-15 20:01:25 +01:00
@ -37,0 +284,4 @@
lineno = frame_summary.lineno
if lineno is None:
continue
Zephyrkul (Migrated from github.com) commented 2023-01-15 20:01:25 +01:00
        source_lines = self._source.splitlines(keepends=True)
```suggestion source_lines = self._source.splitlines(keepends=True) ```
Jackenmen (Migrated from github.com) reviewed 2023-01-15 20:02:28 +01:00
@ -37,0 +284,4 @@
lineno = frame_summary.lineno
if lineno is None:
continue
Jackenmen (Migrated from github.com) commented 2023-01-15 20:02:28 +01:00
Not equivalent, see this commit and the comment above this line: https://github.com/Cog-Creators/Red-DiscordBot/pull/5843/commits/3877520958c44963e4ed055125d5980fa4907c46
Jackenmen (Migrated from github.com) reviewed 2023-01-15 20:07:20 +01:00
@ -37,0 +284,4 @@
lineno = frame_summary.lineno
if lineno is None:
continue
Jackenmen (Migrated from github.com) commented 2023-01-15 20:07:20 +01:00

The difference can most easily be seen by checking against this test case:
github.com/Jackenmen/Red-DiscordBot@f6f7bd5e9e/tests/core/test_dev_commands.py (L234-L243)

    async def _run_dev_output(
        monkeypatch: pytest.MonkeyPatch,
        source: str,
        result: str,
        *,
        debug: bool = False,
        eval: bool = False,
        repl: bool = False,
    ) -> None:
        source = textwrap.dedent(source)
        result = textwrap.dedent(result)
        monkeypatch.setattr("redbot.core.dev_commands.sanitize_output", lambda ctx, s: s)
    
        if debug:
            output = _get_dev_output(source)
            await output.run_debug()
>           assert str(output) == result
E           AssertionError: assert 'Traceback (m...ion by zero\n' == 'Traceback (m...ion by zero\n'
E             Skipping 89 identical leading characters in diff, use -v to show
E               )
E             -         ~~^~~
E             +          ~~^~~
E             ? +
E               ZeroDivisionError: division by zero

tests/core/test_dev_commands.py:162: AssertionError
The difference can most easily be seen by checking against this test case: https://github.com/Jackenmen/Red-DiscordBot/blob/f6f7bd5e9e4032aec2c8449b0cebd08f1712bc51/tests/core/test_dev_commands.py#L234-L243 ``` async def _run_dev_output( monkeypatch: pytest.MonkeyPatch, source: str, result: str, *, debug: bool = False, eval: bool = False, repl: bool = False, ) -> None: source = textwrap.dedent(source) result = textwrap.dedent(result) monkeypatch.setattr("redbot.core.dev_commands.sanitize_output", lambda ctx, s: s) if debug: output = _get_dev_output(source) await output.run_debug() > assert str(output) == result E AssertionError: assert 'Traceback (m...ion by zero\n' == 'Traceback (m...ion by zero\n' E Skipping 89 identical leading characters in diff, use -v to show E ) E - ~~^~~ E + ~~^~~ E ? + E ZeroDivisionError: division by zero tests/core/test_dev_commands.py:162: AssertionError ```
Kreusada (Migrated from github.com) reviewed 2023-02-22 23:43:35 +01:00
Kreusada (Migrated from github.com) left a comment

I've tested these changes and they seem good to me, I don't have any additional comments (although maybe to be picky, ctx parameters could be type hinted).

On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util.

image

I've tested these changes and they seem good to me, I don't have any additional comments (although maybe to be picky, `ctx` parameters could be type hinted). On a separate note, do you think that `cleanup_code` should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util. ![image](https://user-images.githubusercontent.com/67752638/220776599-8fdb30d2-cd97-4f96-b106-3512a614e747.png)
japandotorg commented 2023-02-23 08:21:03 +01:00 (Migrated from github.com)

On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util.

+1 from me on this

> On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util. +1 from me on this
AAA3A-AAA3A commented 2023-02-23 11:34:41 +01:00 (Migrated from github.com)

I have tested this change a bit and it works.

On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util.

I also think that cleanup_code should become a utility as it is used a lot.

I have tested this change a bit and it works. > On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util. I also think that `cleanup_code` should become a utility as it is used a lot.
Jackenmen commented 2023-02-23 13:52:28 +01:00 (Migrated from github.com)

On a separate note, do you think that cleanup_code should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util.

I'm not sure, I've seen it used in a few different scenarios in 3rd-party cogs so I see some merit to this request. If this were to be pursued, I think it should be a part of a separate PR.

> On a separate note, do you think that `cleanup_code` should be added as a chat_formatting util? I've used this a lot by simply pasting this function in my code, maybe it would be helpful as a util. I'm not sure, I've seen it used in a few different scenarios in 3rd-party cogs so I see some merit to this request. If this were to be pursued, I think it should be a part of a separate PR.
Kreusada commented 2023-02-23 15:12:13 +01:00 (Migrated from github.com)

I'm not sure, I've seen it used in a few different scenarios in 3rd-party cogs so I see some merit to this request. If this were to be pursued, I think it should be a part of a separate PR.

Agreed, I'm happy to make a separate PR implementing this if/when it is wanted.

> I'm not sure, I've seen it used in a few different scenarios in 3rd-party cogs so I see some merit to this request. If this were to be pursued, I think it should be a part of a separate PR. Agreed, I'm happy to make a separate PR implementing this if/when it is wanted.
Kreusada (Migrated from github.com) approved these changes 2023-04-15 11:30:12 +02:00
Kreusada (Migrated from github.com) left a comment

LGTM 👍

LGTM 👍
Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!5843
No description provided.