Skip to content

pdb loses local variable change after command longlist #101673

Description

@gaogaotiantian

Bug report

In pdb, ll will clear the local variable change.

def main():
    a = 1
    breakpoint()
    print(a)

main()
-> print(a)
(Pdb) p a
1
(Pdb) !a = 2
(Pdb) p a
2
(Pdb) ll
  1     def main():
  2         a = 1
  3         breakpoint()
  4  ->     print(a)
(Pdb) p a
1
(Pdb) s
1
--Return--

As you can tell, a was changed through !a = 2 but it was reverted after ll. print(a) also prints the unmodified value. Without ll, everything works fine.

The reason lies in getsourcelines in pdb.py. In that function, it tried to access obj.f_locals, which will refresh the dict with PyFrame_FastToLocalsWithError as it's a property now.

if inspect.isframe(obj) and obj.f_globals is obj.f_locals:

As a result, the local variable changes will be erased.

The original reason to check if obj.f_globals is obj.f_locals is to check whether obj is an module frame. Now that it has side effects to pdb, we can do the check with:

if inspect.isframe(obj) and obj.f_code.co_name == "<module>":

It might not be the most delicate way, but I can't think of a situation where this won't work.

I did this change locally and I have confirmed:

  1. ./python -m test -j3 passed
  2. The original bug is fixed
  3. ll still prints the full file for module frame

I'll make a PR soon and please let me know if there are any concerns about the fix.

Your environment

  • CPython versions tested on: 3.11.1
  • Operating system and architecture: Ubuntu 20.04

Linked PRs

Activity

  1. gaogaotiantian commented on Mar 5, 2023

    @gaogaotiantian
    MemberAuthor

    Hi all, it's been about a month since this issue is created. I think this issue is pretty straightforward and easy to reproduce. The fix in #101674 is easy to understand and well contained. Could someone give this issue/PR a review? Thanks.

  2. iritkatriel commented on Mar 11, 2023

    @iritkatriel
    Member

    Any bugfix PR should include a unit test to prevent regressions.

  3. gaogaotiantian commented on Mar 12, 2023

    @gaogaotiantian
    MemberAuthor

    Hi @iritkatriel , thank you for the reminder! I've added the regression test for this bug - I confirmed that the test fails without the fix and passes with the fix.

  4. added a commit that references this issue on Mar 12, 2023
  5. added a commit that references this issue on Mar 12, 2023
  6. added a commit that references this issue on Mar 12, 2023
  7. added a commit that references this issue on Mar 12, 2023
  8. added a commit that references this issue on Mar 13, 2023
  9. added a commit that references this issue on Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions