Skip to content

gh-103068: Check condition expression of breakpoints for pdb #103069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 29, 2023

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Mar 28, 2023

If the condition expression is not even valid, we should ignore the command and warn the users.

As this is a similar check as display, I made some modifications for display command so they are using the same logic. This also freed _getval_except to do what it used to do - provide the string regardless of the exception. Also, self.error is used for display command when it failed because that's what other(most of?) commands do and it will make the most of sense to users.

walrus operator is used but this will merge back to 3.10 at most, so all the versions affected should support it.

@@ -603,7 +609,7 @@ def test_pdb_display_command():
> <doctest test.test_pdb.test_pdb_display_command[0]>(4)test_function()
-> a = 1
(Pdb) display +
Unable to display +: ** raised SyntaxError: invalid syntax **
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have two separate error types for display now

  • ** raised <...> **
  • *** Unable to display <...>

I think it's worth covering ** raised <...> ** cases in the tests as well, for instance:

(Pdb) display undefined
display undefined: ** raised NameError: name 'undefined' is not defined **

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I noticed that break behaviour is different: pdb conservatively stops on the breakpoint ignoring the runtime errors. I think it's worth covering in the tests as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a suggestion for a test for the break behavior?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, here is a patch:

--- a/Lib/test/test_pdb.py
+++ b/Lib/test/test_pdb.py
@@ -266,6 +266,8 @@ def test_pdb_breakpoint_commands():
     ...     'commands 10',  # out of range
     ...     'commands a',   # display help
     ...     'commands 4',   # already deleted
+    ...     'break 6, undefined', # condition causing `NameError` during evaluation
+    ...     'continue', # will stop, ignoring runtime error
     ...     'continue',
     ... ]):
     ...    test_function()
@@ -337,8 +339,13 @@ def test_pdb_breakpoint_commands():
             end
     (Pdb) commands 4
     *** cannot set commands: Breakpoint 4 already deleted
+    (Pdb) break 6, undefined
+    Breakpoint 5 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:6
     (Pdb) continue
     3
+    > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(6)test_function()
+    -> print(4)
+    (Pdb) continue
     4
     """

@ambv
Copy link
Contributor

ambv commented Mar 28, 2023

I noticed that the pattern to call traceback.format_exception_only(exc)[-1].strip() is repeated a few times so I made this into a method. Moreover, even though this was sometimes using the two-argument form and sometimes the single-argument form, since Python 3.10 the two are equivalent so it makes no difference. Therefore, I just added the single-argument form.

I changed the name of the check method, and added a test for an otherwise failing display, per Artem's suggestions.

@gaogaotiantian
Copy link
Member Author

LGTM

@gaogaotiantian
Copy link
Member Author

Side note: do we use type hints in standard library? Most of the builtin libraries do not have type hints(at least inline). Just saw you added a function with one and it just stands out.

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 28, 2023

Side note: do we use type hints in standard library? Most of the builtin libraries do not have type hints(at least inline). Just saw you added a function with one and it just stands out.

Most stdlib modules don't have type hints, but there isn't a hard-and-fast rule against including them for new code, if it makes the purpose/signature of the function easier to understand. You'll find lots of type hints in tomllib and importlib.metadata, and a few in typing.py itself and parts of asyncio.

@ambv
Copy link
Contributor

ambv commented Mar 28, 2023

There used to be a hard rule against type hints in the past but it's been eroding since. Thanks to Jason's precedent in pdb I felt like adding new type hints wasn't out of place.

@ambv ambv merged commit e375bff into python:main Mar 29, 2023
@gaogaotiantian gaogaotiantian deleted the pdb-condition-check branch March 30, 2023 05:17
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants