001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: public class PyModule extends PyObject {
005: //~ BEGIN GENERATED REGION -- DO NOT EDIT SEE gexpose.py
006: /* type info */
007:
008: public static final String exposed_name = "module";
009:
010: public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
011: dict.__setitem__("__dict__", new PyGetSetDescr("__dict__",
012: PyModule.class, "getDict", "setDict", "delDict"));
013: dict.__setitem__("__doc__", new PyGetSetDescr("__doc__",
014: PyModule.class, "getDoc", null, null));
015: class exposed___repr__ extends PyBuiltinMethodNarrow {
016:
017: exposed___repr__(PyObject self, PyBuiltinFunction.Info info) {
018: super (self, info);
019: }
020:
021: public PyBuiltinFunction bind(PyObject self) {
022: return new exposed___repr__(self, info);
023: }
024:
025: public PyObject __call__() {
026: return new PyString(((PyModule) self).module_toString());
027: }
028:
029: }
030: dict
031: .__setitem__("__repr__", new PyMethodDescr("__repr__",
032: PyModule.class, 0, 0, new exposed___repr__(
033: null, null)));
034: class exposed___setattr__ extends PyBuiltinMethodNarrow {
035:
036: exposed___setattr__(PyObject self,
037: PyBuiltinFunction.Info info) {
038: super (self, info);
039: }
040:
041: public PyBuiltinFunction bind(PyObject self) {
042: return new exposed___setattr__(self, info);
043: }
044:
045: public PyObject __call__(PyObject arg0, PyObject arg1) {
046: try {
047: ((PyModule) self).module___setattr__(
048: arg0.asName(0), arg1);
049: return Py.None;
050: } catch (PyObject.ConversionException e) {
051: String msg;
052: switch (e.index) {
053: case 0:
054: msg = "attribute name must be a string";
055: break;
056: default:
057: msg = "xxx";
058: }
059: throw Py.TypeError(msg);
060: }
061: }
062:
063: }
064: dict.__setitem__("__setattr__", new PyMethodDescr(
065: "__setattr__", PyModule.class, 2, 2,
066: new exposed___setattr__(null, null)));
067: class exposed___delattr__ extends PyBuiltinMethodNarrow {
068:
069: exposed___delattr__(PyObject self,
070: PyBuiltinFunction.Info info) {
071: super (self, info);
072: }
073:
074: public PyBuiltinFunction bind(PyObject self) {
075: return new exposed___delattr__(self, info);
076: }
077:
078: public PyObject __call__(PyObject arg0) {
079: try {
080: ((PyModule) self)
081: .module___delattr__(arg0.asName(0));
082: return Py.None;
083: } catch (PyObject.ConversionException e) {
084: String msg;
085: switch (e.index) {
086: case 0:
087: msg = "attribute name must be a string";
088: break;
089: default:
090: msg = "xxx";
091: }
092: throw Py.TypeError(msg);
093: }
094: }
095:
096: }
097: dict.__setitem__("__delattr__", new PyMethodDescr(
098: "__delattr__", PyModule.class, 1, 1,
099: new exposed___delattr__(null, null)));
100: class exposed___init__ extends PyBuiltinMethod {
101:
102: exposed___init__(PyObject self, PyBuiltinFunction.Info info) {
103: super (self, info);
104: }
105:
106: public PyBuiltinFunction bind(PyObject self) {
107: return new exposed___init__(self, info);
108: }
109:
110: public PyObject __call__(PyObject[] args) {
111: return __call__(args, Py.NoKeywords);
112: }
113:
114: public PyObject __call__(PyObject[] args, String[] keywords) {
115: ((PyModule) self).module_init(args, keywords);
116: return Py.None;
117: }
118:
119: }
120: dict.__setitem__("__init__", new PyMethodDescr("__init__",
121: PyModule.class, -1, -1,
122: new exposed___init__(null, null)));
123: dict.__setitem__("__new__", new PyNewWrapper(PyModule.class,
124: "__new__", -1, -1) {
125:
126: public PyObject new_impl(boolean init, PyType subtype,
127: PyObject[] args, String[] keywords) {
128: PyModule newobj;
129: if (for_type == subtype) {
130: newobj = new PyModule();
131: if (init)
132: newobj.module_init(args, keywords);
133: } else {
134: newobj = new PyModuleDerived(subtype);
135: }
136: return newobj;
137: }
138:
139: });
140: }
141:
142: //~ END GENERATED REGION -- DO NOT EDIT SEE gexpose.py
143:
144: private final PyObject module_doc = new PyString(
145: "module(name[, doc])\n"
146: + "\n"
147: + "Create a module object.\n"
148: + "The name must be a string; the optional doc argument can have any type.");
149:
150: public PyObject __dict__;
151:
152: public PyModule() {
153: super ();
154: }
155:
156: public PyModule(PyType subType) {
157: super (subType);
158: }
159:
160: public PyModule(PyType subType, String name) {
161: super (subType);
162: module_init(new PyString(name), Py.None);
163: }
164:
165: public PyModule(String name) {
166: this (name, null);
167: }
168:
169: public PyModule(String name, PyObject dict) {
170: super ();
171: __dict__ = dict;
172: module_init(new PyString(name), Py.None);
173: }
174:
175: final void module_init(PyObject name, PyObject doc) {
176: ensureDict();
177: __dict__.__setitem__("__name__", name);
178: __dict__.__setitem__("__doc__", doc);
179: }
180:
181: final void module_init(PyObject[] args, String[] keywords) {
182: ArgParser ap = new ArgParser("__init__", args, keywords,
183: new String[] { "name", "doc" });
184: PyObject name = ap.getPyObject(0);
185: PyObject docs = ap.getPyObject(1, Py.None);
186: module_init(name, docs);
187: }
188:
189: public PyObject fastGetDict() {
190: return __dict__;
191: }
192:
193: public PyObject getDict() {
194: if (__dict__ == null)
195: return Py.None;
196: return __dict__;
197: }
198:
199: public void setDict(PyObject newDict) {
200: throw Py.TypeError("readonly attribute");
201: }
202:
203: public void delDict() {
204: throw Py.TypeError("readonly attribute");
205: }
206:
207: public PyObject getDoc() {
208: PyObject d = fastGetDict();
209: if (d != null) {
210: PyObject doc = d.__finditem__("__doc__");
211: if (doc != null) {
212: return doc;
213: }
214: }
215: return module_doc;
216: }
217:
218: protected PyObject impAttr(String attr) {
219: PyObject path = __dict__.__finditem__("__path__");
220: PyObject pyname = __dict__.__finditem__("__name__");
221:
222: if (path == null || pyname == null)
223: return null;
224:
225: String name = pyname.__str__().toString();
226: String fullName = (name + '.' + attr).intern();
227:
228: PyObject ret = null;
229:
230: //System.err.println("PyModule.impAttr " + attr + " " + name + " " + fullName);
231: if (path == Py.None) {
232: /* disabled:
233: ret = imp.loadFromClassLoader(
234: (name+'.'+attr).intern(),
235: Py.getSystemState().getClassLoader());
236: */
237: } else if (path instanceof PyList) {
238: ret = imp.find_module(attr, fullName, (PyList) path);
239: } else {
240: throw Py.TypeError("__path__ must be list or None");
241: }
242:
243: if (ret == null) {
244: ret = PySystemState.packageManager.lookupName(fullName);
245: }
246:
247: if (ret != null) {
248: // Allow a package component to change its own meaning
249: PyObject tmp = Py.getSystemState().modules
250: .__finditem__(fullName);
251: if (tmp != null)
252: ret = tmp;
253: __dict__.__setitem__(attr, ret);
254: return ret;
255: }
256:
257: return null;
258: }
259:
260: public PyObject __findattr__(String attr) {
261: return module___findattr__(attr);
262: }
263:
264: final PyObject module___findattr__(String attr) {
265: PyObject ret;
266:
267: if (__dict__ != null) {
268: ret = __dict__.__finditem__(attr);
269: if (ret != null)
270: return ret;
271: }
272:
273: ret = super .__findattr__(attr);
274: if (ret != null)
275: return ret;
276:
277: if (__dict__ == null) {
278: return null;
279: }
280:
281: PyObject pyname = __dict__.__finditem__("__name__");
282: if (pyname == null)
283: return null;
284:
285: return impHook(pyname.__str__().toString() + '.' + attr);
286: }
287:
288: public void __setattr__(String attr, PyObject value) {
289: module___setattr__(attr, value);
290: }
291:
292: final void module___setattr__(String attr, PyObject value) {
293: if (attr != "__dict__")
294: ensureDict();
295: super .__setattr__(attr, value);
296: }
297:
298: public void __delattr__(String attr) {
299: module___delattr__(attr);
300: }
301:
302: final void module___delattr__(String attr) {
303: super .__delattr__(attr);
304: }
305:
306: public String toString() {
307: return module_toString();
308: }
309:
310: final String module_toString() {
311: PyObject name = null;
312: PyObject filename = null;
313: if (__dict__ != null) {
314: name = __dict__.__finditem__("__name__");
315: filename = __dict__.__finditem__("__file__");
316: }
317: if (name == null)
318: name = new PyString("?");
319: if (filename == null)
320: filename = new PyString("(built-in)");
321: else
322: filename = new PyString("from '" + filename + "'");
323: return "<module '" + name + "' " + filename + ">";
324: }
325:
326: public PyObject __dir__() {
327: if (__dict__ == null)
328: throw Py.TypeError("module.__dict__ is not a dictionary");
329: return __dict__.invoke("keys");
330: }
331:
332: private void ensureDict() {
333: if (__dict__ == null)
334: __dict__ = new PyStringMap();
335: }
336:
337: static private PyObject silly_list = null;
338:
339: private static PyObject impHook(String name) {
340: if (silly_list == null) {
341: silly_list = new PyTuple(new PyString[] { Py
342: .newString("__doc__"), });
343: }
344: try {
345: return __builtin__.__import__(name, null, null, silly_list);
346: } catch (PyException e) {
347: if (Py.matchException(e, Py.ImportError)) {
348: return null;
349: }
350: throw e;
351: }
352: }
353:
354: }
|