From: Markus K. <mar...@go...> - 2009-09-14 07:08:01
|
Hi Charlie, thanks for your very good, fast and precise answer to my question! I have used the URL http://wiki.python.org/jython/UserGuide#using-jsr-223 as a starting point for my code. The last three sentences of this section are: "One difference between embedding with JSR 223 and using PythonInterpreter directly is that the scripting engine manages its own PySystemState per thread so it can always set the classloader to the thread's context classloader. This means if you want to do anything Jython specific to PySystemState, like adding to sys.path or setting sys.stdin and sys.stdout, you should do it after creating the ScriptEngine." Markus 2009/9/13 Charlie Groves <cha...@gm...> > On Fri, Sep 11, 2009 at 3:40 AM, Markus Krosche > <mar...@go...> wrote: > > Currently I am doing this by inside a method: > > > > createEngine(List<String> pathList) > > { > > ... > > Properties preProperties = new Properties(); > > preProperties.setProperty("python.path", > pythonPathStringBuffer.toString()); > > PySystemState.initialize(baseProperties, preProperties, null); > > Just as a general point, initialize only runs once. It sets up the > default PySystemState, and after that, calls to it are a no-op. If > you want to create additional, customized states, you need to call new > PySystemState and then modify that instance. > > > It is mentioned inside the jython documentation, that > > "One difference between embedding with JSR 223 and using > PythonInterpreter > > directly is that the scripting engine manages its own PySystemState per > > thread so it can always set the classloader to the thread's context > > classloader. This means if you want to do anything Jython specific to > > PySystemState, like adding to sys.path or setting sys.stdin and > sys.stdout, > > you should do it after creating the ScriptEngine". > > This is actually no longer the case with the new, builtin jsr 223 > implementation; it uses the normal Jython mapping between threads and > system states. The scripting.dev.java.net 223 implementation used > with 2.2 had this style of system state management. Are you referring > to the docs at http://wiki.python.org/jython/UserGuide or somewhere > else? I'd like to update any references to this. > > Here's the way I'd create engines with a custom sys.path using the new > implementation: > > ScriptEngine createEngine(List<String> pathEntries) { > PySystemState engineSys = new PySystemState(); > for (String pathEntry : pathEntries) { > engineSys.path.append(Py.newString(pathEntry)); > } > Py.setSystemState(engineSys); > return new ScriptEngineManager().getEngineByName("python"); > } > > Then any invocations through that ScriptEngine will use its particular > state. > > Charlie > |