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

G

grbgooglefan

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:
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.
 
G

grbgooglefan

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:


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);
}
}
/**********************************************************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top