From: <nr...@us...> - 2009-07-23 03:12:53
|
Revision: 6565 http://jython.svn.sourceforge.net/jython/?rev=6565&view=rev Author: nriley Date: 2009-07-23 03:12:52 +0000 (Thu, 23 Jul 2009) Log Message: ----------- JSR 223: Invocable implementation. Modified Paths: -------------- branches/jsr223/src/org/python/jsr223/PyScriptEngine.java branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java Modified: branches/jsr223/src/org/python/jsr223/PyScriptEngine.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngine.java 2009-07-23 03:12:10 UTC (rev 6564) +++ branches/jsr223/src/org/python/jsr223/PyScriptEngine.java 2009-07-23 03:12:52 UTC (rev 6565) @@ -21,10 +21,12 @@ private final PythonInterpreter interp; private final ScriptEngineFactory factory; + private final PyModule module; PyScriptEngine(ScriptEngineFactory factory) { this.factory = factory; interp = new PythonInterpreter(new PyScriptEngineScope(this, context)); + module = (PyModule)Py.getSystemState().modules.__finditem__("__main__"); } public Object eval(String script, ScriptContext context) throws ScriptException { @@ -85,10 +87,9 @@ public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { try { - if (thiz instanceof PyObject) { - return ((PyObject) thiz).invoke(name, java2py(args)).__tojava__(Object.class); - } - throw new NoSuchMethodException(name); + if (!(thiz instanceof PyObject)) + thiz = Py.java2py(thiz); + return ((PyObject) thiz).invoke(name, java2py(args)).__tojava__(Object.class); } catch (PyException pye) { return throwInvokeException(pye, name); } @@ -96,30 +97,37 @@ public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { try { - return interp.get(name).__call__(java2py(args)).__tojava__(Object.class); + PyObject function = interp.get(name); + if (function == null) + throw new NoSuchMethodException(name); + return function.__call__(java2py(args)).__tojava__(Object.class); } catch (PyException pye) { return throwInvokeException(pye, name); } } public <T> T getInterface(Class<T> clazz) { - return getInterface(null, clazz); + return getInterface(module, clazz); } - public <T> T getInterface(Object thiz, Class<T> clazz) { + public <T> T getInterface(Object obj, Class<T> clazz) { + if (obj == null) + throw new IllegalArgumentException("object expected"); + if (clazz == null || !clazz.isInterface()) + throw new IllegalArgumentException("interface expected"); + final Object thiz = Py.java2py(obj); return (T) Proxy.newProxyInstance( - clazz.getClassLoader(), - new Class[] { clazz }, - new InvocationHandler() { - - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - try { - return ((PyObject) proxy).invoke(method.getName(), java2py(args)).__tojava__(Object.class); - } catch (PyException pye) { - return throwInvokeException(pye, method.getName()); + clazz.getClassLoader(), + new Class[] { clazz }, + new InvocationHandler() { + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + try { + return ((PyObject) thiz).invoke(method.getName(), java2py(args)).__tojava__(Object.class); + } catch (PyException pye) { + return throwInvokeException(pye, method.getName()); + } } - } - }); + }); } private static Object throwInvokeException(PyException pye, String methodName) Modified: branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java =================================================================== --- branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java 2009-07-23 03:12:10 UTC (rev 6564) +++ branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java 2009-07-23 03:12:52 UTC (rev 6565) @@ -1,14 +1,17 @@ package org.python.jsr223; +import java.io.IOException; import java.io.StringReader; import javax.script.Compilable; import javax.script.CompiledScript; +import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; import junit.framework.TestCase; +import org.python.core.PyString; public class ScriptEngineTest extends TestCase { @@ -98,4 +101,56 @@ assertNull(pythonEngine.eval("del x")); assertNull(pythonEngine.get("x")); } + + public void testInvoke() throws ScriptException, NoSuchMethodException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + Invocable invocableEngine = (Invocable)pythonEngine; + + assertNull(pythonEngine.eval("def f(x): return abs(x)")); + assertEquals(Integer.valueOf(5), invocableEngine.invokeFunction("f", Integer.valueOf(-5))); + assertEquals("spam", invocableEngine.invokeMethod(new PyString(" spam "), "strip")); + assertEquals("spam", invocableEngine.invokeMethod(" spam ", "strip")); + } + + public void testInvokeFunctionNoSuchMethod() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + Invocable invocableEngine = (Invocable)manager.getEngineByName("python"); + + try { + invocableEngine.invokeFunction("undefined"); + } catch (NoSuchMethodException e) { + return; + } + assertTrue("Expected a NoSuchMethodException", false); + } + + public void testInvokeMethodNoSuchMethod() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + Invocable invocableEngine = (Invocable)manager.getEngineByName("python"); + + try { + invocableEngine.invokeMethod("eggs", "undefined"); + } catch (NoSuchMethodException e) { + return; + } + assertTrue("Expected a NoSuchMethodException", false); + } + + public void testGetInterface() throws ScriptException, IOException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + Invocable invocableEngine = (Invocable)pythonEngine; + + assertNull(pythonEngine.eval("def read(cb): return 1")); + Readable readable = invocableEngine.getInterface(Readable.class); + assertEquals(1, readable.read(null)); + + assertNull(pythonEngine.eval( + "class C(object):\n" + + " def read(self, cb): return 2\n" + + "c = C()")); + readable = invocableEngine.getInterface(pythonEngine.get("c"), Readable.class); + assertEquals(2, readable.read(null)); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |