473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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.
Jan 15 '08 #1
1 1711
On Jan 15, 5:45*pm, grbgooglefan <ganeshbo...@gmail.comwrote:
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.
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_ImportModule("cStringIO");
if(PyErr_Occurred() || modStringIO == NULL){
printf("pyParserEvaluator::Init::PyImport cStringIO
failed:");
PyErr_Print();
goto PY_INIT_ERR;
}
// get StringIO constructor
obFuncStringIO = PyObject_GetAttrString(modStringIO,
"StringIO");
if(PyErr_Occurred() || obFuncStringIO == NULL){
printf("pyParserEvaluator::Init: cant find
cStringIO.StringIO:");
PyErr_Print();
goto PY_INIT_ERR;
}
// Construct cStringIO object
_pPyobStringIO = PyObject_CallObject(obFuncStringIO, NULL);
if(PyErr_Occurred() || _pPyobStringIO==NULL){
printf("pyParserEvaluator::Init: cStringIO.StringIO()
failed:");
PyErr_Print();
goto PY_INIT_ERR;
}
// get the getvalue function ptr
_pPyGetValFunc = PyObject_GetAttrString(_pPyobStringIO,
"getvalue");
if(PyErr_Occurred() || _pPyGetValFunc==NULL){
printf("pyParserEvaluator::Init: 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("assigned _pPyobStringIO to stderr\n");
PY_INIT_ERR:
printf("pyParseEvaluator::pyParseEvaluator failed\n");
/
************************************************** ********************/
int ret = PyRun_SimpleString(strFunction);
if(ret != 0){
// call getvalue() method in StringIO instance
PyObject *obResult=NULL;
obResult = PyObject_CallObject(_pPyGetValFunc, NULL);
if(PyErr_Occurred() || obResult==NULL){
printf("getvalue() failed\n");
return -1;
} else printf("PyObject_CallObject on getvalue is ok\n");
// did getvalue return a string?
if(!PyString_Check(obResult)){
printf("getvalue() did not return error string\n");
return -1;
} else printf("getvalue returned string object\n");
// retrieve error message string from this object
char *sresult = NULL;
if(NULL != (sresult = PyString_AsString(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
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 -------------------------------------------------------------------------------- ...
6
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...
4
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...
6
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...
3
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...
1
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...
149
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
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...
18
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.