Message12822

Author alexgobbo
Recipients alexgobbo
Date 2019-12-11.13:18:39
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1576070320.77.0.423485417625.issue2846@roundup.psfhosted.org>
In-reply-to
Content
Both in Jython 2.7.1 ands 2.7.2b2 the name of the top-level scope is wrong when running Jython under Java Scripting API.

If you execute:
   org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
   interp.exec("print __name__");

You get the familiar:
   __main__

However, executing: 
   javax.script.ScriptEngine engine = new javax.script.ScriptEngineManager().getEngineByName("python");
   engine.eval("print __name__");
You get:
   __builtin__

This creates of course a lot of problems. 
Many people do local workarounds like in their code:
  if __name__ in ['__builtin__', '__main__']:

I make a more global workaround to cope with code within libraries checking __name__. 
Just after initialising the engine I do:
   engine.put("__name__", "__main__");
   engine.eval("import sys");
   engine.eval("sys.modules['__main__']=sys.modules['__builtin__']"); 

This solve some issues, but not all. 
In particularly it is a show stopper for unittest, which does not work at all under Java Scripting API.


Note: This is an old bug, which has been reported in other places, but I didn't find it in bugs.jython:
   https://github.com/scijava/scripting-jython/issues/9
   https://forum.image.sc/t/jython-isssue-with-if---name-----main--/5544
History
Date User Action Args
2019-12-11 13:18:40alexgobbosetrecipients: + alexgobbo
2019-12-11 13:18:40alexgobbosetmessageid: <1576070320.77.0.423485417625.issue2846@roundup.psfhosted.org>
2019-12-11 13:18:40alexgobbolinkissue2846 messages
2019-12-11 13:18:39alexgobbocreate