PyErr_Print() but not to stderr?

Ken Kinder bouncing at _nospam_velocitypenguin.com
Thu May 20 23:41:35 EDT 1999


Don't re-open sys.stderr, just make it point to your own file descriptor:

errorlog = open('foo', 'w')
sys.stderr = errorlog

and make sure when your program closes:

errorlog.close()

Or, you could just do the sensible thing, and NOT USE PyErr_Print.  Whenever
you use it, sooner or later, you want something more extensible (ie logfiles). 
Just make your own logging functions that initially write to sys.stdout.

On Wed, 19 May 1999, Mike Romberg wrote:
>Does anyone know if it is possible to redirect where the output of
>PyErr_Print() is sent?  I have an embeded python process which is
>running as a daemon and has shut down stdin, stderr and stdout.  So,
>when a python error occurs I can detect it but can find an easy way to
>redirect the output of PyErr_Print() or PyErr_PrintEx() to a logfile.
>
>  It occurs to me that I might reopen the stderr file descriptor and
>FILE object onto my file to do this.  This seems a bit ugly.  Perhaps
>there is another method.  I did take a quick look at the definition of
>PyErr_PrintEx() in the 1.5.2 release.  It looks a bit too involved for
>me to just place a modified version in my program.  Perhaps there is
>some better method I am missing?
>
>Thanks,
>
>Mike Romberg (romberg at fsl.noaa.gov)




More information about the Python-list mailing list