From: <cg...@us...> - 2008-12-22 02:18:15
|
Revision: 5785 http://jython.svn.sourceforge.net/jython/?rev=5785&view=rev Author: cgroves Date: 2008-12-22 02:18:12 +0000 (Mon, 22 Dec 2008) Log Message: ----------- Use PyJavaType(java.lang.Class) as the metatype for instances of PyJavaType wrapping a Java class. This exposes Class methods like getMethods and getFields through the instance. Modified Paths: -------------- trunk/jython/CoreExposed.includes trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyException.java trunk/jython/src/org/python/core/PyJavaType.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/core/PyReflectedFunction.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/modules/ArrayModule.java Modified: trunk/jython/CoreExposed.includes =================================================================== --- trunk/jython/CoreExposed.includes 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/CoreExposed.includes 2008-12-22 02:18:12 UTC (rev 5785) @@ -19,6 +19,7 @@ org/python/core/PyFunction.class org/python/core/PyGenerator.class org/python/core/PyInteger.class +org/python/core/PyJavaType.class org/python/core/PyList.class org/python/core/PyLong.class org/python/core/PyMethod.class Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/Py.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -923,8 +923,7 @@ stderr.println("Java Traceback:"); java.io.CharArrayWriter buf = new java.io.CharArrayWriter(); if (t instanceof PyException) { - ((PyException) t).super__printStackTrace( - new java.io.PrintWriter(buf)); + ((PyException)t).super__printStackTrace(new java.io.PrintWriter(buf)); } else { t.printStackTrace(new java.io.PrintWriter(buf)); } @@ -1551,7 +1550,6 @@ */ public static PyObject makeClass(String name, PyObject[] bases, PyObject dict) { PyFrame frame = getFrame(); - if (dict.__finditem__("__module__") == null) { PyObject module = frame.getglobal("__name__"); if (module != null) { Modified: trunk/jython/src/org/python/core/PyException.java =================================================================== --- trunk/jython/src/org/python/core/PyException.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/PyException.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -3,24 +3,20 @@ import java.io.*; /** - * A wrapper for all python exception. Note that the wellknown - * python exception are <b>not</b> subclasses of PyException. - * Instead the python exception class is stored in the - * <code>type</code> field and value or class instance is stored - * in the <code>value</code> field. + * A wrapper for all python exception. Note that the wellknown python exception are <b>not</b> + * subclasses of PyException. Instead the python exception class is stored in the <code>type</code> + * field and value or class instance is stored in the <code>value</code> field. */ - public class PyException extends RuntimeException { + /** - * The python exception class (for class exception) or - * identifier (for string exception). + * The python exception class (for class exception) or identifier (for string exception). */ public PyObject type; /** - * The exception instance (for class exception) or exception - * value (for string exception). + * The exception instance (for class exception) or exception value (for string exception). */ public PyObject value = Py.None; Modified: trunk/jython/src/org/python/core/PyJavaType.java =================================================================== --- trunk/jython/src/org/python/core/PyJavaType.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/PyJavaType.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -16,10 +16,12 @@ import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; +import org.python.expose.ExposedMethod; import org.python.expose.ExposedType; import org.python.util.Generic; -public class PyJavaType extends PyType implements ExposeAsSuperclass { +@ExposedType(name = "javatype") +public class PyJavaType extends PyType { private final static Class<?>[] OO = {PyObject.class, PyObject.class}; @@ -35,6 +37,14 @@ super(TYPE == null ? fromClass(PyType.class) : TYPE); } + @ExposedMethod(defaults = "null") + final PyList javatype_mro(PyObject o) { + if (o == null) { + return new PyList(mro); + } + return new PyList(((PyJavaType)o).mro); + } + @Override public Class<?> getProxyType() { return PyObject.class.isAssignableFrom(underlying_class) ? null : underlying_class; @@ -48,6 +58,10 @@ @Override protected void checkSetattr() {} + protected boolean useMetatypeFirst(PyObject attr) { + return !(attr instanceof PyReflectedField || attr instanceof PyReflectedFunction); + } + @Override protected void init() { name = underlying_class.getName(); @@ -62,10 +76,14 @@ // their interfaces computeLinearMro(baseClass); } else { + javaProxy = underlying_class; + objtype = PyType.fromClass(Class.class); // Wrapped Java types fill in their mro first using their base class and then all of // their interfaces. if (baseClass == null) { base = PyType.fromClass(PyObject.class); + } else if(underlying_class == Class.class) { + base = PyType.fromClass(PyType.class); } else { base = PyType.fromClass(baseClass); } @@ -300,8 +318,9 @@ PyBuiltinCallable equals = new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override public PyObject __call__(PyObject o) { - Object oAsJava = o.__tojava__(self.getJavaProxy().getClass()); - return self.getJavaProxy().equals(oAsJava) ? Py.True : Py.False; + Object proxy = self.getJavaProxy(); + Object oAsJava = o.__tojava__(proxy.getClass()); + return proxy.equals(oAsJava) ? Py.True : Py.False; } }; dict.__setitem__("__eq__", new PyMethodDescr(this, equals)); Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/PyObject.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -44,7 +44,7 @@ */ protected Object javaProxy; - private PyType objtype; + PyType objtype; @ExposedGet(name = "__class__") public PyType getType() { @@ -1007,7 +1007,7 @@ protected void mergeClassDict(PyDictionary accum, PyObject aClass) { // Merge in the type's dict (if any) aClass.mergeDictAttr(accum, "__dict__"); - + // Recursively merge in the base types' (if any) dicts PyObject bases = aClass.__findattr__("__bases__"); if (bases == null) { Modified: trunk/jython/src/org/python/core/PyReflectedFunction.java =================================================================== --- trunk/jython/src/org/python/core/PyReflectedFunction.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/PyReflectedFunction.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -46,10 +46,6 @@ return new PyMethod(this, container, wherefound); } - public boolean _doset(PyObject container) { - throw Py.TypeError("java function not settable: " + __name__); - } - public PyObject getDoc() { return __doc__; } Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/core/PyType.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -133,6 +133,12 @@ } metatype = winner; } + // Use PyType as the metaclass for Python subclasses of Java classes rather than PyJavaType. + // Using PyJavaType as metaclass exposes the java.lang.Object methods on the type, which + // doesn't make sense for python subclasses. + if (metatype == PyType.fromClass(Class.class)) { + metatype = TYPE; + } if (bases_list.length == 0) { bases_list = new PyObject[] {object_type}; } @@ -997,6 +1003,7 @@ newtype = new PyJavaType(); } + // If filling in the type above filled the type under creation, use that one PyType type = class_to_type.get(c); if (type != null) { @@ -1041,11 +1048,10 @@ PyType metatype = getType(); PyObject metaattr = metatype.lookup(name); - PyObject res = null; - if (metaattr != null) { + if (metaattr != null && useMetatypeFirst(metaattr)) { if (metaattr.isDataDescr()) { - res = metaattr.__get__(this, metatype); + PyObject res = metaattr.__get__(this, metatype); if (res != null) return res; } @@ -1054,7 +1060,7 @@ PyObject attr = lookup(name); if (attr != null) { - res = attr.__get__(null, this); + PyObject res = attr.__get__(null, this); if (res != null) { return res; } @@ -1067,6 +1073,14 @@ return null; } + /** + * Returns true if the given attribute retrieved from an object's metatype should be used before + * looking for the object on the actual object. + */ + protected boolean useMetatypeFirst(PyObject attr) { + return true; + } + @ExposedMethod final void type___setattr__(PyObject name, PyObject value) { type___setattr__(asName(name), value); Modified: trunk/jython/src/org/python/modules/ArrayModule.java =================================================================== --- trunk/jython/src/org/python/modules/ArrayModule.java 2008-12-21 23:25:39 UTC (rev 5784) +++ trunk/jython/src/org/python/modules/ArrayModule.java 2008-12-22 02:18:12 UTC (rev 5785) @@ -11,7 +11,7 @@ * The python array module, plus jython extensions from jarray. */ public class ArrayModule implements ClassDictInit { - + public static PyString __doc__ = new PyString( "This module defines a new object type which can efficiently represent\n" + "an array of basic values: characters, integers, floating point\n" + @@ -42,16 +42,16 @@ "\n" + "ArrayType -- type object for array objects\n" ); - - public static void classDictInit(PyObject dict){ + + public static void classDictInit(PyObject dict) { dict.__setitem__("array", PyType.fromClass(PyArray.class)); dict.__setitem__("ArrayType", PyType.fromClass(PyArray.class)); } - + /* - * These are jython extensions (from jarray module). + * These are jython extensions (from jarray module). * Note that the argument order is consistent with - * python array module, but is reversed from jarray module. + * python array module, but is reversed from jarray module. */ public static PyArray zeros(char typecode, int n) { return PyArray.zeros(n, typecode); @@ -59,5 +59,5 @@ public static PyArray zeros(Class type, int n) { return PyArray.zeros(n, type); - } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |