gh-50571: Check bytecode before accepting a breakpoint in pdb#145121
gh-50571: Check bytecode before accepting a breakpoint in pdb#145121aisk wants to merge 12 commits intopython:mainfrom
Conversation
gaogaotiantian
left a comment
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Some pdb/bdb tests do that without using <...> names? I think it might be fine but could you elaborate on this specific thing? I need to think it through. linecache caches lines globally so if we had issues with caching these line numbers globally on bdb, we should have similar issues with linecache. Is linecache explicitly cleared somehow?
There was a problem hiding this comment.
I found that we call linecache.checkcache() to check whether the module has changed, so linecache itself works without issues. I think we should add a similar check for the lineno cache to fix this issue.
Changing it to an instance variable is not really the right solution, although it fixed the test.
If it's ok, I'll update this PR tomorrow.
Misc/NEWS.d/next/Library/2026-02-23-00-58-24.gh-issue-50571.jZIR3T.rst
Outdated
Show resolved
Hide resolved
…IR3T.rst Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
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.