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 — gh-102864: Remove access to f_locals in pdb by gaogaotiantian · Pull Request #102865 · python/cpython

Conversation

@gaogaotiantian

@gaogaotiantian gaogaotiantian commented Mar 21, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Avoid accessing f_locals in pdb which will clear local variable changes.

  • Issue: Switching frame will erase the local variable changes in pdb #102864

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gaogaotiantian
Remove access to f_locals in pdb
1283c2f
@bedevere-bot bedevere-bot mentioned this pull request Mar 21, 2023
Switching frame will erase the local variable changes in pdb #102864
Closed
@bedevere-bot bedevere-bot added the awaiting review label Mar 21, 2023
blurb-it Bot and others added 2 commits March 21, 2023 04:38
@blurb-it
📜🤖 Added by blurb_it.
fb82b6a
@gaogaotiantian
Polish comments
3d85f0a
@arhadthedev arhadthedev added the stdlib Standard Library Python modules in the Lib/ directory label Mar 22, 2023
@ambv ambv changed the title gh-102864 Remove access to f_locals in pdb gh-102864: Remove access to f_locals in pdb Mar 22, 2023
ambv
ambv reviewed Mar 22, 2023
View reviewed changes
Comment thread Lib/pdb.py Outdated
self.frame_locals[frame_idx],
prompt_prefix))

def format_stack_entry(self, frame, lineno, f_locals, lprefix=': '):

@ambv ambv Mar 22, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This signature change breaks Liskov with Bdb. You could keep the tuple, no?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ambv ambv Mar 22, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I let myself refactor this in a way that doesn't require this overload. Let me know what you think.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Mar 22, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So if we are going to use a proxy, why not use it everywhere? We can get rid of all the f_local protections and keep all the signatures. The future changes that might involve f_locals won't step into the same issue anymore either. Do you think that'll be too big a change to the code structure? In theory, the functionality of all the methods should be the same.

If you think that's a reasonable change to make, I can do it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@ambv ambv Mar 22, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, I like that direction. Jury's still out whether we will be able to consider this a bug fix. We'll decide when the code change is complete. This will touch a few places but I don't think this will be too deep of a change. And we can document the proxy to explain the problem, so it will be more visible to future programmers that want to interface with the debugger.

You can also look at whether the change wouldn't be better applied to the base Bdb.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Mar 23, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So, I had a draft for the proxy solution. It's a bit more aggressive than the original fix.

The solution introduced two new classes in bdb.py - FrameProxy and TracebackProxy. They are very similar to the original FrameObject and TracebackObject, except it won't load f_locals twice. One noticeable compromise is inspect.getsourcelines() needs the actual frame object(unless we do a really ugly __unwrap__ hack in FrameProxy).

As you can tell, it's not completely backward compatible. pdb.py requires some changes to accomodate, for example, convert tracebacks to the proxy, and get the real frame for the source code.

Like I said, this approach is definitely more aggressive than the original, but it gives a pretty good protection about f_locals. pdb won't need to worry about it anymore(actually I changed the code to cache f_locals to use f_locals directly as it's more intuitive).

Not sure if this is something we want to pursue. I do have a solution that only has the proxy in pdb(HEAD~3 I think). We can use that if this is too much.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ambv ambv Mar 23, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, I almost forgot the most important thing. There are in fact two competing PEPs on fixing the core issue: PEP 558 and PEP 667. The former is currently out-of-date in terms of the current implementation of CPython. The latter lacks an implementation but I can work on that.

If PEP 667 got accepted, that would have been a better solution to the problem than patching pdb with frame proxies.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Mar 23, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, I should've mentioned the reason I need to put the cache in FrameProxy and the fact that I tried weakref and it did not work.

If PEP 667 is implemented, we don't need the proxy anymore. This issue would be solved. So at this point, do we want to proceed on this PR? I agree that having the traceback proxy in setup is better. I can take a look at it and see if it's feasible if we decided to still finish this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ambv ambv Mar 23, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Let's keep this bugfix open as context for PEP 667 and weakref support in frame objects. I'll convert it to Draft for the time being. Thanks for being active on this!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Mar 23, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Okay let's do that for now!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gaogaotiantian gaogaotiantian Mar 23, 2023

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So, the better solution is definitely waiting for weakref on frames or that f_locals performs well overall. But is this still considered as a bug? If so, should we fix it in 3.10 and 3.11? Maybe do a much better contained fix for all the versions, then move to a better solution?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ambv
ambv reviewed Mar 22, 2023
View reviewed changes
Comment thread Lib/pdb.py Outdated

def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
frame, lineno = frame_lineno
def print_stack_entry(self, frame_idx, prompt_prefix=line_prefix):

@ambv ambv Mar 22, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This is a breaking change in terms of method signature. That would mean we would only be able to accept this change for 3.12. If this is what you want, you can keep the change, as it makes using the method safer.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ambv and others added 5 commits March 22, 2023 18:42
@ambv
Don't overload format_stack_entry
d346454
@gaogaotiantian
Use proxy for all frames in pdb
f2587e6
@gaogaotiantian
Implemented proxy for Frame and Traceback
e358cd8
@gaogaotiantian
Replace curframe_f_locals to curframe.f_locals
a70d520
@gaogaotiantian
Add tests
ee4e545
@ambv
ambv marked this pull request as draft March 23, 2023 17:23
@ambv ambv added the DO-NOT-MERGE label Mar 23, 2023
@ambv ambv mentioned this pull request Mar 23, 2023
Support for weak references in frames #102960
Closed
@gaogaotiantian

gaogaotiantian commented Apr 24, 2023

Copy link
Copy Markdown
Member Author

It doesn't look like that we will have a well-rounded solution for the issue in 3.12. Should we consider fixing it in a containable way for now? @ambv

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian gaogaotiantian mentioned this pull request Feb 5, 2024
Local variable assignment is broken when combined with threads + tracing + closures #74929
Closed
@gaogaotiantian gaogaotiantian closed this May 5, 2024
@gaogaotiantian

gaogaotiantian commented May 5, 2024

Copy link
Copy Markdown
Member Author

This PR is not needed anymore as PEP 667 is merged.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gaogaotiantian
gaogaotiantian deleted the fix-flocals-usage branch May 25, 2024 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review DO-NOT-MERGE stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@gaogaotiantian @ambv @arhadthedev @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.