Skip to content

Commit 1427c57

Browse files
committed
Address reviews
1 parent 9d2037e commit 1427c57

File tree

4 files changed

+7
-21
lines changed

4 files changed

+7
-21
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_compilation_of_ast_nodes_with_default_end_position_values(self):
204204
# Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
205205
compile(tree, "<string>", "exec")
206206

207-
def test_compilation_of_ast_nodes_with_negative_position_values(self):
207+
def test_negative_locations_for_compile(self):
208208
# See https://github.com/python/cpython/issues/130775
209209
alias = ast.alias(name='traceback', lineno=0, col_offset=0)
210210
for attrs in (
@@ -219,11 +219,10 @@ def test_compilation_of_ast_nodes_with_negative_position_values(self):
219219
], type_ignores=[])
220220

221221
# It used to crash on this step:
222-
with self.assertRaisesRegex(
223-
ValueError,
224-
'AST node has invalid location',
225-
):
226-
compile(tree, "<string>", "exec")
222+
compile(tree, "<string>", "exec")
223+
224+
# This also must not crash:
225+
ast.parse(tree, optimize=2)
227226

228227
def test_slice(self):
229228
slc = ast.parse("x[::]").body[0].value.slice
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Validate negative :mod:`ast` locations, we only allow ``-1`` as a special
2-
value.
1+
Do not crash on negative ``column`` and ``end_column`` in :mod:`ast` locations.

Python/assemble.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,8 @@ write_location_info_entry(struct assembler* a, location loc, int isize)
297297
int line_delta = loc.lineno - a->a_lineno;
298298
int column = loc.col_offset;
299299
int end_column = loc.end_col_offset;
300-
// Values are validated in `VALIDATE_POSITIONS` in `ast.c`:
301-
assert(column >= -1);
302-
assert(end_column >= -1);
303300
if (column < 0 || end_column < 0) {
304-
if (loc.end_lineno == loc.lineno || loc.end_lineno == -1) {
301+
if (loc.end_lineno == loc.lineno || loc.end_lineno < 0) {
305302
write_location_info_no_column(a, isize, line_delta);
306303
a->a_lineno = loc.lineno;
307304
return SUCCESS;

Python/ast.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ static int validate_typeparam(type_param_ty);
4040
node->col_offset, node->end_col_offset, node->lineno, node->end_lineno); \
4141
return 0; \
4242
} \
43-
if (node->lineno < -1 || node->end_lineno < -1 || \
44-
node->col_offset < -1 || node->end_col_offset < -1) { \
45-
PyErr_Format(PyExc_ValueError, \
46-
"AST node has invalid location: lineno=%d, end_lineno=%d, " \
47-
"col_offset=%d, end_col_offset=%d", \
48-
node->lineno, node->end_lineno, \
49-
node->col_offset, node->end_col_offset); \
50-
return 0; \
51-
} \
5243
if (node->lineno == node->end_lineno && node->col_offset > node->end_col_offset) { \
5344
PyErr_Format(PyExc_ValueError, \
5445
"line %d, column %d-%d is not a valid range", \

0 commit comments

Comments
 (0)