Issue2223

classification
Title: __file__ not defined when running from ScriptEngine
Type: rfe Severity: major
Components: None Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: jythonuser1, zyasoft
Priority: Keywords:

Created on 2014-10-20.00:29:14 by jythonuser1, last changed 2016-01-14.05:25:15 by zyasoft.

Messages
msg9161 (view) Author: (jythonuser1) Date: 2014-10-20.00:29:13
Some user of ours is trying to run an existing script which uses __file__ and finding that the value is undefined.

I distilled it down to a test case which I have verified on 2.7b3:

    public static void main(String[] args) throws Exception {
        ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("py");
        engine.put(ScriptEngine.FILENAME, "/tmp/some_script.py");
        System.out.println(engine.eval("__file__"));
    }

Instead of printing the filename of the script, you get an exception:

    javax.script.ScriptException: NameError: name '__file__' is not defined in /tmp/some_script.py at line number 1
        at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
        at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)

As an ironic twist, the error message itself the filename, so clearly *someone* knows the filename.

For the sake of comparison, the test program for JRuby's equivalent constant prints out the filename:

    public void testJRuby() throws Exception
    {
        ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("rb");
        engine.put(ScriptEngine.FILENAME, "/tmp/some_script.rb");
        System.out.println(engine.eval("__FILE__"));
    }
msg10599 (view) Author: Jim Baker (zyasoft) Date: 2016-01-07.04:42:13
Fixed as of https://hg.python.org/jython/rev/50126c4efa22

Rather appropriate to see this issue is numbered 2223, given that it's about JSR 223 support.

I was cleaning up our javatest support in the previous pushed change set, saw this fix would be a quick win, and therefore this fix got into 2.7.1
History
Date User Action Args
2016-01-14 05:25:15zyasoftsetstatus: pending -> closed
2016-01-07 04:42:13zyasoftsetstatus: open -> pending
assignee: zyasoft
resolution: fixed
messages: + msg10599
milestone: Jython 2.7.2 -> Jython 2.7.1
2015-12-29 23:43:08zyasoftsetmilestone: Jython 2.7.1 -> Jython 2.7.2
2015-04-15 20:42:38zyasoftsetassignee: zyasoft -> (no value)
milestone: Jython 2.7.1
2015-01-18 15:56:13zyasoftsetassignee: zyasoft
nosy: + zyasoft
2014-10-20 00:29:14jythonuser1create