[Python-bugs-list] [ python-Bugs-505490 ] Improve I/O redirection to embedding app

noreply@sourceforge.net noreply@sourceforge.net
Thu, 24 Jan 2002 11:35:55 -0800


Bugs item #505490, was opened at 2002-01-18 11:00
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=505490&group_id=5470

Category: Extension Modules
Group: Feature Request
Status: Open
Resolution: None
Priority: 5
Submitted By: Stefan Franke (sfranke)
Assigned to: Nobody/Anonymous (nobody)
Summary: Improve I/O redirection to embedding app

Initial Comment:
When embedding Python into an existing application, you
usually do two things (among others):
1. Make Python use your own memory allocation
2. Redirect its I/O to the embedding app

While the former is very easy due to the excellent
pymem.h interface, the latter is quite painful, since
the interpreter uses stdin/out/err directly.

The common workaround I found on c.l.p is providing
your own file-like objects in an extension module and
assign them to sys.stdout/err within the interpreter.
This solution still has its problems (like getting
errors while import site.py)

I wish Python would encapsulate I/O in a similar way
than its malloc interface, and in some way I would
expect this from a language which is said to be 
designed for extending and embedding.

Unluckily I'm not able to this myself. I started my 
first embedding project a few days ago and still miss
the big picture about Python's C API.


----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-01-24 11:35

Message:
Logged In: YES 
user_id=21627

What do you mean with "you don't get tracebacks"? The
attached a.py prints the traceback into the redirected file
just fine (with 2.2, on Solaris 8).

Also, which other output do you have in mind?

----------------------------------------------------------------------

Comment By: Jack Jansen (jackjansen)
Date: 2002-01-21 02:14

Message:
Logged In: YES 
user_id=45365

Assigning to sys.stdout and friends is only half a solution: you don't get tracebacks and any other output produced by the C code.

Unfortunately there is no platform-independent solution without getting rid of stdio completely.

Fortunately there are platform-dependent solutions for many platforms. MacPython has a lowlevel hook to do this, for instance. Depending on your stdio implementation you may be able to hook into stdio on a low level.

An alternative 90% solution is to do the sys.stdout assignment and add Python code to print your tracebacks and such. An example of such code can be found in  Idle (or IDE, or PythonWin, or any other windowing Python IDE).

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-01-18 13:26

Message:
Logged In: YES 
user_id=21627

I don't think that assigning to sys.stdout etc. is a
work-around; it is the proper solution. If you worry about
site.py, you can set Py_NoSiteFlag (which may be a good idea
in an embedded interpreter, anyway); if you then still need
site.py, you can import it on your own.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=505490&group_id=5470