bumping version to 3.5-rc1
[supercollider.git] / external_libraries / pycxx-6.2.2 / Src / Python3 / cxx_extensions.cxx
blob9f26166d9dfb265f433bd1e46c56387bd5244b1c
1 //-----------------------------------------------------------------------------
2 //
3 // Copyright (c) 1998 - 2007, The Regents of the University of California
4 // Produced at the Lawrence Livermore National Laboratory
5 // All rights reserved.
6 //
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
34 // DAMAGE.
36 //-----------------------------------------------------------------------------
37 #include "CXX/Extensions.hxx"
38 #include "CXX/Exception.hxx"
40 #include <assert.h>
42 #ifdef PYCXX_DEBUG
44 // Functions useful when debugging PyCXX
46 void bpt( void )
50 void printRefCount( PyObject *obj )
52 std::cout << "RefCount of 0x" << std::hex << reinterpret_cast< unsigned int >( obj ) << std::dec << " is " << Py_REFCNT( obj ) << std::endl;
54 #endif
56 namespace Py
59 void Object::validate()
61 // release pointer if not the right type
62 if( !accepts( p ) )
64 #if defined( _CPPRTTI ) || defined( __GNUG__ )
65 std::string s( "PyCXX: Error creating object of type " );
66 s += (typeid( *this )).name();
68 if( p != NULL )
70 String from_repr = repr();
71 s += " from ";
72 s += from_repr.as_std_string();
74 else
76 s += " from (nil)";
78 #endif
79 release();
80 if( PyErr_Occurred() )
81 { // Error message already set
82 throw Exception();
84 // Better error message if RTTI available
85 #if defined( _CPPRTTI ) || defined( __GNUG__ )
86 throw TypeError( s );
87 #else
88 throw TypeError( "PyCXX: type error." );
89 #endif
93 //================================================================================
95 // Implementation of MethodTable
97 //================================================================================
99 PyMethodDef MethodTable::method( const char *method_name, PyCFunction f, int flags, const char *doc )
101 PyMethodDef m;
102 m.ml_name = const_cast<char *>( method_name );
103 m.ml_meth = f;
104 m.ml_flags = flags;
105 m.ml_doc = const_cast<char *>( doc );
106 return m;
109 MethodTable::MethodTable()
111 t.push_back( method( 0, 0, 0, 0 ) );
112 mt = NULL;
115 MethodTable::~MethodTable()
117 delete [] mt;
120 void MethodTable::add( const char *method_name, PyCFunction f, const char *doc, int flag )
122 if( !mt )
124 t.insert( t.end()-1, method( method_name, f, flag, doc ) );
126 else
128 throw RuntimeError( "Too late to add a module method!" );
132 PyMethodDef *MethodTable::table()
134 if( !mt )
136 Py_ssize_t t1size = t.size();
137 mt = new PyMethodDef[ t1size ];
138 int j = 0;
139 for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
141 mt[ j++ ] = *i;
144 return mt;
147 //================================================================================
149 // Implementation of ExtensionModule
151 //================================================================================
152 ExtensionModuleBase::ExtensionModuleBase( const char *name )
153 : m_module_name( name )
154 , m_full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : m_module_name )
155 , m_method_table()
156 //m_module_def
157 , m_module( NULL )
160 ExtensionModuleBase::~ExtensionModuleBase()
163 const std::string &ExtensionModuleBase::name() const
165 return m_module_name;
168 const std::string &ExtensionModuleBase::fullName() const
170 return m_full_module_name;
173 class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
175 public:
176 ExtensionModuleBasePtr( ExtensionModuleBase *_module )
177 : module( _module )
180 virtual ~ExtensionModuleBasePtr()
183 ExtensionModuleBase *module;
186 void ExtensionModuleBase::initialize( const char *module_doc )
188 memset( &m_module_def, 0, sizeof( m_module_def ) );
190 m_module_def.m_name = const_cast<char *>( m_module_name.c_str() );
191 m_module_def.m_doc = const_cast<char *>( module_doc );
192 m_module_def.m_methods = m_method_table.table();
193 // where does module_ptr get passed in?
195 m_module = PyModule_Create( &m_module_def );
198 Py::Module ExtensionModuleBase::module( void ) const
200 return Module( m_module );
203 Py::Dict ExtensionModuleBase::moduleDictionary( void ) const
205 return module().getDict();
208 //================================================================================
210 // Implementation of PythonType
212 //================================================================================
213 extern "C"
215 static void standard_dealloc( PyObject *p );
217 // All the following functions redirect the call from Python
218 // onto the matching virtual function in PythonExtensionBase
220 static int print_handler( PyObject *, FILE *, int );
221 static PyObject *getattr_handler( PyObject *, char * );
222 static int setattr_handler( PyObject *, char *, PyObject * );
223 static PyObject *getattro_handler( PyObject *, PyObject * );
224 static int setattro_handler( PyObject *, PyObject *, PyObject * );
225 static PyObject *rich_compare_handler( PyObject *, PyObject *, int );
226 static PyObject *repr_handler( PyObject * );
227 static PyObject *str_handler( PyObject * );
228 static long hash_handler( PyObject * );
229 static PyObject *call_handler( PyObject *, PyObject *, PyObject * );
230 static PyObject *iter_handler( PyObject * );
231 static PyObject *iternext_handler( PyObject * );
233 // Sequence methods
234 static Py_ssize_t sequence_length_handler( PyObject * );
235 static PyObject *sequence_concat_handler( PyObject *,PyObject * );
236 static PyObject *sequence_repeat_handler( PyObject *, Py_ssize_t );
237 static PyObject *sequence_item_handler( PyObject *, Py_ssize_t );
238 static int sequence_ass_item_handler( PyObject *, Py_ssize_t, PyObject * );
240 // Mapping
241 static Py_ssize_t mapping_length_handler( PyObject * );
242 static PyObject *mapping_subscript_handler( PyObject *, PyObject * );
243 static int mapping_ass_subscript_handler( PyObject *, PyObject *, PyObject * );
245 // Numeric methods
246 static PyObject *number_negative_handler( PyObject * );
247 static PyObject *number_positive_handler( PyObject * );
248 static PyObject *number_absolute_handler( PyObject * );
249 static PyObject *number_invert_handler( PyObject * );
250 static PyObject *number_int_handler( PyObject * );
251 static PyObject *number_float_handler( PyObject * );
252 static PyObject *number_add_handler( PyObject *, PyObject * );
253 static PyObject *number_subtract_handler( PyObject *, PyObject * );
254 static PyObject *number_multiply_handler( PyObject *, PyObject * );
255 static PyObject *number_remainder_handler( PyObject *, PyObject * );
256 static PyObject *number_divmod_handler( PyObject *, PyObject * );
257 static PyObject *number_lshift_handler( PyObject *, PyObject * );
258 static PyObject *number_rshift_handler( PyObject *, PyObject * );
259 static PyObject *number_and_handler( PyObject *, PyObject * );
260 static PyObject *number_xor_handler( PyObject *, PyObject * );
261 static PyObject *number_or_handler( PyObject *, PyObject * );
262 static PyObject *number_power_handler( PyObject *, PyObject *, PyObject * );
264 // Buffer
265 // QQQ
268 extern "C" void standard_dealloc( PyObject *p )
270 PyMem_DEL( p );
273 bool PythonType::readyType()
275 return PyType_Ready( table ) >= 0;
278 PythonType &PythonType::supportSequenceType()
280 if( !sequence_table )
282 sequence_table = new PySequenceMethods;
283 memset( sequence_table, 0, sizeof( PySequenceMethods ) ); // ensure new fields are 0
284 table->tp_as_sequence = sequence_table;
285 sequence_table->sq_length = sequence_length_handler;
286 sequence_table->sq_concat = sequence_concat_handler;
287 sequence_table->sq_repeat = sequence_repeat_handler;
288 sequence_table->sq_item = sequence_item_handler;
290 sequence_table->sq_ass_item = sequence_ass_item_handler; // BAS setup seperately?
291 // QQQ sq_inplace_concat
292 // QQQ sq_inplace_repeat
294 return *this;
297 PythonType &PythonType::supportMappingType()
299 if( !mapping_table )
301 mapping_table = new PyMappingMethods;
302 memset( mapping_table, 0, sizeof( PyMappingMethods ) ); // ensure new fields are 0
303 table->tp_as_mapping = mapping_table;
304 mapping_table->mp_length = mapping_length_handler;
305 mapping_table->mp_subscript = mapping_subscript_handler;
306 mapping_table->mp_ass_subscript = mapping_ass_subscript_handler; // BAS setup seperately?
308 return *this;
311 PythonType &PythonType::supportNumberType()
313 if( !number_table )
315 number_table = new PyNumberMethods;
316 memset( number_table, 0, sizeof( PyNumberMethods ) ); // ensure new fields are 0
317 table->tp_as_number = number_table;
318 number_table->nb_add = number_add_handler;
319 number_table->nb_subtract = number_subtract_handler;
320 number_table->nb_multiply = number_multiply_handler;
321 number_table->nb_remainder = number_remainder_handler;
322 number_table->nb_divmod = number_divmod_handler;
323 number_table->nb_power = number_power_handler;
324 number_table->nb_negative = number_negative_handler;
325 number_table->nb_positive = number_positive_handler;
326 number_table->nb_absolute = number_absolute_handler;
327 number_table->nb_invert = number_invert_handler;
328 number_table->nb_lshift = number_lshift_handler;
329 number_table->nb_rshift = number_rshift_handler;
330 number_table->nb_and = number_and_handler;
331 number_table->nb_xor = number_xor_handler;
332 number_table->nb_or = number_or_handler;
333 number_table->nb_int = number_int_handler;
334 number_table->nb_float = number_float_handler;
336 // QQQ lots of new methods to add
338 return *this;
341 PythonType &PythonType::supportBufferType()
343 if( !buffer_table )
345 buffer_table = new PyBufferProcs;
346 memset( buffer_table, 0, sizeof( PyBufferProcs ) ); // ensure new fields are 0
347 table->tp_as_buffer = buffer_table;
348 // QQQ bf_getbuffer
349 // QQQ bf_releasebuffer
351 return *this;
354 // if you define one sequence method you must define
355 // all of them except the assigns
357 PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
358 : table( new PyTypeObject )
359 , sequence_table( NULL )
360 , mapping_table( NULL )
361 , number_table( NULL )
362 , buffer_table( NULL )
364 // PyTypeObject is defined in <python-sources>/Include/object.h
366 memset( table, 0, sizeof( PyTypeObject ) ); // ensure new fields are 0
367 *reinterpret_cast<PyObject *>( table ) = py_object_initializer;
368 // QQQ table->ob_type = _Type_Type();
369 // QQQ table->ob_size = 0;
370 table->tp_name = const_cast<char *>( default_name );
371 table->tp_basicsize = basic_size;
372 table->tp_itemsize = itemsize;
374 // Methods to implement standard operations
375 table->tp_dealloc = (destructor)standard_dealloc;
376 table->tp_print = 0;
377 table->tp_getattr = 0;
378 table->tp_setattr = 0;
379 table->tp_repr = 0;
381 // Method suites for standard classes
382 table->tp_as_number = 0;
383 table->tp_as_sequence = 0;
384 table->tp_as_mapping = 0;
386 // More standard operations (here for binary compatibility)
387 table->tp_hash = 0;
388 table->tp_call = 0;
389 table->tp_str = 0;
390 table->tp_getattro = 0;
391 table->tp_setattro = 0;
393 // Functions to access object as input/output buffer
394 table->tp_as_buffer = 0;
396 // Flags to define presence of optional/expanded features
397 table->tp_flags = Py_TPFLAGS_DEFAULT;
399 // Documentation string
400 table->tp_doc = 0;
402 table->tp_traverse = 0;
404 // delete references to contained objects
405 table->tp_clear = 0;
407 // Assigned meaning in release 2.1
408 // rich comparisons
409 table->tp_richcompare = 0;
410 // weak reference enabler
411 table->tp_weaklistoffset = 0;
413 // Iterators
414 table->tp_iter = 0;
415 table->tp_iternext = 0;
417 // Attribute descriptor and subclassing stuff
418 table->tp_methods = 0;
419 table->tp_members = 0;
420 table->tp_getset = 0;
421 table->tp_base = 0;
422 table->tp_dict = 0;
423 table->tp_descr_get = 0;
424 table->tp_descr_set = 0;
425 table->tp_dictoffset = 0;
426 table->tp_init = 0;
427 table->tp_alloc = 0;
428 table->tp_new = 0;
429 table->tp_free = 0; // Low-level free-memory routine
430 table->tp_is_gc = 0; // For PyObject_IS_GC
431 table->tp_bases = 0;
432 table->tp_mro = 0; // method resolution order
433 table->tp_cache = 0;
434 table->tp_subclasses = 0;
435 table->tp_weaklist = 0;
436 table->tp_del = 0;
438 // Type attribute cache version tag. Added in version 2.6
439 table->tp_version_tag = 0;
441 #ifdef COUNT_ALLOCS
442 table->tp_alloc = 0;
443 table->tp_free = 0;
444 table->tp_maxalloc = 0;
445 table->tp_orev = 0;
446 table->tp_next = 0;
447 #endif
450 PythonType::~PythonType()
452 delete table;
453 delete sequence_table;
454 delete mapping_table;
455 delete number_table;
456 delete buffer_table;
459 PyTypeObject *PythonType::type_object() const
461 return table;
464 PythonType &PythonType::name( const char *nam )
466 table->tp_name = const_cast<char *>( nam );
467 return *this;
470 const char *PythonType::getName() const
472 return table->tp_name;
475 PythonType &PythonType::doc( const char *d )
477 table->tp_doc = const_cast<char *>( d );
478 return *this;
481 const char *PythonType::getDoc() const
483 return table->tp_doc;
486 PythonType &PythonType::set_tp_dealloc( void (*tp_dealloc)( PyObject *self ) )
488 table->tp_dealloc = tp_dealloc;
489 return *this;
492 PythonType &PythonType::set_tp_init( int (*tp_init)( PyObject *self, PyObject *args, PyObject *kwds ) )
494 table->tp_init = tp_init;
495 return *this;
498 PythonType &PythonType::set_tp_new( PyObject *(*tp_new)( PyTypeObject *subtype, PyObject *args, PyObject *kwds ) )
500 table->tp_new = tp_new;
501 return *this;
504 PythonType &PythonType::set_methods( PyMethodDef *methods )
506 table->tp_methods = methods;
507 return *this;
510 PythonType &PythonType::supportClass()
512 table->tp_flags |= Py_TPFLAGS_BASETYPE;
513 return *this;
516 #ifdef PYCXX_PYTHON_2TO3
517 PythonType &PythonType::supportPrint()
519 table->tp_print = print_handler;
520 return *this;
522 #endif
524 PythonType &PythonType::supportGetattr()
526 table->tp_getattr = getattr_handler;
527 return *this;
530 PythonType &PythonType::supportSetattr()
532 table->tp_setattr = setattr_handler;
533 return *this;
536 PythonType &PythonType::supportGetattro()
538 table->tp_getattro = getattro_handler;
539 return *this;
542 PythonType &PythonType::supportSetattro()
544 table->tp_setattro = setattro_handler;
545 return *this;
548 #ifdef PYCXX_PYTHON_2TO3
549 PythonType &PythonType::supportCompare( void )
551 return *this;
553 #endif
556 PythonType &PythonType::supportRichCompare()
558 table->tp_richcompare = rich_compare_handler;
559 return *this;
562 PythonType &PythonType::supportRepr()
564 table->tp_repr = repr_handler;
565 return *this;
568 PythonType &PythonType::supportStr()
570 table->tp_str = str_handler;
571 return *this;
574 PythonType &PythonType::supportHash()
576 table->tp_hash = hash_handler;
577 return *this;
580 PythonType &PythonType::supportCall()
582 table->tp_call = call_handler;
583 return *this;
586 PythonType &PythonType::supportIter()
588 table->tp_iter = iter_handler;
589 table->tp_iternext = iternext_handler;
590 return *this;
593 //--------------------------------------------------------------------------------
595 // Handlers
597 //--------------------------------------------------------------------------------
598 PythonExtensionBase *getPythonExtensionBase( PyObject *self )
600 if( self->ob_type->tp_flags&Py_TPFLAGS_BASETYPE )
602 PythonClassInstance *instance = reinterpret_cast<PythonClassInstance *>( self );
603 return instance->m_pycxx_object;
605 else
607 return static_cast<PythonExtensionBase *>( self );
611 #ifdef PYCXX_PYTHON_2TO3
612 extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
616 PythonExtensionBase *p = getPythonExtensionBase( self );
617 return p->print( fp, flags );
619 catch( Py::Exception & )
621 return -1; // indicate error
624 #endif
626 extern "C" PyObject *getattr_handler( PyObject *self, char *name )
630 PythonExtensionBase *p = getPythonExtensionBase( self );
631 return new_reference_to( p->getattr( name ) );
633 catch( Py::Exception & )
635 return NULL; // indicate error
639 extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
643 PythonExtensionBase *p = getPythonExtensionBase( self );
644 return p->setattr( name, Py::Object( value ) );
646 catch( Py::Exception & )
648 return -1; // indicate error
652 extern "C" PyObject *getattro_handler( PyObject *self, PyObject *name )
656 PythonExtensionBase *p = getPythonExtensionBase( self );
657 return new_reference_to( p->getattro( Py::String( name ) ) );
659 catch( Py::Exception & )
661 return NULL; // indicate error
665 extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
669 PythonExtensionBase *p = getPythonExtensionBase( self );
670 return p->setattro( Py::String( name ), Py::Object( value ) );
672 catch( Py::Exception & )
674 return -1; // indicate error
678 extern "C" PyObject *rich_compare_handler( PyObject *self, PyObject *other, int op )
682 PythonExtensionBase *p = getPythonExtensionBase( self );
683 return new_reference_to( p->rich_compare( Py::Object( other ), op ) );
685 catch( Py::Exception & )
687 return NULL; // indicate error
691 extern "C" PyObject *repr_handler( PyObject *self )
695 PythonExtensionBase *p = getPythonExtensionBase( self );
696 return new_reference_to( p->repr() );
698 catch( Py::Exception & )
700 return NULL; // indicate error
704 extern "C" PyObject *str_handler( PyObject *self )
708 PythonExtensionBase *p = getPythonExtensionBase( self );
709 return new_reference_to( p->str() );
711 catch( Py::Exception & )
713 return NULL; // indicate error
717 extern "C" long hash_handler( PyObject *self )
721 PythonExtensionBase *p = getPythonExtensionBase( self );
722 return p->hash();
724 catch( Py::Exception & )
726 return -1; // indicate error
730 extern "C" PyObject *call_handler( PyObject *self, PyObject *args, PyObject *kw )
734 PythonExtensionBase *p = getPythonExtensionBase( self );
735 if( kw != NULL )
736 return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
737 else
738 return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
740 catch( Py::Exception & )
742 return NULL; // indicate error
746 extern "C" PyObject *iter_handler( PyObject *self )
750 PythonExtensionBase *p = getPythonExtensionBase( self );
751 return new_reference_to( p->iter() );
753 catch( Py::Exception & )
755 return NULL; // indicate error
759 extern "C" PyObject *iternext_handler( PyObject *self )
763 PythonExtensionBase *p = getPythonExtensionBase( self );
764 return p->iternext(); // might be a NULL ptr on end of iteration
766 catch( Py::Exception & )
768 return NULL; // indicate error
773 // Sequence methods
774 extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
778 PythonExtensionBase *p = getPythonExtensionBase( self );
779 return p->sequence_length();
781 catch( Py::Exception & )
783 return -1; // indicate error
787 extern "C" PyObject *sequence_concat_handler( PyObject *self, PyObject *other )
791 PythonExtensionBase *p = getPythonExtensionBase( self );
792 return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
794 catch( Py::Exception & )
796 return NULL; // indicate error
800 extern "C" PyObject *sequence_repeat_handler( PyObject *self, Py_ssize_t count )
804 PythonExtensionBase *p = getPythonExtensionBase( self );
805 return new_reference_to( p->sequence_repeat( count ) );
807 catch( Py::Exception & )
809 return NULL; // indicate error
813 extern "C" PyObject *sequence_item_handler( PyObject *self, Py_ssize_t index )
817 PythonExtensionBase *p = getPythonExtensionBase( self );
818 return new_reference_to( p->sequence_item( index ) );
820 catch( Py::Exception & )
822 return NULL; // indicate error
826 extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
830 PythonExtensionBase *p = getPythonExtensionBase( self );
831 return p->sequence_ass_item( index, Py::Object( value ) );
833 catch( Py::Exception & )
835 return -1; // indicate error
839 // Mapping
840 extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
844 PythonExtensionBase *p = getPythonExtensionBase( self );
845 return p->mapping_length();
847 catch( Py::Exception & )
849 return -1; // indicate error
853 extern "C" PyObject *mapping_subscript_handler( PyObject *self, PyObject *key )
857 PythonExtensionBase *p = getPythonExtensionBase( self );
858 return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
860 catch( Py::Exception & )
862 return NULL; // indicate error
866 extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
870 PythonExtensionBase *p = getPythonExtensionBase( self );
871 return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
873 catch( Py::Exception & )
875 return -1; // indicate error
879 // Number
880 extern "C" PyObject *number_negative_handler( PyObject *self )
884 PythonExtensionBase *p = getPythonExtensionBase( self );
885 return new_reference_to( p->number_negative() );
887 catch( Py::Exception & )
889 return NULL; // indicate error
893 extern "C" PyObject *number_positive_handler( PyObject *self )
897 PythonExtensionBase *p = getPythonExtensionBase( self );
898 return new_reference_to( p->number_positive() );
900 catch( Py::Exception & )
902 return NULL; // indicate error
906 extern "C" PyObject *number_absolute_handler( PyObject *self )
910 PythonExtensionBase *p = getPythonExtensionBase( self );
911 return new_reference_to( p->number_absolute() );
913 catch( Py::Exception & )
915 return NULL; // indicate error
919 extern "C" PyObject *number_invert_handler( PyObject *self )
923 PythonExtensionBase *p = getPythonExtensionBase( self );
924 return new_reference_to( p->number_invert() );
926 catch( Py::Exception & )
928 return NULL; // indicate error
932 extern "C" PyObject *number_int_handler( PyObject *self )
936 PythonExtensionBase *p = getPythonExtensionBase( self );
937 return new_reference_to( p->number_int() );
939 catch( Py::Exception & )
941 return NULL; // indicate error
945 extern "C" PyObject *number_float_handler( PyObject *self )
949 PythonExtensionBase *p = getPythonExtensionBase( self );
950 return new_reference_to( p->number_float() );
952 catch( Py::Exception & )
954 return NULL; // indicate error
958 extern "C" PyObject *number_add_handler( PyObject *self, PyObject *other )
962 PythonExtensionBase *p = getPythonExtensionBase( self );
963 return new_reference_to( p->number_add( Py::Object( other ) ) );
965 catch( Py::Exception & )
967 return NULL; // indicate error
971 extern "C" PyObject *number_subtract_handler( PyObject *self, PyObject *other )
975 PythonExtensionBase *p = getPythonExtensionBase( self );
976 return new_reference_to( p->number_subtract( Py::Object( other ) ) );
978 catch( Py::Exception & )
980 return NULL; // indicate error
984 extern "C" PyObject *number_multiply_handler( PyObject *self, PyObject *other )
988 PythonExtensionBase *p = getPythonExtensionBase( self );
989 return new_reference_to( p->number_multiply( Py::Object( other ) ) );
991 catch( Py::Exception & )
993 return NULL; // indicate error
997 extern "C" PyObject *number_remainder_handler( PyObject *self, PyObject *other )
1001 PythonExtensionBase *p = getPythonExtensionBase( self );
1002 return new_reference_to( p->number_remainder( Py::Object( other ) ) );
1004 catch( Py::Exception & )
1006 return NULL; // indicate error
1010 extern "C" PyObject *number_divmod_handler( PyObject *self, PyObject *other )
1014 PythonExtensionBase *p = getPythonExtensionBase( self );
1015 return new_reference_to( p->number_divmod( Py::Object( other ) ) );
1017 catch( Py::Exception & )
1019 return NULL; // indicate error
1023 extern "C" PyObject *number_lshift_handler( PyObject *self, PyObject *other )
1027 PythonExtensionBase *p = getPythonExtensionBase( self );
1028 return new_reference_to( p->number_lshift( Py::Object( other ) ) );
1030 catch( Py::Exception & )
1032 return NULL; // indicate error
1036 extern "C" PyObject *number_rshift_handler( PyObject *self, PyObject *other )
1040 PythonExtensionBase *p = getPythonExtensionBase( self );
1041 return new_reference_to( p->number_rshift( Py::Object( other ) ) );
1043 catch( Py::Exception & )
1045 return NULL; // indicate error
1049 extern "C" PyObject *number_and_handler( PyObject *self, PyObject *other )
1053 PythonExtensionBase *p = getPythonExtensionBase( self );
1054 return new_reference_to( p->number_and( Py::Object( other ) ) );
1056 catch( Py::Exception & )
1058 return NULL; // indicate error
1062 extern "C" PyObject *number_xor_handler( PyObject *self, PyObject *other )
1066 PythonExtensionBase *p = getPythonExtensionBase( self );
1067 return new_reference_to( p->number_xor( Py::Object( other ) ) );
1069 catch( Py::Exception & )
1071 return NULL; // indicate error
1075 extern "C" PyObject *number_or_handler( PyObject *self, PyObject *other )
1079 PythonExtensionBase *p = getPythonExtensionBase( self );
1080 return new_reference_to( p->number_or( Py::Object( other ) ) );
1082 catch( Py::Exception & )
1084 return NULL; // indicate error
1088 extern "C" PyObject *number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
1092 PythonExtensionBase *p = getPythonExtensionBase( self );
1093 return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
1095 catch( Py::Exception & )
1097 return NULL; // indicate error
1101 // Buffer
1103 //================================================================================
1105 // Implementation of PythonExtensionBase
1107 //================================================================================
1108 #define missing_method( method ) \
1109 throw RuntimeError( "Extension object missing implement of " #method );
1111 PythonExtensionBase::PythonExtensionBase()
1113 ob_refcnt = 0;
1116 PythonExtensionBase::~PythonExtensionBase()
1118 assert( ob_refcnt == 0 );
1121 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name )
1123 Py::TupleN args;
1124 return self().callMemberFunction( fn_name, args );
1127 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1128 const Py::Object &arg1 )
1130 Py::TupleN args( arg1 );
1131 return self().callMemberFunction( fn_name, args );
1134 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1135 const Py::Object &arg1, const Py::Object &arg2 )
1137 Py::TupleN args( arg1, arg2 );
1138 return self().callMemberFunction( fn_name, args );
1141 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1142 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3 )
1144 Py::TupleN args( arg1, arg2, arg3 );
1145 return self().callMemberFunction( fn_name, args );
1148 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1149 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1150 const Py::Object &arg4 )
1152 Py::TupleN args( arg1, arg2, arg3, arg4 );
1153 return self().callMemberFunction( fn_name, args );
1156 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1157 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1158 const Py::Object &arg4, const Py::Object &arg5 )
1160 Py::TupleN args( arg1, arg2, arg3, arg4, arg5 );
1161 return self().callMemberFunction( fn_name, args );
1164 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1165 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1166 const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6 )
1168 Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6 );
1169 return self().callMemberFunction( fn_name, args );
1172 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1173 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1174 const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1175 const Py::Object &arg7 )
1177 Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7 );
1178 return self().callMemberFunction( fn_name, args );
1181 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1182 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1183 const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1184 const Py::Object &arg7, const Py::Object &arg8 )
1186 Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 );
1187 return self().callMemberFunction( fn_name, args );
1190 Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1191 const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1192 const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1193 const Py::Object &arg7, const Py::Object &arg8, const Py::Object &arg9 )
1195 Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 );
1196 return self().callMemberFunction( fn_name, args );
1199 void PythonExtensionBase::reinit( Tuple &args, Dict &kwds )
1201 throw RuntimeError( "Must not call __init__ twice on this class" );
1205 Py::Object PythonExtensionBase::genericGetAttro( const Py::String &name )
1207 return asObject( PyObject_GenericGetAttr( selfPtr(), name.ptr() ) );
1210 int PythonExtensionBase::genericSetAttro( const Py::String &name, const Py::Object &value )
1212 return PyObject_GenericSetAttr( selfPtr(), name.ptr(), value.ptr() );
1215 #ifdef PYCXX_PYTHON_2TO3
1216 int PythonExtensionBase::print( FILE *, int )
1218 missing_method( print );
1219 return -1;
1221 #endif
1223 Py::Object PythonExtensionBase::getattr( const char * )
1225 missing_method( getattr );
1226 return Py::None();
1229 int PythonExtensionBase::setattr( const char *, const Py::Object & )
1231 missing_method( setattr );
1232 return -1;
1235 Py::Object PythonExtensionBase::getattro( const Py::String &name )
1237 return asObject( PyObject_GenericGetAttr( selfPtr(), name.ptr() ) );
1240 int PythonExtensionBase::setattro( const Py::String &name, const Py::Object &value )
1242 return PyObject_GenericSetAttr( selfPtr(), name.ptr(), value.ptr() );
1246 int PythonExtensionBase::compare( const Py::Object & )
1248 missing_method( compare );
1249 return -1;
1252 Py::Object PythonExtensionBase::rich_compare( const Py::Object &, int )
1254 missing_method( rich_compare );
1255 return Py::None();
1258 Py::Object PythonExtensionBase::repr()
1260 missing_method( repr );
1261 return Py::None();
1264 Py::Object PythonExtensionBase::str()
1266 missing_method( str );
1267 return Py::None();
1270 long PythonExtensionBase::hash()
1272 missing_method( hash );
1273 return -1; }
1276 Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & )
1278 missing_method( call );
1279 return Py::None();
1282 Py::Object PythonExtensionBase::iter()
1284 missing_method( iter );
1285 return Py::None();
1288 PyObject *PythonExtensionBase::iternext()
1290 missing_method( iternext );
1291 return NULL; }
1295 // Sequence methods
1296 int PythonExtensionBase::sequence_length()
1298 missing_method( sequence_length );
1299 return -1; }
1302 Py::Object PythonExtensionBase::sequence_concat( const Py::Object & )
1304 missing_method( sequence_concat );
1305 return Py::None();
1308 Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t )
1310 missing_method( sequence_repeat );
1311 return Py::None();
1314 Py::Object PythonExtensionBase::sequence_item( Py_ssize_t )
1316 missing_method( sequence_item );
1317 return Py::None();
1320 int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object & )
1322 missing_method( sequence_ass_item );
1323 return -1;
1327 // Mapping
1328 int PythonExtensionBase::mapping_length()
1330 missing_method( mapping_length );
1331 return -1;
1335 Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & )
1337 missing_method( mapping_subscript );
1338 return Py::None();
1341 int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & )
1343 missing_method( mapping_ass_subscript );
1344 return -1;
1347 Py::Object PythonExtensionBase::number_negative()
1349 missing_method( number_negative );
1350 return Py::None();
1353 Py::Object PythonExtensionBase::number_positive()
1355 missing_method( number_positive );
1356 return Py::None();
1359 Py::Object PythonExtensionBase::number_absolute()
1361 missing_method( number_absolute );
1362 return Py::None();
1365 Py::Object PythonExtensionBase::number_invert()
1367 missing_method( number_invert );
1368 return Py::None();
1371 Py::Object PythonExtensionBase::number_int()
1373 missing_method( number_int );
1374 return Py::None();
1377 Py::Object PythonExtensionBase::number_float()
1379 missing_method( number_float );
1380 return Py::None();
1383 Py::Object PythonExtensionBase::number_long()
1385 missing_method( number_long );
1386 return Py::None();
1389 Py::Object PythonExtensionBase::number_add( const Py::Object & )
1391 missing_method( number_add );
1392 return Py::None();
1395 Py::Object PythonExtensionBase::number_subtract( const Py::Object & )
1397 missing_method( number_subtract );
1398 return Py::None();
1401 Py::Object PythonExtensionBase::number_multiply( const Py::Object & )
1403 missing_method( number_multiply );
1404 return Py::None();
1407 Py::Object PythonExtensionBase::number_remainder( const Py::Object & )
1409 missing_method( number_remainder );
1410 return Py::None();
1413 Py::Object PythonExtensionBase::number_divmod( const Py::Object & )
1415 missing_method( number_divmod );
1416 return Py::None();
1419 Py::Object PythonExtensionBase::number_lshift( const Py::Object & )
1421 missing_method( number_lshift );
1422 return Py::None();
1425 Py::Object PythonExtensionBase::number_rshift( const Py::Object & )
1427 missing_method( number_rshift );
1428 return Py::None();
1431 Py::Object PythonExtensionBase::number_and( const Py::Object & )
1433 missing_method( number_and );
1434 return Py::None();
1437 Py::Object PythonExtensionBase::number_xor( const Py::Object & )
1439 missing_method( number_xor );
1440 return Py::None();
1443 Py::Object PythonExtensionBase::number_or( const Py::Object & )
1445 missing_method( number_or );
1446 return Py::None();
1449 Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & )
1451 missing_method( number_power );
1452 return Py::None();
1456 // Buffer
1457 // QQQ
1459 //--------------------------------------------------------------------------------
1461 // Method call handlers for
1462 // PythonExtensionBase
1463 // ExtensionModuleBase
1465 //--------------------------------------------------------------------------------
1466 // Note: Python calls noargs as varargs buts args==NULL
1467 extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, PyObject * )
1471 Tuple self_and_name_tuple( _self_and_name_tuple );
1473 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1474 void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1475 if( self_as_void == NULL )
1476 return NULL;
1478 ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1480 Object result( self->invoke_method_noargs( PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ) ) );
1482 return new_reference_to( result.ptr() );
1484 catch( Exception & )
1486 return 0;
1490 extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
1494 Tuple self_and_name_tuple( _self_and_name_tuple );
1496 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1497 void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1498 if( self_as_void == NULL )
1499 return NULL;
1501 ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1502 Tuple args( _args );
1503 Object result
1505 self->invoke_method_varargs
1507 PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1508 args
1512 return new_reference_to( result.ptr() );
1514 catch( Exception & )
1516 return 0;
1520 extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
1524 Tuple self_and_name_tuple( _self_and_name_tuple );
1526 PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1527 void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1528 if( self_as_void == NULL )
1529 return NULL;
1531 ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1533 Tuple args( _args );
1535 if( _keywords == NULL )
1537 Dict keywords; // pass an empty dict
1539 Object result
1541 self->invoke_method_keyword
1543 PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1544 args,
1545 keywords
1549 return new_reference_to( result.ptr() );
1551 else
1553 Dict keywords( _keywords ); // make dict
1555 Object result
1557 self->invoke_method_keyword
1559 PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1560 args,
1561 keywords
1565 return new_reference_to( result.ptr() );
1568 catch( Exception & )
1570 return 0;
1574 extern "C" void do_not_dealloc( void * )
1578 //--------------------------------------------------------------------------------
1580 // ExtensionExceptionType
1582 //--------------------------------------------------------------------------------
1583 ExtensionExceptionType::ExtensionExceptionType()
1584 : Py::Object()
1588 void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
1590 std::string module_name( module.fullName() );
1591 module_name += ".";
1592 module_name += name;
1594 set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
1597 void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent )
1599 std::string module_name( module.fullName() );
1600 module_name += ".";
1601 module_name += name;
1603 set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
1606 ExtensionExceptionType::~ExtensionExceptionType()
1610 Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
1612 PyErr_SetString( exception.ptr(), reason.c_str() );
1615 Exception::Exception( ExtensionExceptionType &exception, Object &reason )
1617 PyErr_SetObject( exception.ptr(), reason.ptr() );
1620 Exception::Exception( PyObject *exception, Object &reason )
1622 PyErr_SetObject( exception, reason.ptr() );
1625 #if 1
1626 //------------------------------------------------------------
1627 // compare operators
1628 bool operator!=( const Long &a, const Long &b )
1630 return a.as_long() != b.as_long();
1633 bool operator!=( const Long &a, int b )
1635 return a.as_long() != b;
1638 bool operator!=( const Long &a, long b )
1640 return a.as_long() != b;
1643 bool operator!=( int a, const Long &b )
1645 return a != b.as_long();
1648 bool operator!=( long a, const Long &b )
1650 return a != b.as_long();
1653 //------------------------------
1654 bool operator==( const Long &a, const Long &b )
1656 return a.as_long() == b.as_long();
1659 bool operator==( const Long &a, int b )
1661 return a.as_long() == b;
1664 bool operator==( const Long &a, long b )
1666 return a.as_long() == b;
1669 bool operator==( int a, const Long &b )
1671 return a == b.as_long();
1674 bool operator==( long a, const Long &b )
1676 return a == b.as_long();
1679 //------------------------------
1680 bool operator>( const Long &a, const Long &b )
1682 return a.as_long() > b.as_long();
1685 bool operator>( const Long &a, int b )
1687 return a.as_long() > b;
1690 bool operator>( const Long &a, long b )
1692 return a.as_long() > b;
1695 bool operator>( int a, const Long &b )
1697 return a > b.as_long();
1700 bool operator>( long a, const Long &b )
1702 return a > b.as_long();
1705 //------------------------------
1706 bool operator>=( const Long &a, const Long &b )
1708 return a.as_long() >= b.as_long();
1711 bool operator>=( const Long &a, int b )
1713 return a.as_long() >= b;
1716 bool operator>=( const Long &a, long b )
1718 return a.as_long() >= b;
1721 bool operator>=( int a, const Long &b )
1723 return a >= b.as_long();
1726 bool operator>=( long a, const Long &b )
1728 return a >= b.as_long();
1731 //------------------------------
1732 bool operator<( const Long &a, const Long &b )
1734 return a.as_long() < b.as_long();
1737 bool operator<( const Long &a, int b )
1739 return a.as_long() < b;
1742 bool operator<( const Long &a, long b )
1744 return a.as_long() < b;
1747 bool operator<( int a, const Long &b )
1749 return a < b.as_long();
1752 bool operator<( long a, const Long &b )
1754 return a < b.as_long();
1757 //------------------------------
1758 bool operator<=( const Long &a, const Long &b )
1760 return a.as_long() <= b.as_long();
1763 bool operator<=( int a, const Long &b )
1765 return a <= b.as_long();
1768 bool operator<=( long a, const Long &b )
1770 return a <= b.as_long();
1773 bool operator<=( const Long &a, int b )
1775 return a.as_long() <= b;
1778 bool operator<=( const Long &a, long b )
1780 return a.as_long() <= b;
1783 #ifdef HAVE_LONG_LONG
1784 //------------------------------
1785 bool operator!=( const Long &a, PY_LONG_LONG b )
1787 return a.as_long_long() != b;
1790 bool operator!=( PY_LONG_LONG a, const Long &b )
1792 return a != b.as_long_long();
1795 //------------------------------
1796 bool operator==( const Long &a, PY_LONG_LONG b )
1798 return a.as_long_long() == b;
1801 bool operator==( PY_LONG_LONG a, const Long &b )
1803 return a == b.as_long_long();
1806 //------------------------------
1807 bool operator>( const Long &a, PY_LONG_LONG b )
1809 return a.as_long_long() > b;
1812 bool operator>( PY_LONG_LONG a, const Long &b )
1814 return a > b.as_long_long();
1817 //------------------------------
1818 bool operator>=( const Long &a, PY_LONG_LONG b )
1820 return a.as_long_long() >= b;
1823 bool operator>=( PY_LONG_LONG a, const Long &b )
1825 return a >= b.as_long_long();
1828 //------------------------------
1829 bool operator<( const Long &a, PY_LONG_LONG b )
1831 return a.as_long_long() < b;
1834 bool operator<( PY_LONG_LONG a, const Long &b )
1836 return a < b.as_long_long();
1839 //------------------------------
1840 bool operator<=( const Long &a, PY_LONG_LONG b )
1842 return a.as_long_long() <= b;
1845 bool operator<=( PY_LONG_LONG a, const Long &b )
1847 return a <= b.as_long_long();
1849 #endif
1850 #endif
1852 //------------------------------------------------------------
1853 // compare operators
1854 bool operator!=( const Float &a, const Float &b )
1856 return a.as_double() != b.as_double();
1859 bool operator!=( const Float &a, double b )
1861 return a.as_double() != b;
1864 bool operator!=( double a, const Float &b )
1866 return a != b.as_double();
1869 //------------------------------
1870 bool operator==( const Float &a, const Float &b )
1872 return a.as_double() == b.as_double();
1875 bool operator==( const Float &a, double b )
1877 return a.as_double() == b;
1880 bool operator==( double a, const Float &b )
1882 return a == b.as_double();
1885 //------------------------------
1886 bool operator>( const Float &a, const Float &b )
1888 return a.as_double() > b.as_double();
1891 bool operator>( const Float &a, double b )
1893 return a.as_double() > b;
1896 bool operator>( double a, const Float &b )
1898 return a > b.as_double();
1901 //------------------------------
1902 bool operator>=( const Float &a, const Float &b )
1904 return a.as_double() >= b.as_double();
1907 bool operator>=( const Float &a, double b )
1909 return a.as_double() >= b;
1912 bool operator>=( double a, const Float &b )
1914 return a >= b.as_double();
1917 //------------------------------
1918 bool operator<( const Float &a, const Float &b )
1920 return a.as_double() < b.as_double();
1923 bool operator<( const Float &a, double b )
1925 return a.as_double() < b;
1928 bool operator<( double a, const Float &b )
1930 return a < b.as_double();
1933 //------------------------------
1934 bool operator<=( const Float &a, const Float &b )
1936 return a.as_double() <= b.as_double();
1939 bool operator<=( double a, const Float &b )
1941 return a <= b.as_double();
1944 bool operator<=( const Float &a, double b )
1946 return a.as_double() <= b;
1949 } // end of namespace Py