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

Created on 2011-09-22.13:17:21 by dturanski, last changed 2014-06-25.16:04:54 by zyasoft.

File name Uploaded Description Edit Remove dturanski, 2011-09-22.13:17:20 Junit Test
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
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.

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

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.

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.
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
>>> ast.parse("7").body
msg8714 (view) Author: Jim Baker (zyasoft) Date: 2014-06-19.05:42:41
My reading of "[w]hen scripts for which the interpreter returns no value are executed, null is returned" is that the current behavior is acceptable. Trying to do AST analysis to workaround this is going to be a challenging problem in the general case, even if it can be readily done for simple cases.
Date User Action Args
2014-06-25 16:04:54zyasoftsetstatus: pending -> closed
2014-06-19 05:42:41zyasoftsetstatus: open -> pending
resolution: invalid
messages: + msg8714
nosy: + zyasoft
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