Skip to content

Commit 59435ee

Browse files
authored
bpo-46042: Improve SyntaxError locations in the symbol table (GH-30059)
1 parent e029c53 commit 59435ee

File tree

4 files changed

+98
-65
lines changed

4 files changed

+98
-65
lines changed

Include/internal/pycore_symtable.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ struct _mod; // Type defined in pycore_ast.h
1313
typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock, AnnotationBlock }
1414
_Py_block_ty;
1515

16+
typedef enum _comprehension_type {
17+
NoComprehension = 0,
18+
ListComprehension = 1,
19+
DictComprehension = 2,
20+
SetComprehension = 3,
21+
GeneratorExpression = 4 } _Py_comprehension_ty;
22+
1623
struct _symtable_entry;
1724

1825
struct symtable {
@@ -42,14 +49,14 @@ typedef struct _symtable_entry {
4249
PyObject *ste_varnames; /* list of function parameters */
4350
PyObject *ste_children; /* list of child blocks */
4451
PyObject *ste_directives;/* locations of global and nonlocal statements */
45-
_Py_block_ty ste_type; /* module, class, or function */
52+
_Py_block_ty ste_type; /* module, class or function */
4653
int ste_nested; /* true if block is nested */
4754
unsigned ste_free : 1; /* true if block has free variables */
4855
unsigned ste_child_free : 1; /* true if a child block has free vars,
4956
including free refs to globals */
5057
unsigned ste_generator : 1; /* true if namespace is a generator */
5158
unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
52-
unsigned ste_comprehension : 1; /* true if namespace is a list comprehension */
59+
_Py_comprehension_ty ste_comprehension; /* Kind of comprehension (if any) */
5360
unsigned ste_varargs : 1; /* true if block has varargs */
5461
unsigned ste_varkeywords : 1; /* true if block has varkeywords */
5562
unsigned ste_returns_value : 1; /* true if namespace uses return with

Lib/test/test_exceptions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ def baz():
268268
check(b"\xef\xbb\xbf#coding: utf8\nprint('\xe6\x88\x91')\n", 0, -1)
269269

270270
# Errors thrown by symtable.c
271-
check('x = [(yield i) for i in range(3)]', 1, 5)
272-
check('def f():\n from _ import *', 1, 1)
273-
check('def f(x, x):\n pass', 1, 1)
271+
check('x = [(yield i) for i in range(3)]', 1, 7)
272+
check('def f():\n from _ import *', 2, 17)
273+
check('def f(x, x):\n pass', 1, 10)
274+
check('{i for i in range(5) if (j := 0) for j in range(5)}', 1, 38)
274275
check('def f(x):\n nonlocal x', 2, 3)
275276
check('def f(x):\n x = 1\n global x', 3, 3)
276277
check('nonlocal x', 1, 1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the location of the caret in :exc:`SyntaxError` exceptions emitted
2+
by the symbol table. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)