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. |