Skip to content

bpo-43950: use 0-indexed column offsets for bytecode positions #27011

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 1 commit into from
Jul 4, 2021
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
2 changes: 1 addition & 1 deletion Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_co_positions_artificial_instructions(self):
# get assigned the first_lineno but they don't have other positions.
# There is no easy way of inferring them at that stage, so for now
# we don't support it.
self.assertTrue(all(positions) or not any(positions))
self.assertTrue(positions.count(None) in [0, 4])

if not any(positions):
artificial_instructions.append(instr)
Expand Down
20 changes: 10 additions & 10 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,8 @@ def generic_visit(self, node):
return
lines.add(node.lineno)
end_lines.add(node.end_lineno)
columns.add(node.col_offset + 1)
end_columns.add(node.end_col_offset + 1)
columns.add(node.col_offset)
end_columns.add(node.end_col_offset)

SourceOffsetVisitor().visit(ast_tree)

Expand Down Expand Up @@ -1058,10 +1058,10 @@ def test_compiles_to_extended_op_arg(self):

self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_SUBTRACT',
line=10_000 + 2, end_line=10_000 + 2,
column=3, end_column=9)
column=2, end_column=8)
self.assertOpcodeSourcePositionIs(compiled_code, 'INPLACE_ADD',
line=10_000 + 4, end_line=10_000 + 4,
column=3, end_column=10)
column=2, end_column=9)

def test_multiline_expression(self):
snippet = """\
Expand All @@ -1071,7 +1071,7 @@ def test_multiline_expression(self):
"""
compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_FUNCTION',
line=1, end_line=3, column=1, end_column=2)
line=1, end_line=3, column=0, end_column=1)

def test_very_long_line_end_offset(self):
# Make sure we get None for when the column offset is too large to
Expand All @@ -1088,15 +1088,15 @@ def test_complex_single_line_expression(self):

compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBSCR',
line=1, end_line=1, column=14, end_column=22)
line=1, end_line=1, column=13, end_column=21)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_MULTIPLY',
line=1, end_line=1, column=10, end_column=22)
line=1, end_line=1, column=9, end_column=21)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_ADD',
line=1, end_line=1, column=10, end_column=27)
line=1, end_line=1, column=9, end_column=26)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_MATRIX_MULTIPLY',
line=1, end_line=1, column=5, end_column=28)
line=1, end_line=1, column=4, end_column=27)
self.assertOpcodeSourcePositionIs(compiled_code, 'BINARY_SUBTRACT',
line=1, end_line=1, column=1, end_column=28)
line=1, end_line=1, column=0, end_column=27)


class TestExpressionStackSize(unittest.TestCase):
Expand Down
19 changes: 8 additions & 11 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,6 @@ PyCode_Addr2Location(PyCodeObject *co, int addrq,
int *end_line, int *end_column)
{
*start_line = PyCode_Addr2Line(co, addrq);
if (*start_line == -1) {
*start_line = 0;
}
*start_column = _PyCode_Addr2Offset(co, addrq);
*end_line = _PyCode_Addr2EndLine(co, addrq);
*end_column = _PyCode_Addr2EndOffset(co, addrq);
Expand All @@ -626,7 +623,7 @@ _PyCode_Addr2EndLine(PyCodeObject* co, int addrq)
return co->co_firstlineno;
}
else if (co->co_endlinetable == Py_None) {
return 0;
return -1;
}

assert(addrq >= 0 && addrq < PyBytes_GET_SIZE(co->co_code));
Expand All @@ -639,34 +636,34 @@ int
_PyCode_Addr2Offset(PyCodeObject* co, int addrq)
{
if (co->co_columntable == Py_None || addrq < 0) {
return 0;
return -1;
}
if (addrq % 2 == 1) {
--addrq;
}
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
return 0;
return -1;
}

unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
return bytes[addrq];
return bytes[addrq] - 1;
}

int
_PyCode_Addr2EndOffset(PyCodeObject* co, int addrq)
{
if (co->co_columntable == Py_None || addrq < 0) {
return 0;
return -1;
}
if (addrq % 2 == 0) {
++addrq;
}
if (addrq >= PyBytes_GET_SIZE(co->co_columntable)) {
return 0;
return -1;
}

unsigned char* bytes = (unsigned char*)PyBytes_AS_STRING(co->co_columntable);
return bytes[addrq];
return bytes[addrq] - 1;
}

void
Expand Down Expand Up @@ -980,7 +977,7 @@ positionsiter_dealloc(positionsiterator* pi)

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