01: // Copyright 2002 Finn Bock
02:
03: package org.python.core;
04:
05: public class PyGenerator extends PyIterator {
06: public PyFrame gi_frame;
07: PyObject closure;
08: public boolean gi_running;
09:
10: public PyGenerator(PyFrame frame, PyObject closure) {
11: this .gi_frame = frame;
12: this .closure = closure;
13: this .gi_running = false;
14: }
15:
16: private static final String[] __members__ = { "gi_frame",
17: "gi_running", "next", };
18:
19: public PyObject __dir__() {
20: PyString members[] = new PyString[__members__.length];
21: for (int i = 0; i < __members__.length; i++)
22: members[i] = new PyString(__members__[i]);
23: PyList ret = new PyList(members);
24: PyDictionary accum = new PyDictionary();
25: addKeys(accum, "__dict__");
26: ret.extend(accum.keys());
27: ret.sort();
28: return ret;
29: }
30:
31: public PyObject __iternext__() {
32: if (gi_running)
33: throw Py.ValueError("generator already executing");
34: if (gi_frame.f_lasti == -1)
35: return null;
36: gi_running = true;
37: PyObject result = null;
38: try {
39: result = gi_frame.f_code.call(gi_frame, closure);
40: } catch (PyException e) {
41: if (!e.type.equals(Py.StopIteration)) {
42: throw e;
43: } else {
44: stopException = e;
45: return null;
46: }
47: } finally {
48: gi_running = false;
49: }
50: // System.out.println("lasti:" + gi_frame.f_lasti);
51: //if (result == Py.None)
52: // new Exception().printStackTrace();
53: if (result == Py.None && gi_frame.f_lasti == -1)
54: return null;
55: return result;
56: }
57: }
|