1
//-----------------------------------------------------------------------------
3
// Copyright (c) 1998 - 2007, The Regents of the University of California
4
// Produced at the Lawrence Livermore National Laboratory
5
// All rights reserved.
7
// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
8
// full copyright notice is contained in the file COPYRIGHT located at the root
9
// of the PyCXX distribution.
11
// Redistribution and use in source and binary forms, with or without
12
// modification, are permitted provided that the following conditions are met:
14
// - Redistributions of source code must retain the above copyright notice,
15
// this list of conditions and the disclaimer below.
16
// - Redistributions in binary form must reproduce the above copyright notice,
17
// this list of conditions and the disclaimer (as noted below) in the
18
// documentation and/or materials provided with the distribution.
19
// - Neither the name of the UC/LLNL nor the names of its contributors may be
20
// used to endorse or promote products derived from this software without
21
// specific prior written permission.
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
// ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF
27
// CALIFORNIA, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR
28
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36
//-----------------------------------------------------------------------------
37
#include "CXX/Extensions.hxx"
38
#include "CXX/Exception.hxx"
45
void Object::validate()
47
// release pointer if not the right type
50
#if defined( _CPPRTTI ) || defined( __GNUG__ )
51
std::string s( "PyCXX: Error creating object of type " );
52
s += (typeid( *this )).name();
56
String from_repr = repr();
58
s += from_repr.as_std_string();
66
if( PyErr_Occurred() )
67
{ // Error message already set
70
// Better error message if RTTI available
71
#if defined( _CPPRTTI ) || defined( __GNUG__ )
74
throw TypeError( "PyCXX: type error." );
79
//================================================================================
81
// Implementation of MethodTable
83
//================================================================================
85
PyMethodDef MethodTable::method( const char* method_name, PyCFunction f, int flags, const char* doc )
88
m.ml_name = const_cast<char*>( method_name );
91
m.ml_doc = const_cast<char*>( doc );
95
MethodTable::MethodTable()
97
t.push_back( method( 0, 0, 0, 0 ) );
101
MethodTable::~MethodTable()
106
void MethodTable::add( const char* method_name, PyCFunction f, const char* doc, int flag )
110
t.insert( t.end()-1, method( method_name, f, flag, doc ) );
114
throw RuntimeError( "Too late to add a module method!" );
118
PyMethodDef* MethodTable::table()
122
Py_ssize_t t1size = t.size();
123
mt = new PyMethodDef[t1size];
125
for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
133
//================================================================================
135
// Implementation of ExtensionModule
137
//================================================================================
138
ExtensionModuleBase::ExtensionModuleBase( const char *name )
139
: module_name( name )
140
, full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : module_name )
144
ExtensionModuleBase::~ExtensionModuleBase()
147
const std::string &ExtensionModuleBase::name() const
152
const std::string &ExtensionModuleBase::fullName() const
154
return full_module_name;
157
class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
160
ExtensionModuleBasePtr( ExtensionModuleBase *_module )
163
virtual ~ExtensionModuleBasePtr()
166
ExtensionModuleBase *module;
170
void ExtensionModuleBase::initialize( const char *module_doc )
172
PyObject *module_ptr = new ExtensionModuleBasePtr( this );
176
const_cast<char *>( module_name.c_str() ), // name
177
method_table.table(), // methods
178
const_cast<char *>( module_doc ), // docs
179
module_ptr, // pass to functions as "self"
180
PYTHON_API_VERSION // API version
184
Py::Module ExtensionModuleBase::module(void) const
186
return Module( full_module_name );
189
Py::Dict ExtensionModuleBase::moduleDictionary(void) const
191
return module().getDict();
194
//--------------------------------------------------------------------------------
196
//================================================================================
198
// Implementation of PythonType
200
//================================================================================
204
static void standard_dealloc(PyObject* p);
206
// All the following functions redirect the call from Python
207
// onto the matching virtual function in PythonExtensionBase
209
static int print_handler (PyObject*, FILE *, int);
210
static PyObject* getattr_handler (PyObject*, char*);
211
static int setattr_handler (PyObject*, char*, PyObject*);
212
static PyObject* getattro_handler (PyObject*, PyObject*);
213
static int setattro_handler (PyObject*, PyObject*, PyObject*);
214
static int compare_handler (PyObject*, PyObject*);
215
static PyObject* richcompare_handler (PyObject*, PyObject*, int op);
216
static PyObject* repr_handler (PyObject*);
217
static PyObject* str_handler (PyObject*);
218
static long hash_handler (PyObject*);
219
static PyObject* call_handler (PyObject*, PyObject*, PyObject*);
220
static PyObject* iter_handler (PyObject*);
221
static PyObject* iternext_handler (PyObject*);
224
static Py_ssize_t sequence_length_handler(PyObject*);
225
static PyObject* sequence_concat_handler(PyObject*,PyObject*);
226
static PyObject* sequence_repeat_handler(PyObject*, Py_ssize_t);
227
static PyObject* sequence_item_handler(PyObject*, Py_ssize_t);
228
static PyObject* sequence_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t);
229
static int sequence_ass_item_handler(PyObject*, Py_ssize_t, PyObject*);
230
static int sequence_ass_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*);
232
static Py_ssize_t mapping_length_handler(PyObject*);
233
static PyObject* mapping_subscript_handler(PyObject*, PyObject*);
234
static int mapping_ass_subscript_handler(PyObject*, PyObject*, PyObject*);
237
static int number_nonzero_handler (PyObject*);
238
static PyObject* number_negative_handler (PyObject*);
239
static PyObject* number_positive_handler (PyObject*);
240
static PyObject* number_absolute_handler (PyObject*);
241
static PyObject* number_invert_handler (PyObject*);
242
static PyObject* number_int_handler (PyObject*);
243
static PyObject* number_float_handler (PyObject*);
244
static PyObject* number_long_handler (PyObject*);
245
static PyObject* number_oct_handler (PyObject*);
246
static PyObject* number_hex_handler (PyObject*);
247
static PyObject* number_add_handler (PyObject*, PyObject*);
248
static PyObject* number_subtract_handler (PyObject*, PyObject*);
249
static PyObject* number_multiply_handler (PyObject*, PyObject*);
250
static PyObject* number_divide_handler (PyObject*, PyObject*);
251
static PyObject* number_remainder_handler (PyObject*, PyObject*);
252
static PyObject* number_divmod_handler (PyObject*, PyObject*);
253
static PyObject* number_lshift_handler (PyObject*, PyObject*);
254
static PyObject* number_rshift_handler (PyObject*, PyObject*);
255
static PyObject* number_and_handler (PyObject*, PyObject*);
256
static PyObject* number_xor_handler (PyObject*, PyObject*);
257
static PyObject* number_or_handler (PyObject*, PyObject*);
258
static PyObject* number_power_handler(PyObject*, PyObject*, PyObject*);
261
static Py_ssize_t buffer_getreadbuffer_handler (PyObject*, Py_ssize_t, void**);
262
static Py_ssize_t buffer_getwritebuffer_handler (PyObject*, Py_ssize_t, void**);
263
static Py_ssize_t buffer_getsegcount_handler (PyObject*, Py_ssize_t*);
267
extern "C" void standard_dealloc( PyObject* p )
272
PythonType & PythonType::supportSequenceType()
274
if( !sequence_table )
276
sequence_table = new PySequenceMethods;
277
memset( sequence_table, 0, sizeof( PySequenceMethods ) ); // ensure new fields are 0
278
table->tp_as_sequence = sequence_table;
279
sequence_table->sq_length = sequence_length_handler;
280
sequence_table->sq_concat = sequence_concat_handler;
281
sequence_table->sq_repeat = sequence_repeat_handler;
282
sequence_table->sq_item = sequence_item_handler;
283
sequence_table->sq_slice = sequence_slice_handler;
285
sequence_table->sq_ass_item = sequence_ass_item_handler; // BAS setup seperately?
286
sequence_table->sq_ass_slice = sequence_ass_slice_handler; // BAS setup seperately?
291
PythonType & PythonType::supportMappingType()
295
mapping_table = new PyMappingMethods;
296
memset( mapping_table, 0, sizeof( PyMappingMethods ) ); // ensure new fields are 0
297
table->tp_as_mapping = mapping_table;
298
mapping_table->mp_length = mapping_length_handler;
299
mapping_table->mp_subscript = mapping_subscript_handler;
300
mapping_table->mp_ass_subscript = mapping_ass_subscript_handler; // BAS setup seperately?
305
PythonType & PythonType::supportNumberType()
309
number_table = new PyNumberMethods;
310
memset( number_table, 0, sizeof( PyNumberMethods ) ); // ensure new fields are 0
311
table->tp_as_number = number_table;
312
number_table->nb_add = number_add_handler;
313
number_table->nb_subtract = number_subtract_handler;
314
number_table->nb_multiply = number_multiply_handler;
315
number_table->nb_divide = number_divide_handler;
316
number_table->nb_remainder = number_remainder_handler;
317
number_table->nb_divmod = number_divmod_handler;
318
number_table->nb_power = number_power_handler;
319
number_table->nb_negative = number_negative_handler;
320
number_table->nb_positive = number_positive_handler;
321
number_table->nb_absolute = number_absolute_handler;
322
number_table->nb_nonzero = number_nonzero_handler;
323
number_table->nb_invert = number_invert_handler;
324
number_table->nb_lshift = number_lshift_handler;
325
number_table->nb_rshift = number_rshift_handler;
326
number_table->nb_and = number_and_handler;
327
number_table->nb_xor = number_xor_handler;
328
number_table->nb_or = number_or_handler;
329
number_table->nb_coerce = 0;
330
number_table->nb_int = number_int_handler;
331
number_table->nb_long = number_long_handler;
332
number_table->nb_float = number_float_handler;
333
number_table->nb_oct = number_oct_handler;
334
number_table->nb_hex = number_hex_handler;
339
PythonType & PythonType::supportBufferType()
343
buffer_table = new PyBufferProcs;
344
memset( buffer_table, 0, sizeof( PyBufferProcs ) ); // ensure new fields are 0
345
table->tp_as_buffer = buffer_table;
346
buffer_table->bf_getreadbuffer = buffer_getreadbuffer_handler;
347
buffer_table->bf_getwritebuffer = buffer_getwritebuffer_handler;
348
buffer_table->bf_getsegcount = buffer_getsegcount_handler;
353
// if you define one sequence method you must define
354
// all of them except the assigns
356
PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
357
: table( new PyTypeObject )
358
, sequence_table( NULL )
359
, mapping_table( NULL )
360
, number_table( NULL )
361
, buffer_table( NULL )
363
memset( table, 0, sizeof( PyTypeObject ) ); // ensure new fields are 0
364
*reinterpret_cast<PyObject*>( table ) = py_object_initializer;
365
table->ob_type = _Type_Type();
367
table->tp_name = const_cast<char *>( default_name );
368
table->tp_basicsize = basic_size;
369
table->tp_itemsize = itemsize;
370
table->tp_dealloc = ( destructor ) standard_dealloc;
372
table->tp_getattr = 0;
373
table->tp_setattr = 0;
374
table->tp_compare = 0;
376
table->tp_as_number = 0;
377
table->tp_as_sequence = 0;
378
table->tp_as_mapping = 0;
382
table->tp_getattro = 0;
383
table->tp_setattro = 0;
384
table->tp_as_buffer = 0;
385
table->tp_flags = Py_TPFLAGS_DEFAULT;
387
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 0)
389
table->tp_traverse = 0L;
390
table->tp_clear = 0L;
395
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
396
// first defined in 2.1
397
table->tp_richcompare = 0L;
398
table->tp_weaklistoffset = 0L;
404
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 2)
405
// first defined in 2.3
407
table->tp_iternext = 0L;
413
table->tp_maxalloc = 0;
418
PythonType::~PythonType( )
421
delete sequence_table;
422
delete mapping_table;
427
PyTypeObject* PythonType::type_object( ) const
430
PythonType & PythonType::name( const char* nam )
432
table->tp_name = const_cast<char *>( nam );
436
const char *PythonType::getName() const
438
return table->tp_name;
441
PythonType & PythonType::doc( const char* d )
443
table->tp_doc = const_cast<char *>( d );
447
const char *PythonType::getDoc() const
449
return table->tp_doc;
452
PythonType & PythonType::dealloc( void( *f )( PyObject* ))
454
table->tp_dealloc = f;
458
PythonType & PythonType::supportPrint()
460
table->tp_print = print_handler;
464
PythonType & PythonType::supportGetattr()
466
table->tp_getattr = getattr_handler;
470
PythonType & PythonType::supportSetattr()
472
table->tp_setattr = setattr_handler;
476
PythonType & PythonType::supportGetattro()
478
table->tp_getattro = getattro_handler;
482
PythonType & PythonType::supportSetattro()
484
table->tp_setattro = setattro_handler;
488
PythonType & PythonType::supportCompare()
490
table->tp_compare = compare_handler;
494
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
495
PythonType & PythonType::supportRichCompare()
497
table->tp_richcompare = richcompare_handler;
502
PythonType & PythonType::supportRepr()
504
table->tp_repr = repr_handler;
508
PythonType & PythonType::supportStr()
510
table->tp_str = str_handler;
514
PythonType & PythonType::supportHash()
516
table->tp_hash = hash_handler;
520
PythonType & PythonType::supportCall()
522
table->tp_call = call_handler;
526
PythonType & PythonType::supportIter()
528
table->tp_iter = iter_handler;
529
table->tp_iternext = iternext_handler;
533
//--------------------------------------------------------------------------------
537
//--------------------------------------------------------------------------------
538
extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
542
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
543
return p->print( fp, flags );
545
catch( Py::Exception & )
547
return -1; // indicate error
551
extern "C" PyObject* getattr_handler( PyObject *self, char *name )
555
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
556
return new_reference_to( p->getattr( name ) );
558
catch( Py::Exception & )
560
return NULL; // indicate error
564
extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
568
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
569
return p->setattr( name, Py::Object( value ) );
571
catch( Py::Exception & )
573
return -1; // indicate error
577
extern "C" PyObject* getattro_handler( PyObject *self, PyObject *name )
581
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
582
return new_reference_to( p->getattro( Py::Object( name ) ) );
584
catch( Py::Exception & )
586
return NULL; // indicate error
590
extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
594
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
595
return p->setattro( Py::Object( name ), Py::Object( value ) );
597
catch( Py::Exception & )
599
return -1; // indicate error
603
extern "C" int compare_handler( PyObject *self, PyObject *other )
607
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
608
return p->compare( Py::Object( other ) );
610
catch( Py::Exception & )
612
return -1; // indicate error
616
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
617
extern "C" PyObject* richcompare_handler( PyObject *self, PyObject *other, int op )
621
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
622
return new_reference_to( p->rich_compare( Py::Object( other ), op ) );
624
catch( Py::Exception & )
626
return NULL; // indicate error
631
extern "C" PyObject* repr_handler( PyObject *self )
635
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
636
return new_reference_to( p->repr() );
638
catch( Py::Exception & )
640
return NULL; // indicate error
644
extern "C" PyObject* str_handler( PyObject *self )
648
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
649
return new_reference_to( p->str() );
651
catch( Py::Exception & )
653
return NULL; // indicate error
657
extern "C" long hash_handler( PyObject *self )
661
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
664
catch( Py::Exception & )
666
return -1; // indicate error
670
extern "C" PyObject* call_handler( PyObject *self, PyObject *args, PyObject *kw )
674
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
676
return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
678
return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
680
catch( Py::Exception & )
682
return NULL; // indicate error
686
extern "C" PyObject* iter_handler( PyObject *self )
690
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
691
return new_reference_to( p->iter() );
693
catch( Py::Exception & )
695
return NULL; // indicate error
699
extern "C" PyObject* iternext_handler( PyObject *self )
703
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
704
return p->iternext(); // might be a NULL ptr on end of iteration
706
catch( Py::Exception & )
708
return NULL; // indicate error
714
extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
718
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
719
return p->sequence_length();
721
catch( Py::Exception & )
723
return -1; // indicate error
727
extern "C" PyObject* sequence_concat_handler( PyObject *self, PyObject *other )
731
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
732
return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
734
catch( Py::Exception & )
736
return NULL; // indicate error
740
extern "C" PyObject* sequence_repeat_handler( PyObject *self, Py_ssize_t count )
744
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
745
return new_reference_to( p->sequence_repeat( count ) );
747
catch( Py::Exception & )
749
return NULL; // indicate error
753
extern "C" PyObject* sequence_item_handler( PyObject *self, Py_ssize_t index )
757
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
758
return new_reference_to( p->sequence_item( index ) );
760
catch( Py::Exception & )
762
return NULL; // indicate error
766
extern "C" PyObject* sequence_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last )
770
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
771
return new_reference_to( p->sequence_slice( first, last ) );
773
catch( Py::Exception & )
775
return NULL; // indicate error
779
extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
783
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
784
return p->sequence_ass_item( index, Py::Object( value ) );
786
catch( Py::Exception & )
788
return -1; // indicate error
792
extern "C" int sequence_ass_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *value )
796
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
797
return p->sequence_ass_slice( first, last, Py::Object( value ) );
799
catch( Py::Exception & )
801
return -1; // indicate error
806
extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
810
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
811
return p->mapping_length();
813
catch( Py::Exception & )
815
return -1; // indicate error
819
extern "C" PyObject* mapping_subscript_handler( PyObject *self, PyObject *key )
823
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
824
return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
826
catch( Py::Exception & )
828
return NULL; // indicate error
832
extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
836
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
837
return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
839
catch( Py::Exception & )
841
return -1; // indicate error
846
extern "C" int number_nonzero_handler( PyObject *self )
850
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
851
return p->number_nonzero();
853
catch( Py::Exception & )
855
return -1; // indicate error
859
extern "C" PyObject* number_negative_handler( PyObject *self )
863
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
864
return new_reference_to( p->number_negative() );
866
catch( Py::Exception & )
868
return NULL; // indicate error
872
extern "C" PyObject* number_positive_handler( PyObject *self )
876
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
877
return new_reference_to( p->number_positive() );
879
catch( Py::Exception & )
881
return NULL; // indicate error
885
extern "C" PyObject* number_absolute_handler( PyObject *self )
889
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
890
return new_reference_to( p->number_absolute() );
892
catch( Py::Exception & )
894
return NULL; // indicate error
898
extern "C" PyObject* number_invert_handler( PyObject *self )
902
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
903
return new_reference_to( p->number_invert() );
905
catch( Py::Exception & )
907
return NULL; // indicate error
911
extern "C" PyObject* number_int_handler( PyObject *self )
915
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
916
return new_reference_to( p->number_int() );
918
catch( Py::Exception & )
920
return NULL; // indicate error
924
extern "C" PyObject* number_float_handler( PyObject *self )
928
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
929
return new_reference_to( p->number_float() );
931
catch( Py::Exception & )
933
return NULL; // indicate error
937
extern "C" PyObject* number_long_handler( PyObject *self )
941
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
942
return new_reference_to( p->number_long() );
944
catch( Py::Exception & )
946
return NULL; // indicate error
950
extern "C" PyObject* number_oct_handler( PyObject *self )
954
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
955
return new_reference_to( p->number_oct() );
957
catch( Py::Exception & )
959
return NULL; // indicate error
963
extern "C" PyObject* number_hex_handler( PyObject *self )
967
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
968
return new_reference_to( p->number_hex() );
970
catch( Py::Exception & )
972
return NULL; // indicate error
976
extern "C" PyObject* number_add_handler( PyObject *self, PyObject *other )
980
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
981
return new_reference_to( p->number_add( Py::Object( other ) ) );
983
catch( Py::Exception & )
985
return NULL; // indicate error
989
extern "C" PyObject* number_subtract_handler( PyObject *self, PyObject *other )
993
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
994
return new_reference_to( p->number_subtract( Py::Object( other ) ) );
996
catch( Py::Exception & )
998
return NULL; // indicate error
1002
extern "C" PyObject* number_multiply_handler( PyObject *self, PyObject *other )
1006
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1007
return new_reference_to( p->number_multiply( Py::Object( other ) ) );
1009
catch( Py::Exception & )
1011
return NULL; // indicate error
1015
extern "C" PyObject* number_divide_handler( PyObject *self, PyObject *other )
1019
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1020
return new_reference_to( p->number_divide( Py::Object( other ) ) );
1022
catch( Py::Exception & )
1024
return NULL; // indicate error
1028
extern "C" PyObject* number_remainder_handler( PyObject *self, PyObject *other )
1032
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1033
return new_reference_to( p->number_remainder( Py::Object( other ) ) );
1035
catch( Py::Exception & )
1037
return NULL; // indicate error
1041
extern "C" PyObject* number_divmod_handler( PyObject *self, PyObject *other )
1045
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1046
return new_reference_to( p->number_divmod( Py::Object( other ) ) );
1048
catch( Py::Exception & )
1050
return NULL; // indicate error
1054
extern "C" PyObject* number_lshift_handler( PyObject *self, PyObject *other )
1058
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1059
return new_reference_to( p->number_lshift( Py::Object( other ) ) );
1061
catch( Py::Exception & )
1063
return NULL; // indicate error
1067
extern "C" PyObject* number_rshift_handler( PyObject *self, PyObject *other )
1071
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1072
return new_reference_to( p->number_rshift( Py::Object( other ) ) );
1074
catch( Py::Exception & )
1076
return NULL; // indicate error
1080
extern "C" PyObject* number_and_handler( PyObject *self, PyObject *other )
1084
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1085
return new_reference_to( p->number_and( Py::Object( other ) ) );
1087
catch( Py::Exception & )
1089
return NULL; // indicate error
1093
extern "C" PyObject* number_xor_handler( PyObject *self, PyObject *other )
1097
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1098
return new_reference_to( p->number_xor( Py::Object( other ) ) );
1100
catch( Py::Exception & )
1102
return NULL; // indicate error
1106
extern "C" PyObject* number_or_handler( PyObject *self, PyObject *other )
1110
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1111
return new_reference_to( p->number_or( Py::Object( other ) ) );
1113
catch( Py::Exception & )
1115
return NULL; // indicate error
1119
extern "C" PyObject* number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
1123
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1124
return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
1126
catch( Py::Exception & )
1128
return NULL; // indicate error
1133
extern "C" Py_ssize_t buffer_getreadbuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1137
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1138
return p->buffer_getreadbuffer( index, pp );
1140
catch( Py::Exception & )
1142
return -1; // indicate error
1146
extern "C" Py_ssize_t buffer_getwritebuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1150
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1151
return p->buffer_getwritebuffer( index, pp );
1153
catch( Py::Exception & )
1155
return -1; // indicate error
1159
extern "C" Py_ssize_t buffer_getsegcount_handler( PyObject *self, Py_ssize_t *count )
1163
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1164
return p->buffer_getsegcount( count );
1166
catch( Py::Exception & )
1168
return -1; // indicate error
1173
//================================================================================
1175
// Implementation of PythonExtensionBase
1177
//================================================================================
1178
#define missing_method( method ) \
1179
throw RuntimeError( "Extension object does not support method " #method );
1181
PythonExtensionBase::PythonExtensionBase()
1185
PythonExtensionBase::~PythonExtensionBase()
1187
assert( ob_refcnt == 0 );
1190
int PythonExtensionBase::print( FILE *, int )
1191
{ missing_method( print ); return -1; }
1193
int PythonExtensionBase::setattr( const char*, const Py::Object & )
1194
{ missing_method( setattr ); return -1; }
1196
Py::Object PythonExtensionBase::getattro( const Py::Object & )
1197
{ missing_method( getattro ); return Py::Nothing(); }
1199
int PythonExtensionBase::setattro( const Py::Object &, const Py::Object & )
1200
{ missing_method( setattro ); return -1; }
1202
int PythonExtensionBase::compare( const Py::Object & )
1203
{ missing_method( compare ); return -1; }
1205
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
1206
Py::Object PythonExtensionBase::rich_compare( const Py::Object &, int op )
1207
{ missing_method( rich_compare ); return Py::None(); }
1210
Py::Object PythonExtensionBase::repr()
1211
{ missing_method( repr ); return Py::Nothing(); }
1213
Py::Object PythonExtensionBase::str()
1214
{ missing_method( str ); return Py::Nothing(); }
1216
long PythonExtensionBase::hash()
1217
{ missing_method( hash ); return -1; }
1219
Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & )
1220
{ missing_method( call ); return Py::Nothing(); }
1222
Py::Object PythonExtensionBase::iter()
1223
{ missing_method( iter ); return Py::Nothing(); }
1225
PyObject* PythonExtensionBase::iternext()
1226
{ missing_method( iternext ); return NULL; }
1230
int PythonExtensionBase::sequence_length()
1231
{ missing_method( sequence_length ); return -1; }
1233
Py::Object PythonExtensionBase::sequence_concat( const Py::Object & )
1234
{ missing_method( sequence_concat ); return Py::Nothing(); }
1236
Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t )
1237
{ missing_method( sequence_repeat ); return Py::Nothing(); }
1239
Py::Object PythonExtensionBase::sequence_item( Py_ssize_t )
1240
{ missing_method( sequence_item ); return Py::Nothing(); }
1242
Py::Object PythonExtensionBase::sequence_slice( Py_ssize_t, Py_ssize_t )
1243
{ missing_method( sequence_slice ); return Py::Nothing(); }
1245
int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object & )
1246
{ missing_method( sequence_ass_item ); return -1; }
1248
int PythonExtensionBase::sequence_ass_slice( Py_ssize_t, Py_ssize_t, const Py::Object & )
1249
{ missing_method( sequence_ass_slice ); return -1; }
1253
int PythonExtensionBase::mapping_length()
1254
{ missing_method( mapping_length ); return -1; }
1256
Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & )
1257
{ missing_method( mapping_subscript ); return Py::Nothing(); }
1259
int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & )
1260
{ missing_method( mapping_ass_subscript ); return -1; }
1264
int PythonExtensionBase::number_nonzero()
1265
{ missing_method( number_nonzero ); return -1; }
1267
Py::Object PythonExtensionBase::number_negative()
1268
{ missing_method( number_negative ); return Py::Nothing(); }
1270
Py::Object PythonExtensionBase::number_positive()
1271
{ missing_method( number_positive ); return Py::Nothing(); }
1273
Py::Object PythonExtensionBase::number_absolute()
1274
{ missing_method( number_absolute ); return Py::Nothing(); }
1276
Py::Object PythonExtensionBase::number_invert()
1277
{ missing_method( number_invert ); return Py::Nothing(); }
1279
Py::Object PythonExtensionBase::number_int()
1280
{ missing_method( number_int ); return Py::Nothing(); }
1282
Py::Object PythonExtensionBase::number_float()
1283
{ missing_method( number_float ); return Py::Nothing(); }
1285
Py::Object PythonExtensionBase::number_long()
1286
{ missing_method( number_long ); return Py::Nothing(); }
1288
Py::Object PythonExtensionBase::number_oct()
1289
{ missing_method( number_oct ); return Py::Nothing(); }
1291
Py::Object PythonExtensionBase::number_hex()
1292
{ missing_method( number_hex ); return Py::Nothing(); }
1294
Py::Object PythonExtensionBase::number_add( const Py::Object & )
1295
{ missing_method( number_add ); return Py::Nothing(); }
1297
Py::Object PythonExtensionBase::number_subtract( const Py::Object & )
1298
{ missing_method( number_subtract ); return Py::Nothing(); }
1300
Py::Object PythonExtensionBase::number_multiply( const Py::Object & )
1301
{ missing_method( number_multiply ); return Py::Nothing(); }
1303
Py::Object PythonExtensionBase::number_divide( const Py::Object & )
1304
{ missing_method( number_divide ); return Py::Nothing(); }
1306
Py::Object PythonExtensionBase::number_remainder( const Py::Object & )
1307
{ missing_method( number_remainder ); return Py::Nothing(); }
1309
Py::Object PythonExtensionBase::number_divmod( const Py::Object & )
1310
{ missing_method( number_divmod ); return Py::Nothing(); }
1312
Py::Object PythonExtensionBase::number_lshift( const Py::Object & )
1313
{ missing_method( number_lshift ); return Py::Nothing(); }
1315
Py::Object PythonExtensionBase::number_rshift( const Py::Object & )
1316
{ missing_method( number_rshift ); return Py::Nothing(); }
1318
Py::Object PythonExtensionBase::number_and( const Py::Object & )
1319
{ missing_method( number_and ); return Py::Nothing(); }
1321
Py::Object PythonExtensionBase::number_xor( const Py::Object & )
1322
{ missing_method( number_xor ); return Py::Nothing(); }
1324
Py::Object PythonExtensionBase::number_or( const Py::Object & )
1325
{ missing_method( number_or ); return Py::Nothing(); }
1327
Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & )
1328
{ missing_method( number_power ); return Py::Nothing(); }
1332
Py_ssize_t PythonExtensionBase::buffer_getreadbuffer( Py_ssize_t, void** )
1333
{ missing_method( buffer_getreadbuffer ); return -1; }
1335
Py_ssize_t PythonExtensionBase::buffer_getwritebuffer( Py_ssize_t, void** )
1336
{ missing_method( buffer_getwritebuffer ); return -1; }
1338
Py_ssize_t PythonExtensionBase::buffer_getsegcount( Py_ssize_t* )
1339
{ missing_method( buffer_getsegcount ); return -1; }
1341
//--------------------------------------------------------------------------------
1343
// Method call handlers for
1344
// PythonExtensionBase
1345
// ExtensionModuleBase
1347
//--------------------------------------------------------------------------------
1349
extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
1353
Tuple self_and_name_tuple( _self_and_name_tuple );
1355
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1356
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1357
if( self_as_void == NULL )
1360
ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1362
String py_name( self_and_name_tuple[1] );
1363
std::string name( py_name.as_std_string() );
1365
Tuple args( _args );
1366
if( _keywords == NULL )
1368
Dict keywords; // pass an empty dict
1370
Object result( self->invoke_method_keyword( name, args, keywords ) );
1371
return new_reference_to( result.ptr() );
1374
Dict keywords( _keywords );
1376
Object result( self->invoke_method_keyword( name, args, keywords ) );
1377
return new_reference_to( result.ptr() );
1379
catch( Exception & )
1385
extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
1389
Tuple self_and_name_tuple( _self_and_name_tuple );
1391
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1392
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1393
if( self_as_void == NULL )
1396
ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1398
String py_name( self_and_name_tuple[1] );
1399
std::string name( py_name.as_std_string() );
1401
Tuple args( _args );
1403
Object result( self->invoke_method_varargs( name, args ) );
1405
return new_reference_to( result.ptr() );
1407
catch( Exception & )
1413
extern "C" void do_not_dealloc( void * )
1417
//--------------------------------------------------------------------------------
1419
// ExtensionExceptionType
1421
//--------------------------------------------------------------------------------
1422
ExtensionExceptionType::ExtensionExceptionType()
1427
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
1429
std::string module_name( module.fullName() );
1431
module_name += name;
1433
set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
1436
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent)
1438
std::string module_name( module.fullName() );
1440
module_name += name;
1442
set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
1445
ExtensionExceptionType::~ExtensionExceptionType()
1449
Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
1451
PyErr_SetString (exception.ptr(), reason.c_str());
1454
Exception::Exception( ExtensionExceptionType &exception, Object &reason )
1456
PyErr_SetObject (exception.ptr(), reason.ptr());
1459
Exception::Exception( PyObject* exception, Object &reason )
1461
PyErr_SetObject (exception, reason.ptr());
1464
} // end of namespace Py