I am truly sorry for spaming mails out to you admins and mailing list
members. As it seems the alias "p.kurosch@..." I use for my mail
address "kurosch@..." was not used when sending the mails,
since I register usually with the alias.
Can someone explain to me how to use the setIn(Stream) method from
PythonInterpreter and what is possible to be done with the input stream.
As I would like to use it so that I can feed the PythonInterpreter without
the need to call the exec(PYTHON_CODE).
I would really appreciate any help as the documentation on this function
is unfortunately quite rare.
From: Jeff Allen <ja.py@fa...> - 2013-10-19 12:15:38
I quickly found this discussion:
which may help.
I agree that the documentation is thin. When I touch parts of the code
to change them, I try to explain how they work, firstly for myself, then
for those who come after or may use the API. This area is not code I've
worked on directly, but I've skirted around it while changing the way
the interactive console works.
Here's my understanding of the code. setIn (or setOut, setErr) replace
the objects that at the Python level are known as sys.stdin (or stdout,
stderr). Depending which overloaded setIn you actually call, either the
object you supply, or that object wrapped in a PyFile will end up as
sys.stdin. "sys" itself is an instance of PySystemState.
When you create an interpreter using the static factory
PythonInterpreter.threadLocalStateInterpreter, you supply (or have it
create) a fresh global name space, and it also creates a new
PySystemState that will be the object you get when you "import sys" into
the name space. The correct state and therefore the correct sys.stdin,
etc. are found from the Java thread in which your interpreter runs.
(Less sure about this last bit.)
A couple of untried ideas ...
The subclass of PythonInterpreter called InteractiveConsole may be
worth a look. When the InteractiveConsole reads a command, it does so
via raw_input(), which uses sys.stdin. (The accuracy of this statement
depends somewhat on what version you are using: I have changed code in
this area a couple of times since 2.7b1 but think it is correct again.)
It could be that InteractiveConsole.interact() will give you what you
want. InteractiveConsole lets you override its own raw_input() method,
in a custom sub-class. When it goes for new input it will return
whatever String you want, instead of calling the built-in raw_input()
and sys.stdin.read. That may work for you without setIn().
Have you also looked at the JSR-223 support for Jython? The
implementation uses setIn/Out/Err to redirect to streams to a Reader and
Writers the javax.script.ScriptContext holds. There is surely richer
public documentation of JSR-223 than we have produced for this part of
On 18/10/2013 10:53, Kurosch Petzold wrote:
> I am truly sorry for spaming mails out to you admins and mailing list
> members. As it seems the alias "p.kurosch@..." I use for my mail
> address "kurosch@..." was not used when sending the mails,
> since I register usually with the alias.
> Can someone explain to me how to use the setIn(Stream) method from
> PythonInterpreter and what is possible to be done with the input stream.
> As I would like to use it so that I can feed the PythonInterpreter without
> the need to call the exec(PYTHON_CODE).
> I would really appreciate any help as the documentation on this function
> is unfortunately quite rare.