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.- Hide quoted text -
- Show quoted text -
Well, I managed to do it myself. Here is what I am doing now to do
this. In Python-C/C++ world, if someone wants to do the same thing,
may use this.
Note: For this, am assigning the cStringIO object to sys.stderr once I
do Py_Initialize & then use cStringIO.getvalue() everytime I get
error.
While importing cStringIO module with Python-2.3.3, I faced the
problem of "undefined symbol
yObject_SelfIter". I could resolve that
also. I have put that note in the post which I had opened for that.
/+++++++++++++++++++++++++++++++++++++++++++++++
PyObject *_pPyModule;
PyObject *_pPyDictionary;
PyObject *_pPyGetValFunc;
PyObject *_pPyobStringIO;
Init(){
// Py_Initialize should have been done by now.....
PyObject *modStringIO = NULL;
PyObject *obFuncStringIO = NULL;
// Import cStringIO module
modStringIO = PyImport_ImportModule("cStringIO");
if(PyErr_Occurred() || modStringIO == NULL){
printf("pyParserEvaluator::Init:
yImport 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 getvalue() method in StringIO instance
_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
ret = PySys_SetObject("stderr", _pPyobStringIO);
if(ret != 0){
printf("failed to assign _pPyobStringIO to stderr\n");
goto PY_INIT_ERR;
}
return ret;
PY_INIT_ERR:
Py_XDECREF(modStringIO);
Py_XDECREF(obFuncStringIO);
Py_XDECREF(_pPyobStringIO);
Py_XDECREF(_pPyGetValFunc);
}
int _getPythonErrorMessage()
{
// call getvalue() method in StringIO instance
int ret = 0;
PyObject *obResult=NULL;
char *sresult = NULL;
obResult = PyObject_CallObject(_pPyGetValFunc, NULL);
if(PyErr_Occurred() || obResult==NULL){
printf("getvalue() failed\n");
ret = -1;
goto CLEAN_AND_RETURN;
}
// did getvalue return a string?
if(!PyString_Check(obResult)){
printf("getvalue() did not return error string\n");
ret = -1;
goto CLEAN_AND_RETURN;
}
// retrieve error message string from this object
if(NULL != (sresult = PyString_AsString(obResult))){
pErrorString = strdup(sresult);
} else {
ret = -1;
goto CLEAN_AND_RETURN;
}
return(ret);
CLEAN_AND_RETURN:
Py_XDECREF(obResult);
return(ret);
}
=================================================