00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "hpi_classes.h"
00034
00035 namespace hpi
00036 {
00037
00038 PyObject* python_interface::load_module ( const char* name )
00039 {
00040 #ifdef HPI_VERBOSE
00041 fprintf ( stdout , "HPI: loading module %s\n" , name ) ;
00042 #endif
00043 PyObject* pModule = PyImport_ImportModule ( name );
00044 if ( ! pModule )
00045 {
00046 PyErr_Print () ;
00047 fprintf ( stderr , "HPI ERROR: Failed to load module %s\n" , name );
00048 return NULL ;
00049 }
00050 return pModule;
00051 }
00052
00053 python_interface::~python_interface()
00054 {
00055 if ( activated )
00056 {
00057 #ifdef HPI_VERBOSE
00058 fprintf ( stdout , "HPI: finalizing Python\n" ) ;
00059 #endif
00060
00061 Py_XDECREF ( hsi_module ) ;
00062 Py_XDECREF ( hpi_module ) ;
00063 Py_Finalize();
00064 }
00065 }
00066
00067 bool python_interface::activate()
00068 {
00069 if ( activated )
00070 {
00071 return true ;
00072 }
00073 #ifdef HPI_VERBOSE
00074 fprintf ( stdout , "HPI: initializing Python\n" ) ;
00075 #endif
00076 Py_Initialize();
00077
00078 hsi_module = load_module ( "hsi" ) ;
00079 if ( hsi_module )
00080 {
00081
00082 hpi_module = load_module ("hpi");
00083 if ( hpi_module )
00084 {
00085 activated = true ;
00086 }
00087 else
00088 {
00089 Py_DECREF ( hsi_module ) ;
00090 }
00091 }
00092 if ( ! activated )
00093 {
00094 Py_Finalize() ;
00095 }
00096 return activated ;
00097 }
00098
00099 int python_interface::call_hpi ( const char* hpi_func ,
00100 PyObject* pArgs )
00101 {
00102 int result = -1 ;
00103 #ifdef HPI_VERBOSE
00104 fprintf ( stdout , "HPI: calling %s\n" , hpi_func ) ;
00105 #endif
00106
00107 PyObject* pFunc = PyObject_GetAttrString ( hpi_module , hpi_func ) ;
00108
00109 if ( pFunc && PyCallable_Check ( pFunc ) )
00110 {
00111 PyObject* pValue = PyObject_CallObject ( pFunc , pArgs ) ;
00112 if ( pValue != NULL )
00113 {
00114
00115 result = PyInt_AsLong ( pValue ) ;
00116 Py_DECREF(pValue);
00117 }
00118 }
00119 Py_XDECREF ( pFunc ) ;
00120
00121 #ifdef HPI_VERBOSE
00122 fprintf ( stdout , "HPI: call returns: %d\n" , result );
00123 #endif
00124 return result ;
00125 };
00126
00127 PyObject* python_arglist::make_hsi_object ( const char* hsi_type ,
00128 void* hugin_value )
00129 {
00130 swig_type_info* swigtype = SWIG_Python_TypeQuery ( hsi_type );
00131 #ifdef HPI_VERBOSE
00132 fprintf ( stdout ,
00133 "HPI: making a %s from %p\n" ,
00134 hsi_type ,
00135 hugin_value );
00136 #endif
00137 if ( ! swigtype )
00138 {
00139 fprintf ( stderr,
00140 "HPI ERROR: can't find SWIG type for %s\n" ,
00141 hsi_type );
00142 return NULL;
00143 }
00144
00145
00146
00147 return SWIG_NewPointerObj ( hugin_value , swigtype , 0 );
00148 };
00149
00150 python_arglist::python_arglist ( int _argc ) : argc ( _argc ) , have ( 0 )
00151 {
00152 pArgs = PyTuple_New ( argc );
00153 }
00154
00155 python_arglist::~python_arglist()
00156 {
00157 Py_DECREF ( pArgs ) ;
00158 }
00159
00160 bool python_arglist::add ( PyObject* python_arg )
00161 {
00162 if ( ( ! python_arg ) || ( have >= argc ) )
00163 {
00164 return false ;
00165 }
00166 PyTuple_SetItem ( pArgs , have++ , python_arg ) ;
00167 return true ;
00168 }
00169
00170 bool python_arglist::add ( const char* str )
00171 {
00172 #ifdef HPI_VERBOSE
00173 fprintf ( stdout , "HPI: making a PyString from '%s'\n" , str ) ;
00174 #endif
00175
00176 #if PY_MAJOR_VERSION>=3
00177 return add ( PyUnicode_FromString(str));
00178 #else
00179 return add ( PyString_FromString(str));
00180 #endif
00181 }
00182
00183 PyObject* python_arglist::yield()
00184 {
00185 if ( argc != have )
00186 {
00187 return NULL ;
00188 }
00189 return pArgs ;
00190 }
00191
00192 }