Issue1486

classification
Title: Jython doesn't return last evaluated value in Java ScriptEngine
Type: behaviour Severity: critical
Components: Core Versions: 2.5.1
Milestone:
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: bioform, nriley
Priority: Keywords:

Created on 2009-10-06.08:27:58 by bioform, last changed 2009-12-23.11:25:03 by bioform.

Messages
msg5220 (view) Author: bioform (bioform) Date: 2009-10-06.08:27:56
we always neg null from Jython if we try to evaluate multi-line python
code using Java ScriptEngine.
For example:
--------------------------------------------------------------
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("jython");
String code1 = "print 'mmm'\n\"Hello %s\" % passed_in";
String code2 = "\"Hello %s\" % passed_in";
// get context
ScriptContext context = engine.getContext();
context.setAttribute("passed_in", "World", ScriptContext.ENGINE_SCOPE);

/////////////////////////////////////////////////////
// Compilable
////////////////////////////////////////////////////
Compilable compiledEng=(Compilable) engine;

CompiledScript script1= compiledEng.compile(code1);
CompiledScript script2= compiledEng.compile(code2);

Object result1 = script1.eval();
Object result2 = script2.eval();

System.out.println("result1: " + result1);
System.out.println("result2: " + result2);
--------------------------------------------------------------
msg5231 (view) Author: Nicholas Riley (nriley) Date: 2009-10-18.21:14:16
This is a Python language issue; Python doesn't return the last value 
evaluated.  If you want a result back, evaluate an expression.
msg5390 (view) Author: bioform (bioform) Date: 2009-12-22.15:42:07
both codes contains expression(code1 and code2) but behavior is different.
Besides Python interpretor return last evaluated expression
msg5392 (view) Author: Nicholas Riley (nriley) Date: 2009-12-22.15:50:05
No, code1 is a statement.

I discussed this further here, with some links to the language spec.

http://stackoverflow.com/questions/1887320/
msg5393 (view) Author: bioform (bioform) Date: 2009-12-22.16:11:39
So you mean that one line of code is statement but two lines are
expression ?
Hmmm...
Actually it is very uncomfortable.
For example what I should do if I try to evaluate third party script? I
couldn't know what or which variables I should extract from
"ScriptEngine"(last evaluated expression can return several results).
It will be very useful if "eval" method of "ScriptEngine" always return
last evaluated value(it can be array if last evaluated function returns
NOT only one value)
msg5394 (view) Author: Nicholas Riley (nriley) Date: 2009-12-22.16:17:07
It's not just 1 line versus two.  For example, the first line of what you posted 
("print ...") is a statement whereas the second line is an expression.

Python is not set up to behave as you wish - there's a separate interactive 
grammar from the "batch" grammar, which requires extra returns, etc.  I suggest 
if you have a third-party script to define an entry point (such as a function 
name) you can call after evaluating the script in its entirety, then the return 
value will be explicit with "return".
msg5395 (view) Author: bioform (bioform) Date: 2009-12-23.11:25:03
I think I will wrap third party script to function...
OK
There are no any questions right now :)
Thank you

P.S. But ScriptEngine is Java interface for Jython so it will be useful
always return last evaluated value in the "eval()" function. In this
case you don't change Python behavior - you just implement Java
interface for Jython.
History
Date User Action Args
2009-12-23 11:25:03bioformsetmessages: + msg5395
2009-12-22 16:17:07nrileysetmessages: + msg5394
2009-12-22 16:11:39bioformsetmessages: + msg5393
2009-12-22 15:50:05nrileysetmessages: + msg5392
2009-12-22 15:42:07bioformsettype: behaviour
messages: + msg5390
2009-10-18 21:14:16nrileysetstatus: open -> closed
resolution: invalid
messages: + msg5231
nosy: + nriley
2009-10-06 08:27:59bioformcreate