1 package org.python.core; 2 3 7 public class PrecompiledImporter extends PyObject { 8 9 public PrecompiledImporter() { 10 super(); 11 } 12 13 18 public PyObject find_module(String name) { 19 return find_module(name, Py.None); 20 } 21 22 28 public PyObject find_module(String name, PyObject path) { 29 if (Py.frozenModules != null) { 30 Class c = null; 32 if (Py.frozenModules.get(name+".__init__") != null) { 33 Py.writeDebug("import", "trying " + name + " as precompiled package"); 35 c = findPyClass(name+".__init__"); 36 if (c == null) { 37 return Py.None; 38 } 39 return new PrecompiledLoader(c, true); 41 } else if (Py.frozenModules.get(name) != null) { 42 Py.writeDebug("import", "trying " + name + " as precompiled module"); 43 c = findPyClass(name); 44 if (c == null) { 45 return Py.None; 46 } 47 return new PrecompiledLoader(c, false); 48 } 49 } 50 return Py.None; 51 } 52 53 58 public String toString() { 59 return this.getType().toString(); 60 } 61 62 public class PrecompiledLoader extends PyObject { 63 64 private Class _class; 65 private boolean _package; 66 67 public PrecompiledLoader(Class class_, boolean package_) { 68 this._class = class_; 69 this._package = package_; 70 } 71 72 public PyObject load_module(String name) { 73 if(this._package) { 74 PyModule m = imp.addModule(name); 75 m.__dict__.__setitem__("__path__", new PyList()); 76 m.__dict__.__setitem__("__loader__", this); 77 } 78 Py.writeComment("import", "'" + name + "' as precompiled " 79 + (this._package ? "package" : "module")); 80 return imp.createFromClass(name, this._class); 81 } 82 83 88 public String toString() { 89 return this.getType().toString(); 90 } 91 } 92 93 private Class findPyClass(String name) { 94 if (Py.frozenPackage != null) { 95 name = Py.frozenPackage+"."+name; 96 } 97 return Py.findClassEx(name+"$_PyInner", "precompiled"); 98 } 99 } 100 | Popular Tags |