From: <cg...@us...> - 2008-11-10 03:31:58
|
Revision: 5564 http://jython.svn.sourceforge.net/jython/?rev=5564&view=rev Author: cgroves Date: 2008-11-10 03:31:53 +0000 (Mon, 10 Nov 2008) Log Message: ----------- Checkpoint of replacing PyJavaClass with PyJavaType. Works well enough that ProxyMaker creates PyObjects instead of PyJavaInstances and the interpreter starts up, but that's about it Modified Paths: -------------- branches/newstyle-java-types/Lib/test/test_java_visibility.py branches/newstyle-java-types/src/org/python/compiler/ProxyMaker.java branches/newstyle-java-types/src/org/python/core/Options.java branches/newstyle-java-types/src/org/python/core/Py.java branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java branches/newstyle-java-types/src/org/python/core/PyInstance.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyObject.java branches/newstyle-java-types/src/org/python/core/PyProxy.java branches/newstyle-java-types/src/org/python/core/PyReflectedConstructor.java branches/newstyle-java-types/src/org/python/core/PyReflectedFunction.java branches/newstyle-java-types/src/org/python/core/PyType.java branches/newstyle-java-types/src/org/python/core/ThreadState.java branches/newstyle-java-types/src/org/python/core/adapter/ClassicPyObjectAdapter.java branches/newstyle-java-types/src/org/python/core/adapter/PyObjectAdapter.java Modified: branches/newstyle-java-types/Lib/test/test_java_visibility.py =================================================================== --- branches/newstyle-java-types/Lib/test/test_java_visibility.py 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/Lib/test/test_java_visibility.py 2008-11-10 03:31:53 UTC (rev 5564) @@ -66,8 +66,6 @@ self.failUnless('visibleInstance' in c.__dict__, 'visibleInstance expected in %s __dict__' % c) - - def test_main(): test_support.run_unittest(VisibilityTest) Modified: branches/newstyle-java-types/src/org/python/compiler/ProxyMaker.java =================================================================== --- branches/newstyle-java-types/src/org/python/compiler/ProxyMaker.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/compiler/ProxyMaker.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -12,6 +12,9 @@ import java.util.Set; import org.python.core.Py; +import org.python.core.PyJavaType; +import org.python.core.PyObject; +import org.python.core.PyProxy; import org.python.objectweb.asm.Label; import org.python.objectweb.asm.Opcodes; import org.python.util.Generic; @@ -53,13 +56,40 @@ else return ((Integer)i).intValue(); } + /** + * Retrieves <code>name</code> from the PyObject in <code>proxy</code> if it's defined in + * Python. This is a specialized helper function for internal PyProxy use. + */ + public static PyObject findPython(PyProxy proxy, String name) { + PyObject o = proxy._getPyInstance(); + if (o == null) { + proxy.__initProxy__(new Object[0]); + o = proxy._getPyInstance(); + } + PyObject ret = null; + if (o.getDict() != null) { + ret = o.getDict().__finditem__(name); + } + if (ret == null) { + PyObject[] definedOn = new PyObject[1]; + PyObject typeDefined = o.getType().lookup_where(name, definedOn); + if (!(definedOn[0] instanceof PyJavaType)) { + ret = typeDefined; + } + } + if (ret == null) { + return null; + } + Py.setSystemState(proxy._getPySystemState()); + return ret.__get__(o, null); + } + Class<?> superclass; Class<?>[] interfaces; Set<String> names; Set<String> supernames = Generic.set(); public ClassFile classfile; public String myClass; - public boolean isAdapter=false; public ProxyMaker(String classname, Class<?> superclass) { this.myClass = "org.python.proxies."+classname; @@ -432,7 +462,8 @@ if (!isAbstract) { int tmp = code.getLocal("org/python/core/PyObject"); - code.invokestatic("org/python/core/Py", "jfindattr", "(" + $pyProxy + $str + ")" + $pyObj); + code.invokestatic("org/python/compiler/ProxyMaker", "findPython", "(" + $pyProxy + $str + + ")" + $pyObj); code.astore(tmp); code.aload(tmp); @@ -449,20 +480,15 @@ addSuperMethod("super__"+name, name, superClass, parameters, ret, sig, access); } else { - if (!isAdapter) { - code.invokestatic("org/python/core/Py", "jgetattr", "(" + $pyProxy + $str + ")" + $pyObj); - callMethod(code, name, parameters, ret, method.getExceptionTypes()); - } else { - code.invokestatic("org/python/core/Py", "jfindattr", "(" + $pyProxy + $str + ")" + $pyObj); - code.dup(); - Label returnNull = new Label(); - code.ifnull(returnNull); - - callMethod(code, name, parameters, ret, method.getExceptionTypes()); - code.label(returnNull); - code.pop(); - doNullReturn(code, ret); - } + code.invokestatic("org/python/compiler/ProxyMaker", "findPython", "(" + $pyProxy + $str + + ")" + $pyObj); + code.dup(); + Label returnNull = new Label(); + code.ifnull(returnNull); + callMethod(code, name, parameters, ret, method.getExceptionTypes()); + code.label(returnNull); + code.pop(); + doNullReturn(code, ret); } } @@ -600,23 +626,23 @@ public void addProxy() throws Exception { // implement PyProxy interface - classfile.addField("__proxy", "Lorg/python/core/PyInstance;", + classfile.addField("__proxy", "Lorg/python/core/PyObject;", Modifier.PROTECTED); // setProxy methods Code code = classfile.addMethod("_setPyInstance", - "(Lorg/python/core/PyInstance;)V", + "(Lorg/python/core/PyObject;)V", Modifier.PUBLIC); code.aload(0); code.aload(1); - code.putfield(classfile.name, "__proxy", "Lorg/python/core/PyInstance;"); + code.putfield(classfile.name, "__proxy", "Lorg/python/core/PyObject;"); code.return_(); // getProxy method code = classfile.addMethod("_getPyInstance", - "()Lorg/python/core/PyInstance;", + "()Lorg/python/core/PyObject;", Modifier.PUBLIC); code.aload(0); - code.getfield(classfile.name, "__proxy", "Lorg/python/core/PyInstance;"); + code.getfield(classfile.name, "__proxy", "Lorg/python/core/PyObject;"); code.areturn(); // implement PyProxy interface Modified: branches/newstyle-java-types/src/org/python/core/Options.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/Options.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/Options.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -4,7 +4,7 @@ /** * A class with static fields for each of the settable options. The options from * registry and command line is copied into the fields here and the rest of - * Jyhton checks these fields. + * Jython checks these fields. */ public class Options { // Jython options. Some of these can be set from the command line @@ -24,7 +24,7 @@ public static boolean includeJavaStackInExceptions = false; /** - * When true, python exception raised in overriden methods will be shown on + * When true, python exception raised in overridden methods will be shown on * stderr. This option is remarkably useful when python is used for * implementing CORBA server. Some CORBA servers will turn python exception * (say a NameError) into an anonymous user exception without any Modified: branches/newstyle-java-types/src/org/python/core/Py.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/Py.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/Py.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -1,7 +1,6 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; @@ -16,7 +15,6 @@ import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; -import java.util.Arrays; import java.util.Calendar; import java.util.HashSet; import java.util.Set; @@ -25,7 +23,6 @@ import org.python.compiler.Module; import org.python.core.adapter.ClassicPyObjectAdapter; import org.python.core.adapter.ExtensiblePyObjectAdapter; -import org.python.core.util.StringUtil; import org.python.modules.errno; public final class Py { @@ -84,7 +81,7 @@ public static long TPFLAGS_HEAPTYPE; /** Builtin types that are used to setup PyObject. */ - static final Set<Class> BOOTSTRAP_TYPES = new HashSet<Class>(4); + static final Set<Class<?>> BOOTSTRAP_TYPES = new HashSet<Class<?>>(4); static { BOOTSTRAP_TYPES.add(PyObject.class); BOOTSTRAP_TYPES.add(PyType.class); @@ -476,35 +473,6 @@ } /* Helper functions for PyProxy's */ - /** @deprecated */ - public static PyObject jfindattr(PyProxy proxy, String name) { - PyObject ret = getInstance(proxy, name); - if (ret == null) - return null; - Py.setSystemState(proxy._getPySystemState()); - return ret; - } - - - /** @deprecated */ - public static PyObject jgetattr(PyProxy proxy, String name) { - PyObject ret = getInstance(proxy, name); - if (ret == null) - throw Py.AttributeError("abstract method '" + name + "' not implemented"); - Py.setSystemState(proxy._getPySystemState()); - return ret; - } - - private static PyObject getInstance(PyProxy proxy, String name) { - PyInstance o = proxy._getPyInstance(); - if (o == null) { - proxy.__initProxy__(new Object[0]); - o = proxy._getPyInstance(); - } - PyObject ret = o.__jfindattr__(name); - return ret; - } - /* Convenience methods to create new constants without using "new" */ private static PyInteger[] integerCache = null; @@ -836,36 +804,20 @@ if (proxy._getPyInstance() != null) return; ThreadState ts = getThreadState(); - PyInstance instance = ts.getInitializingProxy(); + PyObject instance = ts.getInitializingProxy(); if (instance != null) { - if (instance.javaProxy != null) + if (instance.javaProxy != null) { throw Py.TypeError("Proxy instance reused"); + } instance.javaProxy = proxy; proxy._setPyInstance(instance); proxy._setPySystemState(ts.systemState); return; } - PyObject mod; - // ??pending: findClass or should avoid sys.path loading? - Class modClass = Py.findClass(module+"$_PyInner"); - if (modClass != null) { - PyCode code=null; - try { - code = ((PyRunnable)modClass.newInstance()).getMain(); - } catch (Throwable t) { - throw Py.JavaError(t); - } - mod = imp.createFromCode(module, code); - } else { - mod = imp.importName(module.intern(), false); - } - PyClass pyc = (PyClass)mod.__getattr__(pyclass.intern()); + PyObject mod = imp.importName(module.intern(), false); + PyType pyc = (PyType)mod.__getattr__(pyclass.intern()); - instance = new PyInstance(pyc); - instance.javaProxy = proxy; - proxy._setPyInstance(instance); - proxy._setPySystemState(ts.systemState); PyObject[] pargs; if (args == null || args.length == 0) { @@ -875,7 +827,10 @@ for(int i=0; i<args.length; i++) pargs[i] = Py.java2py(args[i]); } - instance.__init__(pargs, Py.NoKeywords); + instance = pyc.__call__(pargs); + instance.javaProxy = proxy; + proxy._setPyInstance(instance); + proxy._setPySystemState(ts.systemState); } /** Modified: branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -4,12 +4,10 @@ public class PyBeanProperty extends PyReflectedField { public Method getMethod, setMethod; - public Class myType; + public Class<?> myType; String __name__; - public PyBeanProperty(String name, Class myType, - Method getMethod, Method setMethod) - { + public PyBeanProperty(String name, Class<?> myType, Method getMethod, Method setMethod) { __name__ = name; this.getMethod = getMethod; this.setMethod = setMethod; @@ -56,7 +54,7 @@ if (value instanceof PyTuple) { try { PyTuple vtup = (PyTuple)value; - value = PyJavaClass.lookup(myType).__call__(vtup.getArray()); // xxx PyObject subclasses + value = Py.java2py(myType).__call__(vtup.getArray()); // xxx PyObject subclasses } catch (Throwable t) { // If something goes wrong ignore it? } Modified: branches/newstyle-java-types/src/org/python/core/PyInstance.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -109,7 +109,7 @@ // The javaProxy can be initialized in Py.jfindattr() throw Py.TypeError("Proxy instance already initialized"); } - PyInstance proxyInstance = proxy._getPyInstance(); + PyObject proxyInstance = proxy._getPyInstance(); if (proxyInstance != null && proxyInstance != this) { // The proxy was initialized to another instance!! throw Py.TypeError("Proxy initialization conflict"); Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -14,11 +14,22 @@ private final static Class<?>[] OO = {PyObject.class, PyObject.class}; + public static PyObject wrapJavaObject(Object o) { + PyObject obj = new PyObject(PyType.fromClass(o.getClass())); + obj.javaProxy = o; + return obj; + } + public PyJavaType() { super(TYPE == null ? fromClass(PyType.class) : TYPE); } @Override + public Class<?> getProxyType() { + return underlying_class; + } + + @Override protected void fillDict() { dict = new PyStringMap(); Map<String, Object> propnames = new HashMap<String, Object>(); @@ -83,10 +94,15 @@ } } } - dict.__setitem__(normalize_name(fldname), new PyReflectedField(field)); + if (dict.__finditem__(normalize_name(fldname)) == null) { + dict.__setitem__(normalize_name(fldname), new PyReflectedField(field)); + } } } for (String propname : propnames.keySet()) { + if(propname.equals("")) { + continue; + } String npropname = normalize_name(StringUtil.decapitalize(propname)); PyObject prev = dict.__finditem__(npropname); if (prev != null && prev instanceof PyReflectedFunction) { Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -200,8 +200,15 @@ * @param c the Class to convert this <code>PyObject</code> to. **/ public Object __tojava__(Class<?> c) { - if (c.isInstance(this)) + if ((c == Object.class || c == Serializable.class) && javaProxy != null) { + return javaProxy; + } + if (c.isInstance(this)) { return this; + } + if (c.isInstance(javaProxy)) { + return javaProxy; + } return Py.NoConversion; } @@ -3354,19 +3361,15 @@ // Generated by make_binops.py (End) - /* A convenience function for PyProxy's */ - // Possibly add _jcall(), _jcall(Object, ...) as future optimization /** - * A convenience function for PyProxy's. - * @param args call arguments. - * @exception Throwable + * A convenience function for PyProxys. */ public PyObject _jcallexc(Object[] args) throws Throwable { PyObject[] pargs = new PyObject[args.length]; try { - int n = args.length; - for (int i = 0; i < n; i++) + for (int i = 0; i < args.length; i++) { pargs[i] = Py.java2py(args[i]); + } return __call__(pargs); } catch (PyException e) { if (e.value instanceof PyJavaInstance) { @@ -3927,12 +3930,11 @@ } static { - for (Class unbootstrapped : Py.BOOTSTRAP_TYPES) { + for (Class<?> unbootstrapped : Py.BOOTSTRAP_TYPES) { Py.writeWarning("init", "Bootstrap type wasn't encountered in bootstrapping[class=" + unbootstrapped + "]"); } } - } /* Modified: branches/newstyle-java-types/src/org/python/core/PyProxy.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyProxy.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyProxy.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -3,7 +3,7 @@ /** * Common methods for all generated proxy classes. - * + * * Proxy classes are created whenever a python class inherits from a java class. Instances of such a * python class consists of two objects: * <ul> @@ -12,24 +12,23 @@ * <li>An instance of PyInstance. The PyInstance.javaProxy contains a reference to the proxy class * instance. * </ul> - * - * All proxy classes, both dynamically generated and statically generated by jythonc implements this - * interface. + * + * All proxy classes implement this interface. */ // This interface should be applicable to ANY class // Choose names that are extremely unlikely to have conflicts public interface PyProxy { /** - * Associate an PyInstance with this proxy instance. This is done during construction and + * Associate a PyObject with this proxy instance. This is done during construction and * initialization of the proxy instance. */ - abstract public void _setPyInstance(PyInstance proxy); + void _setPyInstance(PyObject proxy); /** - * Return the associated PyInstance instance. + * Return the associated PyObject. */ - abstract public PyInstance _getPyInstance(); + PyObject _getPyInstance(); /** * Associate an system state with this proxy instance. This is done during construction and @@ -40,16 +39,16 @@ /** * Return the associated system state. */ - abstract public PySystemState _getPySystemState(); + PySystemState _getPySystemState(); /** - * Initialize the proxy instance. If the proxy have not been initialized already, this call will - * call the python constructor with <code>args</code>. + * Initialize the proxy instance. If the proxy is not initialized, this will call the python + * constructor with <code>args</code>. * <p> * In some situations is it necessary to call the __initProxy__ method from the java superclass - * ctor before the ctor makes call to methods that is overriden in python. + * ctor before the ctor makes call to methods that is overridden in python. * <p> * In most situation the __initProxy__ is called automatically by the jython runtime. */ - public abstract void __initProxy__(Object[] args); + void __initProxy__(Object[] args); } Modified: branches/newstyle-java-types/src/org/python/core/PyReflectedConstructor.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyReflectedConstructor.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyReflectedConstructor.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -90,7 +90,7 @@ if (self instanceof PyInstance) { javaClass = ((PyInstance)self).instclass.proxyClass; } else { - javaClass = self.getType().underlying_class; + javaClass = self.getType().getProxyType(); } Class<?> declaringClass = argslist[0].declaringClass; @@ -98,12 +98,6 @@ throw Py.TypeError("self invalid - must implement: " + declaringClass.getName()); } - if (!PyProxy.class.isAssignableFrom(declaringClass) && !(self instanceof PyJavaInstance)) { - PyJavaClass jc = PyJavaClass.lookup(javaClass); - jc.initConstructors(); - return jc.__init__.__call__(self, args, keywords); - } - if (self.javaProxy != null) { Class<?> sup = javaClass; if (PyProxy.class.isAssignableFrom(sup)) { @@ -158,9 +152,7 @@ Object jself = null; ThreadState ts = Py.getThreadState(); try { - if (obj instanceof PyInstance) { - ts.pushInitializingProxy((PyInstance)obj); - } + ts.pushInitializingProxy(obj); try { jself = ctor.newInstance(args); } catch (InvocationTargetException e) { @@ -177,9 +169,7 @@ throw Py.JavaError(t); } } finally { - if (obj instanceof PyInstance) { - ts.popInitializingProxy(); - } + ts.popInitializingProxy(); } obj.javaProxy = jself; } Modified: branches/newstyle-java-types/src/org/python/core/PyReflectedFunction.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyReflectedFunction.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyReflectedFunction.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -138,26 +138,6 @@ Object cself = callData.self; Method m = (Method)method; - // Check to see if we should be using a super__ method instead - // This is probably a bit inefficient... - if (self == null && cself != null && cself instanceof PyProxy && - !__name__.startsWith("super__")) { - PyInstance iself = ((PyProxy)cself)._getPyInstance(); - if (argslist[0].declaringClass != iself.instclass.proxyClass) { - String mname = ("super__"+__name__); - // xxx experimental - Method[] super__methods = (Method[])iself.instclass.super__methods.get(mname); - if (super__methods != null) { - Class[] msig = m.getParameterTypes(); - for (Method super__method : super__methods) { - if (java.util.Arrays.equals(msig,super__method.getParameterTypes())) { - m = super__method; - break; - } - } - } - } - } try { Object o = m.invoke(cself, callData.getArgsArray()); Modified: branches/newstyle-java-types/src/org/python/core/PyType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyType.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/PyType.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -133,6 +133,26 @@ if (bases_list.length == 0) { bases_list = new PyObject[] {object_type}; } + List<Class<?>> interfaces = Generic.list(); + Class<?> baseClass = null; + for (PyObject base : bases_list) { + if (!(base instanceof PyType)) { + continue; + } + Class<?> proxy = ((PyType)base).getProxyType(); + if (proxy == null) { + continue; + } + if (proxy.isInterface()) { + interfaces.add(proxy); + } else { + if (baseClass != null) { + throw Py.TypeError("no multiple inheritance for Java classes: " + + proxy.getName() + " and " + baseClass.getName()); + } + baseClass = proxy; + } + } // XXX can be subclassed ? if (dict.__finditem__("__module__") == null) { @@ -147,6 +167,33 @@ } // XXX also __doc__ __module__ + + if (baseClass != null || interfaces.size() != 0) { + String proxyName = name; + PyObject module = dict.__finditem__("__module__"); + if (module != null) { + proxyName = module.toString() + "$" + proxyName; + } + Class<?> proxyClass = MakeProxies.makeProxy(baseClass, + interfaces, + name, + proxyName, + dict); + PyType proxyType = PyType.fromClass(proxyClass); + List<PyObject> cleanedBases = Generic.list(); + boolean addedProxyType = false; + for (PyObject base : bases_list) { + if (base instanceof PyJavaType) { + if (!addedProxyType) { + cleanedBases.add(proxyType); + addedProxyType = true; + } + } else { + cleanedBases.add(base); + } + } + bases_list = cleanedBases.toArray(new PyObject[cleanedBases.size()]); + } PyType newtype; if (new_.for_type == metatype) { newtype = new PyType(); // XXX set metatype @@ -294,23 +341,19 @@ } private static void fillInMRO(PyType type, Class<?> base) { - PyType[] mro; - if (base == Object.class || base == null) { - if (type.underlying_class == Object.class) { - mro = new PyType[] {type, PyObject.TYPE}; - } else { - mro = new PyType[] {type}; + if (type.underlying_class == PyObject.class) { + type.mro = new PyType[] {type}; + return; } - } else { - PyType baseType = fromClass(base); - mro = new PyType[baseType.mro.length + 1]; - System.arraycopy(baseType.mro, 0, mro, 1, baseType.mro.length); - mro[0] = type; - type.base = baseType; - type.bases = new PyObject[] {baseType}; + base = PyObject.class; } - type.mro = mro; + PyType baseType = fromClass(base); + type.mro = new PyType[baseType.mro.length + 1]; + System.arraycopy(baseType.mro, 0, type.mro, 1, baseType.mro.length); + type.mro[0] = type; + type.base = baseType; + type.bases = new PyObject[] {baseType}; } public PyObject getStatic() { @@ -417,7 +460,6 @@ mro = savedMro; throw t; } - } private void mro_internal() { @@ -504,6 +546,21 @@ return result; } + /** + * Returns the Java Class that this type inherits from, or null if this type is Python-only. + */ + public Class<?> getProxyType() { + for (PyObject base : bases) { + if (base instanceof PyType) { + Class<?> javaType = ((PyType)base).getProxyType(); + if (javaType != null) { + return javaType; + } + } + } + return null; + } + private synchronized void attachSubclass(PyType subtype) { cleanup_subclasses(); subclasses.add(new WeakReference<PyType>(subtype, subclasses_refq)); Modified: branches/newstyle-java-types/src/org/python/core/ThreadState.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/ThreadState.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/ThreadState.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -25,20 +25,20 @@ public TraceFunction profilefunc; - private LinkedList<PyInstance> initializingProxies; + private LinkedList<PyObject> initializingProxies; private PyDictionary compareStateDict; - public PyInstance getInitializingProxy() { + public PyObject getInitializingProxy() { if (initializingProxies == null) { return null; } return initializingProxies.peek(); } - public void pushInitializingProxy(PyInstance proxy) { + public void pushInitializingProxy(PyObject proxy) { if (initializingProxies == null) { - initializingProxies = new LinkedList<PyInstance>(); + initializingProxies = new LinkedList<PyObject>(); } initializingProxies.addFirst(proxy); } Modified: branches/newstyle-java-types/src/org/python/core/adapter/ClassicPyObjectAdapter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/adapter/ClassicPyObjectAdapter.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/adapter/ClassicPyObjectAdapter.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -4,8 +4,7 @@ import org.python.core.PyArray; import org.python.core.PyFloat; import org.python.core.PyInteger; -import org.python.core.PyJavaClass; -import org.python.core.PyJavaInstance; +import org.python.core.PyJavaType; import org.python.core.PyLong; import org.python.core.PyObject; import org.python.core.PyProxy; @@ -14,14 +13,14 @@ /** * Implements the algorithm originally used in {@link Py#java2py} to adapt objects. - * + * * Pre-class adapters are added to handle instances of PyObject, PyProxy and * null values. Class adapters are added to handle builtin Java classes: String, * Integer, Float, Double, Byte, Long, Short, Character, Class and Boolean. An * adapter is added to the post-class adapters to handle wrapping arrays * properly. Finally, if all of the added adapters can handle an object, it's * wrapped in a PyJavaInstance. - * + * */ public class ClassicPyObjectAdapter extends ExtensiblePyObjectAdapter { @@ -67,19 +66,15 @@ add(new ClassAdapter(Character.class) { public PyObject adapt(Object o) { - return Py.makeCharacter((Character) o); - } + return Py.makeCharacter((Character)o); + } }); add(new ClassAdapter(Class.class) { public PyObject adapt(Object o) { - Class cls = (Class) o; - if (PyObject.class.isAssignableFrom(cls)) { - return PyType.fromClass(cls); - } - return PyJavaClass.lookup(cls); - } + return PyType.fromClass((Class<?>)o); + } }); add(new NumberToPyFloat(Double.class)); @@ -126,7 +121,7 @@ if (result != null) { return result; } - return new PyJavaInstance(o); + return PyJavaType.wrapJavaObject(o); } private static class NumberToPyInteger extends ClassAdapter { Modified: branches/newstyle-java-types/src/org/python/core/adapter/PyObjectAdapter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/adapter/PyObjectAdapter.java 2008-11-10 03:29:23 UTC (rev 5563) +++ branches/newstyle-java-types/src/org/python/core/adapter/PyObjectAdapter.java 2008-11-10 03:31:53 UTC (rev 5564) @@ -7,14 +7,13 @@ */ public interface PyObjectAdapter { - /** - * @return true if o can be adapted by this adapter. - */ - public abstract boolean canAdapt(Object o); + /** + * Returns true if o can be adapted by this adapter. + */ + public abstract boolean canAdapt(Object o); - /** - * @return the PyObject version of o or null if canAdapt(o) returns false. - */ - public abstract PyObject adapt(Object o); - + /** + * Returns the PyObject version of o or null if canAdapt(o) returns false. + */ + public abstract PyObject adapt(Object o); } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |