Re: Jython Caching Argv

Jeff Allen <[email protected]> Sat, 11 Aug 2018 22:41:35 +0100
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
I don't know anything about Spring Boot, but the standalone JAR must be 
on your path for you to be able to refer to "PythonInterpreter" in your 
code. So by "not detected" I guess you mean that Jython appear not to 
find its library (which is in the standalone JAR). Jython guesses based 
on the path to the JAR it seems to be running from (that 
org.python.core.Py was loaded from) but telling it is safer.

This bit struck me as odd:

props.put("python.home", "C:\\Dev\\Python27");

That's not the location of CPython is it? That would cause you a world 
of confusion.

However, none of that addresses your question about argv (meaning 
sys.argv I suppose). The important observation here is that 
"PythonInterpreter.initialize" is a static method that sets a default 
argv that all interpreters will see as they are created. It makes a big 
difference now what version you are using. In 2.7.0, all interpreters 
were really the same interpreter: you got a separate namespace for your 
main, but the modules where all the same, in particular every 
interpreter shared sys. In 2.7.1, each interpreter gets its own sys, and 
so each module used is loaded again for each interpreter.

I cannot say off the top of my head what the behaviour of the default 
argv and sys.argv is, but I'm pretty sure the interpreter you make in 
the next line has its own sys and the sys.argv you could set 
independently, although not in the constructor.

Jeff Allen

On 11/08/2018 20:16, Debashish wrote:
> Hi,
>
> I am trying to call a Python Script from a Spring Boot applications. 
> There are two issues I am facing:
> (1) The Jython Standalone JAR is not detected and I am forced to 
> supply the local install path as "python.home" to make it work.
> (2) Once I initialize the /PythonInterpreter/ class, it seems it 
> caches the argv I supply to it as for the subsequent invocation it 
> uses the same argv values. I do call the close() method to do cleanup, 
> but it doesn't help :(
>
>                 Properties preprops = System.getProperties();
> Properties props = new Properties();
>
> props.put("python.home", "C:\\Dev\\Python27");
> props.put("python.console.encoding", "UTF-8");
> props.put("python.security.respectJavaAccessibility", "false");
> props.put("python.import.site", "false");
>
> PythonInterpreter.initialize(preprops, props, arguments);
> PythonInterpreter pyInterpreter = new PythonInterpreter();
>
> try {
> resource = new ClassPathResource("mypyscript.py");
> out = new ByteArrayOutputStream();
> err = new ByteArrayOutputStream();
> pyInterpreter.setOut(out);
> pyInterpreter.setErr(err);
> pyInterpreter.execfile(resource.getInputStream());
> result[0] = out.toString(); // reading the output
> result[1] = err.toString(); // reading any error
> } catch (Exception e) {
> throw new Exception(e);
> } finally {
> try {
> if (out != null)
> out.close();
> if (err != null)
> err.close();
> pyInterpreter.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
>
> Please help.
>
> Thanks,
>
> Deb
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>
>
> _______________________________________________
> Jython-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jython-users


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users