472,133 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

how to get string printed by PyErr_Print( )?

PythonC API function PyErr_Print( ) prints an error string onto stderr
if PyErr_Occurred() is true.
I don't want to print this to stderr because my Python+C code is
running daemon mode & won't have terminal / stderr.
So, I want to retrieve the string which PyErr_Print( ) will print.
E.g., PyErr_Print() printed following string when I tried to call
setTuple with one extra argument
Traceback (most recent call last):
File "<string>", line 2, in isTacticSafe
IndexError: tuple assignment index out of range

How do I get this error message in a local char* & use it for further
error handling?

Also, is there any way to get an error number associated for these
error conditions using some getError function on the object returned
by PyErr_Occurred()?

Thanks.
Dec 19 '07 #1
3 6554
grbgooglefan wrote:
PythonC API function PyErr_Print( ) prints an error string onto stderr
if PyErr_Occurred() is true.
I don't want to print this to stderr because my Python+C code is
running daemon mode & won't have terminal / stderr.
So, I want to retrieve the string which PyErr_Print( ) will print.
E.g., PyErr_Print() printed following string when I tried to call
setTuple with one extra argument
Traceback (most recent call last):
File "<string>", line 2, in isTacticSafe
IndexError: tuple assignment index out of range

How do I get this error message in a local char* & use it for further
error handling?

Also, is there any way to get an error number associated for these
error conditions using some getError function on the object returned
by PyErr_Occurred()?
PyErr_Print() will import the sys module and try to use whatever file-like
object is sys.stderr. Replace this with a StringIO or an open file object just
like you would for output from the Python level.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Dec 19 '07 #2
grbgooglefan wrote:
PythonC API function PyErr_Print( ) prints an error string onto stderr
if PyErr_Occurred() is true.
I don't want to print this to stderr because my Python+C code is
running daemon mode & won't have terminal / stderr.
So, I want to retrieve the string which PyErr_Print( ) will print.
E.g., PyErr_Print() printed following string when I tried to call
setTuple with one extra argument
Traceback (most recent call last):
File "<string>", line 2, in isTacticSafe
IndexError: tuple assignment index out of range
I suggest a different approach. A daemon must have a stdin, stdout and
stderr connected to a terminal. You can use freopen() to redirect stderr
and stdout to a log file and fclose() to close stdin.

http://www.gnu.org/software/libc/man...pening-Streams

Christian

Dec 19 '07 #3
On Dec 19 2007, 5:55*pm, Christian Heimes <li...@cheimes.dewrote:
grbgooglefan wrote:
PythonC API functionPyErr_Print( ) prints an error string onto stderr
if PyErr_Occurred() is true.
I don't want to print this to stderr because my Python+C code is
running daemon mode & won't have terminal / stderr.
So, I want to retrieve the string whichPyErr_Print( ) will print.
E.g.,PyErr_Print() printed following string when I tried to call
setTuple with one extra argument
Traceback (most recent call last):
* File "<string>", line 2, in isTacticSafe
IndexError: tuple assignment index out of range

I suggest a different approach. A daemon must have a stdin, stdout and
stderr connected to a terminal. You can use freopen() to redirect stderr
and stdout to a log file and fclose() to close stdin.

http://www.gnu.org/software/libc/man...html#Opening-S...

Christian
I do not want to redirect anything to file. Because I do not want to
avoid the disk access completely - for reading as well as writing.
I liked the 1st option suggested by Robert Kern.

Can you please explain a little bit how can I replace sys.stderr with
StringIO or my char* buffer?
I have to show the error message of Python code compilation &
execution to the user on the GUI & therefore I want to capture the
error message directly instead of logging it to file.

Thanks in advance for all your help.
Jan 9 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bob | last post: by
3 posts views Thread by roopeman | last post: by
9 posts views Thread by Divick | last post: by
27 posts views Thread by fuch6921 | last post: by
5 posts views Thread by goldtech | last post: by
17 posts views Thread by let_the_best_man_win | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.