Commits

Shashank Bharadwaj committed b1c8b80

test_generators_jy now works, finally; needed to add another node

Comments (0)

Files changed (2)

add-debugging-to-classfile.patch

 # HG changeset patch
-# Parent 797c7d7abbf4e2c3dab9fe2e3c93e211c5f913f6
+# Parent bae3fde39db5ff964225537aea3fdb810d938b26
 
 diff --git a/.externalToolBuilders/ant_expose.launch b/.externalToolBuilders/ant_expose.launch
 --- a/.externalToolBuilders/ant_expose.launch
 +		byte[] ba = cw.toByteArray();
 +		ByteArrayOutputStream baos = new ByteArrayOutputStream(ba.length);
 +		baos.write(ba, 0, ba.length);
++		baos.writeTo(stream);
 +		if (COMMENT) {
 +			debug(baos);
 +			checkClassVerify(ba);
 +		}
-+		baos.writeTo(stream);
 +		baos.close();
 +	}
 +

fix-asm-via-cfg.patch

 +    }
 +
 +}
+diff --git a/src/org/python/antlr/ast/FinallyGoto.java b/src/org/python/antlr/ast/FinallyGoto.java
+new file mode 100644
+--- /dev/null
++++ b/src/org/python/antlr/ast/FinallyGoto.java
+@@ -0,0 +1,29 @@
++package org.python.antlr.ast;
++
++import org.objectweb.asm.Label;
++import org.python.antlr.base.stmt;
++import org.python.core.PyType;
++
++public class FinallyGoto extends stmt {
++	public static final PyType TYPE = PyType.fromClass(FinallyGoto.class);
++	
++	public Label goto_label;
++	
++	public FinallyGoto(Label goto_label) {
++		this.goto_label = goto_label;
++	}
++	
++    public String toString() {
++        return "FinallyGoto";
++    }
++
++    public <R> R accept(VisitorIF<R> visitor) throws Exception {
++        return visitor.visitFinallyGoto(this);
++    }
++
++    public void traverse(VisitorIF<?> visitor) throws Exception {
++        return;
++    }
++
++
++}
 diff --git a/src/org/python/antlr/ast/FinallyStart.java b/src/org/python/antlr/ast/FinallyStart.java
 new file mode 100644
 --- /dev/null
 +++ b/src/org/python/antlr/ast/FinallyStart.java
-@@ -0,0 +1,32 @@
+@@ -0,0 +1,35 @@
 +package org.python.antlr.ast;
 +
 +import java.util.List;
 +
++import org.objectweb.asm.Label;
 +import org.python.antlr.base.stmt;
 +
 +/**
 +public class FinallyStart extends HandlerStart {
 +
 +    public List<stmt> finalBody;
++    public Label finally_body_start;
 +
 +    public FinallyStart(List<stmt> fbody) {
 +        super();
 +        this.finalBody = fbody;
++        finally_body_start = new Label();
 +    }
 +
 +    public <R> R accept(VisitorIF<R> visitor) throws Exception {
 diff --git a/src/org/python/antlr/ast/VisitorBase.java b/src/org/python/antlr/ast/VisitorBase.java
 --- a/src/org/python/antlr/ast/VisitorBase.java
 +++ b/src/org/python/antlr/ast/VisitorBase.java
-@@ -347,6 +347,64 @@
+@@ -347,6 +347,71 @@
          return ret;
      }
  
 +        traverse(node);
 +        return ret;
 +    }
++
 +    public R visitYieldInput(YieldInput node) throws Exception {
 +        R ret = unhandled_node(node);
 +        traverse(node);
 +        return ret;
 +    }
 +
++    public R visitFinallyGoto(FinallyGoto node) throws Exception {
++        R ret = unhandled_node(node);
++        traverse(node);
++        return ret;
++    }
++
      abstract protected R unhandled_node(PythonTree node) throws Exception;
      abstract public void traverse(PythonTree node) throws Exception;
  }
 diff --git a/src/org/python/antlr/ast/VisitorIF.java b/src/org/python/antlr/ast/VisitorIF.java
 --- a/src/org/python/antlr/ast/VisitorIF.java
 +++ b/src/org/python/antlr/ast/VisitorIF.java
-@@ -56,4 +56,14 @@
+@@ -56,4 +56,15 @@
      public R visitExtSlice(ExtSlice node) throws Exception;
      public R visitIndex(Index node) throws Exception;
      public R visitExceptHandler(ExceptHandler node) throws Exception;
 +    public R visitYieldReturn(YieldReturn node) throws Exception;
 +    public R visitYieldRestoreLocals(YieldRestoreLocals node) throws Exception;
 +    public R visitYieldInput(YieldInput node) throws Exception;
++	public R visitFinallyGoto(FinallyGoto node) throws Exception;
  }
 diff --git a/src/org/python/antlr/ast/WithExit.java b/src/org/python/antlr/ast/WithExit.java
 new file mode 100644
  import org.python.antlr.ast.BinOp;
  import org.python.antlr.ast.BoolOp;
  import org.python.antlr.ast.Break;
-@@ -28,6 +29,9 @@
+@@ -28,6 +29,10 @@
  import org.python.antlr.ast.Dict;
  import org.python.antlr.ast.DictComp;
  import org.python.antlr.ast.Ellipsis;
 +import org.python.antlr.ast.ExceptionRangeStop;
++import org.python.antlr.ast.FinallyGoto;
 +import org.python.antlr.ast.FinallyStart;
 +import org.python.antlr.ast.HandlerStart;
  import org.python.antlr.ast.ExceptHandler;
  import org.python.antlr.ast.Exec;
  import org.python.antlr.ast.Expr;
-@@ -59,13 +63,18 @@
+@@ -59,13 +64,18 @@
  import org.python.antlr.ast.Str;
  import org.python.antlr.ast.Subscript;
  import org.python.antlr.ast.Suite;
  import org.python.antlr.ast.alias;
  import org.python.antlr.ast.cmpopType;
  import org.python.antlr.ast.comprehension;
-@@ -75,6 +84,7 @@
+@@ -75,6 +85,7 @@
  import org.python.antlr.base.expr;
  import org.python.antlr.base.mod;
  import org.python.antlr.base.stmt;
  import org.python.core.CompilerFlags;
  import org.python.core.ContextGuard;
  import org.python.core.ContextManager;
-@@ -122,7 +132,6 @@
+@@ -122,7 +133,6 @@
      private boolean optimizeGlobals = true;
      private String className;
      private Stack<Label> continueLabels, breakLabels;
      private Vector<Label> yields = new Vector<Label>();
  
      /*
-@@ -133,7 +142,6 @@
+@@ -133,7 +143,6 @@
       * PyCode, in other words: each 'function'.  When returning through
       * finally's all the exceptionHandlers are executed.
       */
      private int yield_count = 0;
      private Stack<String> stack = new Stack<String>();
  
-@@ -143,7 +151,6 @@
+@@ -143,7 +152,6 @@
  
          continueLabels = new Stack<Label>();
          breakLabels = new Stack<Label>();
      }
  
      public void getNone() throws IOException {
-@@ -627,8 +634,6 @@
+@@ -627,8 +635,6 @@
              throw new ParseException("'break' outside loop", node);
          }
  
          code.goto_(breakLabels.peek());
          return null;
      }
-@@ -640,23 +645,23 @@
+@@ -640,23 +646,23 @@
              throw new ParseException("'continue' not properly in loop", node);
          }
  
          code.goto_(continueLabels.peek());
          return Exit;
      }
--
-+    
+ 
 +    int stackState;
-+    
++
      @Override
 -    public Object visitYield(Yield node) throws Exception {
 +    public Object visitYieldReturn(YieldReturn node) throws Exception {
          } else {
              getNone();
          }
-@@ -665,11 +670,20 @@
+@@ -665,11 +671,20 @@
  
          saveLocals();
          code.areturn();
--
 +        return null;
 +    }
-+    
+ 
 +    @Override
 +    public Object visitYieldRestoreLocals(YieldRestoreLocals node) throws Exception {
          Label restart = new Label();
          restoreLocals();
 +        return null;
 +    }
-+    
++
 +    @Override
 +    public Object visitYieldInput(YieldInput node) throws Exception {
          restoreStack(stackState);
  
          loadFrame();
-@@ -754,8 +768,6 @@
+@@ -754,8 +769,6 @@
      }
  
      private void restoreLocals() throws Exception {
          Vector<String> v = code.getActiveLocals();
  
          loadFrame();
-@@ -776,33 +788,6 @@
+@@ -776,33 +789,6 @@
              code.astore(i);
          }
          code.freeLocal(locals);
      }
  
      private void saveLocals() throws Exception {
-@@ -854,7 +839,6 @@
+@@ -854,7 +840,6 @@
              tmp = code.getReturnLocal();
              code.astore(tmp);
          }
  
          setLastI(-1);
  
-@@ -901,7 +885,7 @@
+@@ -901,7 +886,7 @@
      }
  
      /**
       */
      private void defaultImportLevel() {
      	// already prepared for a future change of DEFAULT_LEVEL
-@@ -909,9 +893,9 @@
+@@ -909,9 +894,9 @@
              code.iconst_0();
          } else {
              code.iconst_m1();
      @Override
      public Object visitImport(Import node) throws Exception {
          setline(node);
-@@ -1151,15 +1135,12 @@
+@@ -1151,15 +1136,12 @@
      public int beginLoop() {
          continueLabels.push(new Label());
          breakLabels.push(new Label());
      }
  
      @Override
-@@ -1240,6 +1221,8 @@
+@@ -1240,6 +1222,8 @@
          code.ifnonnull(start_loop);
  
          finishLoop(savebcf);
  
          if (node.getInternalOrelse() != null) {
              //Do else clause if provided
-@@ -1248,8 +1231,6 @@
+@@ -1248,8 +1232,6 @@
  
          code.label(break_loop);
  
  
          // Probably need to detect "guaranteed exits"
          return null;
-@@ -1258,7 +1239,11 @@
+@@ -1258,7 +1240,11 @@
      public void exceptionTest(int exc, Label end_of_exceptions,
              TryExcept node, int index)
              throws Exception {
              ExceptHandler handler = (ExceptHandler) node.getInternalHandlers().get(i);
  
              //setline(name);
-@@ -1282,6 +1267,11 @@
+@@ -1282,6 +1268,11 @@
                  code.aload(exc);
                  code.getfield(p(PyException.class), "value", ci(PyObject.class));
                  set(handler.getInternalName());
              }
  
              //do exception body
-@@ -1294,41 +1284,29 @@
+@@ -1294,41 +1285,29 @@
      }
  
      @Override
 -            inlineFinally(inFinally);
 -            code.goto_(finallyEnd);
 -        }
-+        code.goto_(handler_end);
++        code.goto_(handlerBlock.finally_body_start);
  
          // Handle any exceptions that get thrown in suite
 -        code.label(handlerStart);
          code.astore(excLocal);
  
          code.aload(excLocal);
-@@ -1337,109 +1315,77 @@
+@@ -1337,109 +1316,88 @@
          code.invokestatic(p(Py.class), "addTraceback",
                  sig(Void.TYPE, Throwable.class, PyFrame.class));
  
 -        inlineFinally(inFinally);
++        code.label(handlerBlock.finally_body_start);
++        suite(node.getInternalFinalbody());
++
++        code.aload(excLocal);
++        code.ifnull(handler_end);
          code.aload(excLocal);
          code.checkcast(p(Throwable.class));
          code.athrow();
 -            handler.finalBody(this);
 -        }
 +    @Override
++    public Object visitFinallyGoto(FinallyGoto node) throws Exception {
++    	code.goto_(node.goto_label);
++    	return null;
++    }
++    
++    @Override
 +    public Object visitExceptionRangeStart(ExceptionRangeStart node) throws Exception {
 +        code.label(node.start);
 +        return null;
          return null;
      }
  
-@@ -1449,13 +1395,14 @@
+@@ -1449,13 +1407,14 @@
      }
  
      public Object suite(java.util.List<stmt> stmts) throws Exception {
      }
  
      @Override
-@@ -2263,7 +2210,7 @@
+@@ -2263,7 +2222,7 @@
              super(tree, value);
          }
      }
      @Override
      public Object visitLambda(Lambda node) throws Exception {
          String name = "<lambda>";
-@@ -2560,19 +2507,31 @@
+@@ -2560,19 +2519,31 @@
          scope.setup_closure();
          scope.dump();
  
              if (i != 0) {
                  n = new For(comp, comp.getInternalTarget(), comp.getInternalIter(), bod,
                          new ArrayList<stmt>());
-@@ -2584,7 +2543,12 @@
+@@ -2584,7 +2555,12 @@
          }
  
          java.util.List<stmt> bod = new ArrayList<stmt>();
          module.codeConstant(new Suite(node, bod), "<genexpr>", true,
                  className, false, false,
                  node.getLine(), scope, cflags).get(code);
-@@ -2614,125 +2578,63 @@
+@@ -2614,125 +2590,63 @@
      }
  
      @Override
 +            code.pop();
 +            break;
          }
--
++        return null;
++    }
+ 
 -        final Label label_body_start = new Label();
 -        final Label label_body_end = new Label();
 -        final Label label_catch = new Label();
 -        final Label label_end = new Label();
 -
-+        return null;
-+    }
-+    
 +    @Override
 +    public Object visitWithSpecialLoad(WithSpecialLoad node) throws Exception {
          final Method contextGuard_getManager = Method.getMethod(
          return null;
      }
  
-@@ -2741,62 +2643,4 @@
+@@ -2741,62 +2655,4 @@
          throw new Exception("Unhandled node " + node);
      }
  
 new file mode 100644
 --- /dev/null
 +++ b/src/org/python/compiler/cfg/IRBuilder.java
-@@ -0,0 +1,403 @@
+@@ -0,0 +1,404 @@
 +package org.python.compiler.cfg;
 +
 +import java.util.ArrayList;
 +import org.python.antlr.ast.ExceptionRangeStart;
 +import org.python.antlr.ast.ExceptionRangeStop;
 +import org.python.antlr.ast.Expr;
++import org.python.antlr.ast.FinallyGoto;
 +import org.python.antlr.ast.FinallyStart;
 +import org.python.antlr.ast.For;
 +import org.python.antlr.ast.FunctionDef;
 +        FinallyStart finally_handler = new FinallyStart(finally_body);
 +
 +        List<stmt> body = suite(node.getInternalBody(), finally_handler);
++//        body.addAll(suite(finally_body));
++        // and now there is a empty try-catch block error?
 +        List<stmt> suited_finally = suite(finally_body);
-+        body.addAll(suited_finally);
-+
-+        suited_finally.add(
-+                0,
-+                new TryExcept(node, null, Collections
-+                        .singletonList((excepthandler) finally_handler), null));
++
++        suited_finally.add(0, new TryExcept(node, null, Collections.singletonList(
++        		(excepthandler) finally_handler), null));
 +
 +        TryFinally new_node = new TryFinally(node.getToken(), body, suited_finally);
 +        return new_node;
 +    private void inlineFinally(HandlerStart handler) throws Exception {
 +        if (handler instanceof FinallyStart) {
 +            FinallyStart fHandler = (FinallyStart) handler;
-+            data.stmts.addAll(suite(fHandler.finalBody));
++//            data.stmts.addAll(suite(fHandler.finalBody));
++            data.stmts.add(new FinallyGoto(fHandler.finally_body_start));
 +        }
 +    }
 +
 new file mode 100644
 --- /dev/null
 +++ b/src/org/python/compiler/cfg/StampTryCatch.java
-@@ -0,0 +1,69 @@
+@@ -0,0 +1,70 @@
 +package org.python.compiler.cfg;
 +
 +import static org.python.util.CodegenUtils.p;
 +        Label handler_start = handlerBlock.start;
 +
 +        Label start, end = null;
-+        for (int i = handlerBlock.startEdges.size() - 1; i >= 0; i--) {
++        // for (int i = handlerBlock.startEdges.size() - 1; i >= 0; i--) {
++        for (int i = 0; i < handlerBlock.startEdges.size(); i++) {
 +            start = ((ExceptionRangeStart) handlerBlock.startEdges.get(i).getSource()).start;
 +            end = ((ExceptionRangeStop) handlerBlock.stopEdges.get(i).getSource()).end;
 +            additions.add(new TryCatchBlock(start, end, handler_start, p(Throwable.class)));
 new file mode 100644
 --- /dev/null
 +++ b/src/org/python/compiler/cfg/TransitiveVisitor.java
-@@ -0,0 +1,659 @@
+@@ -0,0 +1,665 @@
 +package org.python.compiler.cfg;
 +
 +import java.util.ArrayList;
 +import org.python.antlr.ast.Expr;
 +import org.python.antlr.ast.Expression;
 +import org.python.antlr.ast.ExtSlice;
++import org.python.antlr.ast.FinallyGoto;
 +import org.python.antlr.ast.FinallyStart;
 +import org.python.antlr.ast.For;
 +import org.python.antlr.ast.FunctionDef;
 +    }
 +
 +    @Override
++    public Object visitFinallyGoto(FinallyGoto node) throws Exception {
++    	return node;
++    }
++
++    @Override
 +    public Object visitFor(For node) throws Exception {
 +        List<stmt> body = suite(node.getInternalBody());
 +        expr iter = (expr) visitNotNull(node.getInternalIter());
 new file mode 100644
 --- /dev/null
 +++ b/tmp/test.py
-@@ -0,0 +1,13 @@
-+def genTryExceptAroundFinally():
-+    for i in range(3):
+@@ -0,0 +1,10 @@
++def genNestedDeeply():
++    for i in range(4):
 +        try:
-+            try:
-+                1//0
-+            finally:
-+                yield i
-+        except:
-+            pass
-+
-+
-+print 'expected:', [0, 1, 2]
-+print 'got:     ', list(genTryExceptAroundFinally())
++            continue
++        finally:
++            yield (i)
++
++
++print 'expected:', [(2, 1, 0), (3, 1, 0), (3, 2, 0), (3, 2, 1)]
++print 'got:     ', list(genNestedDeeply())