![]() |
|
|
Generated: 24 Nov 2008 |
00001 #include "GaudiKernel/Bootstrap.h" 00002 // ============================================================================ 00003 #include <sstream> //included for stringbuf 00004 // ============================================================================ 00005 #ifdef _POSIX_C_SOURCE 00006 #undef _POSIX_C_SOURCE 00007 #endif 00008 #include "Python.h" //included for the python API 00009 // ============================================================================ 00010 #include "GaudiPython/GaudiPython.h" 00011 // ============================================================================ 00012 #include "GaudiPython/CallbackStreamBuf.h" //include header file 00013 // ============================================================================ 00014 00015 #define min(a,b) (((a)<(b))?(a):(b)) 00016 00017 using namespace std; 00018 00019 // ============================================================================ 00021 // ============================================================================ 00022 int GaudiPython::call_python_method 00023 ( PyObject* self, const char* method, char* buf ) 00024 { 00025 if ( 0 == self || 0 == method ) { return 1 ; } 00026 PyObject* r = PyObject_CallMethod(self, const_cast<char*>(method), 00027 const_cast<char*>("s"), buf); 00028 if ( 0 == r ) { 00029 string err("Unsuccessful call to bound Python method"); 00030 err += method; 00031 PyErr_SetString( PyExc_TypeError , err.c_str() ) ; 00032 PyErr_Print() ; 00033 return 1 ; 00034 } 00035 Py_DECREF( r ); 00036 return 0 ; 00037 } 00038 00039 // ============================================================================ 00041 // ============================================================================ 00042 GaudiPython::CallbackStreamBuf::CallbackStreamBuf 00043 (PyObject* self): stringbuf(), m_self(self) 00044 { 00045 Py_INCREF(this->m_self); 00046 this->m_callbackBuff = new char[512]; //used for passing the flushed chars in the python callback 00047 } 00048 00049 // ============================================================================ 00051 // ============================================================================ 00052 GaudiPython::CallbackStreamBuf::~CallbackStreamBuf() 00053 { 00054 Py_DECREF( this->m_self ); 00055 delete this->m_callbackBuff; 00056 } 00057 00058 // ============================================================================ 00060 // ============================================================================ 00061 int GaudiPython::CallbackStreamBuf::sync () 00062 { 00063 int length; 00064 char *x; 00065 for ( length = 0, x = this->pbase(); x < this->epptr(); x++ , length++ ) ; 00066 //getting in a null terminated buffer the characters 00067 memcpy( this->m_callbackBuff, this->pbase(), min(length, 512) ); 00068 this->m_callbackBuff[ min(length, 512) ] = '\0'; 00069 //calling the python method 00070 GaudiPython::call_python_method(this->m_self, "_sync", this->m_callbackBuff); 00071 //reseting in/out buffer pointers 00072 setp( this->pbase() , this->pbase() ); 00073 setg( this->eback(), this->eback(), this->eback() ); 00074 return 0; 00075 }