From: <cg...@us...> - 2009-07-26 16:32:14
|
Revision: 6590 http://jython.svn.sourceforge.net/jython/?rev=6590&view=rev Author: cgroves Date: 2009-07-26 16:32:05 +0000 (Sun, 26 Jul 2009) Log Message: ----------- Add Py.javas2pys. It takes a varargs array of Object and returns a corresponding array of PyObject. Modified Paths: -------------- trunk/jython/src/com/ziclix/python/sql/DataHandler.java trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java trunk/jython/src/com/ziclix/python/sql/PyCursor.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyObject.java trunk/jython/src/org/python/jsr223/PyScriptEngine.java Modified: trunk/jython/src/com/ziclix/python/sql/DataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/DataHandler.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/com/ziclix/python/sql/DataHandler.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -507,9 +507,9 @@ public static final DataHandler getSystemDataHandler() { DataHandler dh = new DataHandler(); - for (int i = 0; i < SYSTEM_DATAHANDLERS.length; i++) { + for (String element : SYSTEM_DATAHANDLERS) { try { - Class c = Class.forName(SYSTEM_DATAHANDLERS[i]); + Class c = Class.forName(element); Constructor cons = c.getConstructor(new Class[]{DataHandler.class}); dh = (DataHandler) cons.newInstance(new Object[]{dh}); } catch (Throwable t) {} @@ -524,7 +524,7 @@ * @return a list of datahandlers */ public PyObject __chain__() { - return new PyList(new PyObject[] { Py.java2py(this) }); + return new PyList(Py.javas2pys(this)); } /** Modified: trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/com/ziclix/python/sql/Jython22DataHandler.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -446,7 +446,7 @@ * @return a list of datahandlers */ public PyObject __chain__() { - return new PyList(new PyObject[] { Py.java2py(this) }); + return new PyList(Py.javas2pys(this)); } } Modified: trunk/jython/src/com/ziclix/python/sql/PyCursor.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/PyCursor.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/com/ziclix/python/sql/PyCursor.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -750,13 +750,11 @@ SQLWarning warning = event.getWarning(); while (warning != null) { - PyObject[] warn = new PyObject[] { - // there are three parts: (reason, state, vendorCode) - Py.java2py(warning.getMessage()), - Py.java2py(warning.getSQLState()), - Py.newInteger(warning.getErrorCode()) - }; + // there are three parts: (reason, state, vendorCode) + PyObject[] warn = + Py.javas2pys(warning.getMessage(), warning.getSQLState(), warning.getErrorCode()); + // add the warning to the list ((PyList)this.warnings).append(new PyTuple(warn)); Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/org/python/core/Py.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -853,9 +853,7 @@ if (args == null || args.length == 0) { pargs = Py.EmptyObjects; } else { - pargs = new PyObject[args.length]; - for(int i=0; i<args.length; i++) - pargs[i] = Py.java2py(args[i]); + pargs = Py.javas2pys(args); } instance = pyc.__call__(pargs); instance.javaProxy = proxy; @@ -1487,6 +1485,20 @@ } /** + * Uses the PyObjectAdapter passed to {@link PySystemState#initialize} to turn + * <code>objects</code> into an array of PyObjects. + * + * @see ClassicPyObjectAdapter - default PyObjectAdapter type + */ + public static PyObject[] javas2pys(Object... objects) { + PyObject[] objs = new PyObject[objects.length]; + for (int i = 0; i < objs.length; i++) { + objs[i] = java2py(objects[i]); + } + return objs; + } + + /** * @return the ExtensiblePyObjectAdapter used by java2py. */ public static ExtensiblePyObjectAdapter getAdapter() { Modified: trunk/jython/src/org/python/core/PyObject.java =================================================================== --- trunk/jython/src/org/python/core/PyObject.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/org/python/core/PyObject.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -51,7 +51,7 @@ if (Py.BOOTSTRAP_TYPES.size() > 0) { Py.writeWarning("init", "Bootstrap types weren't encountered in bootstrapping: " + Py.BOOTSTRAP_TYPES); - + } } @@ -325,7 +325,7 @@ public PyObject __call__(PyObject args[], String keywords[]) { throw Py.TypeError(String.format("'%s' object is not callable", getType().fastGetName())); } - + public PyObject __call__(ThreadState state, PyObject args[], String keywords[]) { return __call__(args, keywords); } @@ -350,7 +350,7 @@ newArgs[0] = arg1; return __call__(newArgs, keywords); } - + public PyObject __call__(ThreadState state, PyObject arg1, PyObject args[], String keywords[]) { return __call__(arg1, args, keywords); } @@ -366,7 +366,7 @@ public PyObject __call__(PyObject args[]) { return __call__(args, Py.NoKeywords); } - + public PyObject __call__(ThreadState state, PyObject args[]) { return __call__(args); } @@ -380,7 +380,7 @@ public PyObject __call__() { return __call__(Py.EmptyObjects, Py.NoKeywords); } - + public PyObject __call__(ThreadState state) { return __call__(); } @@ -396,7 +396,7 @@ public PyObject __call__(PyObject arg0) { return __call__(new PyObject[] { arg0 }, Py.NoKeywords); } - + public PyObject __call__(ThreadState state, PyObject arg0) { return __call__(arg0); } @@ -431,7 +431,7 @@ public PyObject __call__(PyObject arg0, PyObject arg1, PyObject arg2) { return __call__(new PyObject[] { arg0, arg1, arg2 }, Py.NoKeywords); } - + public PyObject __call__(ThreadState state, PyObject arg0, PyObject arg1, PyObject arg2) { return __call__(arg0, arg1, arg2); } @@ -452,7 +452,7 @@ new PyObject[] { arg0, arg1, arg2, arg3 }, Py.NoKeywords); } - + public PyObject __call__(ThreadState state, PyObject arg0, PyObject arg1, PyObject arg2, PyObject arg3) { return __call__(arg0, arg1, arg2, arg3); } @@ -3496,12 +3496,8 @@ * A convenience function for PyProxys. */ public PyObject _jcallexc(Object[] args) throws Throwable { - PyObject[] pargs = new PyObject[args.length]; try { - for (int i = 0; i < args.length; i++) { - pargs[i] = Py.java2py(args[i]); - } - return __call__(pargs); + return __call__(Py.javas2pys(args)); } catch (PyException e) { if (e.value.getJavaProxy() != null) { Object t = e.value.__tojava__(Throwable.class); Modified: trunk/jython/src/org/python/jsr223/PyScriptEngine.java =================================================================== --- trunk/jython/src/org/python/jsr223/PyScriptEngine.java 2009-07-26 15:23:38 UTC (rev 6589) +++ trunk/jython/src/org/python/jsr223/PyScriptEngine.java 2009-07-26 16:32:05 UTC (rev 6590) @@ -66,43 +66,49 @@ private PyCode compileScript(String script, ScriptContext context) throws ScriptException { try { String filename = (String) context.getAttribute(ScriptEngine.FILENAME); - if (filename == null) + if (filename == null) { return interp.compile(script); - else + } else { return interp.compile(script, filename); + } } catch (PyException pye) { throw scriptException(pye); } } - + private PyCode compileScript(Reader reader, ScriptContext context) throws ScriptException { try { String filename = (String) context.getAttribute(ScriptEngine.FILENAME); - if (filename == null) + if (filename == null) { return interp.compile(reader); - else + } else { return interp.compile(reader, filename); + } } catch (PyException pye) { throw scriptException(pye); } } - public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { + public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, + NoSuchMethodException { try { - if (!(thiz instanceof PyObject)) + if (!(thiz instanceof PyObject)) { thiz = Py.java2py(thiz); - return ((PyObject) thiz).invoke(name, java2py(args)).__tojava__(Object.class); + } + return ((PyObject) thiz).invoke(name, Py.javas2pys(args)).__tojava__(Object.class); } catch (PyException pye) { return throwInvokeException(pye, name); } } - public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { + public Object invokeFunction(String name, Object... args) throws ScriptException, + NoSuchMethodException { try { PyObject function = interp.get(name); - if (function == null) + if (function == null) { throw new NoSuchMethodException(name); - return function.__call__(java2py(args)).__tojava__(Object.class); + } + return function.__call__(Py.javas2pys(args)).__tojava__(Object.class); } catch (PyException pye) { return throwInvokeException(pye, name); } @@ -113,23 +119,28 @@ } public <T> T getInterface(Object obj, Class<T> clazz) { - if (obj == null) + if (obj == null) { throw new IllegalArgumentException("object expected"); - if (clazz == null || !clazz.isInterface()) + } + if (clazz == null || !clazz.isInterface()) { throw new IllegalArgumentException("interface expected"); - final Object thiz = Py.java2py(obj); - return (T) Proxy.newProxyInstance( + } + final PyObject thiz = Py.java2py(obj); + @SuppressWarnings("unchecked") + T proxy = (T) Proxy.newProxyInstance( 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); + PyObject result = thiz.invoke(method.getName(), Py.javas2pys(args)); + return result.__tojava__(Object.class); } catch (PyException pye) { return throwInvokeException(pye, method.getName()); } } }); + return proxy; } private static Object throwInvokeException(PyException pye, String methodName) @@ -162,11 +173,11 @@ offset == null ? 0 : offset.asInt()); } else if (tb != null) { String filename; - if (tb.tb_frame == null || tb.tb_frame.f_code == null) + if (tb.tb_frame == null || tb.tb_frame.f_code == null) { filename = null; - else + } else { filename = tb.tb_frame.f_code.co_filename; - + } se = new ScriptException( Py.formatException(type, value), filename, @@ -182,14 +193,6 @@ return se; } - private static PyObject[] java2py(Object[] args) { - PyObject wrapped[] = new PyObject[args.length]; - for (int i = 0; i < args.length; i++) { - wrapped[i] = Py.java2py(args[i]); - } - return wrapped; - } - private class PyCompiledScript extends CompiledScript { private PyCode code; @@ -197,10 +200,12 @@ this.code = code; } + @Override public ScriptEngine getEngine() { return PyScriptEngine.this; } + @Override public Object eval(ScriptContext ctx) throws ScriptException { return PyScriptEngine.this.eval(code, ctx); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |