Update of /cvsroot/jython/jython/org/python/core In directory slayer.i.sourceforge.net:/tmp/cvs-serv1091/core Modified Files: BytecodeLoader.java PackageManager.java Py.java PyJavaClass.java PyJavaPackage.java PyModule.java PySystemState.java imp.java Log Message: Loading logic fixes. For issues involved see the jython-dev list archives. Index: BytecodeLoader.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/BytecodeLoader.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -r2.6 -r2.7 *** BytecodeLoader.java 1999/10/28 17:47:41 2.6 --- BytecodeLoader.java 2000/11/17 12:32:43 2.7 *************** *** 8,11 **** --- 8,44 ---- public class BytecodeLoader extends ClassLoader { + + public InputStream getResourceAsStream(String res) { + ClassLoader classLoader = Py.getSystemState().getClassLoader(); + if (classLoader != null) return classLoader.getResourceAsStream(res); + + classLoader = this.getClass().getClassLoader(); + + InputStream ret; + + if (classLoader != null) ret = classLoader.getResourceAsStream(res); + else ret = ClassLoader.getSystemResourceAsStream(res); + + if (ret != null) return ret; + + if(res.charAt(0) == '/') res = res.substring(1); + + res.replace('/',File.separatorChar); + + PyList path = Py.getSystemState().path; + for (int i=0; i < path.__len__(); i++) { + String dir = path.get(i).__str__().toString(); + if (dir.length() == 0) dir = null; + try { + return new BufferedInputStream(new FileInputStream(new File(dir,res))); + } + catch (IOException e) { + continue; + } + } + + return null; + } + // override from abstract base class protected Class loadClass(String name, boolean resolve) *************** *** 20,29 **** // Search the sys.path for a .class file matching the named class. // TBD: This registry option is temporary. ! if (Options.extendedClassLoader && // KLUDGE ALERT: without this test, running jpython // interactively from the build directory will fail with // ClassCircularityError or LinkageError. There's gotta be a // better way. ! !name.startsWith("org.python")) { PyList path = Py.getSystemState().path; --- 53,70 ---- // Search the sys.path for a .class file matching the named class. // TBD: This registry option is temporary. ! try { ! return Class.forName(name); ! } ! catch(ClassNotFoundException e) { ! } ! ! /* previously: if ! Options.extendedClassLoader && // KLUDGE ALERT: without this test, running jpython // interactively from the build directory will fail with // ClassCircularityError or LinkageError. There's gotta be a // better way. ! !name.startsWith("org.python") ! */ { PyList path = Py.getSystemState().path; *************** *** 45,51 **** } } ! // couldn't find the .class file on sys.path, so give the system ! // class loader the final shot at it ! return findSystemClass(name); } --- 86,92 ---- } } ! ! // couldn't find the .class file on sys.path ! throw new ClassNotFoundException(name); } *************** *** 63,68 **** } try { ! if (dir == "") ! dir = "."; return new FileInputStream(new File(dir, accum+".class")); } --- 104,109 ---- } try { ! if (dir.length() == 0) ! dir = null; return new FileInputStream(new File(dir, accum+".class")); } Index: PackageManager.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PackageManager.java,v retrieving revision 2.2 retrieving revision 2.3 diff -C2 -r2.2 -r2.3 *** PackageManager.java 1999/05/17 19:59:37 2.2 --- PackageManager.java 2000/11/17 12:32:43 2.3 *************** *** 1,91 **** - // Copyright © Corporation for National Research Initiatives - package org.python.core; - - import java.util.*; - import java.io.*; - import java.util.zip.*; - - class JarEntry implements Serializable { - public long mtime; - public File cachefile; - public JarEntry(File cachefile, long mtime) { - this.mtime = mtime; - this.cachefile = cachefile; - } - public String toString() { - return "JarEntry("+cachefile+":"+mtime+")"; - } - } ! class JarPackage { ! public String name; ! public String classes; ! public String filename; ! public JarPackage(String name, String classes, String filename) { ! this.name = name; ! this.classes = classes; ! this.filename = filename; ! } ! } ! ! public class PackageManager { ! public File cachedir; ! private boolean indexModified; ! public Hashtable jarfiles; ! public Hashtable packages; ! public PyStringMap topLevelPackages; ! public PyList searchPath; ! public PyObject topDirectoryPackage; ! public PackageManager(File cachedir, Properties registry) { ! searchPath = new PyList(); ! packages = new Hashtable(); ! topLevelPackages = new PyStringMap(); ! topDirectoryPackage = new PyJavaDirPackage("", searchPath); ! this.cachedir = cachedir; ! initPackages(registry); } ! public void initPackages(Properties registry) { ! if (cachedir == null) return; ! ! if (!cachedir.isDirectory() && cachedir.mkdirs() == false) { ! Py.writeWarning("packageManager", ! "can't create package cache dir, '"+cachedir+"'"); ! return; ! } ! ! loadIndexFile(); ! findAllPackages(registry); ! saveIndexFile(); ! ! Hashtable packs = packages; ! ! for (Enumeration e = packs.elements(); e.hasMoreElements(); ) { ! JarPackage jp = (JarPackage)e.nextElement(); ! makeJavaPackage(jp.name, jp.classes, jp.filename); } } ! public PyObject lookupName(String name) { ! int dot = name.indexOf('.'); ! String firstName=name; ! String lastName=null; ! if (dot != -1) { ! firstName = name.substring(0,dot); ! lastName = name.substring(dot+1, name.length()); ! } ! firstName = firstName.intern(); ! PyObject top = findName(firstName); ! if (top == null) return null; ! name = lastName; ! while (name != null) { ! dot = lastName.indexOf('.'); ! firstName = name; ! lastName = null; if (dot != -1) { firstName = name.substring(0,dot); --- 1,84 ---- ! package org.python.core; ! /** Abstract package manager. ! */ ! public abstract class PackageManager extends Object { ! ! public PyJavaPackage topLevelPackage; ! ! public PackageManager() { ! topLevelPackage = new PyJavaPackage("",this); ! } ! ! abstract public Class findClass(String pkg,String name); ! ! /** Dynamically check if pkg.name exists as java pkg in the controlled hierarchy. ! * Should be overriden. ! * @param pkg parent pkg name ! * @param name candidate name ! * @return true if pkg exists ! */ ! public abstract boolean packageExists(String pkg,String name); ! ! /** Reports the specified package content names. Should be overriden. ! * Used by {@link PyJavaPackage#__dir__} and {@link PyJavaPackage#fillDir}. ! * @return resulting list of names (PyList of PyString) ! * @param jpkg queried package ! * @param instantiate if true then instatiate reported names in package dict ! * @param exclpkgs exclude packages (just when instantiate is false) ! */ ! public abstract PyList doDir(PyJavaPackage jpkg,boolean instantiate,boolean exclpkgs); ! ! /** Basic helper implementation of {@link #doDir}. ! * It merges information from jpkg {@link PyJavaPackage#clsSet} and {@link PyJavaPackage#__dict__}. ! */ ! protected PyList basicDoDir(PyJavaPackage jpkg,boolean instantiate,boolean exclpkgs) { ! PyStringMap dict = jpkg.__dict__; ! PyStringMap cls = jpkg.clsSet; ! ! if(!instantiate) { ! PyList ret = cls.keys(); ! ! PyList dictKeys = dict.keys(); ! ! for(int i=0; i < dictKeys.__len__(); i++) { ! PyObject name = dictKeys.get(i); ! if (!cls.has_key(name)) { ! if (exclpkgs && dict.get(name) instanceof PyJavaPackage) continue; ! ret.append(name); ! } ! } ! return ret; ! } ! PyList clsNames = cls.keys(); ! for(int i=0; i < clsNames.__len__(); i++) { ! PyObject name = clsNames.get(i); ! if(!dict.has_key(name)) jpkg.addLazyClass(name.toString()); ! } ! return dict.keys(); } ! /** Helper merging list2 into list1. Returns list1. ! */ ! protected PyList merge(PyList list1,PyList list2) { ! for(int i=0; i < list2.__len__() ;i++) { ! PyObject name = list2.get(i); ! list1.append(name); } + + return list1; } ! public PyObject lookupName(String name) { ! PyObject top = topLevelPackage; ! do { ! int dot = name.indexOf('.'); ! String firstName = name; ! String lastName = null; if (dot != -1) { firstName = name.substring(0,dot); *************** *** 95,132 **** top = top.__findattr__(firstName); if (top == null) return null; name = lastName; ! } return top; } - ! public PyObject jarFindName(String name) { ! return topLevelPackages.__finditem__(name); ! } ! ! public PyObject dirFindName(String name) { ! return topDirectoryPackage.__findattr__(name); ! } ! ! /** ! Find a top-level package by name (a top-level package is one without ! any '.' in its name). ! ! @param name the name of the top-level package to find - ! <b> must be an interned string </b>. ! @return a package object or null if name is not found. ! **/ ! public PyObject findName(String name) { ! // First look for this as a top-level .jar package ! PyObject pkg = jarFindName(name); ! if (pkg != null) return pkg; ! // Then check for it in the directory packages (this is slower) ! return dirFindName(name); } - // Check that a given stream is a valid Java .class file - // And return its access permissions as an int - static int checkAccess(DataInputStream istream) throws IOException { int magic = istream.readInt(); int minor = istream.readShort(); --- 88,122 ---- top = top.__findattr__(firstName); if (top == null) return null; + // ??pending: test for jpkg/jclass? name = lastName; ! } while (name != null); return top; } ! /** Creates package/updates statically known classes info. ! * Uses {@link PyJavaPackage#addPackage(java.lang.String, java.lang.String) }, {@link PyJavaPackage#addPlaceholders}. ! * ! * @param name package name ! * @param classes comma-separated string ! * @param jarfile involved jarfile; can be null ! * @return created/updated package ! */ ! public PyJavaPackage makeJavaPackage(String name,String classes,String jarfile) { ! PyJavaPackage p = topLevelPackage; ! if(name.length() != 0) p=p.addPackage(name,jarfile); ! if (classes != null) p.addPlaceholders(classes); ! ! return p; } + + private static final int MAXSKIP = 512; + + /** Check that a given stream is a valid Java .class file. + * And return its access permissions as an int. + */ + static protected int checkAccess(java.io.InputStream cstream) throws java.io.IOException { + java.io.DataInputStream istream=new java.io.DataInputStream(cstream); int magic = istream.readInt(); int minor = istream.readShort(); *************** *** 140,159 **** //System.out.println(""+i+" : "+cid); switch (cid) { ! case 7: istream.skipBytes(2); break; ! case 9: ! case 10: ! case 11: istream.skipBytes(4); break; ! case 8: istream.skipBytes(2); break; ! case 3: ! case 4: istream.skipBytes(4); break; ! case 5: ! case 6: istream.skipBytes(8); i++; break; ! case 12: istream.skipBytes(4); break; ! case 1: //System.out.println("utf: "+istream.readUTF()+";"); ! int slength = istream.readShort(); istream.skipBytes(slength); break; ! default: //System.err.println("unexpected cid: "+cid+", "+i+", "+nconstants); //for (int j=0; j<10; j++) { System.err.print(", "+istream.readByte()); } --- 130,153 ---- //System.out.println(""+i+" : "+cid); switch (cid) { ! case 7: istream.skipBytes(2); break; ! case 9: ! case 10: ! case 11: istream.skipBytes(4); break; ! case 8: istream.skipBytes(2); break; ! case 3: ! case 4: istream.skipBytes(4); break; ! case 5: ! case 6: istream.skipBytes(8); i++; break; ! case 12: istream.skipBytes(4); break; ! case 1: //System.out.println("utf: "+istream.readUTF()+";"); ! int slength = istream.readUnsignedShort(); ! while (slength > MAXSKIP) { // workaround to java1.1 bug ! istream.skipBytes(MAXSKIP); ! slength -= MAXSKIP; ! } istream.skipBytes(slength); break; ! default: //System.err.println("unexpected cid: "+cid+", "+i+", "+nconstants); //for (int j=0; j<10; j++) { System.err.print(", "+istream.readByte()); } *************** *** 165,540 **** } - // Add a single class from zipFile to zipPackages - // Only add valid, public classes - static void addZipEntry(Hashtable zipPackages, ZipEntry entry, - ZipFile zipFile) - throws IOException - { - String name = entry.getName(); - //System.err.println("entry: "+name); - if (!name.endsWith(".class")) return; - // Ignore inner classes (at least for now) - if (name.indexOf("$") != -1) return; - char sep = '/'; - int breakPoint = name.lastIndexOf(sep); - if (breakPoint == -1) { - breakPoint = name.lastIndexOf('\\'); - sep = '\\'; - // Ignore entries in top-level package - if (breakPoint == -1) return; - } - - String packageName = name.substring(0,breakPoint).replace(sep, '.'); - String className = name.substring(breakPoint+1, name.length()-6); - - // An extra careful test, maybe should be ignored???? - InputStream istream = zipFile.getInputStream(entry); - int access = checkAccess( - new DataInputStream(new BufferedInputStream(istream))); - if ((access == -1) || ((access & 0x01) != 1)) return; - - Vector vec = (Vector)zipPackages.get(packageName); - if (vec == null) { - vec = new Vector(); - zipPackages.put(packageName, vec); - } - vec.addElement(className); - } - - /*public static String[] vectorToStrings(Vector vec) { - int n = vec.size(); - String[] ret = new String[n]; - for(int i=0; i<n; i++) { - ret[i] = (String)vec.elementAt(i); - } - return ret; - }*/ - static String vectorToString(Vector vec) { - int n = vec.size(); - StringBuffer ret = new StringBuffer(); - for(int i=0; i<n; i++) { - ret.append((String)vec.elementAt(i)); - if (i<n-1) ret.append(","); - } - return ret.toString(); - } - - - // Extract all of the packages in a single jarfile - static Hashtable getZipPackages(File jarfile) throws IOException { - Hashtable zipPackages = new Hashtable(); - ZipFile zf = new ZipFile(jarfile); - for (Enumeration e = zf.entries() ; e.hasMoreElements() ;) { - ZipEntry entry = (ZipEntry)e.nextElement(); - addZipEntry(zipPackages, entry, zf); - } - // Turn each vector into a comma-separated String - for (Enumeration e = zipPackages.keys() ; e.hasMoreElements() ;) { - Object key = e.nextElement(); - Vector vec = (Vector)zipPackages.get(key); - zipPackages.put(key, vectorToString(vec)); - } - return zipPackages; - } - - - public void addDirectoryToPackages(File directory) { - try { - if (!directory.isDirectory()) return; - - searchPath.append(new PyString(directory.getCanonicalPath())); - } catch (IOException ioe) { - // silently skip any bad directories - Py.writeWarning("packageManager", - "skipping bad directory, '" + - directory.toString() + "'"); - } - } - - - public void addJarToPackages(File jarfile) { - try { - if (!jarfile.exists()) return; - - long mtime = jarfile.lastModified(); - String canonicalJarfile = jarfile.getCanonicalPath(); - JarEntry jarEntry = (JarEntry)jarfiles.get(canonicalJarfile); - - if (jarEntry == null) { - Py.writeMessage("packageManager", "processing new jar, \""+ - canonicalJarfile+"\""); - - jarEntry = new JarEntry(findJarCacheFile(jarfile), 0); - jarfiles.put(canonicalJarfile, jarEntry); - } - - Hashtable zipPackages = null; - if (jarEntry.mtime == mtime) { - zipPackages = readCacheFile(jarEntry.cachefile, mtime, - canonicalJarfile); - } - - if (zipPackages == null) { - indexModified = true; - if (jarEntry.mtime != 0) { - Py.writeMessage("packageManager", - "processing modified jar, \""+ - canonicalJarfile+"\""); - } - jarEntry.mtime = mtime; - - zipPackages = getZipPackages(jarfile); - // Write the cache file - writeCacheFile(jarEntry.cachefile, mtime, - canonicalJarfile, zipPackages); - } - - addPackages(zipPackages, canonicalJarfile); - } catch (IOException ioe) { - // silently skip any bad directories - Py.writeWarning("packageManager", - "skipping bad jarfile, '" + - jarfile.toString() + "'"); - } - } - - void addPackages(Hashtable zipPackages, String jarfile) { - for (Enumeration e = zipPackages.keys() ; e.hasMoreElements() ;) { - Object key = e.nextElement(); - JarPackage value = new JarPackage((String)key, - (String)zipPackages.get(key), - jarfile); - //Object value = zipPackages.get(key); - packages.put(key, value); - } - } - - - // Find a file in the current cachedir to hold cache for this jar - File findJarCacheFile(File jarfile) { - String jname = jarfile.getName(); - jname = jname.substring(0,jname.length()-4); - - int index = 1; - String suffix = ""; - File cachefile = null; - while (true) { - cachefile = new File(cachedir, jname+suffix+".pkc"); - //System.err.println("try cachefile: "+cachefile); - if (!cachefile.exists()) break; - suffix = "$"+index; - index += 1; - } - return cachefile; - } - - // Write a cache file storing package info for a single .jar - public static void writeCacheFile(File cachefile, long mtime, - String canonicalJarfile, - Hashtable zipPackages) - { - try { - DataOutputStream ostream = new DataOutputStream( - new BufferedOutputStream(new FileOutputStream(cachefile))); - ostream.writeUTF(canonicalJarfile); - ostream.writeLong(mtime); - Py.writeComment("packageManager", "rewriting cachefile for '"+ - canonicalJarfile+"'"); - - for (Enumeration e = zipPackages.keys() ; e.hasMoreElements() ;) { - String packageName = (String)e.nextElement(); - String classes = (String)zipPackages.get(packageName); - ostream.writeUTF(packageName); - ostream.writeUTF(classes); - } - ostream.close(); - } catch (IOException ioe) { - Py.writeWarning("packageManager", "can't write cache file to, '"+ - cachefile+"'"); - } - } - - // Read in cache file storing package info for a single .jar - // Return null and delete this cachefile if it is invalid - public static Hashtable readCacheFile(File cachefile, long mtime, - String canonicalJarfile) - { - Py.writeDebug("packageManager", "reading cache, '"+ - canonicalJarfile+"'"); - try { - DataInputStream istream = new DataInputStream( - new BufferedInputStream(new FileInputStream(cachefile))); - String old_jarfile = istream.readUTF(); - long old_mtime = istream.readLong(); - if ((!old_jarfile.equals(canonicalJarfile)) || - (old_mtime != mtime)) - { - Py.writeComment("packageManager", - "invalid cache file: "+ - cachefile+", "+canonicalJarfile+":"+ - old_jarfile+", "+mtime+":"+old_mtime); - cachefile.delete(); - return null; - } - Hashtable packs = new Hashtable(); - try { - while (true) { - String packageName = istream.readUTF(); - String classes = istream.readUTF(); - packs.put(packageName, classes); - } - } catch (EOFException eof) { - ; - } - istream.close(); - - return packs; - } catch (IOException ioe) { - if (cachefile.exists()) cachefile.delete(); - return null; - } - } - - - public void findAllPackages(Properties registry) { - String paths = registry.getProperty( - "python.packages.paths", - "java.class.path,sun.boot.class.path"); - String directories = registry.getProperty( - "python.packages.directories", - "java.ext.dirs"); - String fakepath = registry.getProperty("python.packages.fakepath", ""); - - StringTokenizer tok = new StringTokenizer(paths, ","); - while (tok.hasMoreTokens()) { - String entry = tok.nextToken().trim(); - String tmp = registry.getProperty(entry); - if (tmp == null) continue; - addClassPath(tmp); - } - - tok = new StringTokenizer(directories, ","); - while (tok.hasMoreTokens()) { - String entry = tok.nextToken().trim(); - String tmp = registry.getProperty(entry); - if (tmp == null) continue; - addJarPath(tmp); - } - - addClassPath(fakepath); - } - - - public PyJavaPackage makeJavaPackage(String name, String classes, - String jarfile) - { - int dot = name.indexOf('.'); - String firstName=name; - String lastName=null; - if (dot != -1) { - firstName = name.substring(0,dot); - lastName = name.substring(dot+1, name.length()); - } - - firstName = firstName.intern(); - PyJavaPackage p = - (PyJavaPackage)topLevelPackages.__finditem__(firstName); - if (p == null) { - p = new PyJavaPackage(firstName, jarfile); - topLevelPackages.__setitem__(firstName, p); - } - PyJavaPackage ret = p; - if (lastName != null) ret = p.addPackage(lastName, jarfile); - ret._unparsedAll = classes; - return ret; - } - - public void addJarDir(String jdir) { - File file = new File(jdir); - if (!file.isDirectory()) return; - String[] files = file.list(); - for(int i=0; i<files.length; i++) { - String entry = files[i]; - if (entry.endsWith(".jar") || entry.endsWith(".zip")) { - addJarToPackages(new File(jdir, entry)); - } - } - /*} catch (IOException ioe) { - Py.writeWarning("packageManager", "bad jar directory, '"+jdir+"'"); - }*/ - } - public void addJarPath(String path) { - StringTokenizer tok = - new StringTokenizer(path, java.io.File.pathSeparator); - while (tok.hasMoreTokens()) { - String entry = tok.nextToken(); - addJarDir(entry); - } - } - - public void addClassPath(String path) { - StringTokenizer tok = - new StringTokenizer(path, java.io.File.pathSeparator); - while (tok.hasMoreTokens()) { - String entry = tok.nextToken().trim(); - if (entry.endsWith(".jar") || entry.endsWith(".zip")) { - addJarToPackages(new File(entry)); - } else { - addDirectoryToPackages(new File(entry)); - } - } - } - - public void loadIndexFile() { - indexModified = false; - jarfiles = new Hashtable(); - File indexFile = new File(cachedir, "packages.idx"); - - try { - if (!indexFile.exists()) return; - - DataInputStream istream = new DataInputStream( - new BufferedInputStream(new FileInputStream(indexFile))); - try { - while (true) { - String jarfile = istream.readUTF(); - String cachefile = istream.readUTF(); - long mtime = istream.readLong(); - jarfiles.put(jarfile, new JarEntry(new File(cachefile), - mtime)); - } - } catch (EOFException eof) { - ; - } - istream.close(); - } catch (IOException ioe) { - Py.writeWarning("packageManager", - "invalid index file, '"+indexFile+"'"); - } - } - - public void saveIndexFile() { - if (!indexModified) return; - indexModified = false; - - Py.writeComment("packageManager", "writing modified index file"); - File indexFile = new File(cachedir, "packages.idx"); - - try { - DataOutputStream ostream = new DataOutputStream( - new BufferedOutputStream(new FileOutputStream(indexFile))); - for (Enumeration e = jarfiles.keys(); e.hasMoreElements();) { - String jarfile = (String)e.nextElement(); - JarEntry je = (JarEntry)jarfiles.get(jarfile); - ostream.writeUTF(jarfile); - ostream.writeUTF(je.cachefile.getCanonicalPath()); - ostream.writeLong(je.mtime); - } - ostream.close(); - } catch (IOException ioe) { - Py.writeWarning("packageManager", - "can't write index file, '"+indexFile+"'"); - } - } } --- 159,162 ---- Index: Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.24 retrieving revision 2.25 diff -C2 -r2.24 -r2.25 *** Py.java 2000/11/07 18:44:20 2.24 --- Py.java 2000/11/17 12:32:43 2.25 *************** *** 10,13 **** --- 10,16 ---- static boolean frozen; static String frozenPackage=null; + private final static Object PRESENT=new Object(); + static java.util.Hashtable frozenModules; + static boolean initialized; *************** *** 260,270 **** } ! /** @deprecated **/ public static Object tojava(PyObject o, String s) { ! try { ! return tojava(o, Class.forName(s)); ! } catch (ClassNotFoundException exc) { ! throw Py.TypeError("can't convert to: "+s); ! } } --- 263,271 ---- } ! // ??pending: was @deprecated but is actually used by proxie code. Can get rid of it? public static Object tojava(PyObject o, String s) { ! Class c = findClass(s); ! if (c == null) throw Py.TypeError("can't convert to: "+s); ! return tojava(o, c); // prev:Class.forName } *************** *** 535,556 **** } public static Class findClass(String name) { try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader == null) ! return Class.forName(name); ! else return classLoader.loadClass(name); } catch (ClassNotFoundException e) { ! // e.printStackTrace(); return null; } catch (IllegalArgumentException e) { ! // e.printStackTrace(); return null; } catch (NoClassDefFoundError e) { ! // e.printStackTrace(); return null; } --- 536,582 ---- } + + public static Class relFindClass(Class home,String name) { + try { + ClassLoader loader = home.getClassLoader(); + if (loader != null) return loader.loadClass(name); + else return Class.forName(name); + } catch (ClassNotFoundException exc) { + return null; + } catch (Throwable t) { + throw Py.JavaError(t); + } + } + + private static boolean secEnv=false; + public static Class findClass(String name) { try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader != null) return classLoader.loadClass(name); ! ! if(!secEnv) { ! try { ! classLoader = imp.getSyspathJavaLoader(); ! } ! catch(SecurityException e) { ! secEnv=true; ! } return classLoader.loadClass(name); + } + + return Class.forName(name); + } catch (ClassNotFoundException e) { ! // e.printStackTrace(); return null; } catch (IllegalArgumentException e) { ! // e.printStackTrace(); return null; } catch (NoClassDefFoundError e) { ! // e.printStackTrace(); return null; } *************** *** 561,568 **** try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader == null) ! return Class.forName(name); ! else ! return classLoader.loadClass(name); } catch (ClassNotFoundException e) { --- 587,603 ---- try { ClassLoader classLoader = Py.getSystemState().getClassLoader(); ! if (classLoader != null) return classLoader.loadClass(name); ! ! if(!secEnv) { ! try { ! classLoader = imp.getSyspathJavaLoader(); ! } ! catch(SecurityException e) { ! secEnv=true; ! } ! return classLoader.loadClass(name); ! } ! ! return Class.forName(name); } catch (ClassNotFoundException e) { *************** *** 621,632 **** } - /* if (modules != null) { ! System.out.println("modules: "); ! for (int i = 0; i < modules.length; i++) ! System.out.print(modules[i] + ", "); ! System.out.println(); } - */ if (packages != null) { --- 656,677 ---- } if (modules != null) { ! if(frozenModules == null) ! frozenModules = new java.util.Hashtable(); ! ! // System.err.println("modules: "); // ?? dbg ! for (int i = 0; i < modules.length; i++) { ! String modname = modules[i]; ! // System.err.print(modname + " "); // ?? dbg ! frozenModules.put(modname,PRESENT); ! // py pkgs are potentially java pkgs too. ! if (modname.endsWith(".__init__")) { ! String jpkg = modname.substring(0,modname.length()-9); ! PySystemState.add_package(jpkg); ! // System.err.print(":j "); // ?? dbg ! } ! } ! // System.out.println(); // ?? dbg } if (packages != null) { *************** *** 665,669 **** PyObject mod; ! Class modClass = Py.findClass(module+"$_PyInner"); if (modClass != null) { //System.err.println("found as class: "+modClass); --- 710,714 ---- PyObject mod; ! Class modClass = Py.findClass(module+"$_PyInner"); // ??pending: findClass or should avoid sys.path loading? if (modClass != null) { //System.err.println("found as class: "+modClass); *************** *** 700,704 **** Class mainClass=null; try { ! mainClass = Class.forName(module); } catch (ClassNotFoundException exc) { System.err.println("Error running main. Can't find: "+module); --- 745,749 ---- Class mainClass=null; try { ! mainClass = Class.forName(module); // ??pending: should use Py.findClass? } catch (ClassNotFoundException exc) { System.err.println("Error running main. Can't find: "+module); *************** *** 724,728 **** Class mainClass=null; try { ! mainClass = Class.forName(module); } catch (ClassNotFoundException exc) { System.err.println("Error running main. Can't find: "+module); --- 769,773 ---- Class mainClass=null; try { ! mainClass = Class.forName(module); // ??pending: should use Py.findClass? } catch (ClassNotFoundException exc) { System.err.println("Error running main. Can't find: "+module); *************** *** 1370,1375 **** if (proxyClass != null) pyclass.proxyClass = proxyClass; ! ! pyclass.init(name, new PyTuple(bases), dict); return pyclass; } --- 1415,1420 ---- if (proxyClass != null) pyclass.proxyClass = proxyClass; ! ! pyclass.init(name, new PyTuple(bases), dict); return pyclass; } Index: PyJavaClass.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyJavaClass.java,v retrieving revision 2.23 retrieving revision 2.24 diff -C2 -r2.23 -r2.24 *** PyJavaClass.java 2000/10/28 12:45:06 2.23 --- PyJavaClass.java 2000/11/17 12:32:43 2.24 *************** *** 74,79 **** return; initializing = true; ! if (proxyClass == null) init(Py.findClassEx(__name__)); init__bases__(proxyClass); init__dict__(); --- 74,80 ---- return; initializing = true; ! if (proxyClass == null) { init(Py.findClassEx(__name__)); + } init__bases__(proxyClass); init__dict__(); *************** *** 738,751 **** private PyObject findInnerClass(String name) { ! try { ! Class innerClass = Class.forName(__name__+"$"+name); ! PyJavaClass jinner = new PyJavaInnerClass(innerClass, this); ! __dict__.__setitem__(name, jinner); ! return jinner; ! } catch (ClassNotFoundException exc) { ! return null; ! } catch (Throwable t) { ! throw Py.JavaError(t); ! } } --- 739,748 ---- private PyObject findInnerClass(String name) { ! Class innerClass = Py.relFindClass(getProxyClass(),__name__+"$"+name); ! if (innerClass == null) return null; ! ! PyJavaClass jinner = new PyJavaInnerClass(innerClass, this); ! __dict__.__setitem__(name, jinner); ! return jinner; } Index: PyJavaPackage.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyJavaPackage.java,v retrieving revision 2.2 retrieving revision 2.3 diff -C2 -r2.2 -r2.3 *** PyJavaPackage.java 2000/10/28 12:07:31 2.2 --- PyJavaPackage.java 2000/11/17 12:32:43 2.3 *************** *** 7,15 **** public class PyJavaPackage extends PyObject { public String __name__; ! public PyObject __dict__; ! public String _unparsedAll; public String __file__; //public PyList __all__; public static PyClass __class__; public PyJavaPackage(String name) { --- 7,25 ---- public class PyJavaPackage extends PyObject { public String __name__; ! ! ! public PyStringMap __dict__; ! //public String _unparsedAll; ! /** Its keys are the names of statically known classes. ! * E.g. from jars pre-scan. ! */ ! public PyStringMap clsSet; public String __file__; //public PyList __all__; + /** (Control) package manager whose hierarchy contains this java pkg. + */ + public PackageManager __mgr__; + public static PyClass __class__; public PyJavaPackage(String name) { *************** *** 17,32 **** } ! protected PyJavaPackage __parent__ = null; ! public PyJavaPackage(String name, String jarfile) { this(name, null, jarfile); } ! public PyJavaPackage(String name, PyJavaPackage parent, String jarfile) { super(__class__); ! __parent__ = parent; ! if (jarfile == null && parent != null) ! jarfile = __parent__.__file__; __file__ = jarfile; __name__ = name; __dict__ = new PyStringMap(); __dict__.__setitem__("__name__", new PyString(__name__)); --- 27,52 ---- } ! public PyJavaPackage(String name,String jarfile) { this(name, null, jarfile); } ! public PyJavaPackage(String name,PackageManager mgr) { ! this(name, mgr, null); ! } ! ! ! public PyJavaPackage(String name,PackageManager mgr,String jarfile) { super(__class__); ! __file__ = jarfile; __name__ = name; + + if( mgr == null ) + __mgr__ = PySystemState.packageManager; // default + else + __mgr__ = mgr; + + clsSet= new PyStringMap(); + __dict__ = new PyStringMap(); __dict__.__setitem__("__name__", new PyString(__name__)); *************** *** 45,226 **** lastName = name.substring(dot+1, name.length()); } - if (jarfile != null) { - if (!jarfile.equals(__file__)) { - __file__ = null; - } - } firstName = firstName.intern(); PyJavaPackage p = (PyJavaPackage)__dict__.__finditem__(firstName); if (p == null) { ! p = new PyJavaPackage(__name__+'.'+firstName, this, jarfile); } ! __dict__.__setitem__(firstName, p); ! if (lastName != null) ! return p.addPackage(lastName, jarfile); ! else ! return p; } - - private PyObject __path__ = null; - protected PyObject getPath() { - if (__path__ != null) - return __path__; - //System.err.println("finding path for: "+__name__); - - if (__name__.length() == 0) { - __path__ = Py.getSystemState().path; - return __path__; - } - - PyJavaPackage parent = __parent__; - PyList rootPath; - if (parent == null) { - rootPath = Py.getSystemState().path; - } else { - PyObject tmp = __parent__.getPath(); - if (tmp == Py.None) { - __path__ = Py.None; - return __path__; - } - rootPath = (PyList)tmp; - } - - PyList path = new PyList(); - - if (Py.frozen) { - __path__ = path; - return __path__; - } - - // Search through directories in rootPath looking for appropriate - // subdir - int n = rootPath.__len__(); - - String name = __name__; - int dot = name.lastIndexOf('.'); - if (dot != -1) - name = name.substring(dot+1, name.length()); - - for (int i=0; i<n; i++) { - String dirName = rootPath.get(i).toString(); - File dir = new File(dirName, name); - //System.err.println("pkg: "+__name__+", looking for: "+dir); - if (dir.isDirectory()) { - //System.err.println("found: "+dir.getPath()); - path.append(new PyString(dir.getPath())); - } - } - //Py.println(path); - if (path.__len__() == 0) { - __path__ = Py.None; - } else { - __path__ = path; - } - return __path__; - } ! private PyObject __jdir__; ! private boolean jdirinit = false; ! public PyObject getDirPackage() { ! if (jdirinit) ! return __jdir__; ! jdirinit = true; ! ! if (__name__.length() == 0) { ! __jdir__ = PySystemState.packageManager.topDirectoryPackage; ! return __jdir__; ! } ! ! PyJavaPackage parent = __parent__; ! PyObject ppkg; ! if (parent == null) { ! ppkg = PySystemState.packageManager.topDirectoryPackage; ! } else { ! ppkg = parent.getDirPackage(); ! } ! ! if (ppkg == null) { ! __jdir__ = null; ! return null; ! } ! String name = __name__; ! int dot = name.lastIndexOf('.'); ! if (dot != -1) ! name = name.substring(dot+1, name.length()).intern(); ! __jdir__ = ppkg.__findattr__(name); ! return __jdir__; ! } ! ! private void initialize() { ! if (__dict__ == null) ! __dict__ = new PyStringMap(); ! ! if (_unparsedAll != null) { ! //__all__ = new PyList(); ! StringTokenizer tok = new StringTokenizer(_unparsedAll, ","); ! while (tok.hasMoreTokens()) { ! String p = tok.nextToken(); ! String cname = p.trim().intern(); ! __dict__.__setitem__(cname, PyJavaClass.lookup(__name__+'.'+ ! cname, null)); ! //__all__.append(new PyString(p.trim())); ! } ! _unparsedAll = null; } } public PyObject __dir__() { ! initialize(); ! if (__dict__ instanceof PyStringMap) { ! return ((PyStringMap)__dict__).keys(); ! } else { ! return __dict__.invoke("keys"); ! } } public PyObject __findattr__(String name) { ! if (__dict__ == null) ! __dict__ = new PyStringMap(); ! PyObject ret = __dict__.__finditem__(name); ! if (ret != null) ! return ret; Class c; - if (__name__.length()>0) - c = Py.findClassEx(__name__+'.'+name); - else - c = Py.findClassEx(name); ! if (c != null) { ! ret = PyJavaClass.lookup(c); ! __dict__.__setitem__(name, ret); ! return ret; ! } if (name == "__name__") return new PyString(__name__); if (name == "__dict__") return __dict__; ! if (name == "__file__" && __file__ != null) ! return new PyString(__file__); ! ! // Name not found - check for Java dir package ! PyObject pyjd = getDirPackage(); ! if (pyjd != null) ! ret = pyjd.__findattr__(name); ! ! if (ret == null) { ! // Name not found - check for a Python package/module ! PyObject path = getPath(); ! if (path == Py.None) ! return null; ! ret = imp.loadFromPath(name, (__name__+'.'+name).intern(), ! (PyList)path); } ! ! if (ret != null) ! __dict__.__setitem__(name, ret); ! return ret; ! //return super.__findattr__(name); } --- 65,142 ---- lastName = name.substring(dot+1, name.length()); } firstName = firstName.intern(); PyJavaPackage p = (PyJavaPackage)__dict__.__finditem__(firstName); if (p == null) { ! p = new PyJavaPackage(__name__.length()==0?firstName:__name__+'.'+firstName, __mgr__, jarfile); ! __dict__.__setitem__(firstName, p); ! } else { // this code is ok here, because this is not needed for a top level package ! if (jarfile == null || !jarfile.equals(p.__file__)) p.__file__ = null; } ! if (lastName != null) return p.addPackage(lastName, jarfile); ! else return p; } ! public PyObject addClass(String name,Class c) { ! PyObject ret = PyJavaClass.lookup(c); ! __dict__.__setitem__(name.intern(), ret); ! return ret; ! } ! public PyObject addLazyClass(String name) { ! PyObject ret = PyJavaClass.lookup(__name__+'.'+name,null); ! __dict__.__setitem__(name.intern(), ret); ! return ret; ! } ! /** Add statically known classes. ! * @param classes their names as comma-separated string ! */ ! public void addPlaceholders(String classes) { ! StringTokenizer tok = new StringTokenizer(classes, ","); ! while (tok.hasMoreTokens()) { ! String p = tok.nextToken(); ! String name = p.trim().intern(); ! if ( clsSet.__finditem__(name) == null) clsSet.__setitem__(name, Py.One); } } public PyObject __dir__() { ! return __mgr__.doDir(this,false,false); ! } ! ! /** Used for 'from xyz import *', dynamically dir pkg filling up __dict__. ! * It uses {@link PackageManager#doDir} implementation furnished by the control ! * package manager with instatiate true. The package manager should lazily load ! * classes with {@link #addLazyClass} in the package. ! * ! * @return list of member names ! */ ! public PyObject fillDir() { ! return __mgr__.doDir(this,true,false); } public PyObject __findattr__(String name) { ! PyObject ret = __dict__.__finditem__(name); ! if (ret != null) return ret; ! Class c; ! c = __mgr__.findClass(__name__,name); ! ! if (c != null) return addClass(name,c); ! if (name == "__name__") return new PyString(__name__); if (name == "__dict__") return __dict__; ! if (name == "__file__") { ! if (__file__ != null) return new PyString(__file__); ! ! return Py.None; } ! ! if(__mgr__.packageExists(__name__,name)) return addPackage(name); ! ! return null; } Index: PyModule.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyModule.java,v retrieving revision 2.3 retrieving revision 2.4 diff -C2 -r2.3 -r2.4 *** PyModule.java 2000/09/28 11:37:37 2.3 --- PyModule.java 2000/11/17 12:32:43 2.4 *************** *** 18,64 **** PyObject ret; ret = __dict__.__finditem__(attr); if (ret == null) { ! PyObject path = __dict__.__finditem__("__path__"); ! if (path != null) { ! PyObject pyname = __dict__.__finditem__("__name__"); ! ! if (pyname != null) { ! String name = pyname.__str__().toString(); ! ! if (path == Py.None) { ! ret = imp.loadFromClassLoader( ! (name+'.'+attr).intern(), ! Py.getSystemState().getClassLoader()); ! } ! else if (path instanceof PyList) { ! ret = imp.loadFromPath(attr, (name+'.'+attr).intern(), ! (PyList)path); ! } ! else { ! throw Py.TypeError("__path__ must be list or None"); ! } ! ! if (ret == null) { ! ret = PySystemState.packageManager.lookupName( ! name+'.'+attr); ! /*Class c = Py.findClass(name+'.'+attr); ! if (c != null) ! ret = PyJavaClass.lookup(c);*/ ! } ! ! if (ret != null) { ! // Allow a package component to change its own meaning ! PyObject tmp = __dict__.__finditem__(attr); ! if (tmp != null) ! ret = tmp; ! __dict__.__setitem__(attr, ret); ! return ret; ! } ! } ! // Not found in package ! } ! return super.__findattr__(attr); } ! return ret; //ret._doget(this); } --- 18,69 ---- PyObject ret; ret = __dict__.__finditem__(attr); + if (ret != null) return ret; + + PyObject path = __dict__.__finditem__("__path__"); + PyObject pyname = __dict__.__finditem__("__name__"); + + if (path == null || pyname == null) return super.__findattr__(attr); + + String name = pyname.__str__().toString(); + + if (path == Py.None) { + /* disabled: + ret = imp.loadFromClassLoader( + (name+'.'+attr).intern(), + Py.getSystemState().getClassLoader()); + */ + } + else if (path instanceof PyList) { + ret = imp.loadFromPath(attr, (name+'.'+attr).intern(), + (PyList)path); + } + else { + throw Py.TypeError("__path__ must be list or None"); + } + if (ret == null) { ! ret = super.__findattr__(attr); ! if (ret != null) return ret; ! } ! ! if (ret == null) { ! ret = PySystemState.packageManager.lookupName( ! name+'.'+attr); ! /*Class c = Py.findClass(name+'.'+attr); ! if (c != null) ! ret = PyJavaClass.lookup(c);*/ ! } ! ! if (ret != null) { ! // Allow a package component to change its own meaning ! PyObject tmp = __dict__.__finditem__(attr); ! if (tmp != null) ! ret = tmp; ! __dict__.__setitem__(attr, ret); ! return ret; } ! ! return null; ! } Index: PySystemState.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PySystemState.java,v retrieving revision 2.29 retrieving revision 2.30 diff -C2 -r2.29 -r2.30 *** PySystemState.java 2000/10/11 12:52:24 2.29 --- PySystemState.java 2000/11/17 12:32:43 2.30 *************** *** 380,384 **** pkgdir = null; } ! packageManager = new PackageManager(pkgdir, props); } --- 380,384 ---- pkgdir = null; } ! packageManager = new SysPackageManager(pkgdir, props); } Index: imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.26 retrieving revision 2.27 diff -C2 -r2.26 -r2.27 *** imp.java 2000/10/28 12:07:31 2.26 --- imp.java 2000/11/17 12:32:43 2.27 *************** *** 107,111 **** if (filename == null) filename = "<unknown>"; ! org.python.parser.SimpleNode node; try { node = parser.parse(fp, "exec", filename); --- 107,111 ---- if (filename == null) filename = "<unknown>"; ! org.python.parser.SimpleNode node = null; //*Forte* try { node = parser.parse(fp, "exec", filename); *************** *** 155,159 **** private static BytecodeLoader syspathJavaLoader = null; ! public static BytecodeLoader getSyspathJavaLoader() { if (syspathJavaLoader == null) syspathJavaLoader = new BytecodeLoader(); --- 155,159 ---- private static BytecodeLoader syspathJavaLoader = null; ! public static synchronized BytecodeLoader getSyspathJavaLoader() { if (syspathJavaLoader == null) syspathJavaLoader = new BytecodeLoader(); *************** *** 217,232 **** PyList path) { ! //System.out.println("precomp: "+name+", "+modName); ! Class c = findPyClass(modName); ! if (c == null) { ! //System.err.println("trying: "+modName+".__init__$_PyInner"); ! c = findPyClass(modName+".__init__"); ! if (c == null) return null; ! //System.err.println("found: "+modName+".__init__$_PyInner"); ! PyModule m = addModule(modName); ! m.__dict__.__setitem__("__path__", new PyList()); ! } ! //System.err.println("creating: "+modName+", "+c); ! return createFromClass(modName, c); } --- 217,242 ---- PyList path) { ! if (Py.frozenModules != null) { ! //System.out.println("precomp: "+name+", "+modName); ! Class c; ! ! if (Py.frozenModules.get(modName+".__init__") != null) { ! //System.err.println("trying: "+modName+".__init__$_PyInner"); ! c = findPyClass(modName+".__init__"); ! if (c == null) return null; ! //System.err.println("found: "+modName+".__init__$_PyInner"); ! PyModule m = addModule(modName); ! m.__dict__.__setitem__("__path__", new PyList()); ! } ! else if (Py.frozenModules.get(modName) != null) { ! c = findPyClass(modName); ! if (c == null) return null; ! } ! else return null; ! ! //System.err.println("creating: "+modName+", "+c); ! return createFromClass(modName, c); ! } ! return null; } *************** *** 236,245 **** static PyObject loadFromPath(String name, String modName, PyList path) { ! if (Py.frozen) ! return loadPrecompiled(name, modName, path); String pyName = name+".py"; String className = name+"$py.class"; - String javaName = name+".class"; int n = path.__len__(); --- 246,255 ---- static PyObject loadFromPath(String name, String modName, PyList path) { ! //System.err.println("load-from-path:"+name+" "+modName+" "+path); // ?? dbg ! PyObject o = loadPrecompiled(name, modName, path); ! if (o != null) return o; String pyName = name+".py"; String className = name+"$py.class"; int n = path.__len__(); *************** *** 260,266 **** // this correctly. if (dirName.length() == 0) { ! String userdir = System.getProperty("user.dir"); ! if (userdir != null) ! dirName = userdir; } --- 270,274 ---- // this correctly. if (dirName.length() == 0) { ! dirName = null; } *************** *** 275,279 **** pkgPath.append(new PyString(dir.toString())); m.__dict__.__setitem__("__path__", pkgPath); ! PyObject o = loadFromPath("__init__", modName, pkgPath); if (o == null) continue; --- 283,287 ---- pkgPath.append(new PyString(dir.toString())); m.__dict__.__setitem__("__path__", pkgPath); ! o = loadFromPath("__init__", modName, pkgPath); if (o == null) continue; *************** *** 304,317 **** false); } - - File javaFile = new File(dirName, javaName); - if (javaFile.isFile()) { - try { - return createFromClass(modName, makeStream(javaFile)); - } catch (ClassFormatError exc) { - throw Py.ImportError("bad java class file in: "+ - javaFile.toString()); - } - } } --- 312,315 ---- *************** *** 344,361 **** private static PyObject load(String name, PyList path) { PyObject ret = loadBuiltin(name, path); - if (ret != null) return ret; - - ret = PySystemState.packageManager.jarFindName(name); if (ret != null) return ret; ! ret = loadFromPath(name, path); if (ret != null) return ret; - - if (Py.frozen) { - Class c = Py.findClassEx(name); - if (c != null) return createFromClass(name, c); - } ! ret = PySystemState.packa... [truncated message content] |