From: <th...@us...> - 2007-08-05 00:32:31
|
Revision: 3392 http://jython.svn.sourceforge.net/jython/?rev=3392&view=rev Author: thobes Date: 2007-08-04 17:32:27 -0700 (Sat, 04 Aug 2007) Log Message: ----------- Added support for pep 342 in the framework so that generated bytecode can implement these features. Modified Paths: -------------- branches/newcompiler/src/org/python/core/Py.java branches/newcompiler/src/org/python/core/PyFrame.java branches/newcompiler/src/org/python/core/PyGenerator.java branches/newcompiler/src/org/python/core/exceptions.java Modified: branches/newcompiler/src/org/python/core/Py.java =================================================================== --- branches/newcompiler/src/org/python/core/Py.java 2007-08-05 00:28:47 UTC (rev 3391) +++ branches/newcompiler/src/org/python/core/Py.java 2007-08-05 00:32:27 UTC (rev 3392) @@ -229,6 +229,13 @@ public static PyException StopIteration(String message) { return new PyException(Py.StopIteration, message); } + + // begin newcompiler + public static PyObject GeneratorExit; + public static PyException GeneratorExit(String message) { + return new PyException(Py.GeneratorExit, message); + } + // end newcompiler public static PyObject ImportError; public static PyException ImportError(String message) { @@ -644,6 +651,9 @@ Exception = initExc("Exception", exc, dict); SystemExit = initExc("SystemExit", exc, dict); StopIteration = initExc("StopIteration", exc, dict); + // begin newcompiler + GeneratorExit = initExc("GeneratorExit", exc, dict); + // end newcompiler StandardError = initExc("StandardError", exc, dict); KeyboardInterrupt = initExc("KeyboardInterrupt", exc, dict); ImportError = initExc("ImportError", exc, dict); Modified: branches/newcompiler/src/org/python/core/PyFrame.java =================================================================== --- branches/newcompiler/src/org/python/core/PyFrame.java 2007-08-05 00:28:47 UTC (rev 3391) +++ branches/newcompiler/src/org/python/core/PyFrame.java 2007-08-05 00:32:27 UTC (rev 3392) @@ -78,7 +78,32 @@ members[i] = new PyString(__members__[i]); return new PyList(members); } + + // begin newcompiler + + private PyException exception = null; + private PyObject generatorInput = null; + + public void setGeneratorException(PyException ex) { + exception = ex; + generatorInput = null; + } + + public void setGeneratorInput(PyObject value) { + exception = null; + generatorInput = value; + } + + public PyObject getGeneratorInput() { + if( exception != null ) { + throw exception; + } else { + return generatorInput; + } + } + // end newcompiler + private void throwReadonly(String name) { for (int i = 0; i < __members__.length; i++) if (__members__[i] == name) Modified: branches/newcompiler/src/org/python/core/PyGenerator.java =================================================================== --- branches/newcompiler/src/org/python/core/PyGenerator.java 2007-08-05 00:28:47 UTC (rev 3391) +++ branches/newcompiler/src/org/python/core/PyGenerator.java 2007-08-05 00:32:27 UTC (rev 3392) @@ -2,7 +2,6 @@ package org.python.core; - public class PyGenerator extends PyIterator { public PyFrame gi_frame; PyObject closure; @@ -14,8 +13,12 @@ this.gi_running = false; } - private static final String[] __members__ = { - "gi_frame", "gi_running", "next", + private static final String[] __members__ = { "gi_frame", "gi_running", + "next", + // begin newcompiler + "close", "send", "throw", + // FIXME: it should be throw, not _throw, but throw is a java keyword + // end newcompiler }; public PyObject __dir__() { @@ -30,6 +33,62 @@ return ret; } + // begin newcompiler + + public PyObject next() { + gi_frame.setGeneratorInput(Py.None); + return super.next(); + } + + public PyObject send(PyObject value) { + gi_frame.setGeneratorInput(value); + return super.next(); + } + + private PyObject _throw(PyException ex) { + gi_frame.setGeneratorException(ex); + return super.next(); + } + + public PyObject throw$(PyObject type) { + return _throw(new PyException(type)); + } + + public PyObject throw$(PyObject type, PyObject value) { + return _throw(new PyException(type, value)); + } + + public PyObject throw$(PyObject type, PyObject value, PyTraceback tb) { + return _throw(new PyException(type, value, tb)); + } + + public PyObject close() { + try { + throw$(Py.GeneratorExit); + throw Py.RuntimeError("generator ignored GeneratorExit"); + } catch (PyException e) { + if (!(e.type == Py.StopIteration || e.type == Py.GeneratorExit)) { + throw e; + } + } + return Py.None; + } + + protected void finalize() throws Throwable { + if (gi_frame.f_lasti == -1) /* Generator already closed. */ + return; + try { + close(); + } catch (Throwable e) { + Py.println(Py.getSystemState().stderr, new PyString("Exception " + + e + " in " + this + " ignored.")); + } finally { + super.finalize(); + } + } + + // end newcompiler + public PyObject __iternext__() { if (gi_running) throw Py.ValueError("generator already executing"); @@ -39,19 +98,19 @@ PyObject result = null; try { result = gi_frame.f_code.call(gi_frame, closure); - } catch(PyException e) { - if(!e.type.equals(Py.StopIteration)) { + } catch (PyException e) { + if (!e.type.equals(Py.StopIteration)) { throw e; - }else{ + } else { stopException = e; return null; } } finally { gi_running = false; } -// System.out.println("lasti:" + gi_frame.f_lasti); -//if (result == Py.None) -// new Exception().printStackTrace(); + // System.out.println("lasti:" + gi_frame.f_lasti); + // if (result == Py.None) + // new Exception().printStackTrace(); if (result == Py.None && gi_frame.f_lasti == -1) return null; return result; Modified: branches/newcompiler/src/org/python/core/exceptions.java =================================================================== --- branches/newcompiler/src/org/python/core/exceptions.java 2007-08-05 00:28:47 UTC (rev 3391) +++ branches/newcompiler/src/org/python/core/exceptions.java 2007-08-05 00:32:27 UTC (rev 3392) @@ -19,6 +19,9 @@ + "\n" + "Exception\n" + " |\n" + // begin newcompiler + + " +-- GeneratorExit\n" + // end newcompiler + " +-- SystemExit\n" + " +-- StopIteration\n" + " +-- StandardError\n" @@ -217,6 +220,11 @@ buildClass(dict, "StopIteration", "Exception", "empty__init__", "Signal the end from iterator.next()."); + + // begin newcompiler + buildClass(dict, "GeneratorExit", "Exception", "empty__init__", + "Request that a generator exit."); + // end newcompiler buildClass(dict, "Warning", "Exception", "empty__init__", "Base class for warning categories."); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |