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 — bpo-44554: refactor pdb targets (and internal tweaks) by jaraco · Pull Request #26992 · python/cpython

Conversation

@jaraco

@jaraco jaraco commented Jul 2, 2021 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Refactor module/script handling to share an interface (check method).
  • Import functools and adjust tests for the new line number for find_function.
  • Use cached_property for details.
  • Add blurb.

https://bugs.python.org/issue44554

Automerge-Triggered-By: GH:jaraco

Sorry, something went wrong.

Uh oh!

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

All reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jul 2, 2021
@bedevere-bot bedevere-bot added the awaiting core review label Jul 2, 2021
@jaraco
jaraco requested a review from iritkatriel July 2, 2021 13:26
jaraco
jaraco commented Jul 2, 2021
View reviewed changes
Comment thread Lib/pdb.py
commands = [optarg for opt, optarg in opts if opt in ['-c', '--command']]

if not run_as_module:
mainpyfile = os.path.realpath(mainpyfile)

@jaraco jaraco Jul 2, 2021

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.

This line changes the meaning of mainpyfile mid-function. It's because of this behavior that ScriptTarget contains an 'orig' property (to keep both representations), even though the "real path" form is the preferred one except during error checking.

Sorry, something went wrong.

Uh oh!

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

All reactions
@jaraco jaraco added the 🤖 automerge label Jul 2, 2021
@iritkatriel

iritkatriel commented Jul 2, 2021

Copy link
Copy Markdown
Member

This changes the behavior of pdb, right?

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

This PR extracts the refactoring only, but not the fix for bpo-44461, as discussed in iritkatriel#16.

I would be careful with it - pdb invokes code in several different way (from command line, from the pdb prompt, from Pdb subclasses that various debuggers implement).

One place this refactoring could go awry is if there were other callers of Pdb._runmodule, because this code changes the parameter type for that method. By my estimation, that method was only ever called from main().

Please do have a look at this approach as it focuses on the refactoring and ignores the concerns for now in fixing the reported issue.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021

Copy link
Copy Markdown
Member Author

This changes the behavior of pdb, right?

I do not believe this changes the behavior except for a couple subtle differences. I'll point those out in the diff.

All reactions

Sorry, something went wrong.

Uh oh!

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

iritkatriel
iritkatriel requested changes Jul 2, 2021
View reviewed changes

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

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.

A refactor changes the code to improve its structure without changing its behavior. I don't think you can call this a refactor.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting core review labels Jul 2, 2021
@bedevere-bot

bedevere-bot commented Jul 2, 2021

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

All reactions

Sorry, something went wrong.

Uh oh!

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

jaraco
jaraco commented Jul 2, 2021
View reviewed changes
Comment thread Lib/pdb.py

commands = []
run_as_module = False
for opt, optarg in opts:

@jaraco jaraco Jul 2, 2021

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.

Previously, the opts would be iterated over just once. In this new code, they are iterated over three times. Any performance degradation is negligible and outweighed by the value of disentangling the concerns.

Sorry, something went wrong.

Uh oh!

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

All reactions
jaraco
jaraco commented Jul 2, 2021
View reviewed changes
Comment thread Lib/pdb.py

target.check()

sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list

@jaraco jaraco Jul 2, 2021

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.

In the previous implementation, assignment of sys.argv[:] would happen before the sys.path change. In the new code, the sys.path change happens during the ScriptTarget.check. I contend these two operations are independent and can happen in any order.

Sorry, something went wrong.

Uh oh!

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

All reactions
@iritkatriel

iritkatriel commented Jul 2, 2021

Copy link
Copy Markdown
Member

Please do have a look at this approach as it focuses on the refactoring and ignores the concerns for now in fixing the reported issue.

This PR doesn't really have anything to do with the reported issue. It should be a new BPO.

All reactions

Sorry, something went wrong.

Uh oh!

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

jaraco
jaraco commented Jul 2, 2021
View reviewed changes
Comment thread Lib/pdb.py
return res

def check(self):
if not os.path.exists(self):

@jaraco jaraco Jul 2, 2021

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.

Previously, the existence check was done on mainpyfile before it was mutated to a "real path". Now the existence check is the real path. If compatibility is needed, this line could be changed to:

Suggested change
if not os.path.exists(self):
if not os.path.exists(self.orig):

But in my opinion, the existence check on either form is equivalent, and the form presented is more readable and distracts less from the primary intention for self.orig (the error message).

Sorry, something went wrong.

Uh oh!

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

All reactions

@johnslavik johnslavik Dec 5, 2025

Copy link
Copy Markdown
Member

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.

It's not equivalent for anonymous pipes created by process substitution (see GH-142315).

Sorry, something went wrong.

Uh oh!

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

All reactions
@jaraco

jaraco commented Jul 2, 2021

Copy link
Copy Markdown
Member Author

A refactor changes the code to improve its structure without changing its behavior. I don't think you can call this a refactor.

Perhaps I'm using the term too loosely. I believe the changes do not change the behavior or interfaces for the intended use-cases.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021

Copy link
Copy Markdown
Member Author

This PR doesn't really have anything to do with the reported issue. It should be a new BPO.

Respectfully, I disagree. The whole point of this refactor is to enable an earlier check for a runpy module target, symmetric to the syntax check on a script target, a technique which has been shown to fix the issue (thanks to your analysis and test).

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco jaraco changed the title bpo-44461: refactor pdb targets bpo-44461: refactor pdb targets (and internal tweaks) Jul 2, 2021
@iritkatriel

iritkatriel commented Jul 2, 2021

Copy link
Copy Markdown
Member

A refactor changes the code to improve its structure without changing its behavior. I don't think you can call this a refactor.

Perhaps I'm using the term too loosely. I believe the changes do not change the behavior or interfaces for the intended use-cases.

I think you are using it too loosely. A refactor improves the code without changing its behavior. As I said, pdb has quite a few different use cases. I don't know it well enough to tell which changes are minor and which will break someone's debugger. Any behavior change needs to be associated with a bpo and news item that states clearly what the change is, not sneaked into a refactor.

I'm also not sure about the motivation here. We have a fix for the reported bug.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021

Copy link
Copy Markdown
Member Author

I think you are using it too loosely. A refactor improves the code without changing its behavior.

I've updated the blurb and PR title to reflect that it's more than a refactor. But I do genuinely expect this change to result in no significant behavior change. Can you point to a behavior change that you think is more than a refactor?

I'm also not sure about the motivation here. We have a fix for the reported bug.

The motivation comes from my observation here that when the target is a script, there's a separate check for its existence (and similarly later a separate trap for bad syntax) that doesn't go through the Pdb interactive module. In my opinion, passing an invalid module to -m is equivalent to passing a non-existent script and should be handled similarly. I would not expect the interactive prompt to appear if a non-executable module is indicated as a runpy target. In other words, I don't expect Pdb to break into runpy code any more than I would expect it to break into CPython code if a script didn't exist or had a syntax error.

As I analyzed the issue, following your analysis and initial patch, I felt uneasy with adding the .botframe property. I asked myself, why is the botframe property needed for only this use case? And as you found, it's because the Bdb.run is never called. That got me thinking - the Pdb interactive prompt probably shouldn't be invoked at all if .run is never reached. So I asked myself, why is .run never reached in this case, and it's because runpy is raising an exception loading the module and checking it for runability. And that's what led me to the consideration that the module should be checked for runability early, and a failure to pass runability shouldn't trigger the "Unhandled Exception" logic and interactive prompt.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021

Copy link
Copy Markdown
Member Author

We have a fix for the reported bug.

I believe that by first applying this refactor, then applying the test and fix in iritkatriel#16, you get a superior fix, avoiding adding a .botframe property to Bdb where it was previously not needed and avoiding the need to duplicately check for _user_requested_quit (although I suspect that change may also be independently beneficial). Additionally, with this refactor, the fix for the issue becomes a few lines in a single location (implementing ModuleTarget.check) instead of altering behavior in other classes and states.

All reactions

Sorry, something went wrong.

Uh oh!

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

@iritkatriel

iritkatriel commented Jul 2, 2021

Copy link
Copy Markdown
Member

Can you point to a behavior change that you think is more than a refactor?

Since it handles bad modules differently (we agree on that right?) it's already more than a refactor.

That may be a minor change, it might be a good change, I don't know. I need to spend some time reviewing it. I've moved on to work on something else between last week and now. I don't have time at the moment to convince myself that your change is safe. I'll have a look when I have time, unless someone else will before me.

All reactions

Sorry, something went wrong.

Uh oh!

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

@jaraco

jaraco commented Jul 2, 2021 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Since it handles bad modules differently (we agree on that right?)

Sorry this wasn't clear. In this change, ModuleTarget.check is empty, matching the existing behavior. Bad modules are not handled. This change does not fix bpo-44461, but only refactors the code such that there is a designated place for such a check to exist. I would expect to merge this change prior to the fix for bpo-44461.

But as I think about it, perhaps the concerns this change addresses could be enumerated in its own bug.

I don't have time at the moment

No problem. I understand. Thanks for taking the time to review as much as you have already and for keeping me apprised of your schedule so I'm not left feeling abandoned.

All reactions

Sorry, something went wrong.

Uh oh!

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

jaraco added 6 commits July 2, 2021 14:16
@jaraco
Refactor module/script handling to share an interface (check method).
59b5906
@jaraco
Import functools and adjust tests for the new line number for find_fu…
4df7611
…nction.
@jaraco
Use cached_property for details.
86fbc39
@jaraco
Add blurb.
825e35b
@jaraco
Add comments about 'orig'
914ec41
@jaraco
Rely on singledispatch to dispatch the target.
b959e3e
@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Jul 2, 2021
@bedevere-bot

bedevere-bot commented Jul 2, 2021

Copy link
Copy Markdown

Thanks for making the requested changes!

@iritkatriel: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-bot
bedevere-bot requested a review from iritkatriel July 2, 2021 22:49
iritkatriel
iritkatriel reviewed Jul 15, 2021
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2021-07-02-18-17-56.bpo-44554.aBUmJo.rst Resolved

Uh oh!

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

Comment thread Lib/pdb.py Resolved

Uh oh!

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

jaraco added 2 commits July 18, 2021 08:42
@jaraco
Simplify changelog per suggestion.
e21f085
@jaraco
Replace singledispatch _run with a unified implementation selecting o…
fe8aea5
…n properties of the Target object.
@jaraco

jaraco commented Jul 18, 2021 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

In fe8aea5, I've pushed an additional refactor that consolidates _run(:ModuleTarget) and _run(:ScriptTarget) by implementing the differential behaviors on each target. This approach dramatically simplifies the implementation of _run but also clarifies the purpose of each target.

This change has the additional benefit of restoring the association of the comments with their function. Prior to this change, the two block comments in _run_script were disassociated from their corresponding implementation in _run_module.

All reactions

Sorry, something went wrong.

Uh oh!

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

iritkatriel
iritkatriel reviewed Jul 18, 2021
View reviewed changes

@iritkatriel iritkatriel left a comment •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

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 version is indeed clearer. Can spec() and details() be _spec() and _details()?

Sorry, something went wrong.

Uh oh!

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

All reactions
@jaraco
Suppress spurious errors in pyclbr when it encounters properties and …
0d8611c
…Union in the module.
@jaraco jaraco mentioned this pull request Jul 18, 2021
Refactor module/script handling to share an interface. iritkatriel/cpython#16
Closed
@jaraco
Make _spec and _details private.
c91ba80
@jaraco

jaraco commented Jul 18, 2021

Copy link
Copy Markdown
Member Author

This version is indeed clearer. Can spec() and details() be _spec() and _details()?

Done in c91ba80.

All reactions

Sorry, something went wrong.

Uh oh!

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

iritkatriel
iritkatriel approved these changes Jul 18, 2021
View reviewed changes

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

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.

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Jul 18, 2021
@miss-islington

miss-islington commented Jul 18, 2021

Copy link
Copy Markdown
Contributor

@jaraco: Status check is done, and it's a success ❌ .

All reactions

Sorry, something went wrong.

Uh oh!

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

@miss-islington

miss-islington commented Jul 19, 2021

Copy link
Copy Markdown
Contributor

@jaraco: Status check is done, and it's a success ✅ .

All reactions

Sorry, something went wrong.

Uh oh!

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

@miss-islington

miss-islington commented Jul 19, 2021

Copy link
Copy Markdown
Contributor

Sorry, I can't merge this PR. Reason: 3 of 5 required status checks are expected..

All reactions

Sorry, something went wrong.

Uh oh!

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

@miss-islington

miss-islington commented Jul 19, 2021

Copy link
Copy Markdown
Contributor

@jaraco: Status check is done, and it's a success ✅ .

All reactions

Sorry, something went wrong.

Uh oh!

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

@miss-islington
miss-islington merged commit 2c20558 into main Jul 19, 2021
@miss-islington
miss-islington deleted the bpo-44461-refactor-targets branch July 19, 2021 01:00
@bedevere-bot bedevere-bot removed the awaiting merge label Jul 19, 2021
shihai1991 added a commit to shihai1991/cpython that referenced this pull request Jul 20, 2021
@shihai1991
Merge remote-tracking branch 'origin/main' into bpo_42035
56e8889
* origin/main: (1146 commits)
  bpo-42064: Finalise establishing sqlite3 global state (pythonGH-27155)
  bpo-44678: Separate error message for discontinuous padding in binascii.a2b_base64 strict mode (pythonGH-27249)
  correct spelling (pythonGH-27076)
  bpo-44524: Add missed __name__ and __qualname__ to typing module objects (python#27237)
  bpo-27513: email.utils.getaddresses() now handles Header objects (python#13797)
  Clean up comma usage in Doc/library/functions.rst (python#27083)
  bpo-42238: Fix small rst issue in NEWS.d/. (python#27238)
  bpo-41972: Tweak fastsearch.h string search algorithms (pythonGH-27091)
  bpo-44340: Add support for building with clang full/thin lto (pythonGH-27231)
  bpo-44661: Update property_descr_set to use vectorcall if possible. (pythonGH-27206)
  bpo-44645: Check for interrupts on any potentially backwards edge (pythonGH-27216)
  bpo-41546: make pprint (like print) not write to stdout when it is None (pythonGH-26810)
  bpo-44554: refactor pdb targets (and internal tweaks) (pythonGH-26992)
  bpo-43086: Add handling for out-of-spec data in a2b_base64 (pythonGH-24402)
  bpo-44561: Update hyperlinks in Doc/distributing/index.rst (python#27032)
  bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (pythonGH-23278)
  bpo-44654: Do not export the union type related symbols (pythonGH-27223)
  bpo-44633: Fix parameter substitution of the union type with wrong types. (pythonGH-27218)
  bpo-44654: Refactor and clean up the union type implementation (pythonGH-27196)
  bpo-20291: Fix MSVC warnings in getargs.c (pythonGH-27211)
  ...
@hauntsaninja hauntsaninja mentioned this pull request Apr 24, 2023
gh-103578: Fix pdb reading code with non-utf8 encoding #103581
Merged
@gaogaotiantian gaogaotiantian mentioned this pull request Dec 1, 2023
Refactor pdb executable targets #112570
Merged
@johnslavik johnslavik mentioned this pull request Dec 7, 2025
gh-142315: Don't pass the "real path" of Pdb script target to system functions #142371
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@johnslavik johnslavik johnslavik left review comments
@iritkatriel iritkatriel iritkatriel approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

6 participants

@jaraco @iritkatriel @bedevere-bot @miss-islington @johnslavik @the-knights-who-say-ni
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.