Author: Ronan Lamy <[email protected]>
Branch: py3.5
Changeset: r89713:515869df8814
Date: 2017-01-23 19:11 +0000
http://bitbucket.org/pypy/pypy/changeset/515869df8814/
Log: Prevent unwanted attributes from being merged to the ast.AST base
class
diff --git a/pypy/interpreter/astcompiler/ast.py
b/pypy/interpreter/astcompiler/ast.py
--- a/pypy/interpreter/astcompiler/ast.py
+++ b/pypy/interpreter/astcompiler/ast.py
@@ -31,7 +31,7 @@
class AST(object):
__metaclass__ = extendabletype
- #_attrs_ = ['lineno', 'col_offset']
+ _attrs_ = ['lineno', 'col_offset']
def walkabout(self, visitor):
raise AssertionError("walkabout() implementation not provided")
diff --git a/pypy/interpreter/astcompiler/codegen.py
b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -338,6 +338,7 @@
for i, default in enumerate(args.kw_defaults):
if default:
kwonly = args.kwonlyargs[i]
+ assert isinstance(kwonly, ast.arg)
mangled = self.scope.mangle(kwonly.arg).decode('utf-8')
self.load_const(self.space.wrap(mangled))
default.walkabout(self)
@@ -352,16 +353,20 @@
def _visit_arg_annotations(self, args, names):
if args:
for arg in args:
+ assert isinstance(arg, ast.arg)
self._visit_arg_annotation(arg.arg, arg.annotation, names)
+ @specialize.argtype(1)
def _visit_annotations(self, func, args, returns):
space = self.space
names = []
self._visit_arg_annotations(args.args, names)
- if args.vararg:
- self._visit_arg_annotation(args.vararg.arg, args.vararg.annotation,
+ vararg = args.vararg
+ if vararg:
+ self._visit_arg_annotation(vararg.arg, vararg.annotation,
names)
self._visit_arg_annotations(args.kwonlyargs, names)
+ kwarg = args.kwarg
if args.kwarg:
self._visit_arg_annotation(args.kwarg.arg, args.kwarg.annotation,
names)
@@ -376,6 +381,7 @@
l += 1
return l
+ @specialize.arg(2)
def _visit_function(self, func, function_code_generator):
self.update_position(func.lineno, True)
# Load decorators first, but apply them after the function is created.
@@ -924,10 +930,12 @@
self.update_position(wih.lineno, True)
self.handle_withitem(wih, 0, is_async=False)
+ @specialize.argtype(1)
def handle_withitem(self, wih, pos, is_async):
body_block = self.new_block()
cleanup = self.new_block()
witem = wih.items[pos]
+ assert isinstance(witem, ast.withitem)
witem.context_expr.walkabout(self)
if not is_async:
self.emit_jump(ops.SETUP_WITH, cleanup)
@@ -1289,6 +1297,7 @@
nseen = 0 # the number of keyword arguments on the stack following
if keywords is not None:
for kw in keywords:
+ assert isinstance(kw, ast.keyword)
if kw.arg is None:
# A keyword argument unpacking.
if nseen:
@@ -1346,6 +1355,7 @@
return False
if call.keywords is not None:
for kw in call.keywords:
+ assert isinstance(kw, ast.keyword)
if kw.arg is None:
return False
return True
diff --git a/pypy/interpreter/astcompiler/symtable.py
b/pypy/interpreter/astcompiler/symtable.py
--- a/pypy/interpreter/astcompiler/symtable.py
+++ b/pypy/interpreter/astcompiler/symtable.py
@@ -89,12 +89,12 @@
"""Called when a yield is found."""
raise SyntaxError("'yield' outside function", yield_node.lineno,
yield_node.col_offset)
-
+
def note_yieldFrom(self, yieldFrom_node):
"""Called when a yield from is found."""
raise SyntaxError("'yield' outside function", yieldFrom_node.lineno,
yieldFrom_node.col_offset)
-
+
def note_await(self, await_node):
"""Called when await is found."""
raise SyntaxError("'await' outside function", await_node.lineno,
@@ -260,12 +260,12 @@
self.is_generator = True
if self._in_try_body_depth > 0:
self.has_yield_inside_try = True
-
+
def note_yieldFrom(self, yield_node):
self.is_generator = True
if self._in_try_body_depth > 0:
self.has_yield_inside_try = True
-
+
def note_await(self, await_node):
if self.name == '<genexpr>':
msg = "'await' expressions in comprehensions are not supported"
@@ -315,7 +315,7 @@
def note_yieldFrom(self, yield_node):
raise SyntaxError("'yield from' inside async function",
yield_node.lineno,
yield_node.col_offset)
-
+
def note_await(self, await_node):
# Compatibility with CPython 3.5: set the CO_GENERATOR flag in
# addition to the CO_COROUTINE flag if the function uses the
@@ -414,7 +414,7 @@
func.args.walkabout(self)
self.visit_sequence(func.body)
self.pop_scope()
-
+
def visit_AsyncFunctionDef(self, func):
self.note_symbol(func.name, SYM_ASSIGNED)
# Function defaults and decorators happen in the outer scope.
@@ -429,7 +429,7 @@
func.args.walkabout(self)
self.visit_sequence(func.body)
self.pop_scope()
-
+
def visit_Await(self, aw):
self.scope.note_await(aw)
ast.GenericASTVisitor.visit_Await(self, aw)
@@ -572,7 +572,7 @@
witem.context_expr.walkabout(self)
if witem.optional_vars:
witem.optional_vars.walkabout(self)
-
+
def visit_AsyncWith(self, aw):
self.scope.new_temporary_name()
self.visit_sequence(aw.items)
@@ -595,8 +595,9 @@
scope.note_keywords_arg(arguments.kwarg)
def _handle_params(self, params, is_toplevel):
- for i in range(len(params)):
- arg = params[i].arg
+ for param in params:
+ assert isinstance(param, ast.arg)
+ arg = param.arg
self.note_symbol(arg, SYM_PARAM)
def _visit_annotations(self, func):
@@ -611,6 +612,7 @@
def _visit_arg_annotations(self, args):
for arg in args:
+ assert isinstance(arg, ast.arg)
if arg.annotation:
arg.annotation.walkabout(self)
diff --git a/pypy/interpreter/astcompiler/tools/asdl_py.py
b/pypy/interpreter/astcompiler/tools/asdl_py.py
--- a/pypy/interpreter/astcompiler/tools/asdl_py.py
+++ b/pypy/interpreter/astcompiler/tools/asdl_py.py
@@ -453,7 +453,7 @@
class AST(object):
__metaclass__ = extendabletype
- #_attrs_ = ['lineno', 'col_offset']
+ _attrs_ = ['lineno', 'col_offset']
def walkabout(self, visitor):
raise AssertionError("walkabout() implementation not provided")
diff --git a/pypy/interpreter/astcompiler/validate.py
b/pypy/interpreter/astcompiler/validate.py
--- a/pypy/interpreter/astcompiler/validate.py
+++ b/pypy/interpreter/astcompiler/validate.py
@@ -46,7 +46,7 @@
raise ValidationError(
"expression which can't be assigned to in %s context" %
expr_context_name(ctx))
-
+
class __extend__(ast.Name):
@@ -286,12 +286,12 @@
def visit_Import(self, node):
self._validate_nonempty_seq(node.names, "names", "Import")
-
+
def visit_ImportFrom(self, node):
if node.level < -1:
raise ValidationError("ImportFrom level less than -1")
self._validate_nonempty_seq(node.names, "names", "ImportFrom")
-
+
def visit_Global(self, node):
self._validate_nonempty_seq_s(node.names, "names", "Global")
@@ -353,6 +353,7 @@
if not generators:
raise ValidationError("comprehension with no generators")
for comp in generators:
+ assert isinstance(comp, ast.comprehension)
self._validate_expr(comp.target, ast.Store)
self._validate_expr(comp.iter)
self._validate_exprs(comp.ifs)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit