473,573 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7059
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_P rint( ) 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_Prin t( ) will print.
E.g.,PyErr_Prin t() 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2414
by: John Pastrovick | last post by:
I am new to PHP, coming from ASP coding if (HTTP_GET_VARS==1){ print "Yes"; }else{ print "No"; } when "b" is not passed via (http://www.mydomain.com?b=1
0
1986
by: Bob | last post by:
My understanding of the image string function is this, that it allows the display of text in Graphics As per the parameters it receives: ImageString($image,6,$xpos,$ypos,$out,$col) these params: 1. name of thew image to write on 2. size of the font 3. X co-ordinate of the first letter of the string
3
1191
by: roopeman | last post by:
the following string was from windows 2000 server print eventlog message, i want to get the print info. from the string, anybody can help me ? 1)Document 126, Exprot List 04-15.xls owned by jackzhang was printed on printserver1 via port IP_192.168.0.1. Size in bytes: 46205; pages printed: 2 For more information, see Help and Support...
2
1437
by: roopeman | last post by:
i need your help, my code as below : //----------------------------------------------------------------------- //Wrote by Michael April 30 2005 //----------------------------------------------------------------------- using System.Text; using System.Diagnostics; using System.Threading;
9
7433
by: Divick | last post by:
Hi all, I have a problem related to std::string class. Is it ok to assign a global string variable to a local string object as shown below? I am trying to print the address of local string buffer and then I print the address of global string buffer and come out to be same, but I assume that as soon as func method has returned that...
27
7828
by: fuch6921 | last post by:
I want to read in an Octal number argument and have it stored as an octal number. For instance the user will type: ./a.out 777 and it will store the octal number 777. But it atoi does this as an interger, and sscanf gives me 0.
18
605
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
5
3305
by: goldtech | last post by:
Hi, I'm passing what I think is a string parameter to another Python program (spawn.py) - see the code snip below. But only the counter part gets printed to a log file via spawn.py. Yet the echo print to the output window shows the whole string with the fc part. Better explained below I hope, there's the calling .py and the spawn script...
17
2135
by: let_the_best_man_win | last post by:
How do I print a pointer address into a string buffer and then read it back on a 64 bit machine ? Compulsion is to use string (char * buffer) only. printing with %d does not capture the full 64-bits of the pointer. does %l exist in both printf and scanf for this purpose ?
0
7784
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7705
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7796
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5601
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3734
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.