Issue1798

classification
Title: JSR223 eval of variable or variable expression returns null
Type: Severity: normal
Components: Versions: Jython 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amak, dturanski, fwierzbicki
Priority: normal Keywords:

Created on 2011-09-22.13:17:21 by dturanski, last changed 2013-02-26.00:29:53 by amak.

Files
File name Uploaded Description Edit Remove
JSR223Test.java dturanski, 2011-09-22.13:17:20 Junit Test
Messages
msg6649 (view) Author: David Turanski (dturanski) Date: 2011-09-22.13:17:20
I am not a python programmer, but this seems like a bug.

I would expect consistent behavior. As demonstrated by the test other languages support this. The test fails even if I explicitly reference the variable as in:

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x=7
>>> x
7
msg7778 (view) Author: Alan Kennedy (amak) Date: 2013-02-25.23:58:43
The fundamental issue here is that jython (identically to cpython) treats expressions differently from statements.

"eval" expects to receive an expression, and returns a value.

http://docs.python.org/2/library/functions.html#eval

"exec" expects to receive statements, and does not return a value.

http://docs.python.org/2/reference/simple_stmts.html#exec

JSR 223 is different: it treats has no "exec", and uses "eval" for both of the purposes that jython separates into "eval" and "exec".

Object eval(String script)
 - Executes the specified script.

http://docs.oracle.com/javase/6/docs/api/javax/script/ScriptEngine.html#eval%28java.lang.String%29

Our implementation of JSR 223/javax.script is obviously missing this subtlety.
msg7779 (view) Author: Alan Kennedy (amak) Date: 2013-02-26.00:22:24
This is the part of the JSR specification that is missed in our JSR 223 implementation.

"""
SCR 4.2.1

In particular, 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 scripts.
All scripts are executed using methods which returns an Object.
When scripts for which the interpreter returns no value are executed,
null is returned. Implementations using existing interpreters that use
different methods to execute different types of scripts must internally
determine which interpreter method to call.
"""

download.oracle.com/otn-pub/jcp/java_scripting-1.0-fr-eval-oth-JSpec/java_scripting-1_0-fr-spec.pdf
msg7780 (view) Author: Alan Kennedy (amak) Date: 2013-02-26.00:29:53
We need to be able to differentiate between statements and expressions before deciding whether to call "eval" or "exec" on the jython intepreter.

The ast.parse() function might be one way to do it

Jython 2.7b1+ (default:a30708945630, Feb 23 2013, 15:22:29)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_41
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("x=7").body
[Assign]
>>> ast.parse("7").body
[Expr]
>>>
History
Date User Action Args
2013-02-26 00:29:53amaksetmessages: + msg7780
2013-02-26 00:22:24amaksetmessages: + msg7779
2013-02-25 23:58:43amaksetmessages: + msg7778
2013-02-25 19:42:03fwierzbickisetversions: + Jython 2.5, - 2.5.2
2013-02-25 19:28:52fwierzbickisetpriority: normal
nosy: + fwierzbicki
2011-11-04 21:36:24amaksetnosy: + amak
2011-09-22 13:17:21dturanskicreate