Problem with stdout in embedded python

David Bolen db3l at fitlinxx.com
Thu May 11 21:02:28 EDT 2000


paul_pelletier at iname.com writes:

> I've embedded python in a dll of my application and for some reason I
> dont see any output made by my scripts using print. Would anyone know
> what might be wrong?

Assuming that this is under Windows, if the main application that is
using your DLL is a graphical (versus console) application, then there
may be no console attached to your process for the output to go to and
it just gets thrown away.

What I've found helpful when embedding Python within another
application (even when that application is a console application), is
to reroute stdout/stderr to my own functions, which can then control
exactly how that output is managed.  Not only does this ensure I can
present the output in a manner suitable for the surrounding
environment (or even duplicate it in a log file), but I can massage
the data if necessary (e.g., adding timestamps or highlighting errors
versus standard output).  And because it's still stdout/stderr from
the perspective of Python, it catches everything - from print's to
uncaught tracebacks.

For example, in one application of mine (the surrounding application
provides an "ecna" module for use by embedded scripts), I internally
execute the following code in advance of any scripts:

     import sys

     class StdoutCatcher:
	 def write(self, stuff):
	     ecna.stdout(stuff)

     class StderrCatcher:
	 def write(self, stuff):
	     ecna.stderr(stuff)

     sys.stdout = StdoutCatcher()
     sys.stderr = StderrCatcher()


(The ecna.stdout/stderr functions are provided by the surrounding
application, and display the output in a trace window).


I don't know if something like that would work well in your particular
environment or not, but it might be one approach to try.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list