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"
44
//================================================================================
46
// Implementation of MethodTable
48
//================================================================================
50
PyMethodDef MethodTable::method( const char* method_name, PyCFunction f, int flags, const char* doc )
53
m.ml_name = const_cast<char*>( method_name );
56
m.ml_doc = const_cast<char*>( doc );
60
MethodTable::MethodTable()
62
t.push_back( method( 0, 0, 0, 0 ) );
66
MethodTable::~MethodTable()
71
void MethodTable::add( const char* method_name, PyCFunction f, const char* doc, int flag )
75
t.insert( t.end()-1, method( method_name, f, flag, doc ) );
79
throw RuntimeError( "Too late to add a module method!" );
83
PyMethodDef* MethodTable::table()
87
Py_ssize_t t1size = t.size();
88
mt = new PyMethodDef[t1size];
90
for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
98
//================================================================================
100
// Implementation of ExtensionModule
102
//================================================================================
103
ExtensionModuleBase::ExtensionModuleBase( const char *name )
104
: module_name( name )
105
, full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : module_name )
109
ExtensionModuleBase::~ExtensionModuleBase()
112
const std::string &ExtensionModuleBase::name() const
117
const std::string &ExtensionModuleBase::fullName() const
119
return full_module_name;
122
class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
125
ExtensionModuleBasePtr( ExtensionModuleBase *_module )
128
virtual ~ExtensionModuleBasePtr()
131
ExtensionModuleBase *module;
135
void ExtensionModuleBase::initialize( const char *module_doc )
137
PyObject *module_ptr = new ExtensionModuleBasePtr( this );
141
const_cast<char *>( module_name.c_str() ), // name
142
method_table.table(), // methods
143
const_cast<char *>( module_doc ), // docs
144
module_ptr, // pass to functions as "self"
145
PYTHON_API_VERSION // API version
149
Py::Module ExtensionModuleBase::module(void) const
151
return Module( full_module_name );
154
Py::Dict ExtensionModuleBase::moduleDictionary(void) const
156
return module().getDict();
159
//--------------------------------------------------------------------------------
161
//================================================================================
163
// Implementation of PythonType
165
//================================================================================
169
static void standard_dealloc(PyObject* p);
171
// All the following functions redirect the call from Python
172
// onto the matching virtual function in PythonExtensionBase
174
static int print_handler (PyObject*, FILE *, int);
175
static PyObject* getattr_handler (PyObject*, char*);
176
static int setattr_handler (PyObject*, char*, PyObject*);
177
static PyObject* getattro_handler (PyObject*, PyObject*);
178
static int setattro_handler (PyObject*, PyObject*, PyObject*);
179
static int compare_handler (PyObject*, PyObject*);
180
static PyObject* repr_handler (PyObject*);
181
static PyObject* str_handler (PyObject*);
182
static long hash_handler (PyObject*);
183
static PyObject* call_handler (PyObject*, PyObject*, PyObject*);
184
static PyObject* iter_handler (PyObject*);
185
static PyObject* iternext_handler (PyObject*);
188
static Py_ssize_t sequence_length_handler(PyObject*);
189
static PyObject* sequence_concat_handler(PyObject*,PyObject*);
190
static PyObject* sequence_repeat_handler(PyObject*, Py_ssize_t);
191
static PyObject* sequence_item_handler(PyObject*, Py_ssize_t);
192
static PyObject* sequence_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t);
193
static int sequence_ass_item_handler(PyObject*, Py_ssize_t, PyObject*);
194
static int sequence_ass_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*);
196
static Py_ssize_t mapping_length_handler(PyObject*);
197
static PyObject* mapping_subscript_handler(PyObject*, PyObject*);
198
static int mapping_ass_subscript_handler(PyObject*, PyObject*, PyObject*);
201
static int number_nonzero_handler (PyObject*);
202
static PyObject* number_negative_handler (PyObject*);
203
static PyObject* number_positive_handler (PyObject*);
204
static PyObject* number_absolute_handler (PyObject*);
205
static PyObject* number_invert_handler (PyObject*);
206
static PyObject* number_int_handler (PyObject*);
207
static PyObject* number_float_handler (PyObject*);
208
static PyObject* number_long_handler (PyObject*);
209
static PyObject* number_oct_handler (PyObject*);
210
static PyObject* number_hex_handler (PyObject*);
211
static PyObject* number_add_handler (PyObject*, PyObject*);
212
static PyObject* number_subtract_handler (PyObject*, PyObject*);
213
static PyObject* number_multiply_handler (PyObject*, PyObject*);
214
static PyObject* number_divide_handler (PyObject*, PyObject*);
215
static PyObject* number_remainder_handler (PyObject*, PyObject*);
216
static PyObject* number_divmod_handler (PyObject*, PyObject*);
217
static PyObject* number_lshift_handler (PyObject*, PyObject*);
218
static PyObject* number_rshift_handler (PyObject*, PyObject*);
219
static PyObject* number_and_handler (PyObject*, PyObject*);
220
static PyObject* number_xor_handler (PyObject*, PyObject*);
221
static PyObject* number_or_handler (PyObject*, PyObject*);
222
static PyObject* number_power_handler(PyObject*, PyObject*, PyObject*);
225
static Py_ssize_t buffer_getreadbuffer_handler (PyObject*, Py_ssize_t, void**);
226
static Py_ssize_t buffer_getwritebuffer_handler (PyObject*, Py_ssize_t, void**);
227
static Py_ssize_t buffer_getsegcount_handler (PyObject*, Py_ssize_t*);
231
extern "C" void standard_dealloc( PyObject* p )
236
PythonType & PythonType::supportSequenceType()
238
if( !sequence_table )
240
sequence_table = new PySequenceMethods;
241
memset( sequence_table, 0, sizeof( PySequenceMethods ) ); // ensure new fields are 0
242
table->tp_as_sequence = sequence_table;
243
sequence_table->sq_length = sequence_length_handler;
244
sequence_table->sq_concat = sequence_concat_handler;
245
sequence_table->sq_repeat = sequence_repeat_handler;
246
sequence_table->sq_item = sequence_item_handler;
247
sequence_table->sq_slice = sequence_slice_handler;
249
sequence_table->sq_ass_item = sequence_ass_item_handler; // BAS setup seperately?
250
sequence_table->sq_ass_slice = sequence_ass_slice_handler; // BAS setup seperately?
255
PythonType & PythonType::supportMappingType()
259
mapping_table = new PyMappingMethods;
260
memset( mapping_table, 0, sizeof( PyMappingMethods ) ); // ensure new fields are 0
261
table->tp_as_mapping = mapping_table;
262
mapping_table->mp_length = mapping_length_handler;
263
mapping_table->mp_subscript = mapping_subscript_handler;
264
mapping_table->mp_ass_subscript = mapping_ass_subscript_handler; // BAS setup seperately?
269
PythonType & PythonType::supportNumberType()
273
number_table = new PyNumberMethods;
274
memset( number_table, 0, sizeof( PyNumberMethods ) ); // ensure new fields are 0
275
table->tp_as_number = number_table;
276
number_table->nb_add = number_add_handler;
277
number_table->nb_subtract = number_subtract_handler;
278
number_table->nb_multiply = number_multiply_handler;
279
number_table->nb_divide = number_divide_handler;
280
number_table->nb_remainder = number_remainder_handler;
281
number_table->nb_divmod = number_divmod_handler;
282
number_table->nb_power = number_power_handler;
283
number_table->nb_negative = number_negative_handler;
284
number_table->nb_positive = number_positive_handler;
285
number_table->nb_absolute = number_absolute_handler;
286
number_table->nb_nonzero = number_nonzero_handler;
287
number_table->nb_invert = number_invert_handler;
288
number_table->nb_lshift = number_lshift_handler;
289
number_table->nb_rshift = number_rshift_handler;
290
number_table->nb_and = number_and_handler;
291
number_table->nb_xor = number_xor_handler;
292
number_table->nb_or = number_or_handler;
293
number_table->nb_coerce = 0;
294
number_table->nb_int = number_int_handler;
295
number_table->nb_long = number_long_handler;
296
number_table->nb_float = number_float_handler;
297
number_table->nb_oct = number_oct_handler;
298
number_table->nb_hex = number_hex_handler;
303
PythonType & PythonType::supportBufferType()
307
buffer_table = new PyBufferProcs;
308
memset( buffer_table, 0, sizeof( PyBufferProcs ) ); // ensure new fields are 0
309
table->tp_as_buffer = buffer_table;
310
buffer_table->bf_getreadbuffer = buffer_getreadbuffer_handler;
311
buffer_table->bf_getwritebuffer = buffer_getwritebuffer_handler;
312
buffer_table->bf_getsegcount = buffer_getsegcount_handler;
317
// if you define one sequence method you must define
318
// all of them except the assigns
320
PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
321
: table( new PyTypeObject )
322
, sequence_table( NULL )
323
, mapping_table( NULL )
324
, number_table( NULL )
325
, buffer_table( NULL )
327
memset( table, 0, sizeof( PyTypeObject ) ); // ensure new fields are 0
328
*reinterpret_cast<PyObject*>( table ) = py_object_initializer;
329
table->ob_type = _Type_Type();
331
table->tp_name = const_cast<char *>( default_name );
332
table->tp_basicsize = basic_size;
333
table->tp_itemsize = itemsize;
334
table->tp_dealloc = ( destructor ) standard_dealloc;
336
table->tp_getattr = 0;
337
table->tp_setattr = 0;
338
table->tp_compare = 0;
340
table->tp_as_number = 0;
341
table->tp_as_sequence = 0;
342
table->tp_as_mapping = 0;
346
table->tp_getattro = 0;
347
table->tp_setattro = 0;
348
table->tp_as_buffer = 0;
349
table->tp_flags = Py_TPFLAGS_DEFAULT;
351
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 0)
353
table->tp_traverse = 0L;
354
table->tp_clear = 0L;
359
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
360
// first defined in 2.1
361
table->tp_richcompare = 0L;
362
table->tp_weaklistoffset = 0L;
368
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 2)
369
// first defined in 2.3
371
table->tp_iternext = 0L;
377
table->tp_maxalloc = 0;
382
PythonType::~PythonType( )
385
delete sequence_table;
386
delete mapping_table;
391
PyTypeObject* PythonType::type_object( ) const
394
PythonType & PythonType::name( const char* nam )
396
table->tp_name = const_cast<char *>( nam );
400
const char *PythonType::getName() const
402
return table->tp_name;
405
PythonType & PythonType::doc( const char* d )
407
table->tp_doc = const_cast<char *>( d );
411
const char *PythonType::getDoc() const
413
return table->tp_doc;
416
PythonType & PythonType::dealloc( void( *f )( PyObject* ))
418
table->tp_dealloc = f;
422
PythonType & PythonType::supportPrint()
424
table->tp_print = print_handler;
428
PythonType & PythonType::supportGetattr()
430
table->tp_getattr = getattr_handler;
434
PythonType & PythonType::supportSetattr()
436
table->tp_setattr = setattr_handler;
440
PythonType & PythonType::supportGetattro()
442
table->tp_getattro = getattro_handler;
446
PythonType & PythonType::supportSetattro()
448
table->tp_setattro = setattro_handler;
452
PythonType & PythonType::supportCompare()
454
table->tp_compare = compare_handler;
458
PythonType & PythonType::supportRepr()
460
table->tp_repr = repr_handler;
464
PythonType & PythonType::supportStr()
466
table->tp_str = str_handler;
470
PythonType & PythonType::supportHash()
472
table->tp_hash = hash_handler;
476
PythonType & PythonType::supportCall()
478
table->tp_call = call_handler;
482
PythonType & PythonType::supportIter()
484
table->tp_iter = iter_handler;
485
table->tp_iternext = iternext_handler;
489
//--------------------------------------------------------------------------------
493
//--------------------------------------------------------------------------------
494
extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
498
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
499
return p->print( fp, flags );
501
catch( Py::Exception & )
503
return -1; // indicate error
507
extern "C" PyObject* getattr_handler( PyObject *self, char *name )
511
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
512
return new_reference_to( p->getattr( name ) );
514
catch( Py::Exception & )
516
return NULL; // indicate error
520
extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
524
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
525
return p->setattr( name, Py::Object( value ) );
527
catch( Py::Exception & )
529
return -1; // indicate error
533
extern "C" PyObject* getattro_handler( PyObject *self, PyObject *name )
537
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
538
return new_reference_to( p->getattro( Py::Object( name ) ) );
540
catch( Py::Exception & )
542
return NULL; // indicate error
546
extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
550
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
551
return p->setattro( Py::Object( name ), Py::Object( value ) );
553
catch( Py::Exception & )
555
return -1; // indicate error
559
extern "C" int compare_handler( PyObject *self, PyObject *other )
563
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
564
return p->compare( Py::Object( other ) );
566
catch( Py::Exception & )
568
return -1; // indicate error
572
extern "C" PyObject* repr_handler( PyObject *self )
576
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
577
return new_reference_to( p->repr() );
579
catch( Py::Exception & )
581
return NULL; // indicate error
585
extern "C" PyObject* str_handler( PyObject *self )
589
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
590
return new_reference_to( p->str() );
592
catch( Py::Exception & )
594
return NULL; // indicate error
598
extern "C" long hash_handler( PyObject *self )
602
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
605
catch( Py::Exception & )
607
return -1; // indicate error
611
extern "C" PyObject* call_handler( PyObject *self, PyObject *args, PyObject *kw )
615
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
617
return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
619
return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
621
catch( Py::Exception & )
623
return NULL; // indicate error
627
extern "C" PyObject* iter_handler( PyObject *self )
631
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
632
return new_reference_to( p->iter() );
634
catch( Py::Exception & )
636
return NULL; // indicate error
640
extern "C" PyObject* iternext_handler( PyObject *self )
644
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
645
return p->iternext(); // might be a NULL ptr on end of iteration
647
catch( Py::Exception & )
649
return NULL; // indicate error
655
extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
659
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
660
return p->sequence_length();
662
catch( Py::Exception & )
664
return -1; // indicate error
668
extern "C" PyObject* sequence_concat_handler( PyObject *self, PyObject *other )
672
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
673
return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
675
catch( Py::Exception & )
677
return NULL; // indicate error
681
extern "C" PyObject* sequence_repeat_handler( PyObject *self, Py_ssize_t count )
685
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
686
return new_reference_to( p->sequence_repeat( count ) );
688
catch( Py::Exception & )
690
return NULL; // indicate error
694
extern "C" PyObject* sequence_item_handler( PyObject *self, Py_ssize_t index )
698
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
699
return new_reference_to( p->sequence_item( index ) );
701
catch( Py::Exception & )
703
return NULL; // indicate error
707
extern "C" PyObject* sequence_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last )
711
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
712
return new_reference_to( p->sequence_slice( first, last ) );
714
catch( Py::Exception & )
716
return NULL; // indicate error
720
extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
724
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
725
return p->sequence_ass_item( index, Py::Object( value ) );
727
catch( Py::Exception & )
729
return -1; // indicate error
733
extern "C" int sequence_ass_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *value )
737
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
738
return p->sequence_ass_slice( first, last, Py::Object( value ) );
740
catch( Py::Exception & )
742
return -1; // indicate error
747
extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
751
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
752
return p->mapping_length();
754
catch( Py::Exception & )
756
return -1; // indicate error
760
extern "C" PyObject* mapping_subscript_handler( PyObject *self, PyObject *key )
764
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
765
return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
767
catch( Py::Exception & )
769
return NULL; // indicate error
773
extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
777
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
778
return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
780
catch( Py::Exception & )
782
return -1; // indicate error
787
extern "C" int number_nonzero_handler( PyObject *self )
791
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
792
return p->number_nonzero();
794
catch( Py::Exception & )
796
return -1; // indicate error
800
extern "C" PyObject* number_negative_handler( PyObject *self )
804
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
805
return new_reference_to( p->number_negative() );
807
catch( Py::Exception & )
809
return NULL; // indicate error
813
extern "C" PyObject* number_positive_handler( PyObject *self )
817
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
818
return new_reference_to( p->number_positive() );
820
catch( Py::Exception & )
822
return NULL; // indicate error
826
extern "C" PyObject* number_absolute_handler( PyObject *self )
830
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
831
return new_reference_to( p->number_absolute() );
833
catch( Py::Exception & )
835
return NULL; // indicate error
839
extern "C" PyObject* number_invert_handler( PyObject *self )
843
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
844
return new_reference_to( p->number_invert() );
846
catch( Py::Exception & )
848
return NULL; // indicate error
852
extern "C" PyObject* number_int_handler( PyObject *self )
856
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
857
return new_reference_to( p->number_int() );
859
catch( Py::Exception & )
861
return NULL; // indicate error
865
extern "C" PyObject* number_float_handler( PyObject *self )
869
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
870
return new_reference_to( p->number_float() );
872
catch( Py::Exception & )
874
return NULL; // indicate error
878
extern "C" PyObject* number_long_handler( PyObject *self )
882
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
883
return new_reference_to( p->number_long() );
885
catch( Py::Exception & )
887
return NULL; // indicate error
891
extern "C" PyObject* number_oct_handler( PyObject *self )
895
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
896
return new_reference_to( p->number_oct() );
898
catch( Py::Exception & )
900
return NULL; // indicate error
904
extern "C" PyObject* number_hex_handler( PyObject *self )
908
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
909
return new_reference_to( p->number_hex() );
911
catch( Py::Exception & )
913
return NULL; // indicate error
917
extern "C" PyObject* number_add_handler( PyObject *self, PyObject *other )
921
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
922
return new_reference_to( p->number_add( Py::Object( other ) ) );
924
catch( Py::Exception & )
926
return NULL; // indicate error
930
extern "C" PyObject* number_subtract_handler( PyObject *self, PyObject *other )
934
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
935
return new_reference_to( p->number_subtract( Py::Object( other ) ) );
937
catch( Py::Exception & )
939
return NULL; // indicate error
943
extern "C" PyObject* number_multiply_handler( PyObject *self, PyObject *other )
947
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
948
return new_reference_to( p->number_multiply( Py::Object( other ) ) );
950
catch( Py::Exception & )
952
return NULL; // indicate error
956
extern "C" PyObject* number_divide_handler( PyObject *self, PyObject *other )
960
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
961
return new_reference_to( p->number_divide( Py::Object( other ) ) );
963
catch( Py::Exception & )
965
return NULL; // indicate error
969
extern "C" PyObject* number_remainder_handler( PyObject *self, PyObject *other )
973
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
974
return new_reference_to( p->number_remainder( Py::Object( other ) ) );
976
catch( Py::Exception & )
978
return NULL; // indicate error
982
extern "C" PyObject* number_divmod_handler( PyObject *self, PyObject *other )
986
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
987
return new_reference_to( p->number_divmod( Py::Object( other ) ) );
989
catch( Py::Exception & )
991
return NULL; // indicate error
995
extern "C" PyObject* number_lshift_handler( PyObject *self, PyObject *other )
999
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1000
return new_reference_to( p->number_lshift( Py::Object( other ) ) );
1002
catch( Py::Exception & )
1004
return NULL; // indicate error
1008
extern "C" PyObject* number_rshift_handler( PyObject *self, PyObject *other )
1012
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1013
return new_reference_to( p->number_rshift( Py::Object( other ) ) );
1015
catch( Py::Exception & )
1017
return NULL; // indicate error
1021
extern "C" PyObject* number_and_handler( PyObject *self, PyObject *other )
1025
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1026
return new_reference_to( p->number_and( Py::Object( other ) ) );
1028
catch( Py::Exception & )
1030
return NULL; // indicate error
1034
extern "C" PyObject* number_xor_handler( PyObject *self, PyObject *other )
1038
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1039
return new_reference_to( p->number_xor( Py::Object( other ) ) );
1041
catch( Py::Exception & )
1043
return NULL; // indicate error
1047
extern "C" PyObject* number_or_handler( PyObject *self, PyObject *other )
1051
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1052
return new_reference_to( p->number_or( Py::Object( other ) ) );
1054
catch( Py::Exception & )
1056
return NULL; // indicate error
1060
extern "C" PyObject* number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
1064
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1065
return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
1067
catch( Py::Exception & )
1069
return NULL; // indicate error
1074
extern "C" Py_ssize_t buffer_getreadbuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1078
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1079
return p->buffer_getreadbuffer( index, pp );
1081
catch( Py::Exception & )
1083
return -1; // indicate error
1087
extern "C" Py_ssize_t buffer_getwritebuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1091
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1092
return p->buffer_getwritebuffer( index, pp );
1094
catch( Py::Exception & )
1096
return -1; // indicate error
1100
extern "C" Py_ssize_t buffer_getsegcount_handler( PyObject *self, Py_ssize_t *count )
1104
PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1105
return p->buffer_getsegcount( count );
1107
catch( Py::Exception & )
1109
return -1; // indicate error
1114
//================================================================================
1116
// Implementation of PythonExtensionBase
1118
//================================================================================
1119
#define missing_method( method ) \
1120
throw RuntimeError( "Extension object does not support method " #method );
1122
PythonExtensionBase::PythonExtensionBase()
1126
PythonExtensionBase::~PythonExtensionBase()
1128
assert( ob_refcnt == 0 );
1131
int PythonExtensionBase::print( FILE *, int )
1132
{ missing_method( print ); return -1; }
1134
int PythonExtensionBase::setattr( const char*, const Py::Object & )
1135
{ missing_method( setattr ); return -1; }
1137
Py::Object PythonExtensionBase::getattro( const Py::Object & )
1138
{ missing_method( getattro ); return Py::Nothing(); }
1140
int PythonExtensionBase::setattro( const Py::Object &, const Py::Object & )
1141
{ missing_method( setattro ); return -1; }
1143
int PythonExtensionBase::compare( const Py::Object & )
1144
{ missing_method( compare ); return -1; }
1146
Py::Object PythonExtensionBase::repr()
1147
{ missing_method( repr ); return Py::Nothing(); }
1149
Py::Object PythonExtensionBase::str()
1150
{ missing_method( str ); return Py::Nothing(); }
1152
long PythonExtensionBase::hash()
1153
{ missing_method( hash ); return -1; }
1155
Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & )
1156
{ missing_method( call ); return Py::Nothing(); }
1158
Py::Object PythonExtensionBase::iter()
1159
{ missing_method( iter ); return Py::Nothing(); }
1161
PyObject* PythonExtensionBase::iternext()
1162
{ missing_method( iternext ); return NULL; }
1166
int PythonExtensionBase::sequence_length()
1167
{ missing_method( sequence_length ); return -1; }
1169
Py::Object PythonExtensionBase::sequence_concat( const Py::Object & )
1170
{ missing_method( sequence_concat ); return Py::Nothing(); }
1172
Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t )
1173
{ missing_method( sequence_repeat ); return Py::Nothing(); }
1175
Py::Object PythonExtensionBase::sequence_item( Py_ssize_t )
1176
{ missing_method( sequence_item ); return Py::Nothing(); }
1178
Py::Object PythonExtensionBase::sequence_slice( Py_ssize_t, Py_ssize_t )
1179
{ missing_method( sequence_slice ); return Py::Nothing(); }
1181
int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object & )
1182
{ missing_method( sequence_ass_item ); return -1; }
1184
int PythonExtensionBase::sequence_ass_slice( Py_ssize_t, Py_ssize_t, const Py::Object & )
1185
{ missing_method( sequence_ass_slice ); return -1; }
1189
int PythonExtensionBase::mapping_length()
1190
{ missing_method( mapping_length ); return -1; }
1192
Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & )
1193
{ missing_method( mapping_subscript ); return Py::Nothing(); }
1195
int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & )
1196
{ missing_method( mapping_ass_subscript ); return -1; }
1200
int PythonExtensionBase::number_nonzero()
1201
{ missing_method( number_nonzero ); return -1; }
1203
Py::Object PythonExtensionBase::number_negative()
1204
{ missing_method( number_negative ); return Py::Nothing(); }
1206
Py::Object PythonExtensionBase::number_positive()
1207
{ missing_method( number_positive ); return Py::Nothing(); }
1209
Py::Object PythonExtensionBase::number_absolute()
1210
{ missing_method( number_absolute ); return Py::Nothing(); }
1212
Py::Object PythonExtensionBase::number_invert()
1213
{ missing_method( number_invert ); return Py::Nothing(); }
1215
Py::Object PythonExtensionBase::number_int()
1216
{ missing_method( number_int ); return Py::Nothing(); }
1218
Py::Object PythonExtensionBase::number_float()
1219
{ missing_method( number_float ); return Py::Nothing(); }
1221
Py::Object PythonExtensionBase::number_long()
1222
{ missing_method( number_long ); return Py::Nothing(); }
1224
Py::Object PythonExtensionBase::number_oct()
1225
{ missing_method( number_oct ); return Py::Nothing(); }
1227
Py::Object PythonExtensionBase::number_hex()
1228
{ missing_method( number_hex ); return Py::Nothing(); }
1230
Py::Object PythonExtensionBase::number_add( const Py::Object & )
1231
{ missing_method( number_add ); return Py::Nothing(); }
1233
Py::Object PythonExtensionBase::number_subtract( const Py::Object & )
1234
{ missing_method( number_subtract ); return Py::Nothing(); }
1236
Py::Object PythonExtensionBase::number_multiply( const Py::Object & )
1237
{ missing_method( number_multiply ); return Py::Nothing(); }
1239
Py::Object PythonExtensionBase::number_divide( const Py::Object & )
1240
{ missing_method( number_divide ); return Py::Nothing(); }
1242
Py::Object PythonExtensionBase::number_remainder( const Py::Object & )
1243
{ missing_method( number_remainder ); return Py::Nothing(); }
1245
Py::Object PythonExtensionBase::number_divmod( const Py::Object & )
1246
{ missing_method( number_divmod ); return Py::Nothing(); }
1248
Py::Object PythonExtensionBase::number_lshift( const Py::Object & )
1249
{ missing_method( number_lshift ); return Py::Nothing(); }
1251
Py::Object PythonExtensionBase::number_rshift( const Py::Object & )
1252
{ missing_method( number_rshift ); return Py::Nothing(); }
1254
Py::Object PythonExtensionBase::number_and( const Py::Object & )
1255
{ missing_method( number_and ); return Py::Nothing(); }
1257
Py::Object PythonExtensionBase::number_xor( const Py::Object & )
1258
{ missing_method( number_xor ); return Py::Nothing(); }
1260
Py::Object PythonExtensionBase::number_or( const Py::Object & )
1261
{ missing_method( number_or ); return Py::Nothing(); }
1263
Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & )
1264
{ missing_method( number_power ); return Py::Nothing(); }
1268
Py_ssize_t PythonExtensionBase::buffer_getreadbuffer( Py_ssize_t, void** )
1269
{ missing_method( buffer_getreadbuffer ); return -1; }
1271
Py_ssize_t PythonExtensionBase::buffer_getwritebuffer( Py_ssize_t, void** )
1272
{ missing_method( buffer_getwritebuffer ); return -1; }
1274
Py_ssize_t PythonExtensionBase::buffer_getsegcount( Py_ssize_t* )
1275
{ missing_method( buffer_getsegcount ); return -1; }
1277
//--------------------------------------------------------------------------------
1279
// Method call handlers for
1280
// PythonExtensionBase
1281
// ExtensionModuleBase
1283
//--------------------------------------------------------------------------------
1285
extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
1289
Tuple self_and_name_tuple( _self_and_name_tuple );
1291
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1292
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1293
if( self_as_void == NULL )
1296
ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1298
String py_name( self_and_name_tuple[1] );
1299
std::string name( py_name.as_std_string() );
1301
Tuple args( _args );
1302
if( _keywords == NULL )
1304
Dict keywords; // pass an empty dict
1306
Object result( self->invoke_method_keyword( name, args, keywords ) );
1307
return new_reference_to( result.ptr() );
1310
Dict keywords( _keywords );
1312
Object result( self->invoke_method_keyword( name, args, keywords ) );
1313
return new_reference_to( result.ptr() );
1315
catch( Exception & )
1321
extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
1325
Tuple self_and_name_tuple( _self_and_name_tuple );
1327
PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1328
void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1329
if( self_as_void == NULL )
1332
ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1334
String py_name( self_and_name_tuple[1] );
1335
std::string name( py_name.as_std_string() );
1337
Tuple args( _args );
1339
Object result( self->invoke_method_varargs( name, args ) );
1341
return new_reference_to( result.ptr() );
1343
catch( Exception & )
1349
extern "C" void do_not_dealloc( void * )
1353
//--------------------------------------------------------------------------------
1355
// ExtensionExceptionType
1357
//--------------------------------------------------------------------------------
1358
ExtensionExceptionType::ExtensionExceptionType()
1363
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
1365
std::string module_name( module.fullName() );
1367
module_name += name;
1369
set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
1372
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent)
1374
std::string module_name( module.fullName() );
1376
module_name += name;
1378
set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
1381
ExtensionExceptionType::~ExtensionExceptionType()
1385
Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
1387
PyErr_SetString (exception.ptr(), reason.c_str());
1390
Exception::Exception( ExtensionExceptionType &exception, Object &reason )
1392
PyErr_SetObject (exception.ptr(), reason.ptr());
1395
Exception::Exception( PyObject* exception, Object &reason )
1397
PyErr_SetObject (exception, reason.ptr());
1400
} // end of namespace Py