Repository navigation
gh-161799: Look up one bytecode position in inspect and traceback #161800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8af6c9a
f45a24d
4c696aa
2839ea0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,8 +469,11 @@ def _walk_tb_with_full_positions(tb): | |
|
|
||
|
|
||
| def _get_code_position(code, instruction_index): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment saying that this sbould be kept in sync with |
||
| # Keep this in sync with inspect._get_code_position. | ||
| if instruction_index < 0: | ||
| return (None, None, None, None) | ||
| if isinstance(code, types.CodeType): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
| return code._co_position_at(instruction_index) | ||
| positions_gen = code.co_positions() | ||
| return next(itertools.islice(positions_gen, instruction_index // 2, None)) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Speed up :func:`inspect.getframeinfo` and :mod:`traceback` position lookup | ||
| on large code objects. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1501,6 +1501,42 @@ code_positionsiterator(PyObject *self, PyObject* Py_UNUSED(args)) | |
| return (PyObject*)pi; | ||
| } | ||
|
|
||
| /* One position for inspect and traceback. | ||
|
|
||
| co_positions() yields one tuple per instruction, so islice() builds a | ||
| tuple for every instruction before the requested offset. | ||
| PyCode_Addr2Location walks the same table and only fills four ints. | ||
| The offset is a byte offset (f_lasti / tb_lasti), not an instruction index. | ||
| */ | ||
| static PyObject * | ||
| code_co_position_at(PyObject *self, PyObject *indexobj) | ||
| { | ||
| PyCodeObject *code = (PyCodeObject *)self; | ||
| int start_line, end_line, start_column, end_column; | ||
| int addrq; | ||
| Py_ssize_t nbytes; | ||
|
|
||
| addrq = PyLong_AsInt(indexobj); | ||
| if (addrq == -1 && PyErr_Occurred()) { | ||
| return NULL; | ||
| } | ||
| nbytes = _PyCode_NBYTES(code); | ||
| if (addrq < 0 || addrq >= nbytes) { | ||
| PyErr_SetString(PyExc_ValueError, "bytecode offset out of range"); | ||
| return NULL; | ||
| } | ||
| if (!PyCode_Addr2Location(code, addrq, &start_line, &start_column, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we check that the line table covers the offset before we call |
||
| &end_line, &end_column)) { | ||
| PyErr_SetString(PyExc_SystemError, "PyCode_Addr2Location failed"); | ||
| return NULL; | ||
| } | ||
| return Py_BuildValue("(O&O&O&O&)", | ||
| _source_offset_converter, &start_line, | ||
| _source_offset_converter, &end_line, | ||
| _source_offset_converter, &start_column, | ||
| _source_offset_converter, &end_column); | ||
| } | ||
|
|
||
|
|
||
| /****************** | ||
| * "extra" frame eval info (see PEP 523) | ||
|
|
@@ -2873,6 +2909,7 @@ static struct PyMethodDef code_methods[] = { | |
| {"co_lines", code_linesiterator, METH_NOARGS}, | ||
| {"co_branches", code_branchesiterator, METH_NOARGS}, | ||
| {"co_positions", code_positionsiterator, METH_NOARGS}, | ||
| {"_co_position_at", code_co_position_at, METH_O}, | ||
| CODE_REPLACE_METHODDEF | ||
| CODE__VARNAME_FROM_OPARG_METHODDEF | ||
| {"__replace__", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS, | ||
|
|
||
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.
Can we check the position of the
inspect.stack(0)call itself? I changed the lookup to return the first position in the function and this test still passed.