473,700 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I am in a perculiar situation. I want to use PyRun_SimpleStr ing for
creating Python functions in embedded Python in C++.
But there could be cases when Python function code compilation could
fail & PyRun_SimpleStr ing 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=ob j
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.
Jan 15 '08 #1
1 1727
On Jan 15, 5:45*pm, grbgooglefan <ganeshbo...@gm ail.comwrote:
I am in a perculiar situation. I want to use PyRun_SimpleStr ing for
creating Python functions in embedded Python in C++.
But there could be cases when Python function code compilation could
fail & PyRun_SimpleStr ing 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=o bj

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.
I have got a solution for this. Would like to know if this is the
correct way or will it cause any problems if program runs for long
time?

/*************** *************** *************** ******/
/--1st stage is assign object of cStringIO to sys.stderr at
initialization
/-- once that is done, we can call getvalue() on that object on every
error.
/-----------------------------------------------------/
PyObject *_pPyobStringIO ;
PyObject *_pPyGetValFunc ;
_pPyobStringIO= NULL;
_pPyGetValFunc= NULL;
PyObject *obFuncStringIO = NULL;
int ret1 = 0;

// Import cStringIO module
PyObject * modStringIO = PyImport_Import Module("cString IO");
if(PyErr_Occurr ed() || modStringIO == NULL){
printf("pyParse rEvaluator::Ini t::PyImport cStringIO
failed:");
PyErr_Print();
goto PY_INIT_ERR;
}
// get StringIO constructor
obFuncStringIO = PyObject_GetAtt rString(modStri ngIO,
"StringIO") ;
if(PyErr_Occurr ed() || obFuncStringIO == NULL){
printf("pyParse rEvaluator::Ini t: cant find
cStringIO.Strin gIO:");
PyErr_Print();
goto PY_INIT_ERR;
}
// Construct cStringIO object
_pPyobStringIO = PyObject_CallOb ject(obFuncStri ngIO, NULL);
if(PyErr_Occurr ed() || _pPyobStringIO= =NULL){
printf("pyParse rEvaluator::Ini t: cStringIO.Strin gIO()
failed:");
PyErr_Print();
goto PY_INIT_ERR;
}
// get the getvalue function ptr
_pPyGetValFunc = PyObject_GetAtt rString(_pPyobS tringIO,
"getvalue") ;
if(PyErr_Occurr ed() || _pPyGetValFunc= =NULL){
printf("pyParse rEvaluator::Ini t: cant find getvalue
function:");
PyErr_Print();
goto PY_INIT_ERR;
}
// try assigning this object to sys.stderr
ret1 = PySys_SetObject ("stderr", _pPyobStringIO) ;
if(ret1 != 0){
printf("failed to assign _pPyobStringIO to stderr\n");
} else
printf("assigne d _pPyobStringIO to stderr\n");
PY_INIT_ERR:
printf("pyParse Evaluator::pyPa rseEvaluator failed\n");
/
*************** *************** *************** *************** **********/
int ret = PyRun_SimpleStr ing(strFunction );
if(ret != 0){
// call getvalue() method in StringIO instance
PyObject *obResult=NULL;
obResult = PyObject_CallOb ject(_pPyGetVal Func, NULL);
if(PyErr_Occurr ed() || obResult==NULL) {
printf("getvalu e() failed\n");
return -1;
} else printf("PyObjec t_CallObject on getvalue is ok\n");
// did getvalue return a string?
if(!PyString_Ch eck(obResult)){
printf("getvalu e() did not return error string\n");
return -1;
} else printf("getvalu e returned string object\n");
// retrieve error message string from this object
char *sresult = NULL;
if(NULL != (sresult = PyString_AsStri ng(obResult))){
printf("result string was [%s]\n",sresult);
}
}
/*************** *************** *************** *************/
Jan 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5473
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
6
2878
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or suggestion for improvements or conversion to iteration would be much appreciated
4
2661
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am printing these session variables they are printing. After that I am assigning these things in hidden object and in the form submit action I am receiving these hidden values like the following:
6
4993
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
3
2605
by: zxo102 | last post by:
Hi there, I have a python application (many python scripts) and I start the application like this python myServer.py start in window. It is running in dos window. Now I would like to put it in background as NT service. I got a example code: SmallestService.py from chapter 18 of the book "Python Programming On Win32" by Mark Hammond etc. The code is as follows and runs well as an NT service.
1
3714
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
149
4263
by: jacob navia | last post by:
As a consequence of a heavy discussion in another thread, I wrote this program: #include <stdlib.h> int main(void) { char *p=calloc(65521,65552); if (p) printf("BUG!!!!\n"); }
0
5570
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
18
2718
by: Matt Garman | last post by:
I'm trying to use Python to work with large pipe ('|') delimited data files. The files range in size from 25 MB to 200 MB. Since each line corresponds to a record, what I'm trying to do is create an object from each record. However, it seems that doing this causes the memory overhead to go up two or three times. See the two examples below: running each on the same input file results in 3x the memory usage for Example 2. (Memory...
0
8728
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8647
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8926
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6563
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4404
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.