Right, but the problem is that there is (currently) no way to choose
"evaluate expression" (like PythonInterpreter.eval does) over "run
script" (like the scripting.dev.java.net implementation for both
evaluate and compile/invoke).
And even if there were, I have the further problem in my application
that there is not (currently) a simple way to choose between the two
forms and it would be nicer to not have to choose.
As you say though, we need some help from someone more familiar with
machinery. In an eariler thread "python interpreter and java 6 script
engine ..." there was a comment by "Oti" that he would be "xraying"
ScriptingEngine, so I'm hoping this issue gets addressed in that process.
David Handy wrote:
> I was hoping someone with more expertise in embedding a Jython
> interpreter in Java would answer, but since no one else has spoken
> This page on the Groovy website
> (http://groovy.codehaus.org/Groovy+Console) says: " If the script
> returns a non-null result, that result is printed". Maybe Groovy has
> the concept of returning a value from a script, Jython does not. So I
> think the short answer to your question is "no", there's not a way to
> simply extract the value of the last expression evaluated in a Jython
> What people embedding Jython in Java do typically is run a script
> that sets the values of some known variables, then makes method calls
> to extract those values by name.
> This process is documented here:
> The above page uses the Jython-specific PythonInterpreter class. I
> assume there is a similar way to do the same thing using the Java
> Scripting API.
> Anyone with more embedded Jython experience please correct me if I'm
> wrong. David H
> -----Original Message----- From:
> [mailto:jython-users-bounces@...] On Behalf Of Jim
> White Sent: Sunday, November 18, 2007 4:24 PM To:
> jython-users@... Subject: [Jython-users] A problem
> with java.net's Scripting Engine for Jython
> (Trying again now that I'm actually subscribed...)
> This follows along a bit with a recent thread on this subject.
> I've tried using the https://scripting.dev.java.net/ engine for
> Jython and run into a slightly sticky problem.
> When using JythonScriptEngine.eval the code is compiled as a script
> and then executed. The result is that the return value is always
> null AFAICT.
> But in my application (a literate scripting tool called "Wings for
> Scripting" <http://www.ifcx.org/> which currently just supports
> Groovy but I'm extending to handle many languages starting with the
> Java Scripting API) in many (most?) cases the script is actually an
> expression and the value is what is desired.
> I've hacked up a version of the engine to use
> org.python.util.PythonInterpreter.eval for JythonScriptEngine.eval,
> but that isn't a whole answer because sometimes the script is more
> than an single expression.
> I'm not a Pythonperson (just trying to get the integration working)
> so I don't know what sorts of solutions are possible.
> Is there a way when a script is compiled that it will return the
> value of the last statement if it is an expression?