From: <nr...@us...> - 2009-07-23 05:15:01
|
Revision: 6567 http://jython.svn.sourceforge.net/jython/?rev=6567&view=rev Author: nriley Date: 2009-07-23 05:14:58 +0000 (Thu, 23 Jul 2009) Log Message: ----------- Merged revisions 6285,6541,6551,6559-6565 via svnmerge from https://jython.svn.sourceforge.net/svnroot/jython/branches/jsr223 ........ r6285 | zyasoft | 2009-05-02 01:13:13 -0500 (Sat, 02 May 2009) | 2 lines Some initial code, just to get it going. ........ r6541 | nriley | 2009-07-17 00:27:51 -0500 (Fri, 17 Jul 2009) | 1 line JSR 223 support for compile and eval of Strings. ........ r6551 | nriley | 2009-07-20 12:35:01 -0500 (Mon, 20 Jul 2009) | 1 line JSR 223: test Python exceptions as well as syntax errors. ........ r6559 | nriley | 2009-07-22 16:15:39 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: support for compile and eval with Readers. ........ r6560 | nriley | 2009-07-22 18:51:47 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: fix comments in ParserFacade. ........ r6561 | nriley | 2009-07-22 18:52:00 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: set PySystemState for separate compile/execute. ........ r6562 | nriley | 2009-07-22 18:52:08 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: standardize argument naming. ........ r6563 | nriley | 2009-07-22 18:52:19 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: bindings support. ........ r6564 | nriley | 2009-07-22 22:12:10 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: getInterface support; clean up and factor exception transformation. ........ r6565 | nriley | 2009-07-22 22:12:52 -0500 (Wed, 22 Jul 2009) | 1 line JSR 223: Invocable implementation. ........ Modified Paths: -------------- trunk/jython/CoreExposed.includes trunk/jython/build.xml trunk/jython/src/org/python/core/ParserFacade.java trunk/jython/src/org/python/util/PythonInterpreter.java Added Paths: ----------- trunk/jython/src/META-INF/ trunk/jython/src/META-INF/services/ trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory trunk/jython/src/org/python/jsr223/ trunk/jython/src/org/python/jsr223/PyScriptEngine.java trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java trunk/jython/tests/java/org/python/jsr223/ trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java Removed Paths: ------------- trunk/jython/src/META-INF/services/ trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory trunk/jython/src/org/python/jsr223/PyScriptEngine.java trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java Property Changed: ---------------- trunk/jython/ trunk/jython/tests/java/org/python/tests/RedundantInterfaceDeclarations.java Property changes on: trunk/jython ___________________________________________________________________ Modified: svnmerge-integrated - /branches/jsr223:1-6284 /branches/pbcvm:1-6045 + /branches/jsr223:1-6566 /branches/pbcvm:1-6045 Modified: svn:mergeinfo - /branches/newstyle-java-types:5564-5663,5666-5729 + /branches/jsr223:6285-6565 /branches/newstyle-java-types:5564-5663,5666-5729 Modified: trunk/jython/CoreExposed.includes =================================================================== --- trunk/jython/CoreExposed.includes 2009-07-23 03:26:56 UTC (rev 6566) +++ trunk/jython/CoreExposed.includes 2009-07-23 05:14:58 UTC (rev 6567) @@ -43,6 +43,7 @@ org/python/core/PyXRange.class org/python/modules/PyStruct.class org/python/modules/PyTeeIterator.class +org/python/jsr223/PyScriptEngineScope.class org/python/modules/_codecs$EncodingMap.class org/python/modules/_collections/PyDefaultDict.class org/python/modules/_collections/PyDeque.class Modified: trunk/jython/build.xml =================================================================== --- trunk/jython/build.xml 2009-07-23 03:26:56 UTC (rev 6566) +++ trunk/jython/build.xml 2009-07-23 05:14:58 UTC (rev 6567) @@ -506,6 +506,10 @@ </copy> <!-- grammar must now be up to date --> <property name="antlr.notneeded" value="true" /> + + <copy todir="${compile.dir}/META-INF/services"> + <fileset dir="${source.dir}/META-INF/services" /> + </copy> </target> <!-- Deleted: trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory =================================================================== --- branches/jsr223/src/META-INF/services/javax.script.ScriptEngineFactory 2009-07-23 03:12:52 UTC (rev 6565) +++ trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory 2009-07-23 05:14:58 UTC (rev 6567) @@ -1 +0,0 @@ -org.python.jsr223.PyScriptEngineFactory Copied: trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory (from rev 6565, branches/jsr223/src/META-INF/services/javax.script.ScriptEngineFactory) =================================================================== --- trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory (rev 0) +++ trunk/jython/src/META-INF/services/javax.script.ScriptEngineFactory 2009-07-23 05:14:58 UTC (rev 6567) @@ -0,0 +1 @@ +org.python.jsr223.PyScriptEngineFactory Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2009-07-23 03:26:56 UTC (rev 6566) +++ trunk/jython/src/org/python/core/ParserFacade.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -10,6 +10,7 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.StringReader; import java.io.Writer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; @@ -108,6 +109,34 @@ } /** + * Parse Python source as either an expression (if possible) or module. + * + * Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish + * between scripts which return values and those which do not, nor do they make the + * corresponding distinction between evaluating or executing objects." (SCR.4.2.1) + */ + public static mod parseExpressionOrModule(Reader reader, + String filename, + CompilerFlags cflags) { + ExpectedEncodingBufferedReader bufReader = null; + try { + bufReader = prepBufReader(reader, cflags, filename); + // first, try parsing as an expression + return parse(bufReader, CompileMode.eval, filename, cflags ); + } catch (Throwable t) { + try { + // then, try parsing as a module + bufReader.reset(); + return parse(bufReader, CompileMode.exec, filename, cflags); + } catch (Throwable tt) { + throw fixParseError(bufReader, tt, filename); + } + } finally { + close(bufReader); + } + } + + /** * Internal parser entry point. * * Users of this method should call fixParseError on any Throwable thrown @@ -130,6 +159,21 @@ } } + public static mod parse(Reader reader, + CompileMode kind, + String filename, + CompilerFlags cflags) { + ExpectedEncodingBufferedReader bufReader = null; + try { + bufReader = prepBufReader(reader, cflags, filename); + return parse(bufReader, kind, filename, cflags ); + } catch (Throwable t) { + throw fixParseError(bufReader, t, filename); + } finally { + close(bufReader); + } + } + public static mod parse(InputStream stream, CompileMode kind, String filename, @@ -222,6 +266,22 @@ } } + private static ExpectedEncodingBufferedReader prepBufReader(Reader reader, + CompilerFlags cflags, + String filename) + throws IOException { + cflags.source_is_utf8 = true; + cflags.encoding = "utf-8"; + + BufferedReader bufferedReader = new BufferedReader(reader); + bufferedReader.mark(MARK_LIMIT); + if (findEncoding(bufferedReader) != null) + throw new ParseException("encoding declaration in Unicode string"); + bufferedReader.reset(); + + return new ExpectedEncodingBufferedReader(bufferedReader, null); + } + private static ExpectedEncodingBufferedReader prepBufReader(InputStream input, CompilerFlags cflags, String filename, @@ -289,20 +349,10 @@ CompilerFlags cflags, String filename) throws IOException { - byte[] stringBytes; - if (cflags.source_is_utf8) { - // Passed unicode, re-encode the String to raw bytes - // NOTE: This could be more efficient if we duplicate - // prepBufReader/adjustForBOM/readEncoding to work on Readers, instead of - // encoding - ByteArrayOutputStream out = new ByteArrayOutputStream(); - Writer w = new OutputStreamWriter(out, "utf-8"); - w.write(string); - w.close(); - stringBytes = out.toByteArray(); - } else { - stringBytes = StringUtil.toBytes(string); - } + if (cflags.source_is_utf8) + return prepBufReader(new StringReader(string), cflags, filename); + + byte[] stringBytes = StringUtil.toBytes(string); return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true, false); } Deleted: trunk/jython/src/org/python/jsr223/PyScriptEngine.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngine.java 2009-07-23 03:12:52 UTC (rev 6565) +++ trunk/jython/src/org/python/jsr223/PyScriptEngine.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -1,210 +0,0 @@ -package org.python.jsr223; - -import java.lang.reflect.Method; -import org.python.core.*; -import java.io.Reader; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; -import javax.script.AbstractScriptEngine; -import javax.script.Bindings; -import javax.script.Compilable; -import javax.script.CompiledScript; -import javax.script.Invocable; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineFactory; -import javax.script.ScriptException; -import javax.script.SimpleBindings; -import org.python.util.PythonInterpreter; - -public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { - - 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 { - return eval(compileScript(script, context)); - } - - private Object eval(PyCode code) throws ScriptException { - try { - return interp.eval(code).__tojava__(Object.class); - } catch (PyException pye) { - throw scriptException(pye); - } - } - - public Object eval(Reader reader, ScriptContext context) throws ScriptException { - return eval(compileScript(reader, context)); - } - - public Bindings createBindings() { - return new SimpleBindings(); - } - - public ScriptEngineFactory getFactory() { - return factory; - } - - public CompiledScript compile(String script) throws ScriptException { - return new PyCompiledScript(compileScript(script, context)); - } - - public CompiledScript compile(Reader reader) throws ScriptException { - return new PyCompiledScript(compileScript(reader, context)); - } - - private PyCode compileScript(String script, ScriptContext context) throws ScriptException { - try { - String filename = (String) context.getAttribute(ScriptEngine.FILENAME); - if (filename == null) - return interp.compile(script); - 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) - return interp.compile(reader); - else - return interp.compile(reader, filename); - } catch (PyException pye) { - throw scriptException(pye); - } - } - - public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { - try { - 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); - } - } - - public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { - try { - 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(module, 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) 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) - throws ScriptException, NoSuchMethodException { - if (Py.matchException(pye, Py.AttributeError)) { - throw new NoSuchMethodException(methodName); - } - throw scriptException(pye); - } - - private static ScriptException scriptException(PyException pye) { - ScriptException se = null; - try { - pye.normalize(); - - PyObject type = pye.type; - PyObject value = pye.value; - PyTraceback tb = pye.traceback; - - if (__builtin__.isinstance(value, Py.SyntaxError)) { - PyObject filename = value.__findattr__("filename"); - PyObject lineno = value.__findattr__("lineno"); - PyObject offset = value.__findattr__("offset"); - value = value.__findattr__("msg"); - - se = new ScriptException( - Py.formatException(type, value), - filename == null ? "<script>" : filename.toString(), - lineno == null ? 0 : lineno.asInt(), - offset == null ? 0 : offset.asInt()); - } else if (tb != null) { - String filename; - if (tb.tb_frame == null || tb.tb_frame.f_code == null) - filename = null; - else - filename = tb.tb_frame.f_code.co_filename; - - se = new ScriptException( - Py.formatException(type, value), - filename, - tb.tb_lineno); - } else { - se = new ScriptException(Py.formatException(type, value)); - } - se.initCause(pye); - return se; - } catch (Exception ee) { - se = new ScriptException(pye); - } - 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; - private PySystemState systemState; - - PyCompiledScript(PyCode code) { - this.code = code; - this.systemState = Py.getSystemState(); - } - - public ScriptEngine getEngine() { - return PyScriptEngine.this; - } - - public Object eval(ScriptContext ctx) throws ScriptException { - // can't read filename from context at this point - Py.setSystemState(systemState); - return PyScriptEngine.this.eval(code); - } - } -} Copied: trunk/jython/src/org/python/jsr223/PyScriptEngine.java (from rev 6565, branches/jsr223/src/org/python/jsr223/PyScriptEngine.java) =================================================================== --- trunk/jython/src/org/python/jsr223/PyScriptEngine.java (rev 0) +++ trunk/jython/src/org/python/jsr223/PyScriptEngine.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -0,0 +1,210 @@ +package org.python.jsr223; + +import java.lang.reflect.Method; +import org.python.core.*; +import java.io.Reader; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import javax.script.AbstractScriptEngine; +import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.CompiledScript; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptException; +import javax.script.SimpleBindings; +import org.python.util.PythonInterpreter; + +public class PyScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { + + 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 { + return eval(compileScript(script, context)); + } + + private Object eval(PyCode code) throws ScriptException { + try { + return interp.eval(code).__tojava__(Object.class); + } catch (PyException pye) { + throw scriptException(pye); + } + } + + public Object eval(Reader reader, ScriptContext context) throws ScriptException { + return eval(compileScript(reader, context)); + } + + public Bindings createBindings() { + return new SimpleBindings(); + } + + public ScriptEngineFactory getFactory() { + return factory; + } + + public CompiledScript compile(String script) throws ScriptException { + return new PyCompiledScript(compileScript(script, context)); + } + + public CompiledScript compile(Reader reader) throws ScriptException { + return new PyCompiledScript(compileScript(reader, context)); + } + + private PyCode compileScript(String script, ScriptContext context) throws ScriptException { + try { + String filename = (String) context.getAttribute(ScriptEngine.FILENAME); + if (filename == null) + return interp.compile(script); + 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) + return interp.compile(reader); + else + return interp.compile(reader, filename); + } catch (PyException pye) { + throw scriptException(pye); + } + } + + public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { + try { + 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); + } + } + + public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { + try { + 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(module, 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) 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) + throws ScriptException, NoSuchMethodException { + if (pye.match(Py.AttributeError)) { + throw new NoSuchMethodException(methodName); + } + throw scriptException(pye); + } + + private static ScriptException scriptException(PyException pye) { + ScriptException se = null; + try { + pye.normalize(); + + PyObject type = pye.type; + PyObject value = pye.value; + PyTraceback tb = pye.traceback; + + if (__builtin__.isinstance(value, Py.SyntaxError)) { + PyObject filename = value.__findattr__("filename"); + PyObject lineno = value.__findattr__("lineno"); + PyObject offset = value.__findattr__("offset"); + value = value.__findattr__("msg"); + + se = new ScriptException( + Py.formatException(type, value), + filename == null ? "<script>" : filename.toString(), + lineno == null ? 0 : lineno.asInt(), + offset == null ? 0 : offset.asInt()); + } else if (tb != null) { + String filename; + if (tb.tb_frame == null || tb.tb_frame.f_code == null) + filename = null; + else + filename = tb.tb_frame.f_code.co_filename; + + se = new ScriptException( + Py.formatException(type, value), + filename, + tb.tb_lineno); + } else { + se = new ScriptException(Py.formatException(type, value)); + } + se.initCause(pye); + return se; + } catch (Exception ee) { + se = new ScriptException(pye); + } + 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; + private PySystemState systemState; + + PyCompiledScript(PyCode code) { + this.code = code; + this.systemState = Py.getSystemState(); + } + + public ScriptEngine getEngine() { + return PyScriptEngine.this; + } + + public Object eval(ScriptContext ctx) throws ScriptException { + // can't read filename from context at this point + Py.setSystemState(systemState); + return PyScriptEngine.this.eval(code); + } + } +} Deleted: trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java 2009-07-23 03:12:52 UTC (rev 6565) +++ trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -1,95 +0,0 @@ -package org.python.jsr223; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineFactory; -import org.python.Version; -import org.python.core.Py; - -public class PyScriptEngineFactory implements ScriptEngineFactory { - - public String getEngineName() { - return "jython"; - } - - public String getEngineVersion() { - return String.format("%s.%s.%s", Version.PY_MAJOR_VERSION, Version.PY_MINOR_VERSION, Version.PY_MICRO_VERSION); - } - - public List<String> getExtensions() { - return Collections.unmodifiableList(Arrays.asList("py")); - } - - public String getLanguageName() { - return "python"; - } - - public String getLanguageVersion() { - return "2.5.0"; - } - - public Object getParameter(String key) { - if (key.equals(ScriptEngine.ENGINE)) { - return getEngineName(); - } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { - return getEngineVersion(); - } else if (key.equals(ScriptEngine.NAME)) { - return getEngineName(); - } else if (key.equals(ScriptEngine.LANGUAGE)) { - return getLanguageName(); - } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { - return getLanguageVersion(); - } else if (key.equals("THREADING")) { - return "MULTITHREADED"; - } else { - return null; - } - - } - - public String getMethodCallSyntax(String obj, String m, String... args) { - StringBuilder buffer = new StringBuilder(); - buffer.append(String.format("%s.%s(", obj, m)); - int i = args.length; - for (String arg : args) { - buffer.append(arg); - if (i-- > 0) { - buffer.append(", "); - } - } - buffer.append(")"); - return buffer.toString(); - } - - // presumably a unicode string - public String getOutputStatement(String toDisplay) { - StringBuilder buffer = new StringBuilder(toDisplay.length() + 8); - buffer.append("print "); - buffer.append(Py.newUnicode(toDisplay).__repr__()); - return buffer.toString(); - } - - public String getProgram(String... statements) { - StringBuilder buffer = new StringBuilder(); - for (String statement : statements) { - buffer.append(statement); - buffer.append("\n"); - } - return buffer.toString(); - } - - public ScriptEngine getScriptEngine() { - return new PyScriptEngine(this); - } - - public List<String> getMimeTypes() { - return Collections.EMPTY_LIST; - } - - public List<String> getNames() { - return Collections.unmodifiableList(Arrays.asList("python", "jython")); - } - -} Copied: trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java (from rev 6565, branches/jsr223/src/org/python/jsr223/PyScriptEngineFactory.java) =================================================================== --- trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java (rev 0) +++ trunk/jython/src/org/python/jsr223/PyScriptEngineFactory.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -0,0 +1,95 @@ +package org.python.jsr223; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import org.python.Version; +import org.python.core.Py; + +public class PyScriptEngineFactory implements ScriptEngineFactory { + + public String getEngineName() { + return "jython"; + } + + public String getEngineVersion() { + return String.format("%s.%s.%s", Version.PY_MAJOR_VERSION, Version.PY_MINOR_VERSION, Version.PY_MICRO_VERSION); + } + + public List<String> getExtensions() { + return Collections.unmodifiableList(Arrays.asList("py")); + } + + public String getLanguageName() { + return "python"; + } + + public String getLanguageVersion() { + return "2.5.0"; + } + + public Object getParameter(String key) { + if (key.equals(ScriptEngine.ENGINE)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { + return getEngineVersion(); + } else if (key.equals(ScriptEngine.NAME)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.LANGUAGE)) { + return getLanguageName(); + } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { + return getLanguageVersion(); + } else if (key.equals("THREADING")) { + return "MULTITHREADED"; + } else { + return null; + } + + } + + public String getMethodCallSyntax(String obj, String m, String... args) { + StringBuilder buffer = new StringBuilder(); + buffer.append(String.format("%s.%s(", obj, m)); + int i = args.length; + for (String arg : args) { + buffer.append(arg); + if (i-- > 0) { + buffer.append(", "); + } + } + buffer.append(")"); + return buffer.toString(); + } + + // presumably a unicode string + public String getOutputStatement(String toDisplay) { + StringBuilder buffer = new StringBuilder(toDisplay.length() + 8); + buffer.append("print "); + buffer.append(Py.newUnicode(toDisplay).__repr__()); + return buffer.toString(); + } + + public String getProgram(String... statements) { + StringBuilder buffer = new StringBuilder(); + for (String statement : statements) { + buffer.append(statement); + buffer.append("\n"); + } + return buffer.toString(); + } + + public ScriptEngine getScriptEngine() { + return new PyScriptEngine(this); + } + + public List<String> getMimeTypes() { + return Collections.EMPTY_LIST; + } + + public List<String> getNames() { + return Collections.unmodifiableList(Arrays.asList("python", "jython")); + } + +} Deleted: trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java =================================================================== --- branches/jsr223/src/org/python/jsr223/PyScriptEngineScope.java 2009-07-23 03:12:52 UTC (rev 6565) +++ trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -1,101 +0,0 @@ -package org.python.jsr223; - -import java.util.List; -import javax.script.Bindings; -import javax.script.ScriptContext; -import javax.script.ScriptEngine; -import org.python.core.Py; -import org.python.core.PyList; -import org.python.core.PyObject; -import org.python.core.PyString; -import org.python.core.PyType; -import org.python.expose.ExposedType; -import org.python.expose.ExposedGet; -import org.python.expose.ExposedMethod; - -@ExposedType(name = "scope", isBaseType = false) -public final class PyScriptEngineScope extends PyObject { - public static final PyType TYPE = PyType.fromClass(PyScriptEngineScope.class); - - private final ScriptContext context; - private final ScriptEngine engine; - - PyScriptEngineScope(ScriptEngine engine, ScriptContext context) { - this.context = context; - this.engine = engine; - } - - @ExposedGet(name = "context") - public PyObject pyGetContext() { - return Py.java2py(context); - } - - @ExposedGet(name = "engine") - public PyObject pyGetEngine() { - return Py.java2py(engine); - } - - @ExposedMethod - public PyObject scope_keys() { - PyList members = new PyList(); - synchronized (context) { - List<Integer> scopes = context.getScopes(); - for (int scope : scopes) { - Bindings bindings = context.getBindings(scope); - if (bindings == null) - continue; - for (String key : bindings.keySet()) - members.append(new PyString(key)); - } - } - members.sort(); - return members; - } - - // Not necessary for functionality; present to satisfy __builtin__.PyMapping_check - @ExposedMethod - public PyObject __getitem__(PyObject key) { - return super.__getitem__(key); - } - - public PyObject __finditem__(PyObject key) { - return __finditem__(key.asString()); - } - - public PyObject __finditem__(String key) { - synchronized (context) { - int scope = context.getAttributesScope(key); - if (scope == -1) - return null; - return Py.java2py(context.getAttribute(key, scope)); - } - } - - @ExposedMethod - public void __setitem__(PyObject key, PyObject value) { - __setitem__(key.asString(), value); - } - - public void __setitem__(String key, PyObject value) { - synchronized (context) { - int scope = context.getAttributesScope(key); - if (scope == -1) - scope = ScriptContext.ENGINE_SCOPE; - context.setAttribute(key, value.__tojava__(Object.class), scope); - } - } - - @ExposedMethod - public void __delitem__(PyObject key) { - __delitem__(key.asString()); - } - - public void __delitem__(String key) { - synchronized (context) { - int scope = context.getAttributesScope(key); - if (scope == -1) - throw Py.KeyError(key); - context.removeAttribute(key, scope); - } - } -} Copied: trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java (from rev 6565, branches/jsr223/src/org/python/jsr223/PyScriptEngineScope.java) =================================================================== --- trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java (rev 0) +++ trunk/jython/src/org/python/jsr223/PyScriptEngineScope.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -0,0 +1,101 @@ +package org.python.jsr223; + +import java.util.List; +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import org.python.core.Py; +import org.python.core.PyList; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyType; +import org.python.expose.ExposedType; +import org.python.expose.ExposedGet; +import org.python.expose.ExposedMethod; + +@ExposedType(name = "scope", isBaseType = false) +public final class PyScriptEngineScope extends PyObject { + public static final PyType TYPE = PyType.fromClass(PyScriptEngineScope.class); + + private final ScriptContext context; + private final ScriptEngine engine; + + PyScriptEngineScope(ScriptEngine engine, ScriptContext context) { + this.context = context; + this.engine = engine; + } + + @ExposedGet(name = "context") + public PyObject pyGetContext() { + return Py.java2py(context); + } + + @ExposedGet(name = "engine") + public PyObject pyGetEngine() { + return Py.java2py(engine); + } + + @ExposedMethod + public PyObject scope_keys() { + PyList members = new PyList(); + synchronized (context) { + List<Integer> scopes = context.getScopes(); + for (int scope : scopes) { + Bindings bindings = context.getBindings(scope); + if (bindings == null) + continue; + for (String key : bindings.keySet()) + members.append(new PyString(key)); + } + } + members.sort(); + return members; + } + + // Not necessary for functionality; present to satisfy __builtin__.PyMapping_check + @ExposedMethod + public PyObject __getitem__(PyObject key) { + return super.__getitem__(key); + } + + public PyObject __finditem__(PyObject key) { + return __finditem__(key.asString()); + } + + public PyObject __finditem__(String key) { + synchronized (context) { + int scope = context.getAttributesScope(key); + if (scope == -1) + return null; + return Py.java2py(context.getAttribute(key, scope)); + } + } + + @ExposedMethod + public void __setitem__(PyObject key, PyObject value) { + __setitem__(key.asString(), value); + } + + public void __setitem__(String key, PyObject value) { + synchronized (context) { + int scope = context.getAttributesScope(key); + if (scope == -1) + scope = ScriptContext.ENGINE_SCOPE; + context.setAttribute(key, value.__tojava__(Object.class), scope); + } + } + + @ExposedMethod + public void __delitem__(PyObject key) { + __delitem__(key.asString()); + } + + public void __delitem__(String key) { + synchronized (context) { + int scope = context.getAttributesScope(key); + if (scope == -1) + throw Py.KeyError(key); + context.removeAttribute(key, scope); + } + } +} Modified: trunk/jython/src/org/python/util/PythonInterpreter.java =================================================================== --- trunk/jython/src/org/python/util/PythonInterpreter.java 2009-07-23 03:26:56 UTC (rev 6566) +++ trunk/jython/src/org/python/util/PythonInterpreter.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -1,10 +1,17 @@ package org.python.util; +import java.io.FilterReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; import java.util.Properties; +import org.python.antlr.base.mod; import org.python.core.CompileMode; import org.python.core.CompilerFlags; +import org.python.core.ParserFacade; import org.python.core.Py; +import org.python.core.PyCode; import org.python.core.PyException; import org.python.core.PyFile; import org.python.core.PyFileWriter; @@ -12,6 +19,7 @@ import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyStringMap; +import org.python.core.PySyntaxError; import org.python.core.PySystemState; import org.python.core.__builtin__; @@ -131,6 +139,14 @@ } /** + * Evaluate a Python code object and return the result + */ + public PyObject eval(PyObject code) { + setState(); + return __builtin__.eval(code, locals, locals); + } + + /** * Execute a string of Python source in the local namespace */ public void exec(String s) { @@ -167,6 +183,29 @@ Py.flushLine(); } + /** + * Compile a string of Python source as either an expression (if possible) or module. + * + * Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish + * between scripts which return values and those which do not, nor do they make the + * corresponding distinction between evaluating or executing objects." (SCR.4.2.1) + */ + public PyCode compile(String script) { + return compile(script, "<script>"); + } + public PyCode compile(Reader reader) { + return compile(reader, "<script>"); + } + public PyCode compile(String script, String filename) { + return compile(new StringReader(script), filename); + } + public PyCode compile(Reader reader, String filename) { + mod node = ParserFacade.parseExpressionOrModule(reader, filename, cflags); + setState(); + return Py.compile_flags(node, filename, CompileMode.eval, cflags); + } + + public PyObject getLocals() { return locals; } Deleted: trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java =================================================================== --- branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java 2009-07-23 03:12:52 UTC (rev 6565) +++ trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -1,156 +0,0 @@ -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 { - - public void testEvalString() throws ScriptException { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - assertNull(pythonEngine.eval("x = 5")); - assertEquals(Integer.valueOf(5), pythonEngine.eval("x")); - } - - public void testSyntaxError() { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - try { - pythonEngine.eval("5q"); - } catch (ScriptException e) { - assertEquals(e.getColumnNumber(), 1); - assertEquals(e.getLineNumber(), 1); - assertTrue(e.getMessage().startsWith("SyntaxError: ")); - return; - } - assertTrue("Expected a ScriptException", false); - } - - public void testPythonException() { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - try { - pythonEngine.eval("pass\ndel undefined"); - } catch (ScriptException e) { - assertEquals(e.getLineNumber(), 2); - assertTrue(e.getMessage().startsWith("NameError: ")); - return; - } - assertTrue("Expected a ScriptException", false); - } - - public void testScriptFilename() { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - SimpleScriptContext scriptContext = new SimpleScriptContext(); - scriptContext.setAttribute(ScriptEngine.FILENAME, "sample.py", ScriptContext.ENGINE_SCOPE); - try { - pythonEngine.eval("foo", scriptContext); - } catch (ScriptException e) { - assertEquals("sample.py", e.getFileName()); - return; - } - assertTrue("Expected a ScriptException", false); - } - - public void testCompileEvalString() throws ScriptException { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - CompiledScript five = ((Compilable)pythonEngine).compile("5"); - assertEquals(Integer.valueOf(5), five.eval()); - } - - public void testEvalReader() throws ScriptException { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - assertNull(pythonEngine.eval(new StringReader("x = 5"))); - assertEquals(Integer.valueOf(5), pythonEngine.eval(new StringReader("x"))); - } - - public void testCompileEvalReader() throws ScriptException { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - CompiledScript five = ((Compilable)pythonEngine).compile(new StringReader("5")); - assertEquals(Integer.valueOf(5), five.eval()); - } - - public void testBindings() throws ScriptException { - ScriptEngineManager manager = new ScriptEngineManager(); - ScriptEngine pythonEngine = manager.getEngineByName("python"); - - pythonEngine.put("a", 42); - assertEquals(Integer.valueOf(42), pythonEngine.eval("a")); - assertNull(pythonEngine.eval("x = 5")); - assertEquals(Integer.valueOf(5), pythonEngine.get("x")); - 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)); - } -} Copied: trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java (from rev 6565, branches/jsr223/tests/java/org/python/jsr223/ScriptEngineTest.java) =================================================================== --- trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java (rev 0) +++ trunk/jython/tests/java/org/python/jsr223/ScriptEngineTest.java 2009-07-23 05:14:58 UTC (rev 6567) @@ -0,0 +1,156 @@ +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 { + + public void testEvalString() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + assertNull(pythonEngine.eval("x = 5")); + assertEquals(Integer.valueOf(5), pythonEngine.eval("x")); + } + + public void testSyntaxError() { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + try { + pythonEngine.eval("5q"); + } catch (ScriptException e) { + assertEquals(e.getColumnNumber(), 1); + assertEquals(e.getLineNumber(), 1); + assertTrue(e.getMessage().startsWith("SyntaxError: ")); + return; + } + assertTrue("Expected a ScriptException", false); + } + + public void testPythonException() { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + try { + pythonEngine.eval("pass\ndel undefined"); + } catch (ScriptException e) { + assertEquals(e.getLineNumber(), 2); + assertTrue(e.getMessage().startsWith("NameError: ")); + return; + } + assertTrue("Expected a ScriptException", false); + } + + public void testScriptFilename() { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + SimpleScriptContext scriptContext = new SimpleScriptContext(); + scriptContext.setAttribute(ScriptEngine.FILENAME, "sample.py", ScriptContext.ENGINE_SCOPE); + try { + pythonEngine.eval("foo", scriptContext); + } catch (ScriptException e) { + assertEquals("sample.py", e.getFileName()); + return; + } + assertTrue("Expected a ScriptException", false); + } + + public void testCompileEvalString() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + CompiledScript five = ((Compilable)pythonEngine).compile("5"); + assertEquals(Integer.valueOf(5), five.eval()); + } + + public void testEvalReader() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + assertNull(pythonEngine.eval(new StringReader("x = 5"))); + assertEquals(Integer.valueOf(5), pythonEngine.eval(new StringReader("x"))); + } + + public void testCompileEvalReader() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + CompiledScript five = ((Compilable)pythonEngine).compile(new StringReader("5")); + assertEquals(Integer.valueOf(5), five.eval()); + } + + public void testBindings() throws ScriptException { + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine pythonEngine = manager.getEngineByName("python"); + + pythonEngine.put("a", 42); + assertEquals(Integer.valueOf(42), pythonEngine.eval("a")); + assertNull(pythonEngine.eval("x = 5")); + assertEquals(Integer.valueOf(5), pythonEngine.get("x")); + 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)); + } +} Property changes on: trunk/jython/tests/java/org/python/tests/RedundantInterfaceDeclarations.java ___________________________________________________________________ Modified: svn:mergeinfo - + /branches/jsr223/tests/java/org/python/tests/RedundantInterfaceDeclarations.java:6285-6565 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |