Skip to content

Comments

gh-50571: Check bytecode before accepting a breakpoint in pdb#145121

Open
aisk wants to merge 9 commits intopython:mainfrom
aisk:pdb-break-nocode
Open

gh-50571: Check bytecode before accepting a breakpoint in pdb#145121
aisk wants to merge 9 commits intopython:mainfrom
aisk:pdb-break-nocode

Conversation

@aisk
Copy link
Member

@aisk aisk commented Feb 22, 2026

Collect all linenos in a code object recursively, and check if the target breakpoint is in them. If not, report an error, just like set break point on blank lines.

Copy link
Member

@gaogaotiantian gaogaotiantian left a comment

Choose a reason for hiding this comment

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

Overall I think it's a good fix. Also relatively safe. I have a few commonts.

Lib/bdb.py Outdated
line = linecache.getline(filename, lineno)
if not line:
return 'Line %s:%d does not exist' % (filename, lineno)
source = ''.join(linecache.getlines(filename))
Copy link
Member

Choose a reason for hiding this comment

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

Let's cache this internally. Build a {filename: lineno} cache. Having some overhead for setting breakpoint is fine, but compile + co_lines() is pretty expensive. Let's avoid wasting extra time on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the cache to the Bdb instance instead of using a global variable because some tests register different code objects using the same filename.

Updating the tests would introduce a lot of changes, but since they use different Bdb instances, moving the cache here is very straightforward. In real scenarios, we always set breakpoints on the same instance, so I think there is no difference between using a global variable and an instance variable.

However, if you think this is not the right solution, I can change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants