F:/KPlato/koffice/libs/kross/python/cxx/cxx_extensions.cxx

Aller à la documentation de ce fichier.
00001 #include "Extensions.hxx"
00002 #include "Exception.hxx"
00003 
00004 #include <assert.h>
00005 
00006 namespace Py 
00007 {
00008 //================================================================================
00009 //
00010 //      Implementation of MethodTable
00011 //
00012 //================================================================================
00013 
00014 PyMethodDef MethodTable::method( const char* method_name, PyCFunction f, int flags, const char* doc ) 
00015         {
00016         PyMethodDef m;
00017         m.ml_name = const_cast<char*>( method_name );
00018         m.ml_meth = f;
00019         m.ml_flags = flags;
00020         m.ml_doc = const_cast<char*>( doc );
00021         return m;
00022         }
00023 
00024 MethodTable::MethodTable()
00025         {
00026         t.push_back( method( 0, 0, 0, 0 ) );
00027         mt = 0;
00028         }
00029 
00030 MethodTable::~MethodTable()
00031         {
00032         delete [] mt;
00033         }
00034 
00035 void MethodTable::add( const char* method_name, PyCFunction f, const char* doc, int flag )
00036         {
00037         if( !mt )
00038                 {
00039                 t.insert( t.end()-1, method( method_name, f, flag, doc ) );
00040                 }
00041         else
00042                 {
00043                 throw RuntimeError( "Too late to add a module method!" );
00044                 }
00045         }
00046 
00047 PyMethodDef* MethodTable::table()
00048         {    
00049         if( !mt )
00050                 {
00051                 int t1size = t.size();
00052                 mt = new PyMethodDef[t1size];
00053                 int j = 0;
00054                 for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
00055                         {
00056                         mt[j++] = *i;
00057                         }
00058                 }
00059         return mt;
00060         }
00061 
00062 //================================================================================
00063 //
00064 //      Implementation of ExtensionModule
00065 //
00066 //================================================================================
00067 ExtensionModuleBase::ExtensionModuleBase( const char *name )
00068         : module_name( name )
00069         , full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : module_name )
00070         , method_table()
00071         {}
00072 
00073 ExtensionModuleBase::~ExtensionModuleBase()
00074         {}
00075 
00076 const std::string &ExtensionModuleBase::name() const
00077         {
00078         return module_name;
00079         }
00080 
00081 const std::string &ExtensionModuleBase::fullName() const
00082         {
00083         return full_module_name;
00084         }
00085 
00086 class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
00087         {
00088 public:
00089         ExtensionModuleBasePtr( ExtensionModuleBase *_module )
00090                 : module( _module )
00091                 {}
00092         virtual ~ExtensionModuleBasePtr()
00093                 {}
00094 
00095         ExtensionModuleBase *module;
00096         };
00097 
00098 
00099 void ExtensionModuleBase::initialize( const char *module_doc )
00100         {
00101         PyObject *module_ptr = new ExtensionModuleBasePtr( this );
00102 
00103         Py_InitModule4
00104         (
00105         const_cast<char *>( module_name.c_str() ),      // name
00106         method_table.table(),                           // methods
00107         const_cast<char *>( module_doc ),               // docs
00108         module_ptr,                                     // pass to functions as "self"
00109         PYTHON_API_VERSION                              // API version
00110         );
00111         }
00112 
00113 Py::Module ExtensionModuleBase::module(void) const
00114         {
00115         return Module( full_module_name );
00116         }
00117 
00118 Py::Dict ExtensionModuleBase::moduleDictionary(void) const
00119         {
00120         return module().getDict();
00121         }
00122 
00123 //--------------------------------------------------------------------------------
00124 
00125 //================================================================================
00126 //
00127 //      Implementation of PythonType
00128 //
00129 //================================================================================
00130 
00131 extern "C"
00132         {
00133         static void standard_dealloc(PyObject* p);
00134         //
00135         // All the following functions redirect the call from Python
00136         // onto the matching virtual function in PythonExtensionBase
00137         //
00138         static int print_handler (PyObject*, FILE *, int);
00139         static PyObject* getattr_handler (PyObject*, char*);
00140         static int setattr_handler (PyObject*, char*, PyObject*);
00141         static PyObject* getattro_handler (PyObject*, PyObject*);
00142         static int setattro_handler (PyObject*, PyObject*, PyObject*);
00143         static int compare_handler (PyObject*, PyObject*);
00144         static PyObject* repr_handler (PyObject*);
00145         static PyObject* str_handler (PyObject*);
00146         static long hash_handler (PyObject*);
00147         static PyObject* call_handler (PyObject*, PyObject*, PyObject*);
00148         static PyObject* iter_handler (PyObject*);
00149         static PyObject* iternext_handler (PyObject*);
00150 
00151 #if PY_VERSION_HEX < 0x02050000
00152         typedef int Py_ssize_t;
00153 #endif
00154 
00155         // Sequence methods
00156         static Py_ssize_t sequence_length_handler(PyObject*);
00157         static PyObject* sequence_concat_handler(PyObject*,PyObject*);
00158         static PyObject* sequence_repeat_handler(PyObject*, Py_ssize_t);
00159         static PyObject* sequence_item_handler(PyObject*, Py_ssize_t);
00160         static PyObject* sequence_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t);
00161         static int sequence_ass_item_handler(PyObject*, Py_ssize_t, PyObject*);
00162         static int sequence_ass_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*);
00163         // Mapping
00164         static Py_ssize_t mapping_length_handler(PyObject*);
00165         static PyObject* mapping_subscript_handler(PyObject*, PyObject*);
00166         static int mapping_ass_subscript_handler(PyObject*, PyObject*, PyObject*);
00167 
00168         // Numeric methods
00169         static int number_nonzero_handler (PyObject*);
00170         static PyObject* number_negative_handler (PyObject*);
00171         static PyObject* number_positive_handler (PyObject*);
00172         static PyObject* number_absolute_handler (PyObject*);
00173         static PyObject* number_invert_handler (PyObject*);
00174         static PyObject* number_int_handler (PyObject*);
00175         static PyObject* number_float_handler (PyObject*);
00176         static PyObject* number_long_handler (PyObject*);
00177         static PyObject* number_oct_handler (PyObject*);
00178         static PyObject* number_hex_handler (PyObject*);
00179         static PyObject* number_add_handler (PyObject*, PyObject*);
00180         static PyObject* number_subtract_handler (PyObject*, PyObject*);
00181         static PyObject* number_multiply_handler (PyObject*, PyObject*);
00182         static PyObject* number_divide_handler (PyObject*, PyObject*);
00183         static PyObject* number_remainder_handler (PyObject*, PyObject*);
00184         static PyObject* number_divmod_handler (PyObject*, PyObject*);
00185         static PyObject* number_lshift_handler (PyObject*, PyObject*);
00186         static PyObject* number_rshift_handler (PyObject*, PyObject*);
00187         static PyObject* number_and_handler (PyObject*, PyObject*);
00188         static PyObject* number_xor_handler (PyObject*, PyObject*);
00189         static PyObject* number_or_handler (PyObject*, PyObject*);
00190         static PyObject* number_power_handler(PyObject*, PyObject*, PyObject*);
00191 
00192         // Buffer
00193         static Py_ssize_t buffer_getreadbuffer_handler (PyObject*, Py_ssize_t, void**);
00194         static Py_ssize_t buffer_getwritebuffer_handler (PyObject*, Py_ssize_t, void**);
00195         static Py_ssize_t buffer_getsegcount_handler (PyObject*, Py_ssize_t*);
00196         }
00197 
00198 
00199 extern "C" void standard_dealloc( PyObject* p )
00200         {
00201         PyMem_DEL( p );
00202         }
00203 
00204 void PythonType::supportSequenceType()
00205         {
00206         if( !sequence_table )
00207                 {
00208                 sequence_table = new PySequenceMethods;
00209                 memset( sequence_table, 0, sizeof( PySequenceMethods ) );   // ensure new fields are 0
00210                 table->tp_as_sequence = sequence_table;
00211                 sequence_table->sq_length = sequence_length_handler;
00212                 sequence_table->sq_concat = sequence_concat_handler;
00213                 sequence_table->sq_repeat = sequence_repeat_handler;
00214                 sequence_table->sq_item = sequence_item_handler;
00215                 sequence_table->sq_slice = sequence_slice_handler;
00216 
00217                 sequence_table->sq_ass_item = sequence_ass_item_handler;        // BAS setup separately?
00218                 sequence_table->sq_ass_slice = sequence_ass_slice_handler;      // BAS setup separately?
00219                 }
00220         }
00221 
00222 void PythonType::supportMappingType()
00223         {
00224         if( !mapping_table )
00225                 {
00226                 mapping_table = new PyMappingMethods;
00227                 memset( mapping_table, 0, sizeof( PyMappingMethods ) );   // ensure new fields are 0
00228                 table->tp_as_mapping = mapping_table;
00229                 mapping_table->mp_length = mapping_length_handler;
00230                 mapping_table->mp_subscript = mapping_subscript_handler;
00231                 mapping_table->mp_ass_subscript = mapping_ass_subscript_handler;        // BAS setup separately?
00232                 }
00233         }
00234 
00235 void PythonType::supportNumberType()
00236         {
00237         if( !number_table )
00238                 {
00239                 number_table = new PyNumberMethods;
00240                 memset( number_table, 0, sizeof( PyNumberMethods ) );   // ensure new fields are 0
00241                 table->tp_as_number = number_table;
00242                 number_table->nb_add = number_add_handler;
00243                 number_table->nb_subtract = number_subtract_handler;
00244                 number_table->nb_multiply = number_multiply_handler;
00245                 number_table->nb_divide = number_divide_handler;
00246                 number_table->nb_remainder = number_remainder_handler;
00247                 number_table->nb_divmod = number_divmod_handler;
00248                 number_table->nb_power = number_power_handler;
00249                 number_table->nb_negative = number_negative_handler;
00250                 number_table->nb_positive = number_positive_handler;
00251                 number_table->nb_absolute = number_absolute_handler;
00252                 number_table->nb_nonzero = number_nonzero_handler;
00253                 number_table->nb_invert = number_invert_handler;
00254                 number_table->nb_lshift = number_lshift_handler;
00255                 number_table->nb_rshift = number_rshift_handler;
00256                 number_table->nb_and = number_and_handler;
00257                 number_table->nb_xor = number_xor_handler;
00258                 number_table->nb_or = number_or_handler;
00259                 number_table->nb_coerce = 0;
00260                 number_table->nb_int = number_int_handler;
00261                 number_table->nb_long = number_long_handler;
00262                 number_table->nb_float = number_float_handler;
00263                 number_table->nb_oct = number_oct_handler;
00264                 number_table->nb_hex = number_hex_handler;
00265                 }
00266         }
00267 
00268 void PythonType::supportBufferType()
00269         {
00270         if( !buffer_table )
00271                 {
00272                 buffer_table = new PyBufferProcs;
00273                 memset( buffer_table, 0, sizeof( PyBufferProcs ) );   // ensure new fields are 0
00274                 table->tp_as_buffer = buffer_table;
00275                 buffer_table->bf_getreadbuffer = buffer_getreadbuffer_handler;
00276                 buffer_table->bf_getwritebuffer = buffer_getwritebuffer_handler;
00277                 buffer_table->bf_getsegcount = buffer_getsegcount_handler;
00278                 }
00279         }
00280 
00281 // if you define one sequence method you must define 
00282 // all of them except the assigns
00283 
00284 PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
00285         : table( new PyTypeObject )
00286         , sequence_table( NULL )
00287         , mapping_table( NULL )
00288         , number_table( NULL )
00289         , buffer_table( NULL )
00290         {
00291         memset( table, 0, sizeof( PyTypeObject ) );   // ensure new fields are 0
00292         *reinterpret_cast<PyObject*>( table ) = py_object_initializer;
00293         table->ob_type = _Type_Type();
00294         table->ob_size = 0;
00295         table->tp_name = const_cast<char *>( default_name );
00296         table->tp_basicsize = basic_size;
00297         table->tp_itemsize = itemsize;
00298         table->tp_dealloc = ( destructor ) standard_dealloc;
00299         table->tp_print = 0;
00300         table->tp_getattr = 0;
00301         table->tp_setattr = 0;
00302         table->tp_compare = 0;
00303         table->tp_repr = 0;
00304         table->tp_as_number = 0;
00305         table->tp_as_sequence = 0;
00306         table->tp_as_mapping =  0;
00307         table->tp_hash = 0;
00308         table->tp_call = 0;
00309         table->tp_str = 0;
00310         table->tp_getattro = 0;
00311         table->tp_setattro = 0;
00312         table->tp_as_buffer = 0;
00313         table->tp_flags = Py_TPFLAGS_DEFAULT;
00314         table->tp_doc = 0;
00315 #if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 0)
00316         // first use in 2.0
00317         table->tp_traverse = 0L;
00318         table->tp_clear = 0L;
00319 #else
00320         table->tp_xxx5 = 0L;
00321         table->tp_xxx6 = 0L;
00322 #endif
00323 #if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
00324         // first defined in 2.1
00325         table->tp_richcompare = 0L;
00326         table->tp_weaklistoffset = 0L;
00327 #else
00328         table->tp_xxx7 = 0L;
00329         table->tp_xxx8 = 0L;
00330 #endif
00331 
00332 #if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 2)
00333         // first defined in 2.3
00334         table->tp_iter = 0L;
00335         table->tp_iternext = 0L;
00336 #endif
00337 
00338 #ifdef COUNT_ALLOCS
00339         table->tp_alloc = 0;
00340         table->tp_free = 0;
00341         table->tp_maxalloc = 0;
00342         table->tp_next = 0;
00343 #endif
00344         }
00345 
00346 PythonType::~PythonType( )
00347         {
00348         delete table;
00349         delete sequence_table;
00350         delete mapping_table;
00351         delete number_table;
00352         delete buffer_table;
00353         }
00354 
00355 PyTypeObject* PythonType::type_object( ) const
00356         {return table;}
00357 
00358 void PythonType::name( const char* nam )
00359         {
00360         table->tp_name = const_cast<char *>( nam );
00361         }
00362 
00363 const char *PythonType::getName() const
00364         {
00365         return table->tp_name;
00366         }
00367 
00368 void PythonType::doc( const char* d )
00369         {
00370         table->tp_doc = const_cast<char *>( d );
00371         }
00372 
00373 const char *PythonType::getDoc() const
00374         {
00375         return table->tp_doc;
00376         }
00377 
00378 void PythonType::dealloc( void( *f )( PyObject* ))
00379         {
00380         table->tp_dealloc = f;
00381         }
00382 
00383 void PythonType::supportPrint()
00384         {
00385         table->tp_print = print_handler;
00386         }
00387 
00388 void PythonType::supportGetattr()
00389         {
00390         table->tp_getattr = getattr_handler;
00391         }
00392 
00393 void PythonType::supportSetattr()
00394         {
00395         table->tp_setattr = setattr_handler;
00396         }
00397 
00398 void PythonType::supportGetattro()
00399         {
00400         table->tp_getattro = getattro_handler;
00401         }
00402 
00403 void PythonType::supportSetattro()
00404         {
00405         table->tp_setattro = setattro_handler;
00406         }
00407 
00408 void PythonType::supportCompare()
00409         {
00410         table->tp_compare = compare_handler;
00411         }
00412 
00413 void PythonType::supportRepr()
00414         {
00415         table->tp_repr = repr_handler;
00416         }
00417 
00418 void PythonType::supportStr()
00419         {
00420         table->tp_str = str_handler;
00421         }
00422 
00423 void PythonType::supportHash()
00424         {
00425         table->tp_hash = hash_handler;
00426         }
00427 
00428 void PythonType::supportCall()
00429         {
00430         table->tp_call = call_handler;
00431         }
00432 
00433 void PythonType::supportIter()
00434         {
00435         table->tp_iter = iter_handler;
00436         table->tp_iternext = iternext_handler;
00437         }
00438 
00439 //--------------------------------------------------------------------------------
00440 //
00441 //      Handlers
00442 //
00443 //--------------------------------------------------------------------------------
00444 extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
00445         {
00446         try
00447                 {
00448                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00449                 return p->print( fp, flags );
00450                 }
00451         catch( Py::Exception & )
00452                 {
00453                 return -1;      // indicate error
00454                 }
00455         }
00456 
00457 extern "C" PyObject* getattr_handler( PyObject *self, char *name )
00458         {
00459         try
00460                 {
00461                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00462                 return new_reference_to( p->getattr( name ) );
00463                 }
00464         catch( Py::Exception & )
00465                 {
00466                 return NULL;    // indicate error
00467                 }
00468         }
00469 
00470 extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
00471         {
00472         try
00473                 {
00474                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00475                 return p->setattr( name, Py::Object( value ) );
00476                 }
00477         catch( Py::Exception & )
00478                 {
00479                 return -1;      // indicate error
00480                 }
00481         }
00482 
00483 extern "C" PyObject* getattro_handler( PyObject *self, PyObject *name )
00484         {
00485         try
00486                 {
00487                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00488                 return new_reference_to( p->getattro( Py::Object( name ) ) );
00489                 }
00490         catch( Py::Exception & )
00491                 {
00492                 return NULL;    // indicate error
00493                 }
00494         }
00495 
00496 extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
00497         {
00498         try
00499                 {
00500                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00501                 return p->setattro( Py::Object( name ), Py::Object( value ) );
00502                 }
00503         catch( Py::Exception & )
00504                 {
00505                 return -1;      // indicate error
00506                 }
00507         }
00508 
00509 extern "C" int compare_handler( PyObject *self, PyObject *other )
00510         {
00511         try
00512                 {
00513                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00514                 return p->compare( Py::Object( other ) );
00515                 }
00516         catch( Py::Exception & )
00517                 {
00518                 return -1;      // indicate error
00519                 }
00520         }
00521 
00522 extern "C" PyObject* repr_handler( PyObject *self )
00523         {
00524         try
00525                 {
00526                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00527                 return new_reference_to( p->repr() );
00528                 }
00529         catch( Py::Exception & )
00530                 {
00531                 return NULL;    // indicate error
00532                 }
00533         }
00534 
00535 extern "C" PyObject* str_handler( PyObject *self )
00536         {
00537         try
00538                 {
00539                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00540                 return new_reference_to( p->str() );
00541                 }
00542         catch( Py::Exception & )
00543                 {
00544                 return NULL;    // indicate error
00545                 }
00546         }
00547 
00548 extern "C" long hash_handler( PyObject *self )
00549         {
00550         try
00551                 {
00552                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00553                 return p->hash();
00554                 }
00555         catch( Py::Exception & )
00556                 {
00557                 return -1;      // indicate error
00558                 }
00559         }
00560 
00561 extern "C" PyObject* call_handler( PyObject *self, PyObject *args, PyObject *kw )
00562         {
00563         try
00564                 {
00565                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00566                 return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
00567                 if( kw != NULL )
00568                 return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
00569                 else
00570                 return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
00571                 }
00572         catch( Py::Exception & )
00573                 {
00574                 return NULL;    // indicate error
00575                 }
00576         }
00577 
00578 extern "C" PyObject* iter_handler( PyObject *self )
00579         {
00580         try
00581                 {
00582                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00583                 return new_reference_to( p->iter() );
00584                 }
00585         catch( Py::Exception & )
00586                 {
00587                 return NULL;    // indicate error
00588                 }
00589         }
00590 
00591 extern "C" PyObject* iternext_handler( PyObject *self )
00592         {
00593         try
00594                 {
00595                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00596                 return p->iternext();  // might be a NULL ptr on end of iteration
00597                 }
00598         catch( Py::Exception & )
00599                 {
00600                 return NULL;    // indicate error
00601                 }
00602         }
00603 
00604 
00605 // Sequence methods
00606 extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
00607         {
00608         try
00609                 {
00610                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00611                 return p->sequence_length();
00612                 }
00613         catch( Py::Exception & )
00614                 {
00615                 return -1;      // indicate error
00616                 }
00617         }
00618 
00619 extern "C" PyObject* sequence_concat_handler( PyObject *self, PyObject *other )
00620         {
00621         try
00622                 {
00623                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00624                 return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
00625                 }
00626         catch( Py::Exception & )
00627                 {
00628                 return NULL;    // indicate error
00629                 }
00630         }
00631 
00632 extern "C" PyObject* sequence_repeat_handler( PyObject *self, Py_ssize_t count )
00633         {
00634         try
00635                 {
00636                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00637                 return new_reference_to( p->sequence_repeat( count ) );
00638                 }
00639         catch( Py::Exception & )
00640                 {
00641                 return NULL;    // indicate error
00642                 }
00643         }
00644 
00645 extern "C" PyObject* sequence_item_handler( PyObject *self, Py_ssize_t index )
00646         {
00647         try
00648                 {
00649                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00650                 return new_reference_to( p->sequence_item( index ) );
00651                 }
00652         catch( Py::Exception & )
00653                 {
00654                 return NULL;    // indicate error
00655                 }
00656         }
00657 
00658 extern "C" PyObject* sequence_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last )
00659         {
00660         try
00661                 {
00662                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00663                 return new_reference_to( p->sequence_slice( first, last ) );
00664                 }
00665         catch( Py::Exception & )
00666                 {
00667                 return NULL;    // indicate error
00668                 }
00669         }
00670 
00671 extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
00672         {
00673         try
00674                 {
00675                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00676                 return p->sequence_ass_item( index, Py::Object( value ) );
00677                 }
00678         catch( Py::Exception & )
00679                 {
00680                 return -1;      // indicate error
00681                 }
00682         }
00683 
00684 extern "C" int sequence_ass_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *value )
00685         {
00686         try
00687                 {
00688                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00689                 return p->sequence_ass_slice( first, last, Py::Object( value ) );
00690                 }
00691         catch( Py::Exception & )
00692                 {
00693                 return -1;      // indicate error
00694                 }
00695         }
00696 
00697 // Mapping
00698 extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
00699         {
00700         try
00701                 {
00702                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00703                 return p->mapping_length();
00704                 }
00705         catch( Py::Exception & )
00706                 {
00707                 return -1;      // indicate error
00708                 }
00709         }
00710 
00711 extern "C" PyObject* mapping_subscript_handler( PyObject *self, PyObject *key )
00712         {
00713         try
00714                 {
00715                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00716                 return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
00717                 }
00718         catch( Py::Exception & )
00719                 {
00720                 return NULL;    // indicate error
00721                 }
00722         }
00723 
00724 extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
00725         {
00726         try
00727                 {
00728                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00729                 return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
00730                 }
00731         catch( Py::Exception & )
00732                 {
00733                 return -1;      // indicate error
00734                 }
00735         }
00736 
00737 // Number
00738 extern "C" int number_nonzero_handler( PyObject *self )
00739         {
00740         try
00741                 {
00742                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00743                 return p->number_nonzero();
00744                 }
00745         catch( Py::Exception & )
00746                 {
00747                 return -1;      // indicate error
00748                 }
00749         }
00750 
00751 extern "C" PyObject* number_negative_handler( PyObject *self )
00752         {
00753         try
00754                 {
00755                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00756                 return new_reference_to( p->number_negative() );
00757                 }
00758         catch( Py::Exception & )
00759                 {
00760                 return NULL;    // indicate error
00761                 }
00762         }
00763 
00764 extern "C" PyObject* number_positive_handler( PyObject *self )
00765         {
00766         try
00767                 {
00768                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00769                 return new_reference_to( p->number_positive() );
00770                 }
00771         catch( Py::Exception & )
00772                 {
00773                 return NULL;    // indicate error
00774                 }
00775         }
00776 
00777 extern "C" PyObject* number_absolute_handler( PyObject *self )
00778         {
00779         try
00780                 {
00781                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00782                 return new_reference_to( p->number_absolute() );
00783                 }
00784         catch( Py::Exception & )
00785                 {
00786                 return NULL;    // indicate error
00787                 }
00788         }
00789 
00790 extern "C" PyObject* number_invert_handler( PyObject *self )
00791         {
00792         try
00793                 {
00794                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00795                 return new_reference_to( p->number_invert() );
00796                 }
00797         catch( Py::Exception & )
00798                 {
00799                 return NULL;    // indicate error
00800                 }
00801         }
00802 
00803 extern "C" PyObject* number_int_handler( PyObject *self )
00804         {
00805         try
00806                 {
00807                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00808                 return new_reference_to( p->number_int() );
00809                 }
00810         catch( Py::Exception & )
00811                 {
00812                 return NULL;    // indicate error
00813                 }
00814         }
00815 
00816 extern "C" PyObject* number_float_handler( PyObject *self )
00817         {
00818         try
00819                 {
00820                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00821                 return new_reference_to( p->number_float() );
00822                 }
00823         catch( Py::Exception & )
00824                 {
00825                 return NULL;    // indicate error
00826                 }
00827         }
00828 
00829 extern "C" PyObject* number_long_handler( PyObject *self )
00830         {
00831         try
00832                 {
00833                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00834                 return new_reference_to( p->number_long() );
00835                 }
00836         catch( Py::Exception & )
00837                 {
00838                 return NULL;    // indicate error
00839                 }
00840         }
00841 
00842 extern "C" PyObject* number_oct_handler( PyObject *self )
00843         {
00844         try
00845                 {
00846                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00847                 return new_reference_to( p->number_oct() );
00848                 }
00849         catch( Py::Exception & )
00850                 {
00851                 return NULL;    // indicate error
00852                 }
00853         }
00854 
00855 extern "C" PyObject* number_hex_handler( PyObject *self )
00856         {
00857         try
00858                 {
00859                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00860                 return new_reference_to( p->number_hex() );
00861                 }
00862         catch( Py::Exception & )
00863                 {
00864                 return NULL;    // indicate error
00865                 }
00866         }
00867 
00868 extern "C" PyObject* number_add_handler( PyObject *self, PyObject *other )
00869         {
00870         try
00871                 {
00872                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00873                 return new_reference_to( p->number_add( Py::Object( other ) ) );
00874                 }
00875         catch( Py::Exception & )
00876                 {
00877                 return NULL;    // indicate error
00878                 }
00879         }
00880 
00881 extern "C" PyObject* number_subtract_handler( PyObject *self, PyObject *other )
00882         {
00883         try
00884                 {
00885                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00886                 return new_reference_to( p->number_subtract( Py::Object( other ) ) );
00887                 }
00888         catch( Py::Exception & )
00889                 {
00890                 return NULL;    // indicate error
00891                 }
00892         }
00893 
00894 extern "C" PyObject* number_multiply_handler( PyObject *self, PyObject *other )
00895         {
00896         try
00897                 {
00898                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00899                 return new_reference_to( p->number_multiply( Py::Object( other ) ) );
00900                 }
00901         catch( Py::Exception & )
00902                 {
00903                 return NULL;    // indicate error
00904                 }
00905         }
00906 
00907 extern "C" PyObject* number_divide_handler( PyObject *self, PyObject *other )
00908         {
00909         try
00910                 {
00911                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00912                 return new_reference_to( p->number_divide( Py::Object( other ) ) );
00913                 }
00914         catch( Py::Exception & )
00915                 {
00916                 return NULL;    // indicate error
00917                 }
00918         }
00919 
00920 extern "C" PyObject* number_remainder_handler( PyObject *self, PyObject *other )
00921         {
00922         try
00923                 {
00924                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00925                 return new_reference_to( p->number_remainder( Py::Object( other ) ) );
00926                 }
00927         catch( Py::Exception & )
00928                 {
00929                 return NULL;    // indicate error
00930                 }
00931         }
00932 
00933 extern "C" PyObject* number_divmod_handler( PyObject *self, PyObject *other )
00934         {
00935         try
00936                 {
00937                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00938                 return new_reference_to( p->number_divmod( Py::Object( other ) ) );
00939                 }
00940         catch( Py::Exception & )
00941                 {
00942                 return NULL;    // indicate error
00943                 }
00944         }
00945 
00946 extern "C" PyObject* number_lshift_handler( PyObject *self, PyObject *other )
00947         {
00948         try
00949                 {
00950                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00951                 return new_reference_to( p->number_lshift( Py::Object( other ) ) );
00952                 }
00953         catch( Py::Exception & )
00954                 {
00955                 return NULL;    // indicate error
00956                 }
00957         }
00958 
00959 extern "C" PyObject* number_rshift_handler( PyObject *self, PyObject *other )
00960         {
00961         try
00962                 {
00963                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00964                 return new_reference_to( p->number_rshift( Py::Object( other ) ) );
00965                 }
00966         catch( Py::Exception & )
00967                 {
00968                 return NULL;    // indicate error
00969                 }
00970         }
00971 
00972 extern "C" PyObject* number_and_handler( PyObject *self, PyObject *other )
00973         {
00974         try
00975                 {
00976                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00977                 return new_reference_to( p->number_and( Py::Object( other ) ) );
00978                 }
00979         catch( Py::Exception & )
00980                 {
00981                 return NULL;    // indicate error
00982                 }
00983         }
00984 
00985 extern "C" PyObject* number_xor_handler( PyObject *self, PyObject *other )
00986         {
00987         try
00988                 {
00989                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
00990                 return new_reference_to( p->number_xor( Py::Object( other ) ) );
00991                 }
00992         catch( Py::Exception & )
00993                 {
00994                 return NULL;    // indicate error
00995                 }
00996         }
00997 
00998 extern "C" PyObject* number_or_handler( PyObject *self, PyObject *other )
00999         {
01000         try
01001                 {
01002                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
01003                 return new_reference_to( p->number_or( Py::Object( other ) ) );
01004                 }
01005         catch( Py::Exception & )
01006                 {
01007                 return NULL;    // indicate error
01008                 }
01009         }
01010 
01011 extern "C" PyObject* number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
01012         {
01013         try
01014                 {
01015                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
01016                 return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
01017                 }
01018         catch( Py::Exception & )
01019                 {
01020                 return NULL;    // indicate error
01021                 }
01022         }
01023 
01024 // Buffer
01025 extern "C" Py_ssize_t buffer_getreadbuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
01026         {
01027         try
01028                 {
01029                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
01030                 return p->buffer_getreadbuffer( index, pp );
01031                 }
01032         catch( Py::Exception & )
01033                 {
01034                 return -1;      // indicate error
01035                 }
01036         }
01037 
01038 extern "C" Py_ssize_t buffer_getwritebuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
01039         {
01040         try
01041                 {
01042                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
01043                 return p->buffer_getwritebuffer( index, pp );
01044                 }
01045         catch( Py::Exception & )
01046                 {
01047                 return -1;      // indicate error
01048                 }
01049         }
01050 
01051 extern "C" Py_ssize_t buffer_getsegcount_handler( PyObject *self, Py_ssize_t *count )
01052         {
01053         try
01054                 {
01055                 PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
01056                 int i_count = *count;
01057                 Py_ssize_t r = p->buffer_getsegcount( &i_count );
01058                 *count = i_count;
01059                 return r;
01060                 }
01061         catch( Py::Exception & )
01062                 {
01063                 return -1;      // indicate error
01064                 }
01065         }
01066 
01067 
01068 //================================================================================
01069 //
01070 //      Implementation of PythonExtensionBase
01071 //
01072 //================================================================================
01073 #define missing_method( method ) \
01074 throw RuntimeError( "Extension object does not support method " #method );
01075 
01076 PythonExtensionBase::PythonExtensionBase()
01077         {
01078         }
01079 
01080 PythonExtensionBase::~PythonExtensionBase()
01081         {
01082         assert( ob_refcnt == 0 );
01083         }
01084 
01085 int PythonExtensionBase::print( FILE *, int )
01086         { missing_method( print ); return -1; }
01087 
01088 int PythonExtensionBase::setattr( const char*, const Py::Object & )
01089         { missing_method( setattr ); return -1; }
01090 
01091 Py::Object PythonExtensionBase::getattro( const Py::Object & )
01092         { missing_method( getattro ); return Py::Nothing(); }
01093 
01094 int PythonExtensionBase::setattro( const Py::Object &, const Py::Object & )
01095         { missing_method( setattro ); return -1; }
01096 
01097 int PythonExtensionBase::compare( const Py::Object & )
01098         { missing_method( compare ); return -1; }
01099 
01100 Py::Object PythonExtensionBase::repr()
01101         { missing_method( repr ); return Py::Nothing(); }
01102 
01103 Py::Object PythonExtensionBase::str()
01104         { missing_method( str ); return Py::Nothing(); }
01105 
01106 long PythonExtensionBase::hash()
01107         { missing_method( hash ); return -1; }
01108 
01109 Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & )
01110         { missing_method( call ); return Py::Nothing(); }
01111 
01112 Py::Object PythonExtensionBase::iter()
01113         { missing_method( iter ); return Py::Nothing(); }
01114 
01115 PyObject* PythonExtensionBase::iternext()
01116         { missing_method( iternext ); return NULL; }
01117 
01118 
01119 // Sequence methods
01120 int PythonExtensionBase::sequence_length()
01121         { missing_method( sequence_length ); return -1; }
01122 
01123 Py::Object PythonExtensionBase::sequence_concat( const Py::Object & )
01124         { missing_method( sequence_concat ); return Py::Nothing(); }
01125 
01126 Py::Object PythonExtensionBase::sequence_repeat( int )
01127         { missing_method( sequence_repeat ); return Py::Nothing(); }
01128 
01129 Py::Object PythonExtensionBase::sequence_item( int )
01130         { missing_method( sequence_item ); return Py::Nothing(); }
01131 
01132 Py::Object PythonExtensionBase::sequence_slice( int, int )
01133         { missing_method( sequence_slice ); return Py::Nothing(); }
01134 
01135 int PythonExtensionBase::sequence_ass_item( int, const Py::Object & )
01136         { missing_method( sequence_ass_item ); return -1; }
01137 
01138 int PythonExtensionBase::sequence_ass_slice( int, int, const Py::Object & )
01139         { missing_method( sequence_ass_slice ); return -1; }
01140 
01141 
01142 // Mapping
01143 int PythonExtensionBase::mapping_length()
01144         { missing_method( mapping_length ); return -1; }
01145 
01146 Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & )
01147         { missing_method( mapping_subscript ); return Py::Nothing(); }
01148 
01149 int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & )
01150         { missing_method( mapping_ass_subscript ); return -1; }
01151 
01152 
01153 // Number
01154 int PythonExtensionBase::number_nonzero()
01155         { missing_method( number_nonzero ); return -1; }
01156 
01157 Py::Object PythonExtensionBase::number_negative()
01158         { missing_method( number_negative ); return Py::Nothing(); }
01159 
01160 Py::Object PythonExtensionBase::number_positive()
01161         { missing_method( number_positive ); return Py::Nothing(); }
01162 
01163 Py::Object PythonExtensionBase::number_absolute()
01164         { missing_method( number_absolute ); return Py::Nothing(); }
01165 
01166 Py::Object PythonExtensionBase::number_invert()
01167         { missing_method( number_invert ); return Py::Nothing(); }
01168 
01169 Py::Object PythonExtensionBase::number_int()
01170         { missing_method( number_int ); return Py::Nothing(); }
01171 
01172 Py::Object PythonExtensionBase::number_float()
01173         { missing_method( number_float ); return Py::Nothing(); }
01174 
01175 Py::Object PythonExtensionBase::number_long()
01176         { missing_method( number_long ); return Py::Nothing(); }
01177 
01178 Py::Object PythonExtensionBase::number_oct()
01179         { missing_method( number_oct ); return Py::Nothing(); }
01180 
01181 Py::Object PythonExtensionBase::number_hex()
01182         { missing_method( number_hex ); return Py::Nothing(); }
01183 
01184 Py::Object PythonExtensionBase::number_add( const Py::Object & )
01185         { missing_method( number_add ); return Py::Nothing(); }
01186 
01187 Py::Object PythonExtensionBase::number_subtract( const Py::Object & )
01188         { missing_method( number_subtract ); return Py::Nothing(); }
01189 
01190 Py::Object PythonExtensionBase::number_multiply( const Py::Object & )
01191         { missing_method( number_multiply ); return Py::Nothing(); }
01192 
01193 Py::Object PythonExtensionBase::number_divide( const Py::Object & )
01194         { missing_method( number_divide ); return Py::Nothing(); }
01195 
01196 Py::Object PythonExtensionBase::number_remainder( const Py::Object & )
01197         { missing_method( number_remainder ); return Py::Nothing(); }
01198 
01199 Py::Object PythonExtensionBase::number_divmod( const Py::Object & )
01200         { missing_method( number_divmod ); return Py::Nothing(); }
01201 
01202 Py::Object PythonExtensionBase::number_lshift( const Py::Object & )
01203         { missing_method( number_lshift ); return Py::Nothing(); }
01204 
01205 Py::Object PythonExtensionBase::number_rshift( const Py::Object & )
01206         { missing_method( number_rshift ); return Py::Nothing(); }
01207 
01208 Py::Object PythonExtensionBase::number_and( const Py::Object & )
01209         { missing_method( number_and ); return Py::Nothing(); }
01210 
01211 Py::Object PythonExtensionBase::number_xor( const Py::Object & )
01212         { missing_method( number_xor ); return Py::Nothing(); }
01213 
01214 Py::Object PythonExtensionBase::number_or( const Py::Object & )
01215         { missing_method( number_or ); return Py::Nothing(); }
01216 
01217 Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & )
01218         { missing_method( number_power ); return Py::Nothing(); }
01219 
01220 
01221 // Buffer
01222 int PythonExtensionBase::buffer_getreadbuffer( int, void** )
01223         { missing_method( buffer_getreadbuffer ); return -1; }
01224 
01225 int PythonExtensionBase::buffer_getwritebuffer( int, void** )
01226         { missing_method( buffer_getwritebuffer ); return -1; }
01227 
01228 int PythonExtensionBase::buffer_getsegcount( int* )
01229         { missing_method( buffer_getsegcount ); return -1; }
01230 
01231 //--------------------------------------------------------------------------------
01232 //
01233 //      Method call handlers for
01234 //              PythonExtensionBase
01235 //              ExtensionModuleBase
01236 //
01237 //--------------------------------------------------------------------------------
01238 
01239 extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
01240         {
01241         try
01242                 {
01243                 Tuple self_and_name_tuple( _self_and_name_tuple );
01244 
01245                 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
01246                 void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
01247                 if( self_as_void == NULL )
01248                         return NULL;
01249 
01250                 ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
01251 
01252                 String py_name( self_and_name_tuple[1] );
01253                 std::string name( py_name.as_std_string() );
01254 
01255                 Tuple args( _args );
01256                 if( _keywords == NULL )
01257                         {
01258                         Dict keywords;  // pass an empty dict
01259 
01260                         Object result( self->invoke_method_keyword( name, args, keywords ) );
01261                         return new_reference_to( result.ptr() );
01262                         }
01263 
01264                 Dict keywords( _keywords );
01265 
01266                 Object result( self->invoke_method_keyword( name, args, keywords ) );
01267                 return new_reference_to( result.ptr() );
01268                 }
01269         catch( Exception & )
01270                 {
01271                 return 0;
01272                 }
01273         }
01274 
01275 extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
01276         {
01277         try
01278                 {
01279                 Tuple self_and_name_tuple( _self_and_name_tuple );
01280 
01281                 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
01282                 void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
01283                 if( self_as_void == NULL )
01284                 return NULL;
01285 
01286                 ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
01287 
01288                 String py_name( self_and_name_tuple[1] );
01289                 std::string name( py_name.as_std_string() );
01290 
01291                 Tuple args( _args );
01292 
01293                 Object result( self->invoke_method_varargs( name, args ) );
01294 
01295                 return new_reference_to( result.ptr() );
01296                 }
01297         catch( Exception & )
01298                 {
01299                 return 0;
01300                 }
01301         }
01302 
01303 extern "C" void do_not_dealloc( void * )
01304         {}
01305 
01306 
01307 //--------------------------------------------------------------------------------
01308 //
01309 //      ExtensionExceptionType
01310 //
01311 //--------------------------------------------------------------------------------
01312 ExtensionExceptionType::ExtensionExceptionType()
01313         : Py::Object()
01314         {
01315         }
01316 
01317 void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
01318         {
01319         std::string module_name( module.fullName() );
01320         module_name += ".";
01321         module_name += name;
01322 
01323         set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
01324         }
01325 
01326 void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent)
01327         {
01328         std::string module_name( module.fullName() );
01329         module_name += ".";
01330         module_name += name;
01331 
01332         set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
01333         }
01334  
01335 ExtensionExceptionType::~ExtensionExceptionType()
01336         {
01337         }
01338 
01339 Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
01340         {
01341         PyErr_SetString (exception.ptr(), reason.c_str());
01342         }
01343 
01344 Exception::Exception( ExtensionExceptionType &exception, Object &reason )
01345         {
01346         PyErr_SetObject (exception.ptr(), reason.ptr());
01347         }
01348 
01349 Exception::Exception( PyObject* exception, Object &reason )
01350         {
01351         PyErr_SetObject (exception, reason.ptr());
01352         }               
01353 
01354 }       // end of namespace Py

Généré le Wed Nov 22 23:41:11 2006 pour KPlato par  doxygen 1.5.1-p1