/** * Determine whether obj is an Python exception instance * * @param obj a PyObject * @return true if an exception instance */ public static boolean isExceptionInstance(PyObject obj) { return obj instanceof PyInstance || obj instanceof PyBaseException || obj.getJavaProxy() instanceof Throwable; }
protected Map<Object, Object> asMap() { return (Map<Object, Object>) self.getJavaProxy(); } }
@SuppressWarnings("unchecked") protected Set<Object> asSet() { return (Set<Object>) self.getJavaProxy(); }
protected Class<?> asClass() { return (Class<?>) self.getJavaProxy(); } }
protected List asList() { return (List) self.getJavaProxy(); }
@Override public PyObject __call__(PyObject o) { Object proxy = self.getJavaProxy(); Object oProxy = o.getJavaProxy(); return !proxy.equals(oProxy) ? Py.True : Py.False; } });
@Override public PyObject __call__(PyObject o) { Object proxy = self.getJavaProxy(); Object oProxy = o.getJavaProxy(); return proxy.equals(oProxy) ? Py.True : Py.False; } });
@Override public PyObject __call__() { return Py.newInteger(self.getJavaProxy().hashCode()); } });
@Override public PyObject __call__() { return new JavaIterator(((Iterator) self.getJavaProxy())); } };
@Override public PyObject __call__() { return new EnumerationIter(((Enumeration) self.getJavaProxy())); } };
@Override public PyObject __call__() { return new JavaIterator(((Iterable) self.getJavaProxy())); } };
@Override public PyObject __call__() { /* * java.lang.Object.toString returns Unicode: preserve as a PyUnicode, then let * the repr() built-in decide how to handle it. (Also applies to __str__.) */ String toString = self.getJavaProxy().toString(); return toString == null ? Py.EmptyUnicode : Py.newUnicode(toString); } });
@Override public PyObject __call__() { return Py.newInteger(((Collection<?>) self.getJavaProxy()).size()); } };
@Override public PyObject __call__(PyObject arg) { Object asjava = arg.__tojava__(Object.class); int compare; try { compare = ((Comparable<Object>)self.getJavaProxy()).compareTo(asjava); } catch(ClassCastException classCast) { return Py.NotImplemented; } return getResult(compare) ? Py.True : Py.False; }
protected PyObject makePySet(Set newSet) { PyObject newPySet = self.getType().__call__(); @SuppressWarnings("unchecked") Set<Object> jSet = ((Set<Object>) newPySet.getJavaProxy()); jSet.addAll(newSet); return newPySet; }
@Override public PyObject __call__() { Object obj = self.getJavaProxy(); Method clone; // TODO we could specialize so that for well known objects like collections. // This would avoid needing to use reflection in the general case, // because Object#clone is protected (but most subclasses are not). // // Lastly we can potentially cache the method handle in the proxy instead of looking it up each time try { clone = obj.getClass().getMethod("clone"); Object copy = clone.invoke(obj); return Py.java2py(copy); } catch (Exception ex) { throw Py.TypeError("Could not copy Java object"); } } });
public FixedFileWrapper(PyObject file) { name = "fixed file"; this.file = file; if (file.getJavaProxy() != null) { Object tojava = file.__tojava__(OutputStream.class); if (tojava != null && tojava != Py.NoConversion) { this.file = new PyFile((OutputStream) tojava); } } }
@Override public PyObject __call__(PyObject memo) { Object obj = self.getJavaProxy(); try { Object copy = cloneX(obj); return Py.java2py(copy); } catch (Exception ex) { throw Py.TypeError("Could not copy Java object"); } } });
public boolean isSubset(PyObject other) { Set<Object> selfSet = asSet(); Object oj = other.getJavaProxy(); if (oj != null && oj instanceof Set) { @SuppressWarnings("unchecked") Set<Object> otherSet = (Set<Object>) oj; return otherSet.containsAll(selfSet); } else if (isPySet(other)) { Set<PyObject> otherPySet = ((BaseSet) other).getSet(); for (Object obj : selfSet) { if (!otherPySet.contains(Py.java2py(obj))) { return false; } } return true; } return false; }
public synchronized long id(PyObject o) { Object id = JyAttribute.getAttr(o, JyAttribute.PY_ID_ATTR); if (id != null) { return ((Long) id).longValue(); } Object javaProxy = o.getJavaProxy(); long result; if (javaProxy != null) { result = java_obj_id(javaProxy); } else { result = java_obj_id(o); } JyAttribute.setAttr(o, JyAttribute.PY_ID_ATTR, result); return result; }