![]() |
|
|
Generated: 18 Jul 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, char* method, char* buf ) 00024 { 00025 if ( 0 == self || 0 == method ) { return 1 ; } 00026 PyObject* r = PyObject_CallMethod ( self , method , "s", buf ) ; 00027 if ( 0 == r ) { 00028 string err("Unsuccsesful call to bound Python method"); 00029 err += method; 00030 PyErr_SetString( PyExc_TypeError , err.c_str() ) ; 00031 PyErr_Print() ; 00032 return 1 ; 00033 } 00034 Py_DECREF( r ); 00035 return 0 ; 00036 } 00037 00038 // ============================================================================ 00040 // ============================================================================ 00041 GaudiPython::CallbackStreamBuf::CallbackStreamBuf 00042 (PyObject* self): stringbuf(), m_self(self) 00043 { 00044 Py_INCREF(this->m_self); 00045 this->m_callbackBuff = new char[512]; //used for passing the flushed chars in the python callback 00046 } 00047 00048 // ============================================================================ 00050 // ============================================================================ 00051 GaudiPython::CallbackStreamBuf::~CallbackStreamBuf() 00052 { 00053 Py_DECREF( this->m_self ); 00054 delete this->m_callbackBuff; 00055 } 00056 00057 // ============================================================================ 00059 // ============================================================================ 00060 int GaudiPython::CallbackStreamBuf::sync () 00061 { 00062 int length; 00063 char *x; 00064 for ( length = 0, x = this->pbase(); x < this->epptr(); x++ , length++ ) ; 00065 //getting in a null terminated buffer the characters 00066 memcpy( this->m_callbackBuff, this->pbase(), min(length, 512) ); 00067 this->m_callbackBuff[ min(length, 512) ] = '\0'; 00068 //calling the python method 00069 GaudiPython::call_python_method(this->m_self, "_sync", this->m_callbackBuff); 00070 //reseting in/out buffer pointers 00071 setp( this->pbase() , this->pbase() ); 00072 setg( this->eback(), this->eback(), this->eback() ); 00073 return 0; 00074 }