GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pdb loses local variable change after command longlist · Issue #101673 · python/cpython

pdb loses local variable change after command longlist #101673

New issue
Copy link
New issue
Copy link
Closed
Closed
pdb loses local variable change after command longlist#101673
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@gaogaotiantian
gaogaotiantian
opened on Feb 8, 2023
Issue body actions

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

  • GH-101673: Fix pdb bug that local variable changes lost after longlist #101674
  • [3.11] GH-101673: Fix pdb bug where local variable changes are lost after longlist (GH-101674) #102632
  • [3.10] GH-101673: Fix pdb bug where local variable changes are lost after longlist (#101674) #102633
Reacted by Oleg Iarygin, xpf629629, Travis, zyckk4, Harry, OysterMax, kelerensheng, Yao Xiao, Daniel Hahler and 3willows

Activity

  1. gaogaotiantian
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 8, 2023
  2. gaogaotiantian commented on Mar 5, 2023

    @gaogaotiantian
    gaogaotiantian
    on Mar 5, 2023
    MemberAuthor
    More actions

    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.

  3. iritkatriel commented on Mar 11, 2023

    @iritkatriel
    iritkatriel
    on Mar 11, 2023
    Member
    More actions

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

  4. gaogaotiantian commented on Mar 12, 2023

    @gaogaotiantian
    gaogaotiantian
    on Mar 12, 2023
    MemberAuthor
    More actions

    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.

  5. iritkatriel
    added a commit that references this issue on Mar 12, 2023

    GH-101673: Fix pdb bug where local variable changes are lost after lo…

    5d677c5
  6. miss-islington
    added a commit that references this issue on Mar 12, 2023

    pythonGH-101673: Fix pdb bug where local variable changes are lost af…

    bb6a288
  7. iritkatriel
    added a commit that references this issue on Mar 12, 2023

    pythonGH-101673: Fix pdb bug where local variable changes are lost af…

    4e583c5
  8. miss-islington
    added a commit that references this issue on Mar 12, 2023

    GH-101673: Fix pdb bug where local variable changes are lost after lo…

    7856026
  9. pablogsal
    added a commit that references this issue on Mar 13, 2023

    [3.10] GH-101673: Fix pdb bug where local variable changes are lost a…

    d4f8faf
  10. iritkatriel
    closed this as completedon Mar 13, 2023
  11. warsaw
    added a commit that references this issue on Apr 11, 2023

    pythonGH-101673: Fix pdb bug where local variable changes are lost af…

    21858fd
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 errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.