Setting the Python Path using the JSR 223 Interface ...

View: New views
3 Messages — Rating Filter:   Alert me  

Setting the Python Path using the JSR 223 Interface ...

by Markus Krosche-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi together,

I am using Jython as a scripting language in my application. I want to set the 'python.path' property using the new JSR 223 scripting engine support.

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);

final ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
return engine
}

When I do the following:

engine1 = createEngine(pathList1);
list properties defined in PySystemState

engine2 = createEngine(pathList2);
list properties defined in PySystemState

In both cases the properties listed are the same. In both cases the python.path is set but is set to pathList1.
So the second modification of the PySystemState has no influence.

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".

Please could someone provide an example of how to apply the above sentence in practice? Unfortunately I have no idea :(

Thanks in advance
Markus



------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Setting the Python Path using the JSR 223 Interface ...

by Charlie Groves :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Sep 11, 2009 at 3:40 AM, Markus Krosche
<markus.krosche@...> 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

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: Setting the Python Path using the JSR 223 Interface ...

by Markus Krosche-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <charlie.groves@...>
On Fri, Sep 11, 2009 at 3:40 AM, Markus Krosche
<markus.krosche@...> 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


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users