Repository navigation
Conversation
inspect.getframeinfo() and traceback read the current instruction with itertools.islice(code.co_positions(), lasti // 2). co_positions() yields one tuple per instruction, so islice builds and discards a tuple for every preceding instruction. Callers such as inspect.stack(0) do this for every frame, and the cost grows with the size of the code object. The byte offset is the same value traceback.c already passes to PyCode_Addr2Location. Expose that lookup as the private method code._co_position_at() and use it for real code objects. The returned tuple matches co_positions(), including None where a column is missing. Objects that only implement co_positions() keep the iterator path. Filename selection is unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
blurb rejects gh-issue-0, which failed the documentation build. Co-authored-by: Cursor <cursoragent@cursor.com>
|
I am not convinced by this change. Until it has been more discussed on the issue, I'm closing the PR. |
picnixz
left a comment
There was a problem hiding this comment.
cc @pablogsal as author of PEP-657 and perhaps @gaogaotiantian
| self.assertEqual( | ||
| top.filename, | ||
| inspect.getsourcefile(top.frame) or inspect.getfile(top.frame)) |
There was a problem hiding this comment.
This seems a bit arbitrary here. Why can't we determine in advance the exact value of top.filename?
| import inspect | ||
| import traceback |
There was a problem hiding this comment.
Move those imports as top-level ones. Why are they local?
| with self.assertRaises(ValueError): | ||
| code._co_position_at(-1) | ||
| with self.assertRaises(ValueError): | ||
| code._co_position_at(len(code.co_code)) | ||
| with self.assertRaises(TypeError): | ||
| code._co_position_at(None) |
There was a problem hiding this comment.
| with self.assertRaises(ValueError): | |
| code._co_position_at(-1) | |
| with self.assertRaises(ValueError): | |
| code._co_position_at(len(code.co_code)) | |
| with self.assertRaises(TypeError): | |
| code._co_position_at(None) | |
| self.assertRaises(ValueError, co_position_at, -1) | |
| self.assertRaises(ValueError, co_position_at, len(code.co_code)) | |
| self.assertRaises(TypeError, co_position_at, None) |
and store co_position_at = code._co_position_at at the beginning of the loop. In addition, those tests should be separate, they are not testing the equivalence with co_positions() exactly.
| self.assertEqual( | ||
| traceback._get_code_position(code, offset), expected) | ||
| self.assertEqual( | ||
| inspect._get_code_position(code, -1), |
There was a problem hiding this comment.
Why not testing traceback._get_code_position as well?
| with self.subTest(code=code): | ||
| for index, expected in enumerate(code.co_positions()): | ||
| offset = index * 2 | ||
| self.assertEqual(code._co_position_at(offset), expected) |
There was a problem hiding this comment.
I'd suggest looping over the various implementations of co_position_at:
for co_position_at in [
code._co_position_at,
partial(inspect._get_code_position, code),
partial(traceback._get_code_position, code),
]:
...
| # Byte offset. See inspect._get_code_position. Objects that only | ||
| # implement co_positions() keep the iterator path. | ||
| if isinstance(code, types.CodeType): | ||
| return code._co_position_at(instruction_index) |
There was a problem hiding this comment.
| # Byte offset. See inspect._get_code_position. Objects that only | |
| # implement co_positions() keep the iterator path. | |
| if isinstance(code, types.CodeType): | |
| return code._co_position_at(instruction_index) | |
| if isinstance(code, types.CodeType): | |
| return code._co_position_at(instruction_index) |
| @@ -471,6 +471,10 @@ def _walk_tb_with_full_positions(tb): | |||
| def _get_code_position(code, instruction_index): | |||
There was a problem hiding this comment.
Add a comment saying that this sbould be kept in sync with inspect._get_code_position simply.
| return NULL; | ||
| } | ||
| nbytes = _PyCode_NBYTES(code); | ||
| if (addrq < 0 || addrq >= nbytes || addrq > INT_MAX) { |
There was a problem hiding this comment.
Why not using PyLong_AsInt to avoid the check on INT_MAX?
| PyErr_SetString(PyExc_ValueError, "bytecode offset out of range"); | ||
| return NULL; | ||
| } | ||
| if (!PyCode_Addr2Location(code, (int)addrq, &start_line, &start_column, |
There was a problem hiding this comment.
I just observed that PyCode_Addr2Location never returns 0 and I think it's another issue. Could you open a separate issue for it please?
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Use PyLong_AsInt for the bytecode offset, and test the three lookup paths the same way. Co-authored-by: Cursor <cursoragent@cursor.com>
|
I have made the requested changes; please review again.
|
|
Thanks for making the requested changes! @picnixz: please review the changes made to this pull request. |
|
Opened the separate issue for |
inspect.getframeinfo() and traceback read the current instruction with itertools.islice(code.co_positions(), lasti // 2). co_positions() yields one tuple per instruction, so islice builds and discards a tuple for every preceding instruction. Callers such as inspect.stack(0) do this for every frame, and the cost grows with the size of the code object.
The byte offset is the same value traceback.c already passes to PyCode_Addr2Location. Expose that lookup as the private method code._co_position_at() and use it for real code objects. The returned tuple matches co_positions(), including None where a column is missing. Objects that only implement co_positions() keep the iterator path. Filename selection is unchanged.