KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4
5 /**
6  * A python frame object.
7  */

8
9 public class PyFrame extends PyObject
10 {
11     public PyFrame f_back;
12     public PyTableCode f_code;
13     public PyObject f_locals;
14     public PyObject f_globals;
15     public int f_lineno;
16     public PyObject f_builtins;
17     public PyObject[] f_fastlocals;
18     public PyCell[] f_env; // nested scopes: cell + free env
19
public int f_ncells;
20     public int f_nfreevars;
21     public int f_lasti;
22     public Object JavaDoc[] f_savedlocals;
23
24     // an interface to functions suitable for tracing, e.g. via sys.settrace()
25
public TraceFunction tracefunc;
26
27     private static final String JavaDoc[] __members__ = {
28         "f_back", "f_code", "f_locals", "f_globals", "f_lineno",
29         "f_builtins", "f_trace"
30     };
31
32     public PyFrame(PyTableCode code, PyObject locals, PyObject globals,
33                    PyObject builtins)
34     {
35         f_code = code;
36         f_locals = locals;
37         f_globals = globals;
38         f_builtins = builtins;
39         // This needs work to be efficient with multiple interpreter states
40
if (locals == null && code != null) {
41             // ! f_fastlocals needed for arg passing too
42
if ((code.co_flags&PyTableCode.CO_OPTIMIZED)!=0 ||
43                                         code.nargs > 0) {
44                 if (code.co_nlocals > 0) {
45                     // internal: may change
46
f_fastlocals = new PyObject[
47                                 code.co_nlocals-code.jy_npurecell];
48                 }
49             } else
50                 f_locals = new PyStringMap();
51         }
52         if (code != null) { // reserve space for env
53
int env_sz = 0;
54             if (code.co_freevars != null)
55                 env_sz += (f_nfreevars = code.co_freevars.length);
56             if (code.co_cellvars != null)
57                 env_sz += (f_ncells = code.co_cellvars.length);
58             if (env_sz > 0)
59                 f_env = new PyCell[env_sz];
60         }
61     }
62
63     public PyFrame(PyTableCode code, PyObject globals) {
64         this(code, null, globals, null);
65     }
66
67     public String JavaDoc toString() {
68         if (f_code == null) {
69             return "<frame (unknown code) at line "+f_lineno+">";
70         } else {
71             return "<frame in \""+f_code.co_name+"\" at line "+f_lineno+">";
72         }
73     }
74
75     public PyObject __dir__() {
76         PyString members[] = new PyString[__members__.length];
77         for (int i = 0; i < __members__.length; i++)
78             members[i] = new PyString(__members__[i]);
79         return new PyList(members);
80     }
81
82     private void throwReadonly(String JavaDoc name) {
83         for (int i = 0; i < __members__.length; i++)
84             if (__members__[i] == name)
85                 throw Py.TypeError("readonly attribute");
86         throw Py.AttributeError(name);
87     }
88
89     public void __setattr__(String JavaDoc name, PyObject value) {
90         // In CPython, some of the frame's attributes are read/writeable
91
if (name == "f_trace")
92             tracefunc = new PythonTraceFunction(value);
93         // not yet implemented:
94
// f_exc_type
95
// f_exc_value
96
// f_exc_traceback
97
else throwReadonly(name);
98     }
99
100     public void __delattr__(String JavaDoc name) {
101         if (name == "f_trace")
102             tracefunc = null;
103         // not yet implemented:
104
// f_exc_type
105
// f_exc_value
106
// f_exc_traceback
107
else throwReadonly(name);
108     }
109
110     public PyObject __findattr__(String JavaDoc name) {
111         if (name == "f_locals")
112             return getf_locals();
113         else if (name == "f_trace") {
114             if (tracefunc instanceof PythonTraceFunction) {
115                 return ((PythonTraceFunction)tracefunc).tracefunc;
116             }
117             return Py.None;
118         }
119         return super.__findattr__(name);
120     }
121
122     public PyObject getf_locals() {
123         if (f_locals == null)
124             f_locals = new PyStringMap();
125         if (f_code!=null && f_code.co_nlocals>0) {
126             int i;
127             if (f_fastlocals != null) {
128                 for (i=0; i<f_fastlocals.length; i++) {
129                     PyObject o = f_fastlocals[i];
130                     if (o != null)
131                         f_locals.__setitem__(f_code.co_varnames[i], o);
132                 }
133                 if ((f_code.co_flags&PyTableCode.CO_OPTIMIZED) == 0)
134                     f_fastlocals = null;
135             }
136             int j = 0;
137             for (i=0; i<f_ncells; i++,j++) {
138                 PyObject v = f_env[j].ob_ref;
139                 if (v != null) f_locals.__setitem__(f_code.co_cellvars[i],v);
140             }
141             for (i=0; i<f_nfreevars; i++,j++) {
142                 PyObject v = f_env[j].ob_ref;
143                 if (v != null) f_locals.__setitem__(f_code.co_freevars[i],v);
144             }
145         }
146         return f_locals;
147     }
148
149     public void setline(int line) {
150         f_lineno = line;
151         if (tracefunc != null)
152             tracefunc = tracefunc.traceLine(this, line);
153     }
154
155     public int getline() {
156         return f_lineno;
157     }
158
159     public PyObject getlocal(int index) {
160         if (f_fastlocals != null) {
161             PyObject ret = f_fastlocals[index];
162             if (ret != null)
163                 return ret;
164         }
165         return getlocal(f_code.co_varnames[index]);
166     }
167
168     public PyObject getlocal(String JavaDoc index) {
169         // System.err.println("getlocal: "+index);
170
if (f_locals == null)
171             getf_locals();
172         PyObject ret = f_locals.__finditem__(index);
173         if (ret != null)
174             return ret;
175
176         throw Py.UnboundLocalError("local: '"+index+"'");
177         //return getglobal(index);
178
}
179
180     public PyObject getname(String JavaDoc index) {
181         if (f_locals == null) getf_locals();
182         if (f_locals == f_globals) return getglobal(index);
183
184         PyObject ret = f_locals.__finditem__(index);
185         if (ret != null) return ret;
186         return getglobal(index);
187     }
188
189     public PyObject getglobal(String JavaDoc index) {
190         PyObject ret = f_globals.__finditem__(index);
191         if (ret != null) {
192             return ret;
193         }
194
195         // Set up f_builtins if not already set
196
if (f_builtins == null) {
197             f_builtins = Py.getSystemState().builtins;
198         }
199         ret = f_builtins.__finditem__(index);
200         if (ret != null) return ret;
201
202         throw Py.NameError(index);
203     }
204
205     public void setlocal(int index, PyObject value) {
206         if (f_fastlocals != null)
207             f_fastlocals[index] = value;
208         else
209             setlocal(f_code.co_varnames[index], value);
210     }
211
212     public void setlocal(String JavaDoc index, PyObject value) {
213         if (f_locals == null)
214             getf_locals();
215         f_locals.__setitem__(index, value);
216     }
217
218     public void setglobal(String JavaDoc index, PyObject value) {
219         f_globals.__setitem__(index, value);
220     }
221
222     public void dellocal(int index) {
223         if (f_fastlocals != null) {
224             if (f_fastlocals[index] == null) {
225               throw Py.UnboundLocalError("local: '"+
226                                          f_code.co_varnames[index]+"'");
227             }
228             f_fastlocals[index] = null;
229         } else
230             dellocal(f_code.co_varnames[index]);
231     }
232
233     public void dellocal(String JavaDoc index) {
234         if (f_locals == null)
235             getf_locals();
236         try {
237             f_locals.__delitem__(index);
238         } catch(PyException e) {
239           if (!Py.matchException(e,Py.KeyError)) throw e;
240           throw Py.UnboundLocalError("local: '"+index+"'");
241         }
242     }
243
244     public void delglobal(String JavaDoc index) {
245         f_globals.__delitem__(index);
246     }
247
248     // nested scopes helpers
249

250     public PyObject getclosure(int index) {
251         return f_env[index];
252     }
253
254     public PyObject getderef(int index) {
255         PyObject obj=f_env[index].ob_ref;
256         if (obj != null) return obj;
257         String JavaDoc name;
258         if (index >= f_ncells) name = f_code.co_freevars[index-f_ncells];
259         else name = f_code.co_cellvars[index];
260         throw Py.UnboundLocalError("local: '"+name+"'");
261     }
262
263     public void setderef(int index,PyObject value) {
264         f_env[index].ob_ref = value;
265     }
266
267     public void to_cell(int parm_index,int env_index) {
268         f_env[env_index].ob_ref=f_fastlocals[parm_index];
269     }
270 }
271
Popular Tags