From: <cg...@us...> - 2008-11-10 05:15:14
|
Revision: 5567 http://jython.svn.sourceforge.net/jython/?rev=5567&view=rev Author: cgroves Date: 2008-11-10 05:15:10 +0000 (Mon, 10 Nov 2008) Log Message: ----------- Implement the collection proxies as methods on PyJavaTypes Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/CollectionIter.java branches/newstyle-java-types/src/org/python/core/CollectionProxy.java branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyObject.java Modified: branches/newstyle-java-types/src/org/python/core/CollectionIter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -2,13 +2,10 @@ package org.python.core; -import java.lang.reflect.Method; -import java.util.Collection; import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; -import java.util.Vector; class CollectionIter{ PyObject findCollection(Object object) { @@ -30,7 +27,7 @@ return null; } - + } class EnumerationIter extends PyIterator { Modified: branches/newstyle-java-types/src/org/python/core/CollectionProxy.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionProxy.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/CollectionProxy.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -2,7 +2,6 @@ package org.python.core; -import java.util.Collection; import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; @@ -16,15 +15,15 @@ public static CollectionProxy findCollection(Object object) { if (object == null) return NoProxy; - + if (object instanceof List) { return new ListProxy(((List) object)); } if (object instanceof Map) { return new MapProxy(((Map) object)); } - if (object instanceof Collection) { - return new IteratorProxy(((Collection) object).iterator()); + if (object instanceof Iterable) { + return new IteratorProxy(((Iterable) object).iterator()); } if (object instanceof Iterator) { return new IteratorProxy(((Iterator) object)); @@ -44,7 +43,7 @@ return NoProxy; } - /** The basic functions to implement a mapping* */ + /** The basic functions to implement a mapping */ public int __len__() { throw Py.AttributeError("__len__"); } @@ -92,8 +91,7 @@ public PyObject __finditem__(int key) { if (key != this.counter) { - throw Py - .ValueError("enumeration indices must be consecutive ints starting at 0"); + throw Py.ValueError("enumeration indices must be consecutive ints starting at 0"); } this.counter++; if (this.proxy.hasMoreElements()) { @@ -141,8 +139,7 @@ public void __setitem__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - this.proxy.setElementAt(Py.tojava(value, Object.class), - ((PyInteger) key).getValue()); + this.proxy.setElementAt(Py.tojava(value, Object.class), ((PyInteger)key).getValue()); } else { throw Py.TypeError("only integer keys accepted"); } @@ -278,8 +275,7 @@ public PyObject __finditem__(int key) { if (key != this.counter) { - throw Py - .ValueError("iterator indices must be consecutive ints starting at 0"); + throw Py.ValueError("iterator indices must be consecutive ints starting at 0"); } this.counter++; if (this.proxy.hasNext()) { Modified: branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java 2008-11-10 03:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyBuiltinCallable.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -21,7 +21,7 @@ } /** - * @return a new instance of this type of PyBuiltinFunction bound to self + * Returns a new instance of this type of PyBuiltinFunction bound to self */ abstract public PyBuiltinCallable bind(PyObject self); 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:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -4,18 +4,21 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; +import org.python.util.Generic; public class PyJavaType extends PyType implements ExposeAsSuperclass { private final static Class<?>[] OO = {PyObject.class, PyObject.class}; public static PyObject wrapJavaObject(Object o) { - PyObject obj = new PyObject(PyType.fromClass(o.getClass())); + PyObject obj = new PyObjectDerived(PyType.fromClass(o.getClass())); obj.javaProxy = o; return obj; } @@ -154,6 +157,13 @@ for (Class<?> inner : underlying_class.getClasses()) { dict.__setitem__(inner.getSimpleName(), PyType.fromClass(inner)); } + for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : _collectionProxies.entrySet()) { + if (entry.getKey().isAssignableFrom(underlying_class)) { + for (PyBuiltinMethod meth : entry.getValue()) { + dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); + } + } + } if (ClassDictInit.class.isAssignableFrom(underlying_class) && underlying_class != ClassDictInit.class) { try { @@ -211,4 +221,194 @@ private static PyException error(Exception e) { return Py.JavaError(e); } + + protected static class LenProxy extends PyBuiltinMethodNarrow { + public LenProxy() { + super("__len__", 0, 0); + } + + protected LenProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new LenProxy(getType(), self, info); + } + + @Override + public PyObject __call__() { + return Py.newInteger(((Collection<?>)self.javaProxy).size()); + } + } + + protected static class MapGetProxy extends PyBuiltinMethodNarrow { + public MapGetProxy() { + super("__getitem__", 1, 1); + } + + protected MapGetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapGetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key) { + return Py.java2py(((Map<?, ?>)self.javaProxy).get(Py.tojava(key, Object.class))); + } + } + + protected static class MapPutProxy extends PyBuiltinMethodNarrow { + public MapPutProxy() { + super("__setitem__", 2, 2); + } + + protected MapPutProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapPutProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(((Map<Object, Object>)self.javaProxy).put(Py.tojava(key, Object.class), + Py.tojava(value, + Object.class))); + } + } + + protected static class MapRemoveProxy extends PyBuiltinMethodNarrow { + + public MapRemoveProxy() { + super("__delitem__", 1, 1); + } + + protected MapRemoveProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new MapRemoveProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(((Map<?, ?>)self.javaProxy).remove(Py.tojava(key, Object.class))); + } + } + + protected static class ListGetProxy extends PyBuiltinMethodNarrow { + public ListGetProxy() { + super("__getitem__", 1, 1); + } + + protected ListGetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListGetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key) { + if (key instanceof PyInteger) { + return Py.java2py(((List<?>)self.javaProxy).get(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + } + + protected static class ListSetProxy extends PyBuiltinMethodNarrow { + public ListSetProxy() { + super("__setitem__", 2, 2); + } + + protected ListSetProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListSetProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + ((List<Object>)self.javaProxy).set(((PyInteger)key).getValue(), + Py.tojava(value, Object.class)); + } else { + throw Py.TypeError("only integer keys accepted"); + } + return Py.None; + } + } + + protected static class ListRemoveProxy extends PyBuiltinMethodNarrow { + public ListRemoveProxy() { + super("__delitem__", 1, 1); + } + + protected ListRemoveProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new ListRemoveProxy(getType(), self, info); + } + + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + } + + public static class IterableProxy extends PyBuiltinMethodNarrow { + + public IterableProxy() { + super("__iter__", 0, 0); + } + + protected IterableProxy(PyType type, PyObject self, Info info) { + super(type, self, info); + } + + @Override + public PyBuiltinCallable bind(PyObject self) { + return new IterableProxy(getType(), self, info); + } + + @Override + public PyObject __call__() { + return new IteratorIter(((Iterable)self.javaProxy).iterator()); + } + } + + static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies = Generic.map(); + static { + _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {new IterableProxy()}); + _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {new LenProxy()}); + _collectionProxies.put(Map.class, new PyBuiltinMethod[] {new MapGetProxy(), + new MapPutProxy(), + new MapRemoveProxy()}); + _collectionProxies.put(List.class, new PyBuiltinMethod[] {new ListGetProxy(), + new ListSetProxy(), + new ListRemoveProxy()}); + } } 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:56:44 UTC (rev 5566) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-10 05:15:10 UTC (rev 5567) @@ -547,6 +547,7 @@ * @param value the value to set this key to **/ public void __setitem__(PyObject key, PyObject value) { + Thread.dumpStack(); throw Py.TypeError(String.format("'%.200s' object does not support item assignment", getType().fastGetName())); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-23 23:12:51
|
Revision: 5622 http://jython.svn.sourceforge.net/jython/?rev=5622&view=rev Author: cgroves Date: 2008-11-23 23:12:46 +0000 (Sun, 23 Nov 2008) Log Message: ----------- PyInstance ignored __tojava__ methods on its classes, so Java classes in the old world with a __tojava__ would be ignored as well. This differed from most other dunder methods, so the new PyJavaType exposes __tojava__ through PyObjectDerived. IOBase was using __tojava__ to mean something different than what PyObject was doing with it, so change the name to keep regular jython object conversion from picking up on it. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyFile.java branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java branches/newstyle-java-types/src/org/python/core/io/FileIO.java branches/newstyle-java-types/src/org/python/core/io/IOBase.java branches/newstyle-java-types/src/org/python/core/io/StreamIO.java branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java Modified: branches/newstyle-java-types/src/org/python/core/PyFile.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyFile.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/PyFile.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -529,7 +529,12 @@ } public Object __tojava__(Class<?> cls) { - Object o = file.__tojava__(cls); + Object o = null; + if (InputStream.class.isAssignableFrom(cls)) { + o = file.asInputStream(); + } else if (OutputStream.class.isAssignableFrom(cls)) { + o = file.asOutputStream(); + } if (o == null) { o = super.__tojava__(cls); } Modified: branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/BufferedIOMixin.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,9 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; + import org.python.core.Py; import org.python.core.PyException; @@ -102,8 +105,13 @@ return rawIO.closed(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - return rawIO.__tojava__(cls); + @Override + public InputStream asInputStream() { + return rawIO.asInputStream(); } + + @Override + public OutputStream asOutputStream() { + return rawIO.asOutputStream(); + } } Modified: branches/newstyle-java-types/src/org/python/core/io/FileIO.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/FileIO.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/FileIO.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -14,7 +14,6 @@ import org.python.core.imp; import org.python.core.Py; -import org.python.core.PyObject; import org.python.core.util.RelativeFile; import org.python.modules.errno; @@ -27,7 +26,7 @@ /** The underlying file channel */ private FileChannel fileChannel; - + /** The underlying file (if known) */ private RandomAccessFile file; @@ -336,16 +335,16 @@ super.close(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - if (OutputStream.class.isAssignableFrom(cls) && writing) { - return Channels.newOutputStream(fileChannel); - } else if (InputStream.class.isAssignableFrom(cls) && readable()) { - return Channels.newInputStream(fileChannel); - } - return super.__tojava__(cls); + @Override + public OutputStream asOutputStream() { + return writing ? Channels.newOutputStream(fileChannel) : super.asOutputStream(); } + @Override + public InputStream asInputStream() { + return readable() ? Channels.newInputStream(fileChannel) : super.asInputStream(); + } + /** {@inheritDoc} */ public boolean readable() { return reading || plus; Modified: branches/newstyle-java-types/src/org/python/core/io/IOBase.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/IOBase.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/IOBase.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,9 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; + import org.python.core.Py; import org.python.core.PyException; import org.python.modules.errno; @@ -191,17 +194,20 @@ } /** - * Coerce this Python object (the parent PyFile) into a java - * object. - * - * @param cls the desired Class to coerce the object to - * @return the desired object or null + * Coerce this into an OutputStream if possible, or return null. */ - public Object __tojava__(Class cls) { + public OutputStream asOutputStream() { return null; } /** + * Coerce this into an InputStream if possible, or return null. + */ + public InputStream asInputStream() { + return null; + } + + /** * Raise a TypeError indicating the specified operation is not * supported. * Modified: branches/newstyle-java-types/src/org/python/core/io/StreamIO.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/StreamIO.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/StreamIO.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -18,7 +18,6 @@ import org.python.core.imp; import org.python.core.Py; -import org.python.core.PyObject; /** * Raw I/O implementation for simple streams. @@ -158,7 +157,7 @@ } super.close(); } - + /** Unwrap one or more nested FilterInputStreams. */ private static FileDescriptor getInputFileDescriptor(InputStream stream) throws IOException { if (stream == null) @@ -200,12 +199,12 @@ } return null; } - + /** {@inheritDoc} */ - + public boolean isatty() { checkClosed(); - + FileDescriptor fd; try { if ( ((fd = getInputFileDescriptor(inputStream)) == null) && @@ -214,7 +213,7 @@ } catch (IOException e) { return false; } - + return imp.load("os").__getattr__("isatty").__call__(Py.java2py(fd)).__nonzero__(); } @@ -228,20 +227,26 @@ return writeChannel != null; } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - if (OutputStream.class.isAssignableFrom(cls) && writable()) { + @Override + public OutputStream asOutputStream() { + if (writable()) { if (outputStream == null) { return Channels.newOutputStream(writeChannel); } return outputStream; - } else if (InputStream.class.isAssignableFrom(cls) && readable()) { + } + return super.asOutputStream(); + } + + @Override + public InputStream asInputStream() { + if (readable()) { if (inputStream == null) { return Channels.newInputStream(readChannel); } return inputStream; } - return super.__tojava__(cls); + return super.asInputStream(); } /** {@inheritDoc} */ Modified: branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java 2008-11-23 22:22:05 UTC (rev 5621) +++ branches/newstyle-java-types/src/org/python/core/io/TextIOBase.java 2008-11-23 23:12:46 UTC (rev 5622) @@ -1,6 +1,8 @@ /* Copyright (c) 2007 Jython Developers */ package org.python.core.io; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.ByteBuffer; import org.python.core.Py; @@ -188,11 +190,16 @@ return bufferedIO.closed(); } - /** {@inheritDoc} */ - public Object __tojava__(Class cls) { - return bufferedIO.__tojava__(cls); + @Override + public InputStream asInputStream() { + return bufferedIO.asInputStream(); } + @Override + public OutputStream asOutputStream() { + return bufferedIO.asOutputStream(); + } + /** * Return the known Newline types, as a PyObject, encountered * while reading this file. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-28 08:56:41
|
Revision: 5651 http://jython.svn.sourceforge.net/jython/?rev=5651&view=rev Author: cgroves Date: 2008-11-28 08:56:38 +0000 (Fri, 28 Nov 2008) Log Message: ----------- Add an EQ_PROXY to PyJavaType to pass equals through to wrapped Java instances like PyJavaInstance did. Make PyBuiltinMethod Cloneable and use clone to implement a default bind implementation. This eliminates 75% of the crud that came with creating a builtin method in Java. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/CollectionIter.java branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java Modified: branches/newstyle-java-types/src/org/python/core/CollectionIter.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/CollectionIter.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -1,59 +1,30 @@ -// Copyright (c) Finn Bock - package org.python.core; -import java.util.Dictionary; import java.util.Enumeration; import java.util.Iterator; -import java.util.Map; -class CollectionIter{ - PyObject findCollection(Object object) { - if (object instanceof Map) { - return new IteratorIter(((Map) object).keySet().iterator()); - } - if (object instanceof Iterable) { - return new IteratorIter(((Iterable)object).iterator()); - } - if (object instanceof Iterator) { - return new IteratorIter(((Iterator) object)); - } - if (object instanceof Enumeration) { - return new EnumerationIter(((Enumeration) object)); - } - if (object instanceof Dictionary) { - return new EnumerationIter(((Dictionary) object).keys()); - } - - return null; - } - -} - class EnumerationIter extends PyIterator { - private Enumeration proxy; - public EnumerationIter(Enumeration proxy) { + private Enumeration<Object> proxy; + + public EnumerationIter(Enumeration<Object> proxy) { this.proxy = proxy; } public PyObject __iternext__() { - if (!this.proxy.hasMoreElements()) - return null; - return Py.java2py(this.proxy.nextElement()); + return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; } } class IteratorIter extends PyIterator { - private Iterator proxy; - public IteratorIter(Iterator proxy) { + private Iterator<Object> proxy; + + public IteratorIter(Iterator<Object> proxy) { this.proxy = proxy; } public PyObject __iternext__() { - if (!this.proxy.hasNext()) - return null; - return Py.java2py(this.proxy.next()); + return proxy.hasNext() ? Py.java2py(proxy.next()) : null; } } Modified: branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/PyBuiltinMethod.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -2,7 +2,8 @@ import org.python.expose.ExposeAsSuperclass; -public abstract class PyBuiltinMethod extends PyBuiltinCallable implements ExposeAsSuperclass { +public abstract class PyBuiltinMethod extends PyBuiltinCallable implements ExposeAsSuperclass, + Cloneable { protected PyObject self; @@ -15,15 +16,31 @@ super(info); this.self = self; } - + protected PyBuiltinMethod(String name) { this(null, new DefaultInfo(name)); } - + + @Override + public PyBuiltinCallable bind(PyObject bindTo) { + if(self == null) { + PyBuiltinMethod bindable; + try { + bindable = (PyBuiltinMethod)clone(); + } catch(CloneNotSupportedException e) { + throw new RuntimeException("Didn't expect PyBuiltinMethodto throw " + + "CloneNotSupported since it implements Cloneable", e); + } + bindable.self = bindTo; + return bindable; + } + return this; + } + public PyObject getSelf(){ return self; } - + public PyMethodDescr makeDescriptor(PyType t) { return new PyMethodDescr(t, this); } Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-28 08:12:30 UTC (rev 5650) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-28 08:56:38 UTC (rev 5651) @@ -45,15 +45,12 @@ String methname = meth.getName(); String nmethname = normalize_name(methname); PyReflectedFunction reflfunc = (PyReflectedFunction)dict.__finditem__(nmethname); - boolean added = false; if (reflfunc == null) { dict.__setitem__(nmethname, new PyReflectedFunction(meth)); - added = true; } else { reflfunc.addMethod(meth); - added = true; } - if (added && !Modifier.isStatic(meth.getModifiers())) { + if (!Modifier.isStatic(meth.getModifiers())) { // check for xxxX.* int n = meth.getParameterTypes().length; if (methname.startsWith("get") && n == 0) { @@ -164,6 +161,7 @@ } } } + dict.__setitem__("__eq__", new PyMethodDescr(this, EQUALS_PROXY)); if (ClassDictInit.class.isAssignableFrom(underlying_class) && underlying_class != ClassDictInit.class) { try { @@ -222,104 +220,43 @@ return Py.JavaError(e); } - protected static class LenProxy extends PyBuiltinMethodNarrow { - public LenProxy() { - super("__len__", 0, 0); - } - - protected LenProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow LEN_PROXY = + new PyBuiltinMethodNarrow("__len__", 0, 0) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new LenProxy(getType(), self, info); - } - - @Override public PyObject __call__() { return Py.newInteger(((Collection<?>)self.javaProxy).size()); } - } + }; - protected static class MapGetProxy extends PyBuiltinMethodNarrow { - public MapGetProxy() { - super("__getitem__", 1, 1); - } - - protected MapGetProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow MAP_GET_PROXY = + new PyBuiltinMethodNarrow("__getitem__", 1, 1) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new MapGetProxy(getType(), self, info); - } - - @Override public PyObject __call__(PyObject key) { return Py.java2py(((Map<?, ?>)self.javaProxy).get(Py.tojava(key, Object.class))); } - } + }; - protected static class MapPutProxy extends PyBuiltinMethodNarrow { - public MapPutProxy() { - super("__setitem__", 2, 2); - } - - protected MapPutProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow MAP_PUT_PROXY = + new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new MapPutProxy(getType(), self, info); - } - - @Override public PyObject __call__(PyObject key, PyObject value) { return Py.java2py(((Map<Object, Object>)self.javaProxy).put(Py.tojava(key, Object.class), Py.tojava(value, Object.class))); } - } + }; - protected static class MapRemoveProxy extends PyBuiltinMethodNarrow { - - public MapRemoveProxy() { - super("__delitem__", 1, 1); - } - - protected MapRemoveProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow MAP_REMOVE_PROXY = + new PyBuiltinMethodNarrow("__delitem__", 1, 1) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new MapRemoveProxy(getType(), self, info); - } - - @Override public PyObject __call__(PyObject key, PyObject value) { return Py.java2py(((Map<?, ?>)self.javaProxy).remove(Py.tojava(key, Object.class))); } - } + }; - protected static class ListGetProxy extends PyBuiltinMethodNarrow { - public ListGetProxy() { - super("__getitem__", 1, 1); - } - - protected ListGetProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow LIST_GET_PROXY = + new PyBuiltinMethodNarrow("__getitem__", 1, 1){ @Override - public PyBuiltinCallable bind(PyObject self) { - return new ListGetProxy(getType(), self, info); - } - - @Override public PyObject __call__(PyObject key) { if (key instanceof PyInteger) { return Py.java2py(((List<?>)self.javaProxy).get(((PyInteger)key).getValue())); @@ -327,23 +264,11 @@ throw Py.TypeError("only integer keys accepted"); } } - } + }; - protected static class ListSetProxy extends PyBuiltinMethodNarrow { - public ListSetProxy() { - super("__setitem__", 2, 2); - } - - protected ListSetProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + protected static final PyBuiltinMethodNarrow LIST_SET_PROXY = + new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new ListSetProxy(getType(), self, info); - } - - @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { ((List<Object>)self.javaProxy).set(((PyInteger)key).getValue(), @@ -353,23 +278,11 @@ } return Py.None; } - } + }; - protected static class ListRemoveProxy extends PyBuiltinMethodNarrow { - public ListRemoveProxy() { - super("__delitem__", 1, 1); - } - - protected ListRemoveProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - - @Override - public PyBuiltinCallable bind(PyObject self) { - return new ListRemoveProxy(getType(), self, info); - } - - @Override + protected static final PyBuiltinMethodNarrow LIST_REMOVE_PROXY = + new PyBuiltinMethodNarrow("__delitem__", 1, 1) { + @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); @@ -377,38 +290,33 @@ throw Py.TypeError("only integer keys accepted"); } } - } + }; - public static class IterableProxy extends PyBuiltinMethodNarrow { - - public IterableProxy() { - super("__iter__", 0, 0); + public static final PyBuiltinMethodNarrow ITERABLE_PROXY = + new PyBuiltinMethodNarrow("__iter__", 0, 0) { + public PyObject __call__() { + return new IteratorIter(((Iterable)self.javaProxy).iterator()); } + }; - protected IterableProxy(PyType type, PyObject self, Info info) { - super(type, self, info); - } - + public static final PyBuiltinMethodNarrow EQUALS_PROXY = + new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override - public PyBuiltinCallable bind(PyObject self) { - return new IterableProxy(getType(), self, info); + public PyObject __call__(PyObject o) { + return self.javaProxy.equals(o.__tojava__(self.javaProxy.getClass())) ? Py.True + : Py.False; } + }; - @Override - public PyObject __call__() { - return new IteratorIter(((Iterable)self.javaProxy).iterator()); - } - } - static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies = Generic.map(); static { - _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {new IterableProxy()}); - _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {new LenProxy()}); - _collectionProxies.put(Map.class, new PyBuiltinMethod[] {new MapGetProxy(), - new MapPutProxy(), - new MapRemoveProxy()}); - _collectionProxies.put(List.class, new PyBuiltinMethod[] {new ListGetProxy(), - new ListSetProxy(), - new ListRemoveProxy()}); + _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {ITERABLE_PROXY}); + _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {LEN_PROXY}); + _collectionProxies.put(Map.class, new PyBuiltinMethod[] {MAP_GET_PROXY, + MAP_PUT_PROXY, + MAP_REMOVE_PROXY}); + _collectionProxies.put(List.class, new PyBuiltinMethod[] {LIST_GET_PROXY, + LIST_SET_PROXY, + LIST_REMOVE_PROXY}); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-28 09:13:21
|
Revision: 5653 http://jython.svn.sourceforge.net/jython/?rev=5653&view=rev Author: cgroves Date: 2008-11-28 09:13:19 +0000 (Fri, 28 Nov 2008) Log Message: ----------- PyObject needs to handle primitive conversion in the new world Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyInstance.java branches/newstyle-java-types/src/org/python/core/PyObject.java Modified: branches/newstyle-java-types/src/org/python/core/PyInstance.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-11-28 08:59:49 UTC (rev 5652) +++ branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-11-28 09:13:19 UTC (rev 5653) @@ -82,23 +82,6 @@ if (c.isInstance(this)) return this; - if (c.isPrimitive()) { - if (primitiveMap == null) { - primitiveMap = new Hashtable(); - primitiveMap.put(Character.TYPE, Character.class); - primitiveMap.put(Boolean.TYPE, Boolean.class); - primitiveMap.put(Byte.TYPE, Byte.class); - primitiveMap.put(Short.TYPE, Short.class); - primitiveMap.put(Integer.TYPE, Integer.class); - primitiveMap.put(Long.TYPE, Long.class); - primitiveMap.put(Float.TYPE, Float.class); - primitiveMap.put(Double.TYPE, Double.class); - } - Class tmp = (Class)primitiveMap.get(c); - if (tmp != null) - c = tmp; - } - if (instclass.__tojava__ != null) { // try { PyObject ret = instclass.__tojava__.__call__(this, PyType.fromClass(c)); Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-28 08:59:49 UTC (rev 5652) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-28 09:13:19 UTC (rev 5653) @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.NoSuchElementException; import org.python.expose.ExposedDelete; @@ -15,6 +16,7 @@ import org.python.expose.ExposedNew; import org.python.expose.ExposedSet; import org.python.expose.ExposedType; +import org.python.util.Generic; /** * All objects known to the Jython runtime system are represented by an instance @@ -253,12 +255,30 @@ if (c.isInstance(this)) { return this; } + if (c.isPrimitive()) { + Class<?> tmp = primitiveMap.get(c); + if (tmp != null) { + c = tmp; + } + } if (c.isInstance(javaProxy)) { return javaProxy; } return Py.NoConversion; } + protected static final Map<Class<?>, Class<?>> primitiveMap = Generic.map(); + static { + primitiveMap.put(Character.TYPE, Character.class); + primitiveMap.put(Boolean.TYPE, Boolean.class); + primitiveMap.put(Byte.TYPE, Byte.class); + primitiveMap.put(Short.TYPE, Short.class); + primitiveMap.put(Integer.TYPE, Integer.class); + primitiveMap.put(Long.TYPE, Long.class); + primitiveMap.put(Float.TYPE, Float.class); + primitiveMap.put(Double.TYPE, Double.class); + } + /** * The basic method to override when implementing a callable object. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-29 00:48:42
|
Revision: 5659 http://jython.svn.sourceforge.net/jython/?rev=5659&view=rev Author: cgroves Date: 2008-11-29 00:48:32 +0000 (Sat, 29 Nov 2008) Log Message: ----------- Assign the type on PyBeanProperty to the type returned by its getter and only use a setter if its parameter's type matches that. Push accesses to PyObject.javaProxy that query information from the proxy through getJavaProxy. This calls proxyInit if the proxy doesn't already exist which keeps things from blowing up if something accesses the javaProxy in an __init__. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/IdImpl.java branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyObject.java Modified: branches/newstyle-java-types/src/org/python/core/IdImpl.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -66,7 +66,7 @@ public long id(PyObject o) { if (o.getType() instanceof PyJavaType) { - return java_obj_id(o.javaProxy); + return java_obj_id(o.getJavaProxy()); } else { return java_obj_id(o); } Modified: branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -62,7 +62,7 @@ Object jvalue = Py.tojava(value, myType); try { - setMethod.invoke(iself, new Object[] {jvalue}); + setMethod.invoke(iself, jvalue); } catch (Exception e) { throw Py.JavaError(e); } Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -70,11 +70,12 @@ name = normalize(StringUtil.decapitalize(name)); PyBeanProperty prop = props.get(name); if (prop == null) { - prop = new PyBeanProperty(name, underlying_class, null, null); + prop = new PyBeanProperty(name, null, null, null); props.put(name, prop); } if (get) { prop.getMethod = meth; + prop.myType = meth.getReturnType(); } else { prop.setMethod = meth; } @@ -134,7 +135,7 @@ // If the return types on the set and get methods for a property don't agree, the get // get method takes precedence if (prop.getMethod != null && prop.setMethod != null - && prop.getMethod.getReturnType() != prop.setMethod.getReturnType()) { + && prop.myType != prop.setMethod.getParameterTypes()[0]) { prop.setMethod = null; } dict.__setitem__(prop.__name__, prop); @@ -225,7 +226,7 @@ new PyBuiltinMethodNarrow("__len__", 0, 0) { @Override public PyObject __call__() { - return Py.newInteger(((Collection<?>)self.javaProxy).size()); + return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); } }; @@ -233,7 +234,7 @@ new PyBuiltinMethodNarrow("__getitem__", 1, 1) { @Override public PyObject __call__(PyObject key) { - return Py.java2py(((Map<?, ?>)self.javaProxy).get(Py.tojava(key, Object.class))); + return Py.java2py(((Map<?, ?>)self.getJavaProxy()).get(Py.tojava(key, Object.class))); } }; @@ -241,7 +242,7 @@ new PyBuiltinMethodNarrow("__setitem__", 2, 2) { @Override public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<Object, Object>)self.javaProxy).put(Py.tojava(key, Object.class), + return Py.java2py(((Map<Object, Object>)self.getJavaProxy()).put(Py.tojava(key, Object.class), Py.tojava(value, Object.class))); } @@ -251,7 +252,7 @@ new PyBuiltinMethodNarrow("__delitem__", 1, 1) { @Override public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<?, ?>)self.javaProxy).remove(Py.tojava(key, Object.class))); + return Py.java2py(((Map<?, ?>)self.getJavaProxy()).remove(Py.tojava(key, Object.class))); } }; @@ -260,7 +261,7 @@ @Override public PyObject __call__(PyObject key) { if (key instanceof PyInteger) { - return Py.java2py(((List<?>)self.javaProxy).get(((PyInteger)key).getValue())); + return Py.java2py(((List<?>)self.getJavaProxy()).get(((PyInteger)key).getValue())); } else { throw Py.TypeError("only integer keys accepted"); } @@ -272,7 +273,7 @@ @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - ((List<Object>)self.javaProxy).set(((PyInteger)key).getValue(), + ((List<Object>)self.getJavaProxy()).set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); } else { throw Py.TypeError("only integer keys accepted"); @@ -286,7 +287,7 @@ @Override public PyObject __call__(PyObject key, PyObject value) { if (key instanceof PyInteger) { - return Py.java2py(((List<Object>)self.javaProxy).remove(((PyInteger)key).getValue())); + return Py.java2py(((List<Object>)self.getJavaProxy()).remove(((PyInteger)key).getValue())); } else { throw Py.TypeError("only integer keys accepted"); } @@ -322,7 +323,7 @@ private static final PyBuiltinMethodNarrow ITERABLE_PROXY = new PyBuiltinMethodNarrow("__iter__", 0, 0) { public PyObject __call__() { - return new IteratorIter(((Iterable)self.javaProxy).iterator()); + return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); } }; @@ -330,8 +331,8 @@ new PyBuiltinMethodNarrow("__eq__", 1, 1) { @Override public PyObject __call__(PyObject o) { - return self.javaProxy.equals(o.__tojava__(self.javaProxy.getClass())) ? Py.True - : Py.False; + return self.getJavaProxy().equals(o.__tojava__(self.getJavaProxy().getClass())) ? + Py.True : Py.False; } }; Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-29 00:17:40 UTC (rev 5658) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-11-29 00:48:32 UTC (rev 5659) @@ -37,9 +37,12 @@ } - // This field is only filled on Python wrappers for Java objects and for Python subclasses of - // Java classes. - Object javaProxy; + /** + * An underlying Java instance that this object is wrapping or is a subclass of. Anything + * attempting to use the proxy should go through {@link #getJavaProxy()} which ensures that it's + * initialized. + */ + protected Object javaProxy; private PyType objtype; @@ -243,7 +246,7 @@ * @param c the Class to convert this <code>PyObject</code> to. **/ public Object __tojava__(Class<?> c) { - if ((c == Object.class || c == Serializable.class) && javaProxy != null) { + if ((c == Object.class || c == Serializable.class) && getJavaProxy() != null) { return javaProxy; } if (c.isInstance(this)) { @@ -255,12 +258,19 @@ c = tmp; } } - if (c.isInstance(javaProxy)) { + if (c.isInstance(getJavaProxy())) { return javaProxy; } return Py.NoConversion; } + protected Object getJavaProxy() { + if (javaProxy == null) { + proxyInit(); + } + return javaProxy; + } + private static final Map<Class<?>, Class<?>> primitiveMap = Generic.map(); static { primitiveMap.put(Character.TYPE, Character.class); @@ -1573,6 +1583,8 @@ * @return the result of the comparison **/ public PyObject _is(PyObject o) { + // Access javaProxy directly here as is is for object identity, and at best getJavaProxy + // will initialize a new object with a different identity return this == o || (javaProxy != null && javaProxy == o.javaProxy) ? Py.True : Py.False; } @@ -1583,6 +1595,8 @@ * @return the result of the comparison **/ public PyObject _isnot(PyObject o) { + // Access javaProxy directly here as is is for object identity, and at best getJavaProxy + // will initialize a new object with a different identity return this != o || javaProxy != o.javaProxy ? Py.True : Py.False; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-30 00:40:26
|
Revision: 5662 http://jython.svn.sourceforge.net/jython/?rev=5662&view=rev Author: cgroves Date: 2008-11-30 00:40:22 +0000 (Sun, 30 Nov 2008) Log Message: ----------- Make java.lang.Object and Java interfaces descend from the Python object type. This changed the class initialization order, so put static constants in PyJavaType in a static method so they're actually initialized at time of use. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyType.java Modified: branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-29 07:43:57 UTC (rev 5661) +++ branches/newstyle-java-types/src/org/python/core/PyBeanProperty.java 2008-11-30 00:40:22 UTC (rev 5662) @@ -53,10 +53,9 @@ // Special handling of tuples - try to call a class constructor if (value instanceof PyTuple) { try { - PyTuple vtup = (PyTuple)value; - value = Py.java2py(myType).__call__(vtup.getArray()); // xxx PyObject subclasses + value = Py.java2py(myType).__call__(((PyTuple)value).getArray()); } catch (Throwable t) { - // If something goes wrong ignore it? + throw Py.JavaError(t); } } Object jvalue = Py.tojava(value, myType); Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-29 07:43:57 UTC (rev 5661) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-30 00:40:22 UTC (rev 5662) @@ -18,6 +18,138 @@ private final static Class<?>[] OO = {PyObject.class, PyObject.class}; + + class EnumerationIter extends PyIterator { + + private Enumeration<Object> proxy; + + public EnumerationIter(Enumeration<Object> proxy) { + this.proxy = proxy; + } + + public PyObject __iternext__() { + return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; + } + } + + private static class IteratorIter extends PyIterator { + + private Iterator<Object> proxy; + + public IteratorIter(Iterator<Object> proxy) { + this.proxy = proxy; + } + + public PyObject __iternext__() { + return proxy.hasNext() ? Py.java2py(proxy.next()) : null; + } + } + + static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies; + + protected static class ListMethod extends PyBuiltinMethodNarrow { + protected ListMethod(String name, int minArgs, int maxArgs) { + super(name, minArgs, maxArgs); + } + + protected List<Object> asList(){ + return (List<Object>)self.getJavaProxy(); + } + } + protected static class MapMethod extends PyBuiltinMethodNarrow { + protected MapMethod(String name, int minArgs, int maxArgs) { + super(name, minArgs, maxArgs); + } + + protected Map<Object, Object> asMap(){ + return (Map<Object, Object>)self.getJavaProxy(); + } + } + + public static Map<Class<?>, PyBuiltinMethod[]> getCollectionProxies() { + if (_collectionProxies == null) { + _collectionProxies = Generic.map(); + + PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__", 0, 0) { + @Override + public PyObject __call__() { + return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); + } + }; + + PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key) { + return Py.java2py(asMap().get(Py.tojava(key, Object.class))); + } + }; + + PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2, 2) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(asMap().put(Py.tojava(key, Object.class), + Py.tojava(value, Object.class))); + } + }; + + PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(asMap().remove(Py.tojava(key, Object.class))); + } + }; + + PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key) { + if (key instanceof PyInteger) { + return Py.java2py(asList().get(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + }; + + PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + asList().set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); + } else { + throw Py.TypeError("only integer keys accepted"); + } + return Py.None; + } + }; + + PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + return Py.java2py(asList().remove(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + }; + + PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + public PyObject __call__() { + return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); + } + }; + _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); + _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy}); + _collectionProxies.put(Map.class, new PyBuiltinMethod[] {mapGetProxy, + mapPutProxy, + mapRemoveProxy}); + _collectionProxies.put(List.class, new PyBuiltinMethod[] {listGetProxy, + listSetProxy, + listRemoveProxy}); + } + return _collectionProxies; + } + public static PyObject wrapJavaObject(Object o) { PyObject obj = new PyObjectDerived(PyType.fromClass(o.getClass())); obj.javaProxy = o; @@ -162,14 +294,13 @@ for (Class<?> inner : underlying_class.getClasses()) { dict.__setitem__(inner.getSimpleName(), PyType.fromClass(inner)); } - for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : _collectionProxies.entrySet()) { + for (Map.Entry<Class<?>, PyBuiltinMethod[]> entry : getCollectionProxies().entrySet()) { if (entry.getKey().isAssignableFrom(underlying_class)) { for (PyBuiltinMethod meth : entry.getValue()) { dict.__setitem__(meth.info.getName(), new PyMethodDescr(this, meth)); } } } - dict.__setitem__("__eq__", new PyMethodDescr(this, EQUALS_PROXY)); if (ClassDictInit.class.isAssignableFrom(underlying_class) && underlying_class != ClassDictInit.class) { try { @@ -184,6 +315,16 @@ || getDescrMethod(underlying_class, "_doset", OO) != null; has_delete = getDescrMethod(underlying_class, "__delete__", PyObject.class) != null || getDescrMethod(underlying_class, "_dodel", PyObject.class) != null; + } else { + // Pass __eq__ through to subclasses of Object + 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; + } + }; + dict.__setitem__("__eq__", new PyMethodDescr(this, equals)); } } @@ -221,130 +362,4 @@ } return false; } - - private static final PyBuiltinMethodNarrow LEN_PROXY = - new PyBuiltinMethodNarrow("__len__", 0, 0) { - @Override - public PyObject __call__() { - return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); - } - }; - - private static final PyBuiltinMethodNarrow MAP_GET_PROXY = - new PyBuiltinMethodNarrow("__getitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key) { - return Py.java2py(((Map<?, ?>)self.getJavaProxy()).get(Py.tojava(key, Object.class))); - } - }; - - private static final PyBuiltinMethodNarrow MAP_PUT_PROXY = - new PyBuiltinMethodNarrow("__setitem__", 2, 2) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<Object, Object>)self.getJavaProxy()).put(Py.tojava(key, Object.class), - Py.tojava(value, - Object.class))); - } - }; - - private static final PyBuiltinMethodNarrow MAP_REMOVE_PROXY = - new PyBuiltinMethodNarrow("__delitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(((Map<?, ?>)self.getJavaProxy()).remove(Py.tojava(key, Object.class))); - } - }; - - private static final PyBuiltinMethodNarrow LIST_GET_PROXY = - new PyBuiltinMethodNarrow("__getitem__", 1, 1){ - @Override - public PyObject __call__(PyObject key) { - if (key instanceof PyInteger) { - return Py.java2py(((List<?>)self.getJavaProxy()).get(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } - } - }; - - private static final PyBuiltinMethodNarrow LIST_SET_PROXY = - new PyBuiltinMethodNarrow("__setitem__", 2, 2) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - if (key instanceof PyInteger) { - ((List<Object>)self.getJavaProxy()).set(((PyInteger)key).getValue(), - Py.tojava(value, Object.class)); - } else { - throw Py.TypeError("only integer keys accepted"); - } - return Py.None; - } - }; - - private static final PyBuiltinMethodNarrow LIST_REMOVE_PROXY = - new PyBuiltinMethodNarrow("__delitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - if (key instanceof PyInteger) { - return Py.java2py(((List<Object>)self.getJavaProxy()).remove(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } - } - }; - class EnumerationIter extends PyIterator { - - private Enumeration<Object> proxy; - - public EnumerationIter(Enumeration<Object> proxy) { - this.proxy = proxy; - } - - public PyObject __iternext__() { - return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; - } - } - - private static class IteratorIter extends PyIterator { - - private Iterator<Object> proxy; - - public IteratorIter(Iterator<Object> proxy) { - this.proxy = proxy; - } - - public PyObject __iternext__() { - return proxy.hasNext() ? Py.java2py(proxy.next()) : null; - } - } - - - private static final PyBuiltinMethodNarrow ITERABLE_PROXY = - new PyBuiltinMethodNarrow("__iter__", 0, 0) { - public PyObject __call__() { - return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); - } - }; - - private static final PyBuiltinMethodNarrow EQUALS_PROXY = - new PyBuiltinMethodNarrow("__eq__", 1, 1) { - @Override - public PyObject __call__(PyObject o) { - return self.getJavaProxy().equals(o.__tojava__(self.getJavaProxy().getClass())) ? - Py.True : Py.False; - } - }; - - static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies = Generic.map(); - static { - _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {ITERABLE_PROXY}); - _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {LEN_PROXY}); - _collectionProxies.put(Map.class, new PyBuiltinMethod[] {MAP_GET_PROXY, - MAP_PUT_PROXY, - MAP_REMOVE_PROXY}); - _collectionProxies.put(List.class, new PyBuiltinMethod[] {LIST_GET_PROXY, - LIST_SET_PROXY, - LIST_REMOVE_PROXY}); - } } Modified: branches/newstyle-java-types/src/org/python/core/PyType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyType.java 2008-11-29 07:43:57 UTC (rev 5661) +++ branches/newstyle-java-types/src/org/python/core/PyType.java 2008-11-30 00:40:22 UTC (rev 5662) @@ -341,11 +341,11 @@ } private static void fillInMRO(PyType type, Class<?> base) { - if (base == Object.class || base == null) { - if (type.underlying_class == PyObject.class) { - type.mro = new PyType[] {type}; - return; - } + if (type.underlying_class == PyObject.class) { + type.mro = new PyType[] {type}; + return; + } + if (base == null) { base = PyObject.class; } PyType baseType = fromClass(base); @@ -824,7 +824,7 @@ return false; } - // we're not completely initilized yet; follow tp_base + // we're not completely initialized yet; follow tp_base PyType type = this; do { if (type == supertype) { @@ -958,6 +958,13 @@ } else { 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) { + return type; + } + class_to_type.put(c, newtype); if (base == null) { base = c.getSuperclass(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-11-30 02:30:48
|
Revision: 5663 http://jython.svn.sourceforge.net/jython/?rev=5663&view=rev Author: cgroves Date: 2008-11-30 02:30:42 +0000 (Sun, 30 Nov 2008) Log Message: ----------- Hook bean events up in PyJavaType. All of test_java_integration passes again. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyBeanEvent.java branches/newstyle-java-types/src/org/python/core/PyBeanEventProperty.java branches/newstyle-java-types/src/org/python/core/PyCompoundCallable.java branches/newstyle-java-types/src/org/python/core/PyException.java branches/newstyle-java-types/src/org/python/core/PyJavaType.java Modified: branches/newstyle-java-types/src/org/python/core/PyBeanEvent.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanEvent.java 2008-11-30 00:40:22 UTC (rev 5662) +++ branches/newstyle-java-types/src/org/python/core/PyBeanEvent.java 2008-11-30 02:30:42 UTC (rev 5663) @@ -1,13 +1,17 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; + import java.lang.reflect.Method; -public class PyBeanEvent extends PyObject { +public class PyBeanEvent<T> extends PyObject { + public Method addMethod; - public Class eventClass; + + public Class<T> eventClass; + public String __name__; - public PyBeanEvent(String name, Class eventClass, Method addMethod) { + public PyBeanEvent(String name, Class<T> eventClass, Method addMethod) { __name__ = name.intern(); this.addMethod = addMethod; this.eventClass = eventClass; @@ -17,16 +21,15 @@ throw Py.TypeError("write only attribute"); } - boolean _jdontdel() { + boolean jdontdel() { throw Py.TypeError("can't delete this attribute"); } public boolean _doset(PyObject self, PyObject value) { Object jself = Py.tojava(self, addMethod.getDeclaringClass()); - Object jvalue = Py.tojava(value, eventClass); - + T jvalue = Py.tojava(value, eventClass); try { - addMethod.invoke(jself, new Object[] {jvalue}); + addMethod.invoke(jself, jvalue); } catch (Exception e) { throw Py.JavaError(e); } Modified: branches/newstyle-java-types/src/org/python/core/PyBeanEventProperty.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyBeanEventProperty.java 2008-11-30 00:40:22 UTC (rev 5662) +++ branches/newstyle-java-types/src/org/python/core/PyBeanEventProperty.java 2008-11-30 02:30:42 UTC (rev 5663) @@ -9,16 +9,29 @@ import org.python.util.Generic; -public class PyBeanEventProperty extends PyReflectedField -{ +public class PyBeanEventProperty extends PyReflectedField { + + private static Map<String, Class<?>> adapterClasses = Generic.map(); + + private static Map<Object, Map<String, WeakReference<Object>>> adapters = + new WeakHashMap<Object, Map<String, WeakReference<Object>>>(); + public Method addMethod; + public String eventName; - public Class eventClass; + + public Class<?> eventClass; + public String __name__; - public PyBeanEventProperty(String eventName, Class eventClass, - Method addMethod, Method eventMethod) - { + private Field adapterField; + + private Class<?> adapterClass; + + public PyBeanEventProperty(String eventName, + Class<?> eventClass, + Method addMethod, + Method eventMethod) { __name__ = eventMethod.getName().intern(); this.addMethod = addMethod; this.eventName = eventName; @@ -26,47 +39,50 @@ } public PyObject _doget(PyObject self) { - if (self == null) + if (self == null) { return this; - + } initAdapter(); - Object jself = Py.tojava(self, addMethod.getDeclaringClass()); - Object field; try { field = adapterField.get(getAdapter(jself)); } catch (Exception exc) { throw Py.JavaError(exc); } - PyCompoundCallable func; if (field == null) { func = new PyCompoundCallable(); setFunction(jself, func); return func; } - if (field instanceof PyCompoundCallable) + if (field instanceof PyCompoundCallable) { return (PyCompoundCallable)field; - + } func = new PyCompoundCallable(); setFunction(jself, func); func.append((PyObject)field); return func; } - private synchronized static Class<?> getAdapterClass(Class<?> c) { - Class<?> pc = Py.findClass("org.python.proxies." + c.getName() + "$Adapter"); - if (pc == null) { - pc = MakeProxies.makeAdapter(c); + public boolean _doset(PyObject self, PyObject value) { + Object jself = Py.tojava(self, addMethod.getDeclaringClass()); + if (!(value instanceof PyCompoundCallable)) { + PyCompoundCallable func = new PyCompoundCallable(); + setFunction(jself, func); + func.append(value); + } else { + setFunction(jself, value); } - return pc; + return true; } - protected Map<Object, Map<String, WeakReference<Object>>> adapters = - new WeakHashMap<Object, Map<String, WeakReference<Object>>>(); + public String toString() { + return "<beanEventProperty " + __name__ + " for event " + eventClass.toString() + " " + + Py.idstr(this) + ">"; + } - protected Object getAdapter(Object o, String evc) { + private Object getAdapter(Object o, String evc) { Map<String, WeakReference<Object>> ads = adapters.get(o); if (ads == null) { return null; @@ -78,9 +94,9 @@ return adw.get(); } - protected void putAdapter(Object o, String evc, Object ad) { + private void putAdapter(Object o, String evc, Object ad) { Map<String, WeakReference<Object>> ads = adapters.get(o); - if(ads == null) { + if (ads == null) { ads = Generic.map(); adapters.put(o, ads); } @@ -90,11 +106,12 @@ private synchronized Object getAdapter(Object self) { String eventClassName = eventClass.getName(); Object adapter = getAdapter(self, eventClassName); - if (adapter != null) + if (adapter != null) { return adapter; + } try { adapter = adapterClass.newInstance(); - addMethod.invoke(self, new Object[] {adapter}); + addMethod.invoke(self, adapter); } catch (Exception e) { throw Py.JavaError(e); } @@ -102,14 +119,9 @@ return adapter; } - private Field adapterField; - private Class adapterClass; - private void initAdapter() { if (adapterClass == null) { adapterClass = getAdapterClass(eventClass); - } - if (adapterField == null) { try { adapterField = adapterClass.getField(__name__); } catch (NoSuchFieldException exc) { @@ -127,20 +139,16 @@ } } - public boolean _doset(PyObject self, PyObject value) { - Object jself = Py.tojava(self, addMethod.getDeclaringClass()); - if (!(value instanceof PyCompoundCallable)) { - PyCompoundCallable func = new PyCompoundCallable(); - setFunction(jself, func); - func.append(value); - } else { - setFunction(jself, value); + private synchronized static Class<?> getAdapterClass(Class<?> c) { + String name = "org.python.proxies." + c.getName() + "$Adapter"; + Class<?> pc = Py.findClass(name); + if (pc == null) { + pc = adapterClasses.get(name); + if (pc == null) { + pc = MakeProxies.makeAdapter(c); + adapterClasses.put(name, pc); + } } - return true; + return pc; } - - public String toString() { - return "<beanEventProperty "+__name__+" for event "+ - eventClass.toString()+" "+Py.idstr(this)+">"; - } } Modified: branches/newstyle-java-types/src/org/python/core/PyCompoundCallable.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyCompoundCallable.java 2008-11-30 00:40:22 UTC (rev 5662) +++ branches/newstyle-java-types/src/org/python/core/PyCompoundCallable.java 2008-11-30 02:30:42 UTC (rev 5663) @@ -1,36 +1,34 @@ // Copyright (c) Corporation for National Research Initiatives package org.python.core; -import java.util.Vector; +import java.util.List; + +import org.python.util.Generic; + public class PyCompoundCallable extends PyObject { - private Vector callables; - private PySystemState systemState; - public PyCompoundCallable () { - callables = new Vector(); - systemState = Py.getSystemState(); - } + private List<PyObject> callables = Generic.list(); + private PySystemState systemState = Py.getSystemState(); + public void append(PyObject callable) { - callables.addElement(callable); + callables.add(callable); } public void clear() { - callables.removeAllElements(); + callables.clear(); } public PyObject __call__(PyObject[] args, String[] keywords) { // Set the system state to handle callbacks from java threads Py.setSystemState(systemState); - int n = callables.size(); - //System.out.println("callable: "+n); - for (int i=0; i<n; i++) { - ((PyObject)callables.elementAt(i)).__call__(args, keywords); + for (PyObject callable : callables) { + callable.__call__(args, keywords); } return Py.None; } public String toString() { - return "<CompoundCallable with "+callables.size()+" callables>"; + return "<CompoundCallable with " + callables.size() + " callables>"; } } Modified: branches/newstyle-java-types/src/org/python/core/PyException.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyException.java 2008-11-30 00:40:22 UTC (rev 5662) +++ branches/newstyle-java-types/src/org/python/core/PyException.java 2008-11-30 02:30:42 UTC (rev 5663) @@ -65,12 +65,9 @@ public void printStackTrace() { Py.printException(this); } - + public Throwable fillInStackTrace() { - if (Options.includeJavaStackInExceptions) - return super.fillInStackTrace(); - else - return this; + return Options.includeJavaStackInExceptions ? super.fillInStackTrace() : this; } public synchronized void printStackTrace(PrintStream s) { Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-30 00:40:22 UTC (rev 5662) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-11-30 02:30:42 UTC (rev 5663) @@ -7,6 +7,7 @@ import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Enumeration; +import java.util.EventListener; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -18,138 +19,8 @@ private final static Class<?>[] OO = {PyObject.class, PyObject.class}; + private static Map<Class<?>, PyBuiltinMethod[]> collectionProxies; - class EnumerationIter extends PyIterator { - - private Enumeration<Object> proxy; - - public EnumerationIter(Enumeration<Object> proxy) { - this.proxy = proxy; - } - - public PyObject __iternext__() { - return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; - } - } - - private static class IteratorIter extends PyIterator { - - private Iterator<Object> proxy; - - public IteratorIter(Iterator<Object> proxy) { - this.proxy = proxy; - } - - public PyObject __iternext__() { - return proxy.hasNext() ? Py.java2py(proxy.next()) : null; - } - } - - static Map<Class<?>, PyBuiltinMethod[]> _collectionProxies; - - protected static class ListMethod extends PyBuiltinMethodNarrow { - protected ListMethod(String name, int minArgs, int maxArgs) { - super(name, minArgs, maxArgs); - } - - protected List<Object> asList(){ - return (List<Object>)self.getJavaProxy(); - } - } - protected static class MapMethod extends PyBuiltinMethodNarrow { - protected MapMethod(String name, int minArgs, int maxArgs) { - super(name, minArgs, maxArgs); - } - - protected Map<Object, Object> asMap(){ - return (Map<Object, Object>)self.getJavaProxy(); - } - } - - public static Map<Class<?>, PyBuiltinMethod[]> getCollectionProxies() { - if (_collectionProxies == null) { - _collectionProxies = Generic.map(); - - PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__", 0, 0) { - @Override - public PyObject __call__() { - return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); - } - }; - - PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key) { - return Py.java2py(asMap().get(Py.tojava(key, Object.class))); - } - }; - - PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2, 2) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(asMap().put(Py.tojava(key, Object.class), - Py.tojava(value, Object.class))); - } - }; - - PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - return Py.java2py(asMap().remove(Py.tojava(key, Object.class))); - } - }; - - PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key) { - if (key instanceof PyInteger) { - return Py.java2py(asList().get(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } - } - }; - - PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - if (key instanceof PyInteger) { - asList().set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); - } else { - throw Py.TypeError("only integer keys accepted"); - } - return Py.None; - } - }; - - PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { - @Override - public PyObject __call__(PyObject key, PyObject value) { - if (key instanceof PyInteger) { - return Py.java2py(asList().remove(((PyInteger)key).getValue())); - } else { - throw Py.TypeError("only integer keys accepted"); - } - } - }; - - PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { - public PyObject __call__() { - return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); - } - }; - _collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); - _collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy}); - _collectionProxies.put(Map.class, new PyBuiltinMethod[] {mapGetProxy, - mapPutProxy, - mapRemoveProxy}); - _collectionProxies.put(List.class, new PyBuiltinMethod[] {listGetProxy, - listSetProxy, - listRemoveProxy}); - } - return _collectionProxies; - } - public static PyObject wrapJavaObject(Object o) { PyObject obj = new PyObjectDerived(PyType.fromClass(o.getClass())); obj.javaProxy = o; @@ -172,6 +43,7 @@ // Add methods and determine bean properties declared on this class Map<String, PyBeanProperty> props = Generic.map(); + Map<String, PyBeanEvent> events = Generic.map(); for (Method meth : underlying_class.getMethods()) { if (!declaredOnMember(base, meth) || ignore(meth)) { continue; @@ -184,34 +56,54 @@ } else { reflfunc.addMethod(meth); } - if (!Modifier.isStatic(meth.getModifiers())) { - // check for xxxX.* - int n = meth.getParameterTypes().length; - String name = null; - boolean get = true; - if (methname.startsWith("get") && methname.length() > 3 && n == 0) { - name = methname.substring(3); - } else if (methname.startsWith("is") && methname.length() > 2 && n == 0 - && meth.getReturnType() == Boolean.TYPE) { - name = methname.substring(2); - } else if (methname.startsWith("set") && methname.length() > 3 && n == 1) { - name = methname.substring(3); - get = false; + + // Now check if this is a bean method, for which it must be an instance method + if (Modifier.isStatic(meth.getModifiers())) { + continue; + } + + // First check if this is a bean event addition method + int n = meth.getParameterTypes().length; + if ((methname.startsWith("add") || methname.startsWith("set")) + && methname.endsWith("Listener") && n == 1 && + meth.getReturnType() == Void.TYPE && + EventListener.class.isAssignableFrom(meth.getParameterTypes()[0])) { + Class<?> eventClass = meth.getParameterTypes()[0]; + String ename = eventClass.getName(); + int idot = ename.lastIndexOf('.'); + if (idot != -1) { + ename = ename.substring(idot + 1); } - if (name != null) { - name = normalize(StringUtil.decapitalize(name)); - PyBeanProperty prop = props.get(name); - if (prop == null) { - prop = new PyBeanProperty(name, null, null, null); - props.put(name, prop); - } - if (get) { - prop.getMethod = meth; - prop.myType = meth.getReturnType(); - } else { - prop.setMethod = meth; - } + ename = normalize(StringUtil.decapitalize(ename)); + events.put(ename, new PyBeanEvent(name, eventClass, meth)); + continue; + } + + // Now check if it's a bean property accessor + String name = null; + boolean get = true; + if (methname.startsWith("get") && methname.length() > 3 && n == 0) { + name = methname.substring(3); + } else if (methname.startsWith("is") && methname.length() > 2 && n == 0 + && meth.getReturnType() == Boolean.TYPE) { + name = methname.substring(2); + } else if (methname.startsWith("set") && methname.length() > 3 && n == 1) { + name = methname.substring(3); + get = false; + } + if (name != null) { + name = normalize(StringUtil.decapitalize(name)); + PyBeanProperty prop = props.get(name); + if (prop == null) { + prop = new PyBeanProperty(name, null, null, null); + props.put(name, prop); } + if (get) { + prop.getMethod = meth; + prop.myType = meth.getReturnType(); + } else { + prop.setMethod = meth; + } } } @@ -251,6 +143,23 @@ } } + for (PyBeanEvent ev : events.values()) { + if (dict.__finditem__(ev.__name__) == null) { + dict.__setitem__(ev.__name__, ev); + } + + for (Method meth : ev.eventClass.getMethods()) { + String name = meth.getName().intern(); + if (dict.__finditem__(name) != null) { + continue; + } + dict.__setitem__(name, new PyBeanEventProperty(name, + ev.eventClass, + ev.addMethod, + meth)); + } + } + // Fill in the bean properties picked up while going through the methods for (PyBeanProperty prop : props.values()) { PyObject prev = dict.__finditem__(prop.__name__); @@ -362,4 +271,134 @@ } return false; } + + private class EnumerationIter extends PyIterator { + + private Enumeration<Object> proxy; + + public EnumerationIter(Enumeration<Object> proxy) { + this.proxy = proxy; + } + + public PyObject __iternext__() { + return proxy.hasMoreElements() ? Py.java2py(proxy.nextElement()) : null; + } + } + + private static class IteratorIter extends PyIterator { + + private Iterator<Object> proxy; + + public IteratorIter(Iterator<Object> proxy) { + this.proxy = proxy; + } + + public PyObject __iternext__() { + return proxy.hasNext() ? Py.java2py(proxy.next()) : null; + } + } + + private static class ListMethod extends PyBuiltinMethodNarrow { + protected ListMethod(String name, int minArgs, int maxArgs) { + super(name, minArgs, maxArgs); + } + + protected List<Object> asList(){ + return (List<Object>)self.getJavaProxy(); + } + } + + private static class MapMethod extends PyBuiltinMethodNarrow { + protected MapMethod(String name, int minArgs, int maxArgs) { + super(name, minArgs, maxArgs); + } + + protected Map<Object, Object> asMap(){ + return (Map<Object, Object>)self.getJavaProxy(); + } + } + + private static Map<Class<?>, PyBuiltinMethod[]> getCollectionProxies() { + if (collectionProxies == null) { + collectionProxies = Generic.map(); + + PyBuiltinMethodNarrow lenProxy = new PyBuiltinMethodNarrow("__len__", 0, 0) { + @Override + public PyObject __call__() { + return Py.newInteger(((Collection<?>)self.getJavaProxy()).size()); + } + }; + + PyBuiltinMethodNarrow mapGetProxy = new MapMethod("__getitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key) { + return Py.java2py(asMap().get(Py.tojava(key, Object.class))); + } + }; + + PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2, 2) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(asMap().put(Py.tojava(key, Object.class), + Py.tojava(value, Object.class))); + } + }; + + PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + return Py.java2py(asMap().remove(Py.tojava(key, Object.class))); + } + }; + + PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key) { + if (key instanceof PyInteger) { + return Py.java2py(asList().get(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + }; + + PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2, 2) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + asList().set(((PyInteger)key).getValue(), Py.tojava(value, Object.class)); + } else { + throw Py.TypeError("only integer keys accepted"); + } + return Py.None; + } + }; + + PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1, 1) { + @Override + public PyObject __call__(PyObject key, PyObject value) { + if (key instanceof PyInteger) { + return Py.java2py(asList().remove(((PyInteger)key).getValue())); + } else { + throw Py.TypeError("only integer keys accepted"); + } + } + }; + + PyBuiltinMethodNarrow iterableProxy = new PyBuiltinMethodNarrow("__iter__", 0, 0) { + public PyObject __call__() { + return new IteratorIter(((Iterable)self.getJavaProxy()).iterator()); + } + }; + collectionProxies.put(Iterable.class, new PyBuiltinMethod[] {iterableProxy}); + collectionProxies.put(Collection.class, new PyBuiltinMethod[] {lenProxy}); + collectionProxies.put(Map.class, new PyBuiltinMethod[] {mapGetProxy, + mapPutProxy, + mapRemoveProxy}); + collectionProxies.put(List.class, new PyBuiltinMethod[] {listGetProxy, + listSetProxy, + listRemoveProxy}); + } + return collectionProxies; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-12-01 01:00:40
|
Revision: 5666 http://jython.svn.sourceforge.net/jython/?rev=5666&view=rev Author: cgroves Date: 2008-12-01 01:00:37 +0000 (Mon, 01 Dec 2008) Log Message: ----------- Replace fillDict with init which encapsulates all the differences between PyType and PyJavaType initialization. Have bootstrap types use init as well to remove the duplication of type builder initialization. Include interfaces and superclasses in the mro of wrapped Java types. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyType.java Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-01 00:38:14 UTC (rev 5665) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-01 01:00:37 UTC (rev 5666) @@ -11,6 +11,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; + import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; import org.python.util.Generic; @@ -37,15 +39,56 @@ } @Override - protected void fillDict() { + protected void init() { + name = underlying_class.getName(); + // Strip the java fully qualified class name from Py classes in core + if (name.startsWith("org.python.core.Py")) { + name = name.substring("org.python.core.Py".length()).toLowerCase(); + } dict = new PyStringMap(); - Class<?> base = underlying_class.getSuperclass(); + Class<?> baseClass = underlying_class.getSuperclass(); + if (PyObject.class.isAssignableFrom(underlying_class)) { + // Non-exposed subclasses of PyObject use a simple linear mro to PyObject that ignores + // their interfaces + computeLinearMro(baseClass); + } else { + // 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 { + base = PyType.fromClass(baseClass); + } + bases = new PyObject[1 + underlying_class.getInterfaces().length]; + bases[0] = base; + for (int i = 1; i < bases.length; i++) { + bases[i] = PyType.fromClass(underlying_class.getInterfaces()[i - 1]); + } + Set<PyObject> seen = Generic.set(); + List<PyObject> mros = Generic.list(); + mros.add(this); + for (PyObject obj : bases) { + for (PyObject mroObj : ((PyType)obj).mro) { + if (seen.add(mroObj)) { + mros.add(mroObj); + } + } + } + mro = mros.toArray(new PyObject[mros.size()]); + } + // PyReflected* can't call or access anything from non-public classes that aren't in + // org.python.core + if (!Modifier.isPublic(underlying_class.getModifiers()) && + !name.startsWith("org.python.core")) { + return; + } + // Add methods and determine bean properties declared on this class Map<String, PyBeanProperty> props = Generic.map(); Map<String, PyBeanEvent> events = Generic.map(); for (Method meth : underlying_class.getMethods()) { - if (!declaredOnMember(base, meth) || ignore(meth)) { + if (!declaredOnMember(baseClass, meth) || ignore(meth)) { continue; } String methname = meth.getName(); @@ -75,7 +118,7 @@ ename = ename.substring(idot + 1); } ename = normalize(StringUtil.decapitalize(ename)); - events.put(ename, new PyBeanEvent(name, eventClass, meth)); + events.put(ename, new PyBeanEvent(ename, eventClass, meth)); continue; } @@ -118,7 +161,7 @@ // Add fields declared on this type for (Field field : underlying_class.getFields()) { - if (!declaredOnMember(base, field)) { + if (!declaredOnMember(baseClass, field)) { continue; } String fldname = field.getName(); @@ -219,7 +262,7 @@ throw Py.JavaError(exc); } } - if (base != Object.class) { + if (baseClass != Object.class) { has_set = getDescrMethod(underlying_class, "__set__", OO) != null || getDescrMethod(underlying_class, "_doset", OO) != null; has_delete = getDescrMethod(underlying_class, "__delete__", PyObject.class) != null Modified: branches/newstyle-java-types/src/org/python/core/PyType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyType.java 2008-12-01 00:38:14 UTC (rev 5665) +++ branches/newstyle-java-types/src/org/python/core/PyType.java 2008-12-01 01:00:37 UTC (rev 5666) @@ -333,36 +333,55 @@ return newobj; } - protected void fillDict() { + /** + * Called on builtin types after underlying_class has been set on them. Should fill in dict, + * name, mro, base and bases from the class. + */ + protected void init() { + if (underlying_class == PyObject.class) { + mro = new PyType[] {this}; + } else { + Class<?> baseClass; + if (!Py.BOOTSTRAP_TYPES.contains(underlying_class)) { + baseClass = classToBuilder.get(underlying_class).getBase(); + } else { + baseClass = PyObject.class; + } + if (baseClass == Object.class) { + baseClass = underlying_class.getSuperclass(); + } + computeLinearMro(baseClass); + } if (Py.BOOTSTRAP_TYPES.contains(underlying_class)) { + // init will be called again from addBuilder which also removes underlying_class from + // BOOTSTRAP_TYPES return; } - dict = classToBuilder.get(underlying_class).getDict(this); + TypeBuilder builder = classToBuilder.get(underlying_class); + name = builder.getName(); + dict = builder.getDict(this); + setIsBaseType(builder.getIsBaseType()); instantiable = dict.__finditem__("__new__") != null; fillHasSetAndDelete(); } + /** + * Fills the base and bases of this type with the type of baseClass as sets its mro to this type + * followed by the mro of baseClass. + */ + protected void computeLinearMro(Class<?> baseClass) { + base = PyType.fromClass(baseClass); + mro = new PyType[base.mro.length + 1]; + System.arraycopy(base.mro, 0, mro, 1, base.mro.length); + mro[0] = this; + bases = new PyObject[] {base}; + } + private void fillHasSetAndDelete() { has_set = lookup("__set__") != null; has_delete = lookup("__delete__") != null; } - private static void fillInMRO(PyType type, Class<?> base) { - if (type.underlying_class == PyObject.class) { - type.mro = new PyType[] {type}; - return; - } - if (base == null) { - base = PyObject.class; - } - 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() { PyType cur = this; while (cur.underlying_class == null) { @@ -951,16 +970,7 @@ } // The types in Py.BOOTSTRAP_TYPES are initialized before their builders are assigned, // so do the work of addFromClass & fillFromClass after the fact - PyType objType = fromClass(builder.getTypeClass()); - objType.name = builder.getName(); - objType.dict = builder.getDict(objType); - objType.setIsBaseType(builder.getIsBaseType()); - Class<?> base = builder.getBase(); - if (base == Object.class) { - base = forClass.getSuperclass(); - } - fillInMRO(objType, base); - objType.instantiable = objType.dict.__finditem__("__new__") != null; + fromClass(builder.getTypeClass()).init(); } } @@ -970,29 +980,14 @@ class_to_type.put(c, exposedAs); return exposedAs; } - Class<?> base = null; - boolean isBaseType = true; - String name = null; - TypeBuilder tb = getBuilder(c); - if (tb != null) { - name = tb.getName(); - isBaseType = tb.getIsBaseType(); - if (!tb.getBase().equals(Object.class)) { - base = tb.getBase(); - } - } - PyType type = class_to_type.get(c); - if (type == null) { - type = createType(c, base, name, isBaseType); - } - return type; + return createType(c); } private static TypeBuilder getBuilder(Class<?> c) { return classToBuilder == null ? null : classToBuilder.get(c); } - private static PyType createType(Class<?> c, Class<?> base, String name, boolean isBaseType) { + private static PyType createType(Class<?> c) { PyType newtype; if (c == PyType.class) { newtype = new PyType(false); @@ -1009,28 +1004,9 @@ } class_to_type.put(c, newtype); - if (base == null) { - base = c.getSuperclass(); - } newtype.underlying_class = c; - if (name == null) { - name = c.getName(); - // Strip the java fully qualified class name (specifically remove org.python.core.Py or - // fallback to stripping to the last dot) - if (name.startsWith("org.python.core.Py")) { - name = name.substring("org.python.core.Py".length()).toLowerCase(); - } else if(newtype.getProxyType() == null) { - int lastDot = name.lastIndexOf('.'); - if (lastDot != -1) { - name = name.substring(lastDot + 1); - } - } - } - newtype.name = name; newtype.builtin = true; - newtype.setIsBaseType(isBaseType); - fillInMRO(newtype, base); // basic mro, base, bases - newtype.fillDict(); + newtype.init(); return newtype; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-12-01 01:14:25
|
Revision: 5669 http://jython.svn.sourceforge.net/jython/?rev=5669&view=rev Author: cgroves Date: 2008-12-01 01:14:21 +0000 (Mon, 01 Dec 2008) Log Message: ----------- Reset a PyJavaType's state when reload is called on it. This was just ignored on PyJavaClasses, so this is a little step up Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyObject.java branches/newstyle-java-types/src/org/python/core/__builtin__.java Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-01 01:08:51 UTC (rev 5668) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-01 01:14:21 UTC (rev 5669) @@ -618,7 +618,6 @@ * @param value the value to set this key to **/ public void __setitem__(PyObject key, PyObject value) { - Thread.dumpStack(); throw Py.TypeError(String.format("'%.200s' object does not support item assignment", getType().fastGetName())); } Modified: branches/newstyle-java-types/src/org/python/core/__builtin__.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/__builtin__.java 2008-12-01 01:08:51 UTC (rev 5668) +++ branches/newstyle-java-types/src/org/python/core/__builtin__.java 2008-12-01 01:14:21 UTC (rev 5669) @@ -97,6 +97,8 @@ if (o == Py.NoConversion) { if (arg1 instanceof PySystemState) { return __builtin__.reload((PySystemState)arg1); + } else if(arg1 instanceof PyJavaType) { + return __builtin__.reload((PyJavaType)arg1); } throw Py.TypeError("reload() argument must be a module"); } @@ -1105,6 +1107,12 @@ return o; } + public static PyObject reload(PyJavaType o) { + // Reset the contents of our dict to the state from our class + o.init(); + return o; + } + public static PyString repr(PyObject o) { return o.__repr__(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-12-04 01:10:50
|
Revision: 5688 http://jython.svn.sourceforge.net/jython/?rev=5688&view=rev Author: cgroves Date: 2008-12-04 01:10:43 +0000 (Thu, 04 Dec 2008) Log Message: ----------- Switch to using getJavaProxy() != null from getType() instanceof PyJavaType to detect if a PyObject is wrapping a Java object or a subclass of a Java object. This keeps PyObject subclasses from appearing to be proxy types. The old way was breaking copy as instances of classic classes - which have a type of PyJavaType by virtue of PyClass being a non-type subclass of PyObject - didn't have a Java proxy and were getting a different id with each call. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/IdImpl.java branches/newstyle-java-types/src/org/python/core/Py.java branches/newstyle-java-types/src/org/python/core/PyInstance.java branches/newstyle-java-types/src/org/python/core/PyObject.java branches/newstyle-java-types/src/org/python/core/StdoutWrapper.java branches/newstyle-java-types/src/org/python/core/__builtin__.java branches/newstyle-java-types/src/org/python/core/imp.java Modified: branches/newstyle-java-types/src/org/python/core/IdImpl.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/IdImpl.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -2,36 +2,39 @@ import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; -import java.util.HashMap; +import java.util.Map; +import org.python.util.Generic; + public class IdImpl { public static class WeakIdentityMap { - private transient ReferenceQueue refqueue = new ReferenceQueue(); - private HashMap hashmap = new HashMap(); + private transient ReferenceQueue<Object> idKeys = new ReferenceQueue<Object>(); + private Map<WeakIdKey, Object> objHashcodeToPyId = Generic.map(); + private void cleanup() { Object k; - while ((k = this.refqueue.poll()) != null) { - this.hashmap.remove(k); + while ((k = idKeys.poll()) != null) { + objHashcodeToPyId.remove(k); } } - private class WeakIdKey extends WeakReference { - private int hashcode; + private class WeakIdKey extends WeakReference<Object> { + private final int hashcode; WeakIdKey(Object obj) { - super(obj,WeakIdentityMap.this.refqueue); - this.hashcode = System.identityHashCode(obj); + super(obj, idKeys); + hashcode = System.identityHashCode(obj); } public int hashCode() { - return this.hashcode; + return hashcode; } public boolean equals(Object other) { - Object obj = this.get(); + Object obj = get(); if (obj != null) { return obj == ((WeakIdKey)other).get(); } else { @@ -40,50 +43,45 @@ } } - public int _internal_map_size() { - return this.hashmap.size(); - } - - public void put(Object key,Object val) { + public void put(Object key, Object val) { cleanup(); - this.hashmap.put(new WeakIdKey(key),val); + objHashcodeToPyId.put(new WeakIdKey(key), val); } public Object get(Object key) { cleanup(); - return this.hashmap.get(new WeakIdKey(key)); + return objHashcodeToPyId.get(new WeakIdKey(key)); } public void remove(Object key) { cleanup(); - this.hashmap.remove(new WeakIdKey(key)); + objHashcodeToPyId.remove(new WeakIdKey(key)); } } - private WeakIdentityMap id_map = new WeakIdentityMap(); - private long sequential_id = 0; + private WeakIdentityMap idMap = new WeakIdentityMap(); - public long id(PyObject o) { - if (o.getType() instanceof PyJavaType) { - return java_obj_id(o.getJavaProxy()); + private long sequentialId; + + public synchronized long id(PyObject o) { + Object javaProxy = o.getJavaProxy(); + if (javaProxy != null) { + return java_obj_id(javaProxy); } else { return java_obj_id(o); } } - // XXX maybe should display both this id and identityHashCode - // XXX preserve the old "at ###" style? public String idstr(PyObject o) { return Long.toString(id(o)); } - public synchronized long java_obj_id(Object o) { - Long cand = (Long)this.id_map.get(o); + public long java_obj_id(Object o) { + Long cand = (Long)idMap.get(o); if (cand == null) { - this.sequential_id++; - long new_id = this.sequential_id; - this.id_map.put(o,new Long(new_id)); + long new_id = ++sequentialId; + idMap.put(o, new_id); return new_id; } return cand.longValue(); Modified: branches/newstyle-java-types/src/org/python/core/Py.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/Py.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/Py.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -993,7 +993,7 @@ } } - if (value.getType() instanceof PyJavaType) { + if (value.getJavaProxy() != null) { Object javaError = value.__tojava__(Throwable.class); if (javaError != null && javaError != Py.NoConversion) { @@ -1947,7 +1947,7 @@ name = "fixed file"; this.file = file; - if (file.getType() instanceof PyJavaType) { + if (file.getJavaProxy() != null) { Object tojava = file.__tojava__(OutputStream.class); if (tojava != null && tojava != Py.NoConversion) { this.file = new PyFile((OutputStream) tojava); Modified: branches/newstyle-java-types/src/org/python/core/PyInstance.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/PyInstance.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -374,11 +374,10 @@ public int hashCode() { PyObject ret; ret = invoke_ex("__hash__"); - if (ret == null) { - if (__findattr__("__eq__") != null || - __findattr__("__cmp__") != null) + if (__findattr__("__eq__") != null || __findattr__("__cmp__") != null) { throw Py.TypeError("unhashable instance"); + } return super.hashCode(); } if (ret instanceof PyInteger) { @@ -399,9 +398,9 @@ if (coerced != null) { v = coerced[0]; w = coerced[1]; - if (!(v instanceof PyInstance) && - !(w instanceof PyInstance)) + if (!(v instanceof PyInstance) && !(w instanceof PyInstance)) { return v._cmp(w); + } } else { v = this; w = other; Modified: branches/newstyle-java-types/src/org/python/core/PyObject.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/PyObject.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -3447,7 +3447,7 @@ } return __call__(pargs); } catch (PyException e) { - if (e.value.getType() instanceof PyJavaType) { + if (e.value.getJavaProxy() != null) { Object t = e.value.__tojava__(Throwable.class); if (t != null && t != Py.NoConversion) { throw (Throwable) t; Modified: branches/newstyle-java-types/src/org/python/core/StdoutWrapper.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/StdoutWrapper.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/StdoutWrapper.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -26,7 +26,7 @@ if (obj == null) { throw Py.AttributeError("missing sys." + this.name); } - if (obj.getType() instanceof PyJavaType) { + if (obj.getJavaProxy() != null) { PyFile f = null; Object tojava = obj.__tojava__(OutputStream.class); Modified: branches/newstyle-java-types/src/org/python/core/__builtin__.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/__builtin__.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/__builtin__.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -1579,7 +1579,7 @@ ArgParser ap = new ArgParser("file", args, kwds, new String[] {"name", "mode", "bufsize"}, 1); PyObject obj = ap.getPyObject(0); - if (obj.getType() instanceof PyJavaType) { + if (obj.getJavaProxy() != null) { int bufsize = ap.getInt(2, -1); if (obj.javaProxy instanceof InputStream) { Py.warning(Py.DeprecationWarning, warning); Modified: branches/newstyle-java-types/src/org/python/core/imp.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/imp.java 2008-12-03 22:08:15 UTC (rev 5687) +++ branches/newstyle-java-types/src/org/python/core/imp.java 2008-12-04 01:10:43 UTC (rev 5688) @@ -281,7 +281,7 @@ throw Py.JavaError(e); } } - return PyJavaType.fromClass(c); // xxx? + return PyType.fromClass(c); // xxx? } static PyObject getPathImporter(PyObject cache, PyList hooks, PyObject p) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2008-12-10 07:09:53
|
Revision: 5729 http://jython.svn.sourceforge.net/jython/?rev=5729&view=rev Author: cgroves Date: 2008-12-10 07:09:49 +0000 (Wed, 10 Dec 2008) Log Message: ----------- Allow setattr and delattr to access the dicts of PyJavaTypes. Fixes test_strptime as Time was expected to allow modification to its dict. Modified Paths: -------------- branches/newstyle-java-types/src/org/python/core/PyJavaType.java branches/newstyle-java-types/src/org/python/core/PyType.java Modified: branches/newstyle-java-types/src/org/python/core/PyJavaType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-10 06:47:19 UTC (rev 5728) +++ branches/newstyle-java-types/src/org/python/core/PyJavaType.java 2008-12-10 07:09:49 UTC (rev 5729) @@ -40,7 +40,15 @@ return PyObject.class.isAssignableFrom(underlying_class) ? null : underlying_class; } + // Java types are ok with things being added and removed from their dicts as long as there isn't + // something there, so let these checks through @Override + protected void checkDelattr() {} + + @Override + protected void checkSetattr() {} + + @Override protected void init() { name = underlying_class.getName(); // Strip the java fully qualified class name from Py classes in core Modified: branches/newstyle-java-types/src/org/python/core/PyType.java =================================================================== --- branches/newstyle-java-types/src/org/python/core/PyType.java 2008-12-10 06:47:19 UTC (rev 5728) +++ branches/newstyle-java-types/src/org/python/core/PyType.java 2008-12-10 07:09:49 UTC (rev 5729) @@ -1076,11 +1076,15 @@ type___setattr__(name, value); } - final void type___setattr__(String name, PyObject value) { + protected void checkSetattr() { if (builtin) { throw Py.TypeError(String.format("can't set attributes of built-in/extension type " - + "'%s'", this.name)); + + "'%s'", this.name)); } + } + + final void type___setattr__(String name, PyObject value) { + checkSetattr(); super.__setattr__(name, value); if (name == "__set__") { if (!has_set && lookup("__set__") != null) { @@ -1114,11 +1118,15 @@ type___delattr__(asName(name)); } - final void type___delattr__(String name) { + protected void checkDelattr() { if (builtin) { throw Py.TypeError(String.format("can't set attributes of built-in/extension type " - + "'%s'", this.name)); + + "'%s'", this.name)); } + } + + final void type___delattr__(String name) { + checkDelattr(); super.__delattr__(name); if (name == "__set__") { if (has_set && lookup("__set__") == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |