Skip to content

GH-100117: Make co_lines more efficient #100447

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

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,8 @@ def test_positions(self):

def check_lines(self, func):
co = func.__code__
lines1 = list(dedup(l for (_, _, l) in co.co_lines()))
lines1 = [line for _, _, line in co.co_lines()]
self.assertEqual(lines1, list(dedup(lines1)))
lines2 = list(lines_from_postions(positions_from_location_table(co)))
for l1, l2 in zip(lines1, lines2):
self.assertEqual(l1, l2)
Expand Down
9 changes: 4 additions & 5 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ def test_leading_newlines(self):
s256 = "".join(["\n"] * 256 + ["spam"])
co = compile(s256, 'fn', 'exec')
self.assertEqual(co.co_firstlineno, 1)
lines = list(co.co_lines())
self.assertEqual(lines[0][2], 0)
self.assertEqual(lines[1][2], 257)
lines = [line for _, _, line in co.co_lines()]
self.assertEqual(lines, [0, 257])

def test_literals_with_leading_zeroes(self):
for arg in ["077787", "0xj", "0x.", "0e", "090000000000000",
Expand Down Expand Up @@ -955,9 +954,9 @@ def no_code2():
for func in (no_code1, no_code2):
with self.subTest(func=func):
code = func.__code__
lines = list(code.co_lines())
start, end, line = lines[0]
[(start, end, line)] = code.co_lines()
self.assertEqual(start, 0)
self.assertEqual(end, len(code.co_code))
self.assertEqual(line, code.co_firstlineno)

def get_code_lines(self, code):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the output of ``co_lines`` by emitting only one entry for each line
range.
50 changes: 18 additions & 32 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,38 +1189,32 @@ lineiter_dealloc(lineiterator *li)
Py_TYPE(li)->tp_free(li);
}

static PyObject *
_source_offset_converter(int *value) {
if (*value == -1) {
Py_RETURN_NONE;
}
return PyLong_FromLong(*value);
}

static PyObject *
lineiter_next(lineiterator *li)
{
PyCodeAddressRange *bounds = &li->li_line;
if (!_PyLineTable_NextAddressRange(bounds)) {
return NULL;
}
PyObject *start = NULL;
PyObject *end = NULL;
PyObject *line = NULL;
PyObject *result = PyTuple_New(3);
start = PyLong_FromLong(bounds->ar_start);
end = PyLong_FromLong(bounds->ar_end);
if (bounds->ar_line < 0) {
line = Py_NewRef(Py_None);
}
else {
line = PyLong_FromLong(bounds->ar_line);
}
if (result == NULL || start == NULL || end == NULL || line == NULL) {
goto error;
int start = bounds->ar_start;
int line = bounds->ar_line;
// Merge overlapping entries:
while (_PyLineTable_NextAddressRange(bounds)) {
if (bounds->ar_line != line) {
_PyLineTable_PreviousAddressRange(bounds);
break;
}
}
PyTuple_SET_ITEM(result, 0, start);
PyTuple_SET_ITEM(result, 1, end);
PyTuple_SET_ITEM(result, 2, line);
return result;
error:
Py_XDECREF(start);
Py_XDECREF(end);
Py_XDECREF(line);
Py_XDECREF(result);
return result;
return Py_BuildValue("iiO&", start, bounds->ar_end,
_source_offset_converter, &line);
}

PyTypeObject _PyLineIterator = {
Expand Down Expand Up @@ -1296,14 +1290,6 @@ positionsiter_dealloc(positionsiterator* pi)
Py_TYPE(pi)->tp_free(pi);
}

static PyObject*
_source_offset_converter(int* value) {
if (*value == -1) {
Py_RETURN_NONE;
}
return PyLong_FromLong(*value);
}

static PyObject*
positionsiter_next(positionsiterator* pi)
{
Expand Down