forked from jython/jython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some tests for overriding attribute lookup and adding mapping met…
…hods to wrapped Java types.
- Loading branch information
Showing
5 changed files
with
165 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.python.tests; | ||
|
||
import java.util.Map; | ||
|
||
import org.python.core.Py; | ||
import org.python.core.PyBuiltinMethod; | ||
import org.python.core.PyBuiltinMethodNarrow; | ||
import org.python.core.PyException; | ||
import org.python.core.PyObject; | ||
import org.python.core.PyType; | ||
import org.python.util.Generic; | ||
|
||
|
||
public class CustomizableMapHolder { | ||
|
||
public Map<String, Integer> held = Generic.map(); | ||
|
||
{ | ||
held.put("initial", 7); | ||
} | ||
|
||
public static void clearAdditions() { | ||
PyObject dict = PyType.fromClass(CustomizableMapHolder.class).fastGetDict(); | ||
for (String name : new String[] {"__getitem__", "__setitem__", "__getattribute__"}) { | ||
if (dict.__finditem__(name) != null) { | ||
dict.__delitem__(name); | ||
} | ||
} | ||
} | ||
|
||
public static void addGetitem() { | ||
PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__getitem__", 1) { | ||
@Override | ||
public PyObject __call__(PyObject arg) { | ||
CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); | ||
String key = Py.tojava(arg, String.class); | ||
return Py.java2py(inst.held.get(key)); | ||
} | ||
}; | ||
PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); | ||
} | ||
|
||
public static void addSetitem() { | ||
PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__setitem__", 2) { | ||
@Override | ||
public PyObject __call__(PyObject arg1, PyObject arg2) { | ||
CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); | ||
String key = Py.tojava(arg1, String.class); | ||
Integer val = Py.tojava(arg2, Integer.class); | ||
inst.held.put(key, val); | ||
return Py.None; | ||
} | ||
}; | ||
PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); | ||
} | ||
|
||
public static void addGetattribute() { | ||
final PyObject objectGetattribute = PyObject.TYPE.__getattr__("__getattribute__"); | ||
PyBuiltinMethod meth = new PyBuiltinMethodNarrow("__getattribute__", 1) { | ||
@Override | ||
public PyObject __call__(PyObject name) { | ||
try { | ||
return objectGetattribute.__call__(self, name); | ||
} catch (PyException pye) { | ||
if (!Py.matchException(pye, Py.AttributeError)) { | ||
throw pye; | ||
} | ||
} | ||
CustomizableMapHolder inst = Py.tojava(self, CustomizableMapHolder.class); | ||
return Py.java2py(inst.held.get(Py.tojava(name, String.class))); | ||
} | ||
}; | ||
PyType.fromClass(CustomizableMapHolder.class).addMethod(meth); | ||
} | ||
} |