Jython Embedding - how do I compile and exec with params

Tony Green tonygreen at techie.com
Tue Feb 5 08:31:03 EST 2002


I am writing a scripting extension mechanism to a big server project
we've built over the last year and a half.

What I want to acheive is
- system authors upload scripts (via a web front end)
- the system checks and compiles the script
- stashes the compiled script away against a named event.

When the even occurs the compiled script is called by the system with
a list of parameters (current user id, various numbers describing the
system state).

Now I can upload and compile the script just fine using :
PyCode somePyCode = __builtin__.compile ( script, "<>", "exec");
...and deal with any errors ...and stash the script away against a
named event...etc.

And it is easy enough to call this code with
aPythonInterpreterInstance.exec ( somePyCode )

but that doesn't pass my parameters through.

It seems to me there's three obvious ways to do this:

(1) specify that each script has a named target (eg. 'def
onEvent(eventid , userid):') and use one of the PyCode.call() methods
- but they aren't sensibly documented in the javadocs so it could take
a while to figure out what's really going on.

(2) specify that each script contains a class def and implements a
__call__ method - but I can't see how to get the PyClass object out of
the PyCode object returned by __builtin__.compile

(3) put the variables into the local namespace and then execute the
script (without a named method or class) - but this means that I would
have to clean down the local namespace each time (- and anyway what is
it local to?).

(Did I mention execution speed is important to this app and I need to
service multiple threads?)

Thanks in advance,
Tony



More information about the Python-list mailing list