Skip to content

Commit 1e93b06

Browse files
committed
complain about "global __class__" in a class body (closes #17983)
1 parent c032f16 commit 1e93b06

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_scope.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ def dig(self):
743743
del tester
744744
self.assertIsNone(ref())
745745

746+
def test__Class__Global(self):
747+
s = "class X:\n global __class__\n def f(self): super()"
748+
self.assertRaises(SyntaxError, exec, s)
749+
746750

747751
def test_main():
748752
run_unittest(ScopeTests)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #17983: Raise a SyntaxError for a ``global __class__`` statement in a
16+
class body.
17+
1518
- Issue #17927: Frame objects kept arguments alive if they had been copied into
1619
a cell, even if the cell was cleared.
1720

Python/symtable.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,12 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
12361236
asdl_seq *seq = s->v.Global.names;
12371237
for (i = 0; i < asdl_seq_LEN(seq); i++) {
12381238
identifier name = (identifier)asdl_seq_GET(seq, i);
1239+
if (st->st_cur->ste_type == ClassBlock &&
1240+
!PyUnicode_CompareWithASCIIString(name, "__class__")) {
1241+
PyErr_SetString(PyExc_SyntaxError, "cannot make __class__ global");
1242+
PyErr_SyntaxLocationEx(st->st_filename, s->lineno, s->col_offset);
1243+
return 0;
1244+
}
12391245
long cur = symtable_lookup(st, name);
12401246
if (cur < 0)
12411247
VISIT_QUIT(st, 0);

0 commit comments

Comments
 (0)