[pypy-commit] pypy py3.7: generator expressions aren't allowed in class definitions

cfbolz pypy.commits at gmail.com
Thu Feb 6 11:15:14 EST 2020


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.7
Changeset: r98675:ccca4d0a7b52
Date: 2020-02-06 16:59 +0100
http://bitbucket.org/pypy/pypy/changeset/ccca4d0a7b52/

Log:	generator expressions aren't allowed in class definitions

diff --git a/lib_pypy/_tkinter/app.py b/lib_pypy/_tkinter/app.py
--- a/lib_pypy/_tkinter/app.py
+++ b/lib_pypy/_tkinter/app.py
@@ -516,6 +516,7 @@
     def getint(self, s):
         if isinstance(s, int):
             return s
+        import pdb; pdb.set_trace()
         try:
             s = s.encode('utf-8')
         except AttributeError:
diff --git a/pypy/interpreter/astcompiler/astbuilder.py b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -466,7 +466,7 @@
         # build up a fake Call node so we can extract its pieces
         call_name = ast.Name(name, ast.Load, classdef_node.get_lineno(),
                              classdef_node.get_column())
-        call = self.handle_call(classdef_node.get_child(3), call_name)
+        call = self.handle_call(classdef_node.get_child(3), call_name, genexp_allowed=False)
         body = self.handle_suite(classdef_node.get_child(6))
         return ast.ClassDef(
             name, call.args, call.keywords,
@@ -1085,7 +1085,7 @@
             return ast.Subscript(left_expr, ast.Index(tup), ast.Load,
                                  middle.get_lineno(), middle.get_column())
 
-    def handle_call(self, args_node, callable_expr):
+    def handle_call(self, args_node, callable_expr, genexp_allowed=True):
         arg_count = 0 # position args + iterable args unpackings
         keyword_count = 0 # keyword args + keyword args unpackings
         generator_count = 0
@@ -1105,6 +1105,9 @@
                     keyword_count += 1
             last_is_comma = argument.type == tokens.COMMA
 
+        if generator_count and not genexp_allowed:
+            self.error("generator expression can't be used as bases of class definition",
+                       args_node)
         if (generator_count > 1 or
                 (generator_count and (keyword_count or arg_count)) or
                 (generator_count == 1 and last_is_comma)):
diff --git a/pypy/interpreter/astcompiler/test/test_astbuilder.py b/pypy/interpreter/astcompiler/test/test_astbuilder.py
--- a/pypy/interpreter/astcompiler/test/test_astbuilder.py
+++ b/pypy/interpreter/astcompiler/test/test_astbuilder.py
@@ -464,6 +464,9 @@
             assert isinstance(b, ast.Name)
             assert b.ctx == ast.Load
 
+        with pytest.raises(SyntaxError) as info:
+            self.get_ast("class A(x for x in T): pass")
+
     def test_function(self):
         func = self.get_first_stmt("def f(): pass")
         assert isinstance(func, ast.FunctionDef)


More information about the pypy-commit mailing list