KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > PyGenerator


1 // Copyright 2002 Finn Bock
2

3 package org.python.core;
4
5
6 public class PyGenerator extends PyIterator {
7     public PyFrame gi_frame;
8     PyObject closure;
9     public boolean gi_running;
10
11     public PyGenerator(PyFrame frame, PyObject closure) {
12         this.gi_frame = frame;
13         this.closure = closure;
14         this.gi_running = false;
15     }
16
17     private static final String JavaDoc[] __members__ = {
18         "gi_frame", "gi_running", "next",
19     };
20
21     public PyObject __dir__() {
22         PyString members[] = new PyString[__members__.length];
23         for (int i = 0; i < __members__.length; i++)
24             members[i] = new PyString(__members__[i]);
25         PyList ret = new PyList(members);
26         PyDictionary accum = new PyDictionary();
27         addKeys(accum, "__dict__");
28         ret.extend(accum.keys());
29         ret.sort();
30         return ret;
31     }
32
33     public PyObject __iternext__() {
34         if (gi_running)
35             throw Py.ValueError("generator already executing");
36         if (gi_frame.f_lasti == -1)
37             return null;
38         gi_running = true;
39         PyObject result = null;
40         try {
41             result = gi_frame.f_code.call(gi_frame, closure);
42         } finally {
43             gi_running = false;
44         }
45 // System.out.println("lasti:" + gi_frame.f_lasti);
46
//if (result == Py.None)
47
// new Exception().printStackTrace();
48
if (result == Py.None && gi_frame.f_lasti == -1)
49             return null;
50         return result;
51     }
52 }
53
Popular Tags