[Jython-checkins] jython: Formatting and the odd typo in Map and Set proxies
jeff.allen
jython-checkins at python.org
Sat Feb 1 05:05:32 EST 2020
https://hg.python.org/jython/rev/fe9b60423d2d
changeset: 8324:fe9b60423d2d
user: Jeff Allen <ja.py at farowl.co.uk>
date: Sat Feb 01 08:46:58 2020 +0000
summary:
Formatting and the odd typo in Map and Set proxies
files:
src/org/python/core/JavaProxyMap.java | 149 ++++++++-----
src/org/python/core/JavaProxySet.java | 113 +++++++---
2 files changed, 170 insertions(+), 92 deletions(-)
diff --git a/src/org/python/core/JavaProxyMap.java b/src/org/python/core/JavaProxyMap.java
--- a/src/org/python/core/JavaProxyMap.java
+++ b/src/org/python/core/JavaProxyMap.java
@@ -8,13 +8,14 @@
import java.util.Set;
/**
- * Proxy Java objects implementing java.util.List with Python methods
- * corresponding to the standard list type
+ * Proxy Java objects implementing java.util.List with Python methods corresponding to the standard
+ * list type
*/
class JavaProxyMap {
@Untraversable
private static class MapMethod extends PyBuiltinMethodNarrow {
+
protected MapMethod(String name, int numArgs) {
super(name, numArgs);
}
@@ -30,6 +31,7 @@
@Untraversable
private static class MapClassMethod extends PyBuiltinClassMethodNarrow {
+
protected MapClassMethod(String name, int minArgs, int maxArgs) {
super(name, minArgs, maxArgs);
}
@@ -108,12 +110,12 @@
/** Return Python {@code ValueError} that None is not allowed. */
private static RuntimeException nullException() {
- return Py.ValueError(
- "None is not allowed because underlying container cannot store a Java null.");
+ return Py.ValueError("None is not allowed: underlying container cannot store Java null.");
}
- /** Return Python {@code ValueError} that None is not allowed, or the {@code NullPointerException},
- * if in fact the value was not {@code None}.
+ /**
+ * Return Python {@code ValueError} that None is not allowed, or the
+ * {@code NullPointerException}, if in fact the value was not {@code None}.
*
* @param npe original exception
* @param key possibly causing the problem
@@ -122,7 +124,7 @@
*/
private static RuntimeException nullException(NullPointerException npe, Object key,
Object value) {
- return (value == Py.None || value == Py.None) ? nullException() : npe;
+ return (key == Py.None || value == Py.None) ? nullException() : npe;
}
/*
@@ -152,6 +154,7 @@
// Map doesn't extend Collection, so it needs its own version of len, iter and contains
private static final PyBuiltinMethodNarrow mapLenProxy = new MapMethod("__len__", 0) {
+
@Override
public PyObject __call__() {
return Py.java2py(asMap().size());
@@ -169,7 +172,7 @@
boolean first = true;
for (Map.Entry<Object, Object> entry : asMap().entrySet()) {
if (first) {
- first=false;
+ first = false;
} else {
repr.append(", ");
}
@@ -186,12 +189,14 @@
}
};
private static final PyBuiltinMethodNarrow mapEqProxy = new MapMethod("__eq__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return mapEq(self, other);
}
};
private static final PyBuiltinMethodNarrow mapNeProxy = new MapMethod("__ne__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
// mapEq may return null if we don't know how to compare to other.
@@ -204,36 +209,42 @@
}
};
private static final PyBuiltinMethodNarrow mapLeProxy = new MapMethod("__le__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return mapLe(self, other);
}
};
private static final PyBuiltinMethodNarrow mapGeProxy = new MapMethod("__ge__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return (mapLe(self, other).__not__()).__or__(mapEq(self, other));
}
};
private static final PyBuiltinMethodNarrow mapLtProxy = new MapMethod("__lt__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return mapLe(self, other).__and__(mapEq(self, other).__not__());
}
};
private static final PyBuiltinMethodNarrow mapGtProxy = new MapMethod("__gt__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return mapLe(self, other).__not__();
}
};
private static final PyBuiltinMethodNarrow mapIterProxy = new MapMethod("__iter__", 0) {
+
@Override
public PyObject __call__() {
return new JavaIterator(asMap().keySet());
}
};
private static final PyBuiltinMethodNarrow mapContainsProxy = new MapMethod("__contains__", 1) {
+
@Override
public PyObject __call__(PyObject obj) {
return asMap().containsKey(tojava(obj)) ? Py.True : Py.False;
@@ -264,6 +275,7 @@
};
private static final PyBuiltinMethodNarrow mapGetItemProxy = new MapMethod("__getitem__", 1) {
+
@Override
public PyObject __call__(PyObject key) {
Map<Object, Object> map = asMap();
@@ -276,6 +288,7 @@
};
private static final PyBuiltinMethodNarrow mapPutProxy = new MapMethod("__setitem__", 2) {
+
@Override
public PyObject __call__(PyObject key, PyObject value) {
try {
@@ -288,6 +301,7 @@
};
private static final PyBuiltinMethodNarrow mapRemoveProxy = new MapMethod("__delitem__", 1) {
+
@Override
public PyObject __call__(PyObject key) {
Map<Object, Object> map = asMap();
@@ -301,10 +315,12 @@
};
private static final PyBuiltinMethodNarrow mapIterItemsProxy = new MapMethod("iteritems", 0) {
+
@Override
public PyObject __call__() {
final Iterator<Map.Entry<Object, Object>> entryIterator = asMap().entrySet().iterator();
return new PyIterator() {
+
@Override
public PyObject __iternext__() {
if (entryIterator.hasNext()) {
@@ -319,10 +335,12 @@
};
private static final PyBuiltinMethodNarrow mapIterKeysProxy = new MapMethod("iterkeys", 0) {
+
@Override
public PyObject __call__() {
final Iterator<Object> keyIterator = asMap().keySet().iterator();
return new PyIterator() {
+
@Override
public PyObject __iternext__() {
if (keyIterator.hasNext()) {
@@ -337,10 +355,12 @@
};
private static final PyBuiltinMethodNarrow mapIterValuesProxy = new MapMethod("itervalues", 0) {
+
@Override
public PyObject __call__() {
final Iterator<Object> valueIterator = asMap().values().iterator();
return new PyIterator() {
+
@Override
public PyObject __iternext__() {
if (valueIterator.hasNext()) {
@@ -355,6 +375,7 @@
};
private static final PyBuiltinMethodNarrow mapHasKeyProxy = new MapMethod("has_key", 1) {
+
@Override
public PyObject __call__(PyObject key) {
return asMap().containsKey(tojava(key)) ? Py.True : Py.False;
@@ -362,6 +383,7 @@
};
private static final PyBuiltinMethodNarrow mapKeysProxy = new MapMethod("keys", 0) {
+
@Override
public PyObject __call__() {
PyList keys = new PyList();
@@ -373,6 +395,7 @@
};
private static final PyBuiltinMethod mapValuesProxy = new MapMethod("values", 0) {
+
@Override
public PyObject __call__() {
PyList values = new PyList();
@@ -383,30 +406,33 @@
}
};
- private static final PyBuiltinMethodNarrow mapSetDefaultProxy = new MapMethod("setdefault", 1, 2) {
- @Override
- public PyObject __call__(PyObject key) {
- return __call__(key, Py.None);
- }
+ private static final PyBuiltinMethodNarrow mapSetDefaultProxy =
+ new MapMethod("setdefault", 1, 2) {
+
+ @Override
+ public PyObject __call__(PyObject key) {
+ return __call__(key, Py.None);
+ }
- @Override
- public PyObject __call__(PyObject pykey, PyObject _default) {
- Map<Object, Object> map = asMap();
- Object key = tojava(pykey);
- try {
- if (map.containsKey(key)) {
- return Py.java2py(map.get(key));
- } else {
- map.put(key, tojava(_default));
- return _default;
+ @Override
+ public PyObject __call__(PyObject pykey, PyObject _default) {
+ Map<Object, Object> map = asMap();
+ Object key = tojava(pykey);
+ try {
+ if (map.containsKey(key)) {
+ return Py.java2py(map.get(key));
+ } else {
+ map.put(key, tojava(_default));
+ return _default;
+ }
+ } catch (NullPointerException npe) {
+ throw nullException(npe, key, _default);
+ }
}
- } catch (NullPointerException npe) {
- throw nullException(npe, key, _default);
- }
- }
- };
+ };
private static final PyBuiltinMethodNarrow mapPopProxy = new MapMethod("pop", 1, 2) {
+
@Override
public PyObject __call__(PyObject key) {
return __call__(key, null);
@@ -427,6 +453,7 @@
};
private static final PyBuiltinMethodNarrow mapPopItemProxy = new MapMethod("popitem", 0) {
+
@Override
public PyObject __call__() {
Map<Object, Object> map = asMap();
@@ -441,6 +468,7 @@
};
private static final PyBuiltinMethodNarrow mapItemsProxy = new MapMethod("items", 0) {
+
@Override
public PyObject __call__() {
PyList items = new PyList();
@@ -452,6 +480,7 @@
};
private static final PyBuiltinMethodNarrow mapCopyProxy = new MapMethod("copy", 0) {
+
@Override
public PyObject __call__() {
Map<Object, Object> map = asMap();
@@ -547,49 +576,56 @@
pair = PySequence.fastSequence(pair, "");
} catch (PyException pye) {
if (pye.match(Py.TypeError)) {
- throw Py.TypeError(String.format("cannot convert dictionary update sequence "
- + "element #%d to a sequence", i));
+ throw Py.TypeError(String.format(ERR_SEQ, i));
}
throw pye;
}
int n;
if ((n = pair.__len__()) != 2) {
- throw Py.ValueError(String.format("dictionary update sequence element #%d "
- + "has length %d; 2 is required", i, n));
+ throw Py.ValueError(String.format(ERR_LENGTH, i, n));
}
map.put(tojava(pair.__getitem__(0)), tojava(pair.__getitem__(1)));
}
}
+
+ private static final String ERR_SEQ =
+ "cannot convert dictionary update element #%d to a sequence";
+ private static final String ERR_LENGTH =
+ "dictionary update sequence element #%d has length %d; 2 is required";
};
- private static final PyBuiltinClassMethodNarrow mapFromKeysProxy = new MapClassMethod("fromkeys", 1, 2) {
- @Override
- public PyObject __call__(PyObject keys) {
- return __call__(keys, null);
- }
+ private static final PyBuiltinClassMethodNarrow mapFromKeysProxy =
+ new MapClassMethod("fromkeys", 1, 2) {
+
+ @Override
+ public PyObject __call__(PyObject keys) {
+ return __call__(keys, null);
+ }
- @Override
- public PyObject __call__(PyObject keys, PyObject _default) {
- Object defobj = tojava(_default);
- Class<? extends Map<Object, Object>> clazz;
- try {
- clazz = (Class<Map<Object, Object>>) asClass();
- Constructor<? extends Map<Object, Object>> ctor = clazz.getDeclaredConstructor();
- Map<Object, Object> theMap = ctor.newInstance();
- for (PyObject key : keys.asIterable()) {
- theMap.put(tojava(key), defobj);
+ @Override
+ public PyObject __call__(PyObject keys, PyObject _default) {
+ Object defobj = tojava(_default);
+ Class<? extends Map<Object, Object>> clazz;
+ try {
+ clazz = (Class<Map<Object, Object>>) asClass();
+ Constructor<? extends Map<Object, Object>> ctor =
+ clazz.getDeclaredConstructor();
+ Map<Object, Object> theMap = ctor.newInstance();
+ for (PyObject key : keys.asIterable()) {
+ theMap.put(tojava(key), defobj);
+ }
+ return Py.java2py(theMap);
+ } catch (NullPointerException npe) {
+ throw nullException();
+ } catch (ReflectiveOperationException | SecurityException
+ | IllegalArgumentException e) {
+ throw Py.JavaError(e);
+ }
}
- return Py.java2py(theMap);
- } catch (NullPointerException npe) {
- throw nullException();
- } catch (ReflectiveOperationException | SecurityException
- | IllegalArgumentException e) {
- throw Py.JavaError(e);
- }
- }
- };
+ };
static PyBuiltinMethod[] getProxyMethods() {
+ //@formatter:off
return new PyBuiltinMethod[]{
mapLenProxy,
// map IterProxy can conflict with Iterable.class;
@@ -620,6 +656,7 @@
mapFromKeysProxy // class method
};
+ //@formatter:on
}
static PyBuiltinMethod[] getPostProxyMethods() {
diff --git a/src/org/python/core/JavaProxySet.java b/src/org/python/core/JavaProxySet.java
--- a/src/org/python/core/JavaProxySet.java
+++ b/src/org/python/core/JavaProxySet.java
@@ -65,8 +65,7 @@
}
// All elements are equal so the sets are equal
return Py.True;
- }
- else {
+ } else {
// Being compared to something that is not a Python set
final Object oj = other.getJavaProxy();
if (oj instanceof Set) {
@@ -165,6 +164,7 @@
symDiff.removeAll(intersection);
return symDiff;
}
+
protected void symDiffUpdate(Collection<Object> other) {
Set<Object> selfSet = asSet();
Set<Object> intersection = new HashSet<>(selfSet);
@@ -176,6 +176,7 @@
@Untraversable
private static class SetMethodVarargs extends SetMethod {
+
protected SetMethodVarargs(String name) {
super(name, 0, -1);
}
@@ -187,22 +188,22 @@
@Override
public PyObject __call__(PyObject obj) {
- return __call__(new PyObject[]{obj});
+ return __call__(new PyObject[] {obj});
}
@Override
public PyObject __call__(PyObject obj1, PyObject obj2) {
- return __call__(new PyObject[]{obj1, obj2});
+ return __call__(new PyObject[] {obj1, obj2});
}
@Override
public PyObject __call__(PyObject obj1, PyObject obj2, PyObject obj3) {
- return __call__(new PyObject[]{obj1, obj2, obj3});
+ return __call__(new PyObject[] {obj1, obj2, obj3});
}
@Override
public PyObject __call__(PyObject obj1, PyObject obj2, PyObject obj3, PyObject obj4) {
- return __call__(new PyObject[]{obj1, obj2, obj3, obj4});
+ return __call__(new PyObject[] {obj1, obj2, obj3, obj4});
}
}
@@ -214,7 +215,7 @@
private static Collection<Object> getJavaSet(PyObject self, String op, PyObject obj) {
Collection<Object> items;
if (isPySet(obj)) {
- Set<PyObject> otherPySet = ((BaseSet)obj).getSet();
+ Set<PyObject> otherPySet = ((BaseSet) obj).getSet();
items = new ArrayList<>(otherPySet.size());
for (PyObject pyobj : otherPySet) {
items.add(pyobj.__tojava__(Object.class));
@@ -226,9 +227,9 @@
Set<Object> jSet = (Set<Object>) oj;
items = jSet;
} else {
- throw Py.TypeError(String.format(
- "unsupported operand type(s) for %s: '%.200s' and '%.200s'",
- op, self.getType().fastGetName(), obj.getType().fastGetName()));
+ throw Py.TypeError(
+ String.format("unsupported operand type(s) for %s: '%.200s' and '%.200s'",
+ op, self.getType().fastGetName(), obj.getType().fastGetName()));
}
}
return items;
@@ -244,7 +245,7 @@
items = jCollection;
} else if (oj instanceof Iterable) {
items = new HashSet<>();
- for (Object item: (Iterable) oj) {
+ for (Object item : (Iterable) oj) {
items.add(item);
}
} else {
@@ -299,18 +300,21 @@
}
private static final SetMethod cmpProxy = new SetMethod("__cmp__", 1) {
+
@Override
public PyObject __call__(PyObject value) {
throw Py.TypeError("cannot compare sets using cmp()");
}
};
private static final SetMethod eqProxy = new SetMethod("__eq__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return isEqual(other);
}
};
private static final SetMethod neProxy = new SetMethod("__ne__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
// isEqual may return null if we don't know how to compare to other.
@@ -323,6 +327,7 @@
}
};
private static final SetMethod ltProxy = new SetMethod("__lt__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return isEqual(other).__not__().__and__(Py.newBoolean(isSubset(other)));
@@ -358,6 +363,7 @@
}
private static final SetMethod gtProxy = new SetMethod("__gt__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return isEqual(other).__not__().__and__(Py.newBoolean(isSuperset(other)));
@@ -365,28 +371,34 @@
};
private static final SetMethod isDisjointProxy = new SetMethod("isdisjoint", 1) {
+
@Override
public PyObject __call__(PyObject other) {
- return Py.newBoolean(intersect(new Collection[]{getJavaCollection(other)}).size() == 0);
+ Collection[] otherJava = new Collection[] {getJavaCollection(other)};
+ return Py.newBoolean(intersect(otherJava).size() == 0);
}
};
private static final SetMethod differenceProxy = new SetMethodVarargs("difference") {
+
@Override
public PyObject __call__(PyObject[] others) {
return makePySet(difference(getCombinedJavaCollections(others)));
}
};
- private static final SetMethod differenceUpdateProxy = new SetMethodVarargs("difference_update") {
- @Override
- public PyObject __call__(PyObject[] others) {
- differenceUpdate(getCombinedJavaCollections(others));
- return Py.None;
- }
- };
+ private static final SetMethod differenceUpdateProxy =
+ new SetMethodVarargs("difference_update") {
+
+ @Override
+ public PyObject __call__(PyObject[] others) {
+ differenceUpdate(getCombinedJavaCollections(others));
+ return Py.None;
+ }
+ };
private static final SetMethod subProxy = new SetMethod("__sub__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return makePySet(difference(getJavaSet(self, "-", other)));
@@ -394,6 +406,7 @@
};
private static final SetMethod isubProxy = new SetMethod("__isub__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
differenceUpdate(getJavaSet(self, "-=", other));
@@ -402,51 +415,60 @@
};
private static final SetMethod intersectionProxy = new SetMethodVarargs("intersection") {
+
@Override
public PyObject __call__(PyObject[] others) {
return makePySet(intersect(getJavaCollections(others)));
}
};
- private static final SetMethod intersectionUpdateProxy = new SetMethodVarargs("intersection_update") {
- @Override
- public PyObject __call__(PyObject[] others) {
- intersectUpdate(getJavaCollections(others));
- return Py.None;
- }
- };
+ private static final SetMethod intersectionUpdateProxy =
+ new SetMethodVarargs("intersection_update") {
+
+ @Override
+ public PyObject __call__(PyObject[] others) {
+ intersectUpdate(getJavaCollections(others));
+ return Py.None;
+ }
+ };
private static final SetMethod andProxy = new SetMethod("__and__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
- return makePySet(intersect(new Collection[]{getJavaSet(self, "&", other)}));
+ return makePySet(intersect(new Collection[] {getJavaSet(self, "&", other)}));
}
};
private static final SetMethod iandProxy = new SetMethod("__iand__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
- intersectUpdate(new Collection[]{getJavaSet(self, "&=", other)});
+ intersectUpdate(new Collection[] {getJavaSet(self, "&=", other)});
return self;
}
};
private static final SetMethod symDiffProxy = new SetMethod("symmetric_difference", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return makePySet(symDiff(getJavaCollection(other)));
}
};
- private static final SetMethod symDiffUpdateProxy = new SetMethod("symmetric_difference_update", 1) {
- @Override
- public PyObject __call__(PyObject other) {
- symDiffUpdate(getJavaCollection(other));
- return Py.None;
- }
- };
+ private static final SetMethod symDiffUpdateProxy =
+ new SetMethod("symmetric_difference_update", 1) {
+
+ @Override
+ public PyObject __call__(PyObject other) {
+ symDiffUpdate(getJavaCollection(other));
+ return Py.None;
+ }
+ };
private static final SetMethod xorProxy = new SetMethod("__xor__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return makePySet(symDiff(getJavaSet(self, "^", other)));
@@ -454,6 +476,7 @@
};
private static final SetMethod ixorProxy = new SetMethod("__ixor__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
symDiffUpdate(getJavaSet(self, "^=", other));
@@ -462,6 +485,7 @@
};
private static final SetMethod unionProxy = new SetMethodVarargs("union") {
+
@Override
public PyObject __call__(PyObject[] others) {
return makePySet(union(getCombinedJavaCollections(others)));
@@ -469,6 +493,7 @@
};
private static final SetMethod updateProxy = new SetMethodVarargs("update") {
+
@Override
public PyObject __call__(PyObject[] others) {
update(getCombinedJavaCollections(others));
@@ -477,6 +502,7 @@
};
private static final SetMethod orProxy = new SetMethod("__or__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
return makePySet(union(getJavaSet(self, "|", other)));
@@ -484,6 +510,7 @@
};
private static final SetMethod iorProxy = new SetMethod("__ior__", 1) {
+
@Override
public PyObject __call__(PyObject other) {
update(getJavaSet(self, "|=", other));
@@ -493,9 +520,11 @@
@Untraversable
private static class CopyMethod extends SetMethod {
+
protected CopyMethod(String name) {
super(name, 0);
}
+
@Override
public PyObject __call__() {
return makePySet(asSet());
@@ -503,6 +532,7 @@
}
private static final SetMethod deepcopyOverrideProxy = new SetMethod("__deepcopy__", 1) {
+
@Override
public PyObject __call__(PyObject memo) {
Set<Object> newSet = new HashSet<>();
@@ -516,6 +546,7 @@
};
private static final SetMethod reduceProxy = new SetMethod("__reduce__", 0) {
+
@Override
public PyObject __call__() {
PyObject args = new PyTuple(new PyList(new JavaIterator(asSet())));
@@ -528,20 +559,24 @@
};
private static final SetMethod containsProxy = new SetMethod("__contains__", 1) {
+
@Override
public PyObject __call__(PyObject value) {
return Py.newBoolean(asSet().contains(value.__tojava__(Object.class)));
}
};
private static final SetMethod hashProxy = new SetMethod("__hash__", 0) {
+
// in general, we don't know if this is really true or not
@Override
public PyObject __call__(PyObject value) {
- throw Py.TypeError(String.format("unhashable type: '%.200s'", self.getType().fastGetName()));
+ throw Py.TypeError(
+ String.format("unhashable type: '%.200s'", self.getType().fastGetName()));
}
};
private static final SetMethod discardProxy = new SetMethod("discard", 1) {
+
@Override
public PyObject __call__(PyObject value) {
asSet().remove(value.__tojava__(Object.class));
@@ -549,6 +584,7 @@
}
};
private static final SetMethod popProxy = new SetMethod("pop", 0) {
+
@Override
public PyObject __call__() {
Set selfSet = asSet();
@@ -568,6 +604,7 @@
}
};
private static final SetMethod removeOverrideProxy = new SetMethod("remove", 1) {
+
@Override
public PyObject __call__(PyObject value) {
boolean removed = asSet().remove(value.__tojava__(Object.class));
@@ -579,6 +616,7 @@
};
static PyBuiltinMethod[] getProxyMethods() {
+ //@formatter:off
return new PyBuiltinMethod[]{
cmpProxy,
eqProxy,
@@ -621,13 +659,16 @@
discardProxy,
popProxy
};
+ //@formatter:on
}
static PyBuiltinMethod[] getPostProxyMethods() {
+ //@formatter:off
return new PyBuiltinMethod[]{
deepcopyOverrideProxy,
removeOverrideProxy
};
+ //@formatter:on
}
}
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list