KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10
11 /**
12  * Utility functions for "import" support.
13  */

14 public class imp
15 {
16     public static final int APIVersion = 12;
17
18     private static Object JavaDoc syspathJavaLoaderLock = new Object JavaDoc();
19     private static ClassLoader JavaDoc syspathJavaLoader = null;
20
21     public static ClassLoader JavaDoc getSyspathJavaLoader() {
22         synchronized (syspathJavaLoaderLock) {
23             if (syspathJavaLoader == null) {
24                 syspathJavaLoader = new SyspathJavaLoader();
25             }
26         }
27         return syspathJavaLoader;
28     }
29
30     private imp() { ; }
31
32     public static PyModule addModule(String JavaDoc name) {
33 // System.err.println("AddModule:" + name);
34
name = name.intern();
35         PyObject modules = Py.getSystemState().modules;
36         PyModule module = (PyModule)modules.__finditem__(name);
37         if (module != null) {
38             return module;
39         }
40         module = new PyModule(name, null);
41         modules.__setitem__(name, module);
42         return module;
43     }
44
45     private static byte[] readBytes(InputStream JavaDoc fp) {
46         try {
47             byte[] buf = FileUtil.readBytes( fp );
48             fp.close();
49             return buf;
50         } catch (IOException JavaDoc ioe) {
51             throw Py.IOError(ioe);
52         }
53     }
54
55     private static InputStream JavaDoc makeStream(File JavaDoc file) {
56         try {
57             return new FileInputStream JavaDoc(file);
58         } catch (IOException JavaDoc ioe) {
59             throw Py.IOError(ioe);
60         }
61     }
62
63     static PyObject createFromPyClass(String JavaDoc name, InputStream JavaDoc fp,
64                                               boolean testing,
65                                               String JavaDoc fileName)
66     {
67         byte[] data = readBytes(fp);
68         int n = data.length;
69
70         int api = (data[n-4]<<24)+(data[n-3]<<16)+(data[n-2]<<8)+data[n-1];
71         if (api != APIVersion) {
72             if (testing) {
73                 return null;
74             } else {
75                 throw Py.ImportError("invalid api version("+api+" != "+
76                                      APIVersion+") in: "+name);
77             }
78         }
79         //System.err.println("APIVersion: "+api);
80
PyCode code;
81         try {
82             code = BytecodeLoader.makeCode(name+"$py", data);
83         } catch (Throwable JavaDoc t) {
84             if (testing)
85                 return null;
86             else
87                 throw Py.JavaError(t);
88         }
89
90         Py.writeComment("import", "'" + name + "' as " + fileName);
91
92         return createFromCode(name, code);
93     }
94
95     public static byte[] compileSource(String JavaDoc name, File JavaDoc file) {
96         return compileSource(name, file, null, null);
97     }
98
99     public static byte[] compileSource(String JavaDoc name, File JavaDoc file,
100                                        String JavaDoc filename, String JavaDoc outFilename)
101     {
102         if (filename == null) {
103             filename = file.toString();
104         }
105
106         if (outFilename == null) {
107             outFilename = filename.substring(0,filename.length()-3)+
108                 "$py.class";
109         }
110
111         return compileSource(name, makeStream(file), filename, outFilename);
112     }
113
114     static byte[] compileSource(String JavaDoc name, InputStream JavaDoc fp,
115                                 String JavaDoc filename)
116     {
117         String JavaDoc outFilename = null;
118         if (filename != null) {
119             outFilename = filename.substring(0,filename.length()-3)+
120                 "$py.class";
121         }
122         return compileSource(name, fp, filename, outFilename);
123     }
124
125     static byte[] compileSource(String JavaDoc name, InputStream JavaDoc fp, String JavaDoc filename,
126                                 String JavaDoc outFilename)
127     {
128         try {
129             ByteArrayOutputStream JavaDoc ofp = new ByteArrayOutputStream JavaDoc();
130
131             if (filename == null)
132                 filename = "<unknown>";
133             org.python.parser.ast.modType node = null; //*Forte*
134
try {
135                 node = parser.parse(fp, "exec", filename, null);
136             } finally {
137                 fp.close();
138             }
139             org.python.compiler.Module.compile(node, ofp, name+"$py",
140                                                filename, true, false, true,
141                                                null);
142
143             if (outFilename != null) {
144                 File JavaDoc classFile = new File JavaDoc(outFilename);
145                 try {
146                     FileOutputStream JavaDoc fop = new FileOutputStream JavaDoc(classFile);
147                     ofp.writeTo(fop);
148                     fop.close();
149                 } catch (IOException JavaDoc exc) {
150                     // If we can't write the cache file, just fail silently
151
}
152             }
153
154             return ofp.toByteArray();
155         } catch (Throwable JavaDoc t) {
156             throw parser.fixParseError(null, t, filename);
157         }
158     }
159
160     public static PyObject createFromSource(String JavaDoc name, InputStream JavaDoc fp,
161                                              String JavaDoc filename)
162     {
163         byte[] bytes = compileSource(name, fp, filename);
164
165         Py.writeComment("import", "'" + name + "' as " + filename);
166
167         PyCode code = BytecodeLoader.makeCode(name+"$py", bytes);
168         return createFromCode(name, code);
169     }
170
171     static PyObject createFromSource(String JavaDoc name, InputStream JavaDoc fp,
172                                              String JavaDoc filename,
173                                              String JavaDoc outFilename)
174     {
175         byte[] bytes = compileSource(name, fp, filename, outFilename);
176
177         Py.writeComment("import", "'" + name + "' as " + filename);
178
179         PyCode code = BytecodeLoader.makeCode(name+"$py", bytes);
180         return createFromCode(name, code);
181     }
182
183
184     static PyObject createFromCode(String JavaDoc name, PyCode c) {
185         PyModule module = addModule(name);
186
187         PyTableCode code = null;
188         if (c instanceof PyTableCode)
189             code = (PyTableCode)c;
190         PyFrame f = new PyFrame(code, module.__dict__, module.__dict__, null);
191         code.call(f);
192
193         return module;
194     }
195
196     static PyObject createFromClass(String JavaDoc name, Class JavaDoc c) {
197         //Two choices. c implements PyRunnable or c is Java package
198
//System.err.println("create from class: "+name+", "+c);
199
Class JavaDoc interfaces[] = c.getInterfaces();
200         for (int i=0; i<interfaces.length; i++) {
201             if (interfaces[i] == PyRunnable.class) {
202                 //System.err.println("is runnable");
203
try {
204                     PyObject o = createFromCode(
205                         name, ((PyRunnable)c.newInstance()).getMain());
206                     return o;
207                 } catch (InstantiationException JavaDoc e) {
208                     throw Py.JavaError(e);
209                 } catch (IllegalAccessException JavaDoc e) {
210                     throw Py.JavaError(e);
211                 }
212             }
213         }
214         return PyJavaClass.lookup(c); // xxx?
215
}
216
217     static PyObject getPathImporter(PyObject cache, PyList hooks, PyObject p) {
218
219         // attempt to get an importer for the path
220
// use null as default value since Py.None is
221
// a valid value in the cache for the default
222
// importer
223
PyObject importer = cache.__finditem__(p);
224         if (importer != null) {
225             return importer;
226         }
227
228         // nothing in the cache, so check all hooks
229
PyObject iter = hooks.__iter__();
230         for(PyObject hook; (hook = iter.__iternext__()) != null;) {
231             try {
232                 importer = hook.__call__(p);
233                 break;
234             } catch (PyException e) {
235                 if(!Py.matchException(e, Py.ImportError)) {
236                     throw e;
237                 }
238             }
239         }
240
241         importer = (importer == null ? Py.None : importer);
242         cache.__setitem__(p, importer);
243
244         return importer;
245     }
246
247     static PyObject replacePathItem(PyObject path) {
248         if(path instanceof SyspathArchive) {
249             // already an archive
250
return null;
251         }
252
253         try {
254             // this has the side affect of adding the jar to the PackageManager
255
// during the initialization of the SyspathArchive
256
return new SyspathArchive(path.toString());
257         } catch (Exception JavaDoc e) {
258             return null;
259         }
260     }
261
262     static PyObject find_module(String JavaDoc name, String JavaDoc moduleName, PyList path) {
263
264         PyObject loader = Py.None;
265         PySystemState sys = Py.getSystemState();
266         PyObject metaPath = sys.meta_path;
267
268         /*
269          Needed to convert all entries on the path to SyspathArchives if
270          necessary.
271         */

272         PyList ppath = path == null ? sys.path : path;
273         for(int i=0;i<ppath.__len__();i++) {
274             PyObject p = ppath.__getitem__(i);
275             PyObject q = replacePathItem(p);
276             if(q == null) {
277                 continue;
278             }
279             ppath.__setitem__(i, q);
280         }
281
282         PyObject iter = metaPath.__iter__();
283         for(PyObject importer; (importer = iter.__iternext__()) != null;) {
284             PyObject findModule = importer.__getattr__("find_module");
285             loader = findModule.__call__(new PyObject[] {
286                 new PyString(moduleName), path == null ? Py.None : path
287             });
288             if(loader != Py.None) {
289                 return loadFromLoader(loader, moduleName);
290             }
291         }
292
293         PyObject ret = loadBuiltin(moduleName);
294         if (ret != null) return ret;
295
296         path = path == null ? sys.path : path;
297         for(int i=0;i<path.__len__();i++) {
298             PyObject p = path.__getitem__(i);
299             //System.err.println("find_module (" + name + ", " + moduleName + ") Path: " + path);
300
PyObject importer = getPathImporter(sys.path_importer_cache, sys.path_hooks, p);
301             if (importer != Py.None) {
302                 PyObject findModule = importer.__getattr__("find_module");
303                 loader = findModule.__call__(new PyObject[] { new PyString(moduleName) });
304                 if(loader != Py.None) {
305                     return loadFromLoader(loader, moduleName);
306                 }
307             }
308             ret = loadFromSource(name, moduleName, p);
309             if (ret != null) return ret;
310         }
311
312         return ret;
313     }
314
315     private static PyObject loadBuiltin(String JavaDoc name) {
316         if (name == "sys") {
317             Py.writeComment("import", "'" + name + "' as sys in " +
318                             "builtin modules");
319             return Py.java2py(Py.getSystemState());
320         }
321         String JavaDoc mod = PySystemState.getBuiltin(name);
322         if (mod != null) {
323             Class JavaDoc c = Py.findClassEx(mod, "builtin modules");
324             if (c != null) {
325                 Py.writeComment("import", "'" + name + "' as " + mod +
326                                 " in builtin modules");
327                 try {
328                     if (PyObject.class.isAssignableFrom(c)) { // xxx ok?
329
return PyType.fromClass(c);
330                     }
331                     return createFromClass(name, c);
332                 }
333                 catch (NoClassDefFoundError JavaDoc e) {
334                     throw Py.ImportError("Cannot import " + name +
335                                          ", missing class " +
336                                          c.getName());
337                 }
338             }
339         }
340         return null;
341     }
342
343     static PyObject loadFromLoader(PyObject importer, String JavaDoc name) {
344         PyObject load_module = importer.__getattr__("load_module");
345         return load_module.__call__(new PyObject[] { new PyString(name) });
346     }
347
348     public static PyObject loadFromSource(String JavaDoc name, InputStream JavaDoc stream, String JavaDoc filename) {
349         return createFromSource(name, stream, filename);
350     }
351
352     public static PyObject loadFromCompiled(String JavaDoc name, InputStream JavaDoc stream, String JavaDoc filename) {
353         return createFromPyClass(name, stream, false, filename);
354     }
355
356     static PyObject loadFromSource(String JavaDoc name, String JavaDoc modName, PyObject entry) {
357         //System.err.println("load-from-source: "+name+" "+modName+" "+entry);
358

359         int nlen = name.length();
360         String JavaDoc sourceName = "__init__.py";
361         String JavaDoc compiledName = "__init__$py.class";
362         String JavaDoc directoryName = entry.toString();
363
364         // The empty string translates into the current working
365
// directory, which is usually provided on the system property
366
// "user.dir". Don't rely on File's constructor to provide
367
// this correctly.
368
if (directoryName.length() == 0) {
369             directoryName = null;
370         }
371
372         // First check for packages
373
File JavaDoc dir = new File JavaDoc(directoryName, name);
374         File JavaDoc sourceFile = new File JavaDoc(dir, sourceName);
375         File JavaDoc compiledFile = new File JavaDoc(dir, compiledName);
376
377         boolean pkg = (dir.isDirectory() && caseok(dir, name, nlen)
378                 && (sourceFile.isFile() || compiledFile.isFile()));
379
380         if(!pkg) {
381             Py.writeDebug("import", "trying source " + dir.getPath());
382             sourceName = name + ".py";
383             compiledName = name + "$py.class";
384             sourceFile = new File JavaDoc(directoryName, sourceName);
385             compiledFile = new File JavaDoc(directoryName, compiledName);
386         } else {
387             PyModule m = addModule(modName);
388             PyObject filename = new PyString(dir.getPath());
389             m.__dict__.__setitem__("__path__",
390                 new PyList(new PyObject[] { filename }));
391             m.__dict__.__setitem__("__file__", filename);
392         }
393
394         if (sourceFile.isFile() && caseok(sourceFile, sourceName, nlen)) {
395             if (compiledFile.isFile() && caseok(compiledFile, compiledName, nlen)) {
396                 Py.writeDebug("import", "trying precompiled " + compiledFile.getPath());
397                 long pyTime = sourceFile.lastModified();
398                 long classTime = compiledFile.lastModified();
399                 if (classTime >= pyTime) {
400                     PyObject ret = createFromPyClass(modName,
401                         makeStream(compiledFile), true, compiledFile.getPath());
402                     if (ret != null)
403                         return ret;
404                 }
405             }
406             return createFromSource(modName, makeStream(sourceFile),
407                 sourceFile.getAbsolutePath());
408         }
409
410         // If no source, try loading precompiled
411
Py.writeDebug("import", "trying " + compiledFile.getPath());
412         if (compiledFile.isFile() && caseok(compiledFile, compiledName, nlen)) {
413             return createFromPyClass(modName, makeStream(compiledFile),
414                 false, compiledFile.getPath());
415         }
416         return null;
417     }
418
419     public static boolean caseok(File JavaDoc file, String JavaDoc filename, int namelen) {
420         if (Options.caseok)
421             return true;
422         try {
423             File JavaDoc canFile = new File JavaDoc(file.getCanonicalPath());
424             return filename.regionMatches(0, canFile.getName(), 0, namelen);
425         } catch (IOException JavaDoc exc) {
426             return false;
427         }
428     }
429
430 // static PyObject loadFromClassLoader(String name,
431
// ClassLoader classLoader)
432
// {
433
// String path = name.replace('.', '/');
434
//
435
// // First check to see if a package exists (look for name/__init__.py)
436
// InputStream istream = classLoader.getResourceAsStream(path+"/__init__.py");
437
// if (istream != null) {
438
// PyModule m = addModule(name);
439
// m.__dict__.__setitem__("__path__", Py.None);
440
// return createFromSource(name, istream, null);
441
// }
442
//
443
// // Finally, try to load from source
444
// istream = classLoader.getResourceAsStream(path+".py");
445
// if (istream != null) return createFromSource(name, istream, null);
446
//
447
// return null;
448
// }
449

450     /**
451      * Load the module by name. Upon loading the module it will be added
452      * to sys.modules.
453      * @param name the name of the module to load
454      * @return the loaded module
455      */

456     public static PyObject load(String JavaDoc name) {
457         return import_first(name, new StringBuffer JavaDoc());
458     }
459
460     /**
461      * Find the parent module name for a module. If __name__ does not
462      * exist in the module then the parent is null. If __name__ does
463      * exist then the __path__ is checked for the parent module. For
464      * example, the __name__ 'a.b.c' would return 'a.b'.
465      * @param dict the __dict__ of a loaded module
466      * @return the parent name for a module
467      */

468     private static String JavaDoc getParent(PyObject dict) {
469         PyObject tmp = dict.__finditem__("__name__");
470         if (tmp == null) return null;
471         String JavaDoc name = tmp.toString();
472
473         tmp = dict.__finditem__("__path__");
474         if (tmp != null && tmp instanceof PyList) {
475             return name.intern();
476         } else {
477             int dot = name.lastIndexOf('.');
478             if (dot == -1) return null;
479             return name.substring(0, dot).intern();
480         }
481     }
482
483     /**
484      *
485      * @param mod a previously loaded module
486      * @param parentNameBuffer
487      * @param name the name of the module to load
488      * @return null or None
489      */

490     private static PyObject import_next(PyObject mod,
491                                         StringBuffer JavaDoc parentNameBuffer,
492                                         String JavaDoc name)
493     {
494         if (parentNameBuffer.length() > 0) {
495             parentNameBuffer.append('.');
496         }
497         parentNameBuffer.append(name);
498
499         String JavaDoc fullName = parentNameBuffer.toString().intern();
500
501         PyObject modules = Py.getSystemState().modules;
502         PyObject ret = modules.__finditem__(fullName);
503         if (ret != null) return ret;
504         if (mod == null) {
505             ret = find_module(fullName.intern(), name, null);
506         } else {
507             ret = mod.impAttr(name.intern());
508         }
509         if (ret == null || ret == Py.None) return ret;
510         if (modules.__finditem__(fullName) == null)
511             modules.__setitem__(fullName, ret);
512         else
513             ret = modules.__finditem__(fullName);
514         return ret;
515     }
516
517     // never returns null or None
518
private static PyObject import_first(String JavaDoc name,
519                                          StringBuffer JavaDoc parentNameBuffer)
520     {
521         PyObject ret = import_next(null,parentNameBuffer,name);
522         if (ret == null || ret == Py.None)
523             throw Py.ImportError("no module named "+name);
524         return ret;
525     }
526
527     // Hierarchy-recursively search for dotted name in mod;
528
// never returns null or None
529
// ??pending: check if result is really a module/jpkg/jclass?
530
private static PyObject import_logic(PyObject mod,
531                                          StringBuffer JavaDoc parentNameBuffer,
532                                          String JavaDoc dottedName)
533     {
534         int dot = 0;
535         int last_dot= 0;
536
537         do {
538             String JavaDoc name;
539             dot = dottedName.indexOf('.', last_dot);
540             if (dot == -1) {
541                 name = dottedName.substring(last_dot);
542             } else {
543                 name = dottedName.substring(last_dot, dot);
544             }
545             mod = import_next(mod,parentNameBuffer,name);
546             if (mod == null || mod == Py.None)
547             throw Py.ImportError("No module named " + name);
548             last_dot = dot + 1;
549         } while (dot != -1);
550
551         return mod;
552     }
553
554     /**
555      * Most similar to import.c:import_module_ex.
556      *
557      * @param name
558      * @param top
559      * @param modDict
560      * @return a module
561      */

562     private static PyObject import_name(String JavaDoc name, boolean top, PyObject modDict) {
563         //System.err.println("import_name " + name);
564
if (name.length() == 0)
565             throw Py.ValueError("Empty module name");
566         PyObject modules = Py.getSystemState().modules;
567         PyObject pkgMod = null;
568         String JavaDoc pkgName = null;
569         if (modDict != null) {
570             pkgName = getParent(modDict);
571             pkgMod = modules.__finditem__(pkgName);
572             //System.err.println("GetParent: " + pkgName + " => " + pkgMod);
573
if (pkgMod != null && !(pkgMod instanceof PyModule))
574                 pkgMod = null;
575         }
576         int dot = name.indexOf('.');
577         String JavaDoc firstName;
578         if (dot == -1)
579             firstName = name;
580         else
581             firstName = name.substring(0,dot);
582         StringBuffer JavaDoc parentNameBuffer = new StringBuffer JavaDoc(
583                 pkgMod != null ? pkgName : "");
584         PyObject topMod = import_next(pkgMod, parentNameBuffer, firstName);
585         if (topMod == Py.None || topMod == null) {
586             if (topMod == null) {
587                 modules.__setitem__(parentNameBuffer.toString().intern(),
588                                     Py.None);
589             }
590             parentNameBuffer = new StringBuffer JavaDoc("");
591             // could throw ImportError
592
topMod = import_first(firstName,parentNameBuffer);
593         }
594         PyObject mod = topMod;
595         if (dot != -1) {
596             // could throw ImportError
597
mod = import_logic(topMod,parentNameBuffer,name.substring(dot+1));
598         }
599         if (top)
600             return topMod;
601         else
602             return mod;
603     }
604
605     /**
606      * Import a module by name.
607      * @param name the name of the package to import
608      * @param top if true, return the top module in the name, otherwise the last
609      * @return an imported module (Java or Python)
610      */

611     public static PyObject importName(String JavaDoc name, boolean top) {
612         return import_name(name, top, null);
613     }
614
615     /**
616      * Import a module by name. This is the default call for
617      * __builtin__.__import__.
618      * @param name the name of the package to import
619      * @param top if true, return the top module in the name, otherwise the last
620      * @param modDict the __dict__ of an already imported module
621      * @return an imported module (Java or Python)
622      */

623     public synchronized static PyObject importName(String JavaDoc name, boolean top,
624                                                    PyObject modDict) {
625         return import_name(name, top, modDict);
626     }
627
628     /**
629      * Called from jpython generated code when a statement like "import spam"
630      * is executed.
631      */

632     public static PyObject importOne(String JavaDoc mod, PyFrame frame) {
633         //System.out.println("importOne(" + mod + ")");
634
PyObject module = __builtin__.__import__(mod,
635                                                  frame.f_globals,
636                                                  frame.getf_locals(),
637                                                  Py.EmptyTuple);
638         /*int dot = mod.indexOf('.');
639         if (dot != -1) {
640             mod = mod.substring(0, dot).intern();
641         }*/

642         //System.err.println("mod: "+mod+", "+dot);
643
return module;
644     }
645
646     /**
647      * Called from jpython generated code when a statement like
648      * "import spam as foo" is executed.
649      */

650     public static PyObject importOneAs(String JavaDoc mod, PyFrame frame) {
651         //System.out.println("importOne(" + mod + ")");
652
PyObject module = __builtin__.__import__(mod,
653                                                  frame.f_globals,
654                                                  frame.getf_locals(),
655                                                  getStarArg());
656         // frame.setlocal(asname, module);
657
return module;
658     }
659
660     /**
661      * Called from jpython generated code when a stamenet like
662      * "from spam.eggs import foo, bar" is executed.
663      */

664     public static PyObject[] importFrom(String JavaDoc mod, String JavaDoc[] names,
665                                         PyFrame frame)
666     {
667         return importFromAs(mod, names, null, frame);
668     }
669
670     /**
671      * Called from jpython generated code when a stamenet like
672      * "from spam.eggs import foo as spam" is executed.
673      */

674     public static PyObject[] importFromAs(String JavaDoc mod, String JavaDoc[] names,
675                                     String JavaDoc[] asnames, PyFrame frame)
676     {
677         //StringBuffer sb = new StringBuffer();
678
//for(int i=0; i<names.length; i++)
679
// sb.append(names[i] + " ");
680
//System.out.println("importFrom(" + mod + ", [" + sb + "]");
681

682         PyObject[] pynames = new PyObject[names.length];
683         for (int i=0; i<names.length; i++)
684             pynames[i] = Py.newString(names[i]);
685
686         PyObject module = __builtin__.__import__(mod,
687                                                  frame.f_globals,
688                                                  frame.getf_locals(),
689                                                  new PyTuple(pynames));
690         PyObject[] submods = new PyObject[names.length];
691         for (int i=0; i<names.length; i++) {
692             PyObject submod = module.__findattr__(names[i]);
693             if (submod == null)
694                 throw Py.ImportError("cannot import name " + names[i]);
695             submods[i] = submod;
696         }
697         return submods;
698     }
699
700     private static PyTuple all = null;
701
702     private synchronized static PyTuple getStarArg() {
703         if (all == null)
704             all = new PyTuple(new PyString[] { Py.newString('*') });
705         return all;
706     }
707
708     /**
709      * Called from jython generated code when a statement like
710      * "from spam.eggs import *" is executed.
711      */

712     public static void importAll(String JavaDoc mod, PyFrame frame) {
713         //System.out.println("importAll(" + mod + ")");
714
PyObject module = __builtin__.__import__(mod,
715                                                  frame.f_globals,
716                                                  frame.getf_locals(),
717                                                  getStarArg());
718         PyObject names;
719         boolean filter = true;
720         if (module instanceof PyJavaPackage)
721             names = ((PyJavaPackage)module).fillDir();
722         else {
723             PyObject __all__ = module.__findattr__("__all__");
724             if (__all__ != null) {
725                 names = __all__;
726                 filter = false;
727             } else names = module.__dir__();
728         }
729
730         loadNames(names, module, frame.getf_locals(), filter);
731     }
732
733     /**
734      * From a module, load the attributes found in <code>names</code>
735      * into locals.
736      *
737      * @param filter if true, if the name starts with an underscore '_'
738      * do not add it to locals
739      * @param locals the namespace into which names will be loaded
740      * @param names the names to load from the module
741      * @param module the fully imported module
742      */

743     private static void loadNames(PyObject names, PyObject module,
744                                   PyObject locals, boolean filter)
745     {
746         PyObject iter = names.__iter__();
747         for (PyObject name; (name = iter.__iternext__()) != null; ) {
748             String JavaDoc sname = ((PyString)name).internedString();
749             if (filter && sname.startsWith("_")) {
750                 continue;
751             } else {
752                 try {
753                     locals.__setitem__(sname, module.__getattr__(sname));
754                 } catch (Exception JavaDoc exc) {
755                     continue;
756                 }
757             }
758         }
759     }
760
761     /* Reloading */
762     static PyObject reload(PyJavaClass c) {
763         // This is a dummy placeholder for the feature that allow
764
// reloading of java classes. But this feature does not yet
765
// work.
766
return c;
767     }
768
769     static PyObject reload(PyModule m) {
770         String JavaDoc name = m.__getattr__("__name__").toString().intern();
771
772         PyObject modules = Py.getSystemState().modules;
773         PyModule nm = (PyModule)modules.__finditem__(name);
774
775         if (nm == null || !nm.__getattr__("__name__").toString()
776                                                      .equals(name)) {
777             throw Py.ImportError("reload(): module "+name+
778                                  " not in sys.modules");
779         }
780
781         PyList path = Py.getSystemState().path;
782         String JavaDoc modName = name;
783         int dot = name.lastIndexOf('.');
784         if (dot != -1) {
785             String JavaDoc iname = name.substring(0, dot).intern();
786             PyObject pkg = modules.__finditem__(iname);
787             if (pkg == null) {
788                 throw Py.ImportError("reload(): parent not in sys.modules");
789             }
790             path = (PyList)pkg.__getattr__("__path__");
791             name = name.substring(dot+1, name.length()).intern();
792         }
793
794         // This should be better "protected"
795
//((PyStringMap)nm.__dict__).clear();
796

797         nm.__setattr__("__name__", new PyString(modName));
798         PyObject ret = find_module(name, modName, path);
799         modules.__setitem__(modName, ret);
800         return ret;
801     }
802 }
803
Popular Tags