1 package org.python.core; 3 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 11 14 public class imp 15 { 16 public static final int APIVersion = 12; 17 18 private static Object syspathJavaLoaderLock = new Object (); 19 private static ClassLoader syspathJavaLoader = null; 20 21 public static ClassLoader 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 name) { 33 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 fp) { 46 try { 47 byte[] buf = FileUtil.readBytes( fp ); 48 fp.close(); 49 return buf; 50 } catch (IOException ioe) { 51 throw Py.IOError(ioe); 52 } 53 } 54 55 private static InputStream makeStream(File file) { 56 try { 57 return new FileInputStream (file); 58 } catch (IOException ioe) { 59 throw Py.IOError(ioe); 60 } 61 } 62 63 static PyObject createFromPyClass(String name, InputStream fp, 64 boolean testing, 65 String 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 PyCode code; 81 try { 82 code = BytecodeLoader.makeCode(name+"$py", data); 83 } catch (Throwable 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 name, File file) { 96 return compileSource(name, file, null, null); 97 } 98 99 public static byte[] compileSource(String name, File file, 100 String filename, String 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 name, InputStream fp, 115 String filename) 116 { 117 String 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 name, InputStream fp, String filename, 126 String outFilename) 127 { 128 try { 129 ByteArrayOutputStream ofp = new ByteArrayOutputStream (); 130 131 if (filename == null) 132 filename = "<unknown>"; 133 org.python.parser.ast.modType node = null; 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 classFile = new File (outFilename); 145 try { 146 FileOutputStream fop = new FileOutputStream (classFile); 147 ofp.writeTo(fop); 148 fop.close(); 149 } catch (IOException exc) { 150 } 152 } 153 154 return ofp.toByteArray(); 155 } catch (Throwable t) { 156 throw parser.fixParseError(null, t, filename); 157 } 158 } 159 160 public static PyObject createFromSource(String name, InputStream fp, 161 String 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 name, InputStream fp, 172 String filename, 173 String 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 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 name, Class c) { 197 Class interfaces[] = c.getInterfaces(); 200 for (int i=0; i<interfaces.length; i++) { 201 if (interfaces[i] == PyRunnable.class) { 202 try { 204 PyObject o = createFromCode( 205 name, ((PyRunnable)c.newInstance()).getMain()); 206 return o; 207 } catch (InstantiationException e) { 208 throw Py.JavaError(e); 209 } catch (IllegalAccessException e) { 210 throw Py.JavaError(e); 211 } 212 } 213 } 214 return PyJavaClass.lookup(c); } 216 217 static PyObject getPathImporter(PyObject cache, PyList hooks, PyObject p) { 218 219 PyObject importer = cache.__finditem__(p); 224 if (importer != null) { 225 return importer; 226 } 227 228 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 return null; 251 } 252 253 try { 254 return new SyspathArchive(path.toString()); 257 } catch (Exception e) { 258 return null; 259 } 260 } 261 262 static PyObject find_module(String name, String moduleName, PyList path) { 263 264 PyObject loader = Py.None; 265 PySystemState sys = Py.getSystemState(); 266 PyObject metaPath = sys.meta_path; 267 268 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 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 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 mod = PySystemState.getBuiltin(name); 322 if (mod != null) { 323 Class 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)) { return PyType.fromClass(c); 330 } 331 return createFromClass(name, c); 332 } 333 catch (NoClassDefFoundError 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 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 name, InputStream stream, String filename) { 349 return createFromSource(name, stream, filename); 350 } 351 352 public static PyObject loadFromCompiled(String name, InputStream stream, String filename) { 353 return createFromPyClass(name, stream, false, filename); 354 } 355 356 static PyObject loadFromSource(String name, String modName, PyObject entry) { 357 359 int nlen = name.length(); 360 String sourceName = "__init__.py"; 361 String compiledName = "__init__$py.class"; 362 String directoryName = entry.toString(); 363 364 if (directoryName.length() == 0) { 369 directoryName = null; 370 } 371 372 File dir = new File (directoryName, name); 374 File sourceFile = new File (dir, sourceName); 375 File compiledFile = new File (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 (directoryName, sourceName); 385 compiledFile = new File (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 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 file, String filename, int namelen) { 420 if (Options.caseok) 421 return true; 422 try { 423 File canFile = new File (file.getCanonicalPath()); 424 return filename.regionMatches(0, canFile.getName(), 0, namelen); 425 } catch (IOException exc) { 426 return false; 427 } 428 } 429 430 450 456 public static PyObject load(String name) { 457 return import_first(name, new StringBuffer ()); 458 } 459 460 468 private static String getParent(PyObject dict) { 469 PyObject tmp = dict.__finditem__("__name__"); 470 if (tmp == null) return null; 471 String 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 490 private static PyObject import_next(PyObject mod, 491 StringBuffer parentNameBuffer, 492 String name) 493 { 494 if (parentNameBuffer.length() > 0) { 495 parentNameBuffer.append('.'); 496 } 497 parentNameBuffer.append(name); 498 499 String 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 private static PyObject import_first(String name, 519 StringBuffer 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 private static PyObject import_logic(PyObject mod, 531 StringBuffer parentNameBuffer, 532 String dottedName) 533 { 534 int dot = 0; 535 int last_dot= 0; 536 537 do { 538 String 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 562 private static PyObject import_name(String name, boolean top, PyObject modDict) { 563 if (name.length() == 0) 565 throw Py.ValueError("Empty module name"); 566 PyObject modules = Py.getSystemState().modules; 567 PyObject pkgMod = null; 568 String pkgName = null; 569 if (modDict != null) { 570 pkgName = getParent(modDict); 571 pkgMod = modules.__finditem__(pkgName); 572 if (pkgMod != null && !(pkgMod instanceof PyModule)) 574 pkgMod = null; 575 } 576 int dot = name.indexOf('.'); 577 String firstName; 578 if (dot == -1) 579 firstName = name; 580 else 581 firstName = name.substring(0,dot); 582 StringBuffer parentNameBuffer = new StringBuffer ( 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 (""); 591 topMod = import_first(firstName,parentNameBuffer); 593 } 594 PyObject mod = topMod; 595 if (dot != -1) { 596 mod = import_logic(topMod,parentNameBuffer,name.substring(dot+1)); 598 } 599 if (top) 600 return topMod; 601 else 602 return mod; 603 } 604 605 611 public static PyObject importName(String name, boolean top) { 612 return import_name(name, top, null); 613 } 614 615 623 public synchronized static PyObject importName(String name, boolean top, 624 PyObject modDict) { 625 return import_name(name, top, modDict); 626 } 627 628 632 public static PyObject importOne(String mod, PyFrame frame) { 633 PyObject module = __builtin__.__import__(mod, 635 frame.f_globals, 636 frame.getf_locals(), 637 Py.EmptyTuple); 638 642 return module; 644 } 645 646 650 public static PyObject importOneAs(String mod, PyFrame frame) { 651 PyObject module = __builtin__.__import__(mod, 653 frame.f_globals, 654 frame.getf_locals(), 655 getStarArg()); 656 return module; 658 } 659 660 664 public static PyObject[] importFrom(String mod, String [] names, 665 PyFrame frame) 666 { 667 return importFromAs(mod, names, null, frame); 668 } 669 670 674 public static PyObject[] importFromAs(String mod, String [] names, 675 String [] asnames, PyFrame frame) 676 { 677 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 712 public static void importAll(String mod, PyFrame frame) { 713 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 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 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 exc) { 755 continue; 756 } 757 } 758 } 759 } 760 761 762 static PyObject reload(PyJavaClass c) { 763 return c; 767 } 768 769 static PyObject reload(PyModule m) { 770 String 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 modName = name; 783 int dot = name.lastIndexOf('.'); 784 if (dot != -1) { 785 String 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 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 |