From: <cg...@us...> - 2007-12-17 07:55:22
|
Revision: 3830 http://jython.svn.sourceforge.net/jython/?rev=3830&view=rev Author: cgroves Date: 2007-12-16 23:55:21 -0800 (Sun, 16 Dec 2007) Log Message: ----------- Expose PyBuiltinFunction using annotations, and add support for the equivalent to exposed_as with a marker interface, ExposeAsSuperclass. Rip out exposed_as since only PyBuiltinFunction was using it and rip out exposed_methods as well since that wasn't used at all. Modified Paths: -------------- branches/exposed_annotation/CoreExposed.includes branches/exposed_annotation/src/org/python/core/PyBuiltinFunction.java branches/exposed_annotation/src/org/python/core/PyBuiltinFunctionSet.java branches/exposed_annotation/src/org/python/core/PyBuiltinMethod.java branches/exposed_annotation/src/org/python/core/PyType.java Added Paths: ----------- branches/exposed_annotation/src/org/python/expose/ExposeAsSuperclass.java Modified: branches/exposed_annotation/CoreExposed.includes =================================================================== --- branches/exposed_annotation/CoreExposed.includes 2007-12-17 07:53:35 UTC (rev 3829) +++ branches/exposed_annotation/CoreExposed.includes 2007-12-17 07:55:21 UTC (rev 3830) @@ -1,4 +1,5 @@ org/python/core/PyBaseString.class +org/python/core/PyBuiltinFunction.class org/python/core/PyInteger.class org/python/core/PyNone.class org/python/core/PyObject.class Modified: branches/exposed_annotation/src/org/python/core/PyBuiltinFunction.java =================================================================== --- branches/exposed_annotation/src/org/python/core/PyBuiltinFunction.java 2007-12-17 07:53:35 UTC (rev 3829) +++ branches/exposed_annotation/src/org/python/core/PyBuiltinFunction.java 2007-12-17 07:55:21 UTC (rev 3830) @@ -1,29 +1,11 @@ package org.python.core; -public abstract class PyBuiltinFunction extends PyObject implements - PyType.Newstyle { +import org.python.expose.ExposedGet; +import org.python.expose.ExposedType; - public static final String exposed_name = "builtin_function_or_method"; +@ExposedType(name="builtin_function_or_method") +public abstract class PyBuiltinFunction extends PyObject { - public static void typeSetup(PyObject dict, PyType.Newstyle marker) { - dict.__setitem__("__name__", new PyGetSetDescr("__name__", - PyBuiltinFunction.class, - "fastGetName", - null)); - dict.__setitem__("__self__", new PyGetSetDescr("__self__", - PyBuiltinFunction.class, - "getSelf", - null)); - dict.__setitem__("__doc__", new PyGetSetDescr("__doc__", - PyBuiltinFunction.class, - "fastGetDoc", - null)); - dict.__setitem__("__call__", new PyGetSetDescr("__call__", - PyBuiltinFunction.class, - "makeCall", - null)); - } - public interface Info { String getName(); @@ -119,6 +101,7 @@ */ abstract public PyBuiltinFunction bind(PyObject self); + @ExposedGet(name="__self__") public PyObject getSelf() { return Py.None; } @@ -134,14 +117,17 @@ } } + @ExposedGet(name="__name__") public PyObject fastGetName() { return Py.newString(this.info.getName()); } + @ExposedGet(name="__doc__") public PyObject fastGetDoc() { return Py.None; } + @ExposedGet(name="__call__") public PyObject makeCall() { return this; } Modified: branches/exposed_annotation/src/org/python/core/PyBuiltinFunctionSet.java =================================================================== --- branches/exposed_annotation/src/org/python/core/PyBuiltinFunctionSet.java 2007-12-17 07:53:35 UTC (rev 3829) +++ branches/exposed_annotation/src/org/python/core/PyBuiltinFunctionSet.java 2007-12-17 07:55:21 UTC (rev 3830) @@ -1,6 +1,8 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; +import org.python.expose.ExposeAsSuperclass; + /** * A helper class for faster implementations of commonly called methods. * <p> @@ -8,10 +10,8 @@ * method with a switch on the index number. * */ -public class PyBuiltinFunctionSet extends PyBuiltinFunction { +public class PyBuiltinFunctionSet extends PyBuiltinFunction implements ExposeAsSuperclass { - public static final Class exposed_as = PyBuiltinFunction.class; - // used as an index into a big switch statement in the various derived // class's __call__() methods. protected int index; Modified: branches/exposed_annotation/src/org/python/core/PyBuiltinMethod.java =================================================================== --- branches/exposed_annotation/src/org/python/core/PyBuiltinMethod.java 2007-12-17 07:53:35 UTC (rev 3829) +++ branches/exposed_annotation/src/org/python/core/PyBuiltinMethod.java 2007-12-17 07:55:21 UTC (rev 3830) @@ -1,9 +1,9 @@ package org.python.core; +import org.python.expose.ExposeAsSuperclass; -public abstract class PyBuiltinMethod extends PyBuiltinFunction { - public static final Class exposed_as = PyBuiltinFunction.class; +public abstract class PyBuiltinMethod extends PyBuiltinFunction implements ExposeAsSuperclass { protected PyBuiltinMethod(PyObject self, Info info) { super(info); Modified: branches/exposed_annotation/src/org/python/core/PyType.java =================================================================== --- branches/exposed_annotation/src/org/python/core/PyType.java 2007-12-17 07:53:35 UTC (rev 3829) +++ branches/exposed_annotation/src/org/python/core/PyType.java 2007-12-17 07:55:21 UTC (rev 3830) @@ -14,6 +14,7 @@ import java.util.Iterator; import java.util.List; +import org.python.expose.ExposeAsSuperclass; import org.python.expose.ExposedDelete; import org.python.expose.ExposedGet; import org.python.expose.ExposedMethod; @@ -786,7 +787,6 @@ Class base, boolean newstyle, Method setup, - String[] exposed_methods, TypeBuilder tb) { if(base == null) { @@ -827,7 +827,7 @@ } else { dict = new PyStringMap(); if(newstyle) { - fillInNewstyle(newtype, setup, exposed_methods, dict); + fillInNewstyle(newtype, setup, dict); } else { fillInClassic(c, base, dict); } @@ -987,13 +987,7 @@ private static void fillInNewstyle(PyType newtype, Method setup, - String[] exposed_methods, PyObject dict) { - for(int i = 0; i < exposed_methods.length; i++) { - String methname = exposed_methods[i]; - dict.__setitem__(normalize_name(methname), - new PyReflectedFunction(methname)); - } if(setup != null) { try { setup.invoke(null, new Object[] {dict, null}); @@ -1016,21 +1010,26 @@ classToBuilder = new HashMap<Class, TypeBuilder>(); } classToBuilder.put(forClass, builder); - if(builder.getTypeClass().equals(PyObject.class) - || builder.getTypeClass().equals(PyType.class)) { + + if(class_to_type.containsKey(forClass)) { // PyObject and PyType are loaded as part of creating their // builders, so they need to be bootstrapped PyType objType = fromClass(builder.getTypeClass()); + objType.name = builder.getName(); objType.dict = builder.getDict(objType); } } private static PyType addFromClass(Class c) { + if(ExposeAsSuperclass.class.isAssignableFrom(c)) { + PyType exposedAs = fromClass(c.getSuperclass()); + class_to_type.put(c, exposedAs); + return exposedAs; + } Method setup = null; boolean newstyle = Newstyle.class.isAssignableFrom(c); Class base = null; String name = null; - String[] exposed_methods = null; TypeBuilder tb = classToBuilder == null ? null : classToBuilder.get(c); if(tb != null) { name = tb.getName(); @@ -1049,28 +1048,13 @@ if(newstyle) { // newstyle base = (Class)exposed_decl_get_object(c, "base"); name = (String)exposed_decl_get_object(c, "name"); - if(base == null) { - Class cur = c; - while(cur != PyObject.class) { - Class exposed_as = (Class)exposed_decl_get_object(cur, "as"); - if(exposed_as != null) { - PyType exposed_as_type = fromClass(exposed_as); - class_to_type.put(c, exposed_as_type); - return exposed_as_type; - } - cur = cur.getSuperclass(); - } - } - exposed_methods = (String[])exposed_decl_get_object(c, "methods"); - if(exposed_methods == null) - exposed_methods = new String[0]; } } PyType newtype = class_to_type.get(c); if (newtype == null) { newtype = c == PyType.class ? new PyType(true) : new PyType(); class_to_type.put(c, newtype); - fillFromClass(newtype, name, c, base, newstyle, setup, exposed_methods, tb); + fillFromClass(newtype, name, c, base, newstyle, setup, tb); } return newtype; } @@ -1090,11 +1074,6 @@ * Class exposed_base * String exposed_name * - * Class exposed_as => instances are exposed as implementing - * just this superclass - * - * (String[] exposed_methods) - * */ public static synchronized PyType fromClass(Class c) { Added: branches/exposed_annotation/src/org/python/expose/ExposeAsSuperclass.java =================================================================== --- branches/exposed_annotation/src/org/python/expose/ExposeAsSuperclass.java (rev 0) +++ branches/exposed_annotation/src/org/python/expose/ExposeAsSuperclass.java 2007-12-17 07:55:21 UTC (rev 3830) @@ -0,0 +1,7 @@ +package org.python.expose; + +/** + * Marker interface that indicates this class and all of its subclasses should + * be exposed to Python with the same Python type as its superclass. + */ +public interface ExposeAsSuperclass {} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |