Python Embedding, stdin/-out redirection

Gustavo Niemeyer niemeyer at conectiva.com
Mon Mar 4 19:58:30 EST 2002


> I'm just trying to embedd python in my 3D Engine
> which works fine, except the problems with my console:
> I'd like to do the
> PyRun_InteractiveLoop(stdin, "<stdin>");
> for my Engines console, because the python interactive
> mode is just perfect. To make it as simple as possible
> I redirected stdin/-out to 2 pipes which I read out
> and print to my 3D window's console. Within my programm
> all I/O stream releated stuff just works fine.
> Mysteriously that doesn't work with python.
> Below you see an example of what I tried:
[...]

I'm trying to understand the usefulness of python being run as an
external process, since it won't be able to change your program's
environment. What you probably want is to run python "inside"
your game, right?

If you have a game console, you probably have your own read/write
routines. It's quite easy to implement your own interactive loop, with
amazing results. Just have a look at Python/pythonrun.c, inside python
source, and check PyRun_StringFlags() routine. To get the output back
in your console, implement an object with a write() method, and replace
sys.stdout and sys.stderr with it (PySys_SetObject() will do the job for
you)

Hint: if you want a behavior similar to the python interactive console,
use Py_single_input when calling PyRun_StringFlags(), and be sure to
implement a read/write attribute named 'softspace' in your output
object.

-- 
Gustavo Niemeyer

[ 2AAC 7928 0FBF 0299 5EB5  60E2 2253 B29A 6664 3A0C ]




More information about the Python-list mailing list