From: <th...@us...> - 2008-10-17 21:21:41
|
Revision: 5457 http://jython.svn.sourceforge.net/jython/?rev=5457&view=rev Author: thobes Date: 2008-10-17 21:21:36 +0000 (Fri, 17 Oct 2008) Log Message: ----------- Added a check for return and yield in the same scope in ScopesCompiler, There are cases that can be missed in CodeCompiler due to dead code elimination, this gets around that. Modified Paths: -------------- trunk/jython/src/org/python/compiler/ScopeInfo.java trunk/jython/src/org/python/compiler/ScopesCompiler.java Modified: trunk/jython/src/org/python/compiler/ScopeInfo.java =================================================================== --- trunk/jython/src/org/python/compiler/ScopeInfo.java 2008-10-17 20:46:47 UTC (rev 5456) +++ trunk/jython/src/org/python/compiler/ScopeInfo.java 2008-10-17 21:21:36 UTC (rev 5457) @@ -5,7 +5,11 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; + +import org.python.antlr.ParseException; import org.python.antlr.PythonTree; +import org.python.antlr.ast.Return; +import org.python.antlr.ast.exprType; public class ScopeInfo extends Object implements ScopeConstants { @@ -56,6 +60,7 @@ public boolean from_import_star; public boolean contains_ns_free_vars; public boolean generator; + private boolean hasReturnWithValue; public int yield_count; public int max_with_count; @@ -264,4 +269,20 @@ return "ScopeInfo[" + scope_name + " " + kind + "]@" + System.identityHashCode(this); } + + public void defineAsGenerator(exprType node) { + generator = true; + if (hasReturnWithValue) { + throw new ParseException("'return' with argument " + + "inside generator", node); + } + } + + public void noteReturnValue(Return node) { + if (generator) { + throw new ParseException("'return' with argument " + + "inside generator", node); + } + hasReturnWithValue = true; + } } Modified: trunk/jython/src/org/python/compiler/ScopesCompiler.java =================================================================== --- trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-10-17 20:46:47 UTC (rev 5456) +++ trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-10-17 21:21:36 UTC (rev 5457) @@ -4,6 +4,7 @@ import org.python.antlr.*; import org.python.antlr.ast.*; + import java.util.*; public class ScopesCompiler extends Visitor implements ScopeConstants { @@ -262,11 +263,20 @@ @Override public Object visitYield(Yield node) throws Exception { - cur.generator = true; + cur.defineAsGenerator(node); cur.yield_count++; traverse(node); return null; } + + @Override + public Object visitReturn(Return node) throws Exception { + if (node.value != null) { + cur.noteReturnValue(node); + } + traverse(node); + return null; + } @Override public Object visitGeneratorExp(GeneratorExp node) throws Exception { @@ -286,7 +296,7 @@ cur.addParam(bound_exp); cur.markFromParam(); - cur.generator = true; + cur.defineAsGenerator(node); cur.yield_count++; // The reset of the iterators are evaluated in the inner scope if (node.elt != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |