8000 gh-122245: move checks for writes and shadowing of __debug__ to symtable · iritkatriel/cpython@fe0a173 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe0a173

Browse files
committed
pythongh-122245: move checks for writes and shadowing of __debug__ to symtable
1 parent a15fede commit fe0a173

File tree

3 files changed

+181
-85
lines changed

3 files changed

+181
-85
lines changed

Lib/test/test_syntax.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959
Traceback (most recent call last):
6060
SyntaxError: cannot assign to __debug__
6161
62+
>>> def __debug__(): pass
63+
Traceback (most recent call last):
64+
SyntaxError: cannot assign to __debug__
65+
66+
>>> async def __debug__(): pass
67+
Traceback (most recent call last):
68+
SyntaxError: cannot assign to __debug__
69+
70+
>>> class __debug__: pass
71+
Traceback (most recent call last):
72+
SyntaxError: cannot assign to __debug__
73+
6274
>>> del __debug__
6375
Traceback (most recent call last):
6476
SyntaxError: cannot delete __debug__
@@ -786,6 +798,9 @@
786798
>>> __debug__: int
787799
Traceback (most recent call last):
788800
SyntaxError: cannot assign to __debug__
801+
>>> x.__debug__: int
802+
Traceback (most recent call last):
803+
SyntaxError: cannot assign to __debug__
789804
>>> f(a=)
790805
Traceback (most recent call last):
791806
SyntaxError: expected argument value expression
@@ -1182,6 +1197,24 @@
11821197
Traceback (most recent call last):
11831198
SyntaxError: expected ':'
11841199
1200+
>>> match x:
1201+
... case a, __debug__, b:
1202+
... pass
1203+
Traceback (most recent call last):
1204+
SyntaxError: cannot assign to __debug__
1205+
1206+
>>> match x:
1207+
... case a, b, *__debug__:
1208+
... pass
1209+
Traceback (most recent call last):
1210+
SyntaxError: cannot assign to __debug__
1211+
1212+
>>> match x:
1213+
... case Foo(a, __debug__=1, b=2):
1214+
... pass
1215+
Traceback (most recent call last):
1216+
SyntaxError: cannot assign to __debug__
1217+
11851218
>>> if x = 3:
11861219
... pass
11871220
Traceback (most recent call last):
@@ -1275,6 +1308,15 @@
12751308
Traceback (most recent call last):
12761309
SyntaxError: expected 'except' or 'finally' block
12771310
1311+
Custom error message for __debug__ as exception variable
1312+
1313+
>>> try:
1314+
... pass
1315+
... except TypeError as __debug__:
1316+
... pass
1317+
Traceback (most recent call last):
1318+
SyntaxError: cannot assign to __debug__
1319+
12781320
Custom error message for try block mixing except and except*
12791321
12801322
>>> try:
@@ -1522,6 +1564,19 @@
15221564
Traceback (most recent call last):
15231565
IndentationError: expected an indented block after class definition on line 1
15241566
1567+
>>> class C(__debug__=42): ...
1568+
Traceback (most recent call last):
1569+
SyntaxError: cannot assign to __debug__
1570+
1571+
>>> class Meta(type):
1572+
... def __new__(*args, **kwargs):
1573+
... pass
1574+
1575+
>>> class C(metaclass=Meta, __debug__=42):
1576+
... pass
1577+
Traceback (most recent call last):
1578+
SyntaxError: cannot assign to __debug__
1579+
15251580
>>> match something:
15261581
... pass
15271582
Traceback (most recent call last):
@@ -1708,6 +1763,26 @@
17081763
Traceback (most recent call last):
17091764
SyntaxError: Did you mean to use 'from ... import ...' instead?
17101765
1766+
>>> import __debug__
1767+
Traceback (most recent call last):
1768+
SyntaxError: cannot assign to __debug__
1769+
1770+
>>> import a as __debug__
1771+
Traceback (most recent call last):
1772+
SyntaxError: cannot assign to __debug__
1773+
1774+
>>> import a.b.c as __debug__
1775+
Traceback (most recent call last):
1776+
SyntaxError: cannot assign to __debug__
1777+
1778+
>>> from a import __debug__
1779+
Traceback (most recent call last):
1780+
SyntaxError: cannot assign to __debug__
1781+
1782+
>>> from a import b as __debug__
1783+
Traceback (most recent call last):
1784+
SyntaxError: cannot assign to __debug__
1785+
17111786
# Check that we dont raise the "trailing comma" error if there is more
17121787
# input to the left of the valid part that we parsed.
17131788
@@ -2186,6 +2261,10 @@ def f(x: *b)
21862261
...
21872262
SyntaxError: yield expression cannot be used within a type alias
21882263
2264+
>>> type __debug__ = int
2265+
Traceback (most recent call last):
2266+
SyntaxError: cannot assign to __debug__
2267+
21892268
>>> class A[T]((x := 3)): ...
21902269
Traceback (most recent call last):
21912270
...

Python/compile.c

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,55 +1964,6 @@ compiler_default_arguments(struct compiler *c, location loc,
19641964
return funcflags;
19651965
}
19661966

1967-
static bool
1968-
forbidden_name(struct compiler *c, location loc, identifier name,
1969-
expr_context_ty ctx)
1970-
{
1971-
if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
1972-
compiler_error(c, loc, "cannot assign to __debug__");
1973-
return true;
1974-
}
1975-
if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
1976-
compiler_error(c, loc, "cannot delete __debug__");
1977-
return true;
1978-
}
1979-
return false;
1980-
}
1981-
1982-
static int
1983-
compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
1984-
{
1985-
if (arg != NULL) {
1986-
if (forbidden_name(c, LOC(arg), arg->arg, Store)) {
1987-
return ERROR;
1988-
}
1989-
}
1990-
return SUCCESS;
1991-
}
1992-
1993-
static int
1994-
compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
1995-
{
1996-
if (args != NULL) {
1997-
for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
1998-
RETURN_IF_ERROR(
1999-
compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)));
2000-
}
2001-
}
2002-
return SUCCESS;
2003-
}
2004-
2005-
static int
2006-
compiler_check_debug_args(struct compiler *c, arguments_ty args)
2007-
{
2008-
RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->posonlyargs));
2009-
RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->args));
2010-
RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->vararg));
2011-
RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->kwonlyargs));
2012-
RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->kwarg));
2013-
return SUCCESS;
2014-
}
2015-
20161967
static int
20171968
wrap_in_stopiteration_handler(struct compiler *c)
20181969
{
@@ -2277,7 +2228,6 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
22772228
type_params = s->v.FunctionDef.type_params;
22782229
}
22792230

2280-
RETURN_IF_ERROR(compiler_check_debug_args(c, args));
22812231
RETURN_IF_ERROR(compiler_decorators(c, decos));
22822232

22832233
firstlineno = s->lineno;
@@ -2920,8 +2870,6 @@ compiler_lambda(struct compiler *c, expr_ty e)
29202870
arguments_ty args = e->v.Lambda.args;
29212871
assert(e->kind == Lambda_kind);
29222872

2923-
RETURN_IF_ERROR(compiler_check_debug_args(c, args));
2924-
29252873
location loc = LOC(e);
29262874
funcflags = compiler_default_arguments(c, loc, args);
29272875
if (funcflags == -1) {
@@ -4096,10 +4044,6 @@ compiler_nameop(struct compiler *c, location loc,
40964044
!_PyUnicode_EqualToASCIIString(name, "True") &&
40974045
!_PyUnicode_EqualToASCIIString(name, "False"));
40984046

4099-
if (forbidden_name(c, loc, name, ctx)) {
4100-
return ERROR;
4101-
}
4102-
41034047
mangled = compiler_maybe_mangle(c, name);
41044048
if (!mangled) {
41054049
return ERROR;
@@ -4905,10 +4849,6 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
49054849
if (key->arg == NULL) {
49064850
continue;
49074851
}
4908-
location loc = LOC(key);
4909-
if (forbidden_name(c, loc, key->arg, Store)) {
4910-
return ERROR;
4911-
}
49124852
for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
49134853
keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
49144854
if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
@@ -6180,9 +6120,6 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
61806120
ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names);
61816121
break;
61826122
case Store:
6183-
if (forbidden_name(c, loc, e->v.Attribute.attr, e->v.Attribute.ctx)) {
6184-
return ERROR;
6185-
}
61866123
ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names);
61876124
break;
61886125
case Del:
@@ -6376,9 +6313,6 @@ compiler_annassign(struct compiler *c, stmt_ty s)
63766313
}
63776314
switch (targ->kind) {
63786315
case Name_kind:
6379-
if (forbidden_name(c, loc, targ->v.Name.id, Store)) {
6380-
return ERROR;
6381-
}
63826316
/* If we have a simple name in a module or class, store annotation. */
63836317
if (s->v.AnnAssign.simple &&
63846318
(c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
@@ -6410,9 +6344,6 @@ compiler_annassign(struct compiler *c, stmt_ty s)
64106344
}
64116345
break;
64126346
case Attribute_kind:
6413-
if (forbidden_name(c, loc, targ->v.Attribute.attr, Store)) {
6414-
return ERROR;
6415-
}
64166347
if (!s->v.AnnAssign.value &&
64176348
check_ann_expr(c, targ->v.Attribute.value) < 0) {
64186349
return ERROR;
@@ -6676,9 +6607,6 @@ pattern_helper_store_name(struct compiler *c, location loc,
66766607
ADDOP(c, loc, POP_TOP);
66776608
return SUCCESS;
66786609
}
6679-
if (forbidden_name(c, loc, n, Store)) {
6680-
return ERROR;
6681-
}
66826610
// Can't assign to the same name twice:
66836611
int duplicate = PySequence_Contains(pc->stores, n);
66846612
RETURN_IF_ERROR(duplicate);
@@ -6836,10 +6764,6 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
68366764
Py_ssize_t nattrs = asdl_seq_LEN(attrs);
68376765
for (Py_ssize_t i = 0; i < nattrs; i++) {
68386766
identifier attr = ((identifier)asdl_seq_GET(attrs, i));
6839-
location loc = LOC((pattern_ty) asdl_seq_GET(patterns, i));
6840-
if (forbidden_name(c, loc, attr, Store)) {
6841-
return ERROR;
6842-
}
68436767
for (Py_ssize_t j = i + 1; j < nattrs; j++) {
68446768
identifier other = ((identifier)asdl_seq_GET(attrs, j));
68456769
if (!PyUnicode_Compare(attr, other)) {

0 commit comments

Comments
 (0)
0