]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
complain about "global __class__" in a class body (closes #17983)
authorBenjamin Peterson <benjamin@python.org>
Wed, 15 May 2013 21:17:25 +0000 (16:17 -0500)
committerBenjamin Peterson <benjamin@python.org>
Wed, 15 May 2013 21:17:25 +0000 (16:17 -0500)
Lib/test/test_scope.py
Misc/NEWS
Python/symtable.c

index 41678b51e7469e6ddf8e91c76c3d716b70274486..26ce0424f89beb09cfa28d021b063ecea6928077 100644 (file)
@@ -743,6 +743,10 @@ class ScopeTests(unittest.TestCase):
         del tester
         self.assertIsNone(ref())
 
+    def test__Class__Global(self):
+        s = "class X:\n    global __class__\n    def f(self): super()"
+        self.assertRaises(SyntaxError, exec, s)
+
 
 def test_main():
     run_unittest(ScopeTests)
index c45a87b3cbf36b9043e768ca6868dbcbced8d849..d6c151ff8f543459bc4e6406c3a2d00a52fcacda 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #17983: Raise a SyntaxError for a ``global __class__`` statement in a
+  class body.
+
 - Issue #17927: Frame objects kept arguments alive if they had been copied into
   a cell, even if the cell was cleared.
 
index 55898f99bf14adadba3aa73ee78483ec2ca0f4bb..e686d9ebc3367c4152fc1e765d5e156aaf187df5 100644 (file)
@@ -1236,6 +1236,12 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
         asdl_seq *seq = s->v.Global.names;
         for (i = 0; i < asdl_seq_LEN(seq); i++) {
             identifier name = (identifier)asdl_seq_GET(seq, i);
+            if (st->st_cur->ste_type == ClassBlock &&
+                !PyUnicode_CompareWithASCIIString(name, "__class__")) {
+                PyErr_SetString(PyExc_SyntaxError, "cannot make __class__ global");
+                PyErr_SyntaxLocationEx(st->st_filename, s->lineno, s->col_offset);
+                return 0;
+            }
             long cur = symtable_lookup(st, name);
             if (cur < 0)
                 VISIT_QUIT(st, 0);