From: A. G. <re...@bu...> - 2019-12-11 13:19:02
|
New submission from A. Gobbo <ale...@ps...>: 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 ---------- components: Core messages: 12822 nosy: alexgobbo severity: normal status: open title: Wrong name of the top-level scope when running Jython under Java Scripting API type: behaviour versions: Jython 2.7.1 _______________________________________ Jython tracker <re...@bu...> <https://bugs.jython.org/issue2846> _______________________________________ |