[help request] how to set sys.stderr to object of cStringIO type

grbgooglefan ganeshborse at gmail.com
Tue Jan 15 04:45:57 EST 2008


I am in a perculiar situation. I want to use PyRun_SimpleString for
creating Python functions in embedded Python in C++.
But there could be cases when Python function code compilation could
fail & PyRun_SimpleString will return -1 as return status. At this
time, it prints the error message to sys.stderr.
So, we do not have any control or cannot use that message to show to
user to correct the errors in the function code.
I could assign an object of cStringIO type to sys.stderr at Python
command prompt as below:
>>> import sys
>>> import cStringIO
>>> obj=cStringIO.StringIO()
>>> sys.stderr=obj

And then using the obj.getvalue(), I could print exceptions printed to
sys.stderr.

But, I am not able to figure out, how can I do this same thing in
Python/C API in my program using Py* functions?

How do we set the "sys.stderr" to cStringIO object from Python
embedded in C++ or C?

Should I be using PyFile_FromFile for convert the cStringIO object?
Please help.



More information about the Python-list mailing list