From: Samuele P. <ped...@us...> - 2001-07-17 20:34:38
|
Update of /cvsroot/jython/jython/org/python/core In directory usw-pr-cvs1:/tmp/cvs-serv23579 Modified Files: Py.java PyModule.java imp.java Log Message: - yet another import logic rework: fixes #438108 __getitem__ called when it shouldn't? - experimental PyMetaClass hook. Index: Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.46 retrieving revision 2.47 diff -C2 -r2.46 -r2.47 *** Py.java 2001/07/03 20:20:27 2.46 --- Py.java 2001/07/17 20:34:35 2.47 *************** *** 1407,1410 **** --- 1407,1412 ---- + private static Class[] pyClassCtrSignature = {String.class,PyTuple.class,PyObject.class,Class.class}; + public static PyObject makeClass(String name, PyObject[] bases, PyCode code, PyObject doc, *************** *** 1433,1437 **** --- 1435,1447 ---- new PyTuple(bases), dict); + } else if (bases[i] instanceof org.python.util.PyMetaClass) { + try { + return (PyObject)bases[i].getClass().getConstructor(pyClassCtrSignature).newInstance( + new Object[] { name, new PyTuple(bases), dict, proxyClass }); + } catch(Exception e) { + throw Py.TypeError("meta-class fails to supply proper ctr: "+bases[i].safeRepr()); + } } + } Index: PyModule.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PyModule.java,v retrieving revision 2.10 retrieving revision 2.11 diff -C2 -r2.10 -r2.11 *** PyModule.java 2001/07/03 21:01:15 2.10 --- PyModule.java 2001/07/17 20:34:35 2.11 *************** *** 24,33 **** String fullName = (name+'.'+attr).intern(); ! PyObject modules = Py.getSystemState().modules; ! ! PyObject ret = modules.__finditem__(fullName); ! if (ret == Py.None) ret = null; ! else if (ret != null) return ret; ! if (path == Py.None) { /* disabled: --- 24,29 ---- String fullName = (name+'.'+attr).intern(); ! PyObject ret = null; ! if (path == Py.None) { /* disabled: *************** *** 49,54 **** if (ret != null) { ! // Allow a package component to change its own meaning ! PyObject tmp = modules.__finditem__(fullName); if (tmp != null) ret = tmp; __dict__.__setitem__(attr, ret); --- 45,50 ---- if (ret != null) { ! // Allow a package component to change its own meaning ! PyObject tmp = Py.getSystemState().modules.__finditem__(fullName); if (tmp != null) ret = tmp; __dict__.__setitem__(attr, ret); Index: imp.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/imp.java,v retrieving revision 2.47 retrieving revision 2.48 diff -C2 -r2.47 -r2.48 *** imp.java 2001/07/14 22:25:35 2.47 --- imp.java 2001/07/17 20:34:35 2.48 *************** *** 396,414 **** } ! Py.writeComment("import", "'" + name + "' not found (ImportError)"); ! throw Py.ImportError("no module named "+name); } public static PyObject load(String name) { ! PyObject modules = Py.getSystemState().modules; ! PyObject ret = modules.__finditem__(name); ! if (ret != null) return ret; ! ! ret = load(name, Py.getSystemState().path); ! if (modules.__finditem__(name) == null) ! modules.__setitem__(name, ret); ! else ! ret = modules.__finditem__(name); ! return ret; } --- 396,406 ---- } ! Py.writeComment("import", "'" + name + "' not found (=> ImportError)"); ! return null; ! } public static PyObject load(String name) { ! return import_first(name,new StringBuffer("")); } *************** *** 428,547 **** } ! // Hierarchy-recursivly search for dotted name in mod. ! private static PyObject dottedFind(PyObject mod, String name) { ! int dot = 0; ! int last_dot= 0; ! ! do { ! String tmpName; ! dot = name.indexOf('.', last_dot); ! if (dot == -1) { ! tmpName = name.substring(last_dot).intern(); } else { ! tmpName = name.substring(last_dot, dot).intern(); } ! mod = mod.impAttr(tmpName); ! if (mod == null) ! throw Py.ImportError("No module named " + tmpName); ! last_dot = dot + 1; ! } while (dot != -1); ! return mod; } ! public static PyObject importName(String name, boolean top) { ! if (name.length() == 0) ! throw Py.ValueError("Empty module name"); ! int dot = name.indexOf('.'); ! if (dot != -1) { ! PyObject modules = Py.getSystemState().modules; ! PyObject mod = modules.__finditem__(name); ! if (mod == Py.None) mod = null; ! else if (mod != null && !top) return mod; ! ! int last_dot = dot; ! String firstName = name.substring(0,dot).intern(); ! PyObject pkg = load(firstName); ! ! if (mod == null) { ! mod = pkg; ! if (dot != -1) mod = dottedFind(mod, name.substring(dot+1)); ! } ! if (modules.__finditem__(name) == null) ! modules.__setitem__(name, mod); ! else ! mod = modules.__finditem__(name); ! if (top) ! return pkg; ! else ! return mod; ! } ! else return load(name); } ! ! // This version should deal properly with package relative imports. ! // Assumption (runtime enforced): ! // x.y.z key in sys.modules => any subseq (e.g x, x.y) is a present ! // key too. ! // ??pending: check if result is really a module/jpkg/jclass? ! public synchronized static PyObject importName(String name, boolean top, ! PyObject modDict) ! { ! //System.err.println("importName: "+name); ! String pkgName = getParent(modDict); ! PyObject mod; ! ! if (pkgName != null) { ! PyObject modules = Py.getSystemState().modules; ! int dot = name.indexOf('.'); ! String firstName; ! if (dot == -1) firstName = name; ! else firstName = name.substring(0,dot); ! ! String topNewName = (pkgName+'.'+firstName).intern(); ! ! PyObject topMod = modules.__finditem__(topNewName); ! ! if (topMod != Py.None) { ! if (dot == -1 && topMod != null) { ! //System.err.println("refound-1-top: "+topMod); // ?? dbg ! return topMod; ! } ! String newName = (pkgName+'.'+name).intern(); ! mod = modules.__finditem__(newName); ! if (mod != null && mod != Py.None) { ! //System.err.println("refound: "+name); // ?? dbg ! if (!top) return mod; ! else return topMod; ! } ! ! PyObject pkg = modules.__finditem__(pkgName); ! if (pkg != null) { ! topMod = pkg.impAttr(firstName.intern()); ! if (topMod != null ) { ! if (dot == -1 ) { ! //System.err.println("found-1-top: "+ ! // topMod); // ?? dbg ! return topMod; ! } ! ! //System.err.println(".-find: "+topMod+","+ ! // name.substring(dot+1)); // ?? dbg ! mod = dottedFind(topMod,name.substring(dot+1)); ! ! if(top) return topMod; ! else return mod; ! ! } ! } ! //System.err.println("mark: "+topNewName); // ?? dbg ! modules.__setitem__(topNewName,Py.None); } } ! return importName(name, top); ! } /** * Called from jpython generated code when a statement like "import spam" --- 420,511 ---- } ! // can return null, None ! private static PyObject import_next(PyObject mod, StringBuffer parentNameBuffer, String name) { ! if (parentNameBuffer.length()>0) parentNameBuffer.append('.'); ! parentNameBuffer.append(name); ! String fullName = parentNameBuffer.toString().intern(); ! PyObject modules = Py.getSystemState().modules; ! PyObject ret = modules.__finditem__(fullName); ! if (ret != null) return ret; ! if (mod == null) { ! ret = load(name.intern(), Py.getSystemState().path); // ?? intern superfluous? } else { ! ret = mod.impAttr(name.intern()); } ! if (ret == null || ret == Py.None) return ret; ! if (modules.__finditem__(fullName) == null) modules.__setitem__(fullName, ret); ! else ret = modules.__finditem__(fullName); ! return ret; } ! // never returns null or None ! private static PyObject import_first(String name, StringBuffer parentNameBuffer) { ! PyObject ret = import_next(null,parentNameBuffer,name); ! if (ret == null || ret == Py.None) throw Py.ImportError("no module named "+name); ! return ret; } ! ! // Hierarchy-recursively search for dotted name in mod; never returns null or None ! // ??pending: check if result is really a module/jpkg/jclass? ! private static PyObject import_logic(PyObject mod, StringBuffer parentNameBuffer, String dottedName) { ! int dot = 0; ! int last_dot= 0; ! ! do { ! String name; ! dot = dottedName.indexOf('.', last_dot); ! if (dot == -1) { ! name = dottedName.substring(last_dot); ! } else { ! name = dottedName.substring(last_dot, dot); ! } ! mod = import_next(mod,parentNameBuffer,name); ! if (mod == null || mod == Py.None) ! throw Py.ImportError("No module named " + name); ! last_dot = dot + 1; ! } while (dot != -1); ! return mod; ! } ! public static PyObject import_name(String name,boolean top,PyObject modDict) { ! if (name.length() == 0) ! throw Py.ValueError("Empty module name"); ! PyObject modules = Py.getSystemState().modules; ! PyObject pkgMod = null; ! String pkgName = null; ! if (modDict != null) { ! pkgName = getParent(modDict); ! pkgMod = modules.__finditem__(pkgName); ! if (pkgMod != null && !(pkgMod instanceof PyModule)) pkgMod = null; ! } ! int dot = name.indexOf('.'); ! String firstName; ! if (dot == -1) firstName = name; ! else firstName = name.substring(0,dot); ! StringBuffer parentNameBuffer = new StringBuffer(pkgMod != null?pkgName:""); ! PyObject topMod = import_next(pkgMod,parentNameBuffer,firstName); // None or null or module-like ! if (topMod == Py.None || topMod == null) { ! if (topMod == null) { ! modules.__setitem__(parentNameBuffer.toString().intern(),Py.None); } + parentNameBuffer = new StringBuffer(""); + topMod = import_first(firstName,parentNameBuffer); // could throw ImportError } + PyObject mod = topMod; + if (dot != -1) mod = import_logic(topMod,parentNameBuffer,name.substring(dot+1)); // could throw ImportError + if (top) return topMod; + else return mod; + } ! public static PyObject importName(String name, boolean top) { ! return import_name(name,top,null); ! } + public synchronized static PyObject importName(String name, boolean top, + PyObject modDict) { + return import_name(name,top,modDict); + } + /** * Called from jpython generated code when a statement like "import spam" |