Go to the documentation of this file.00001 #include "Python.h"
00002
00003 #include "GaudiKernel/Bootstrap.h"
00004
00005 #include <sstream>
00006
00007 #include "GaudiPython/GaudiPython.h"
00008
00009 #include "GaudiPython/CallbackStreamBuf.h"
00010
00011
00012 #define min(a,b) (((a)<(b))?(a):(b))
00013
00014 using namespace std;
00015
00016
00018
00019 int GaudiPython::call_python_method
00020 ( PyObject* self, const char* method, char* buf )
00021 {
00022 if ( 0 == self || 0 == method ) { return 1 ; }
00023 PyObject* r = PyObject_CallMethod(self, const_cast<char*>(method),
00024 const_cast<char*>("s"), buf);
00025 if ( 0 == r ) {
00026 string err("Unsuccessful call to bound Python method");
00027 err += method;
00028 PyErr_SetString( PyExc_TypeError , err.c_str() ) ;
00029 PyErr_Print() ;
00030 return 1 ;
00031 }
00032 Py_DECREF( r );
00033 return 0 ;
00034 }
00035
00036
00038
00039 GaudiPython::CallbackStreamBuf::CallbackStreamBuf
00040 (PyObject* self): stringbuf(), m_self(self)
00041 {
00042 Py_INCREF(this->m_self);
00043 this->m_callbackBuff = new char[512];
00044 }
00045
00046
00048
00049 GaudiPython::CallbackStreamBuf::~CallbackStreamBuf()
00050 {
00051 Py_DECREF( this->m_self );
00052 delete this->m_callbackBuff;
00053 }
00054
00055
00057
00058 int GaudiPython::CallbackStreamBuf::sync ()
00059 {
00060 int length;
00061 char *x;
00062 for ( length = 0, x = this->pbase(); x < this->epptr(); x++ , length++ ) ;
00063
00064 memcpy( this->m_callbackBuff, this->pbase(), min(length, 512) );
00065 this->m_callbackBuff[ min(length, 512) ] = '\0';
00066
00067 GaudiPython::call_python_method(this->m_self, "_sync", this->m_callbackBuff);
00068
00069 setp( this->pbase() , this->pbase() );
00070 setg( this->eback(), this->eback(), this->eback() );
00071 return 0;
00072 }