FreeCAD

Форк
0
/
cxx_extensions.cxx 
1904 строки · 50.3 Кб
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.
10
//
11
// Redistribution  and  use  in  source  and  binary  forms,  with  or  without
12
// modification, are permitted provided that the following conditions are met:
13
//
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.
22
//
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.
35
//
36
//-----------------------------------------------------------------------------
37
#include "CXX/Extensions.hxx"
38
#include "CXX/Exception.hxx"
39
#include <assert.h>
40

41
#ifdef PYCXX_DEBUG
42
//
43
//  Functions useful when debugging PyCXX
44
//
45
void bpt( void )
46
{
47
}
48

49
void printRefCount( PyObject *obj )
50
{
51
    std::cout << "RefCount of 0x" << std::hex << reinterpret_cast< unsigned int >( obj ) << std::dec << " is " << Py_REFCNT( obj ) << std::endl;
52
}
53
#endif
54

55
namespace Py
56
{
57
#ifdef PYCXX_PYTHON_2TO3
58
std::string String::as_std_string( const char *encoding, const char *error ) const
59
{
60
    if( isUnicode() )
61
    {
62
        Bytes encoded( encode( encoding, error ) );
63
        return encoded.as_std_string();
64
    }
65
    else
66
    {
67
        return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
68
    }
69
}
70

71
Bytes String::encode( const char *encoding, const char *error ) const
72
{
73
    if( isUnicode() )
74
    {
75
        return Bytes( PyUnicode_AsEncodedString( ptr(), encoding, error ) );
76
    }
77
    else
78
    {
79
        return Bytes( PyString_AsEncodedObject( ptr(), encoding, error ) );
80
    }
81
}
82

83
#else
84
std::string String::as_std_string( const char *encoding, const char *error ) const
85
{
86
    if( isUnicode() )
87
    {
88
        String encoded( encode( encoding, error ) );
89
        return encoded.as_std_string();
90
    }
91
    else
92
    {
93
        return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
94
    }
95
}
96
#endif
97

98
void Object::validate()
99
{
100
    // release pointer if not the right type
101
    if( !accepts( p ) )
102
    {
103
#if defined( _CPPRTTI ) || defined( __GNUG__ )
104
        std::string s( "PyCXX: Error creating object of type " );
105
        s += (typeid( *this )).name();
106

107
        if( p != NULL )
108
        {
109
            String from_repr = repr();
110
            s += " from ";
111
            s += from_repr.as_std_string( "utf-8" );
112
        }
113
        else
114
        {
115
            s += " from (nil)";
116
        }
117
#endif
118
        release();
119
        if( PyErr_Occurred() )
120
        { // Error message already set
121
            throw Exception();
122
        }
123
        // Better error message if RTTI available
124
#if defined( _CPPRTTI ) || defined( __GNUG__ )
125
        throw TypeError( s );
126
#else
127
        throw TypeError( "PyCXX: type error." );
128
#endif
129
    }
130
}
131

132
//================================================================================
133
//
134
//    Implementation of MethodTable
135
//
136
//================================================================================
137
PyMethodDef MethodTable::method( const char *method_name, PyCFunction f, int flags, const char *doc )
138
{
139
    PyMethodDef m;
140
    m.ml_name = const_cast<char*>( method_name );
141
    m.ml_meth = f;
142
    m.ml_flags = flags;
143
    m.ml_doc = const_cast<char*>( doc );
144
    return m;
145
}
146

147
MethodTable::MethodTable()
148
{
149
    t.push_back( method( 0, 0, 0, 0 ) );
150
    mt = NULL;
151
}
152

153
MethodTable::~MethodTable()
154
{
155
    delete [] mt;
156
}
157

158
void MethodTable::add( const char *method_name, PyCFunction f, const char *doc, int flag )
159
{
160
    if( !mt )
161
    {
162
        t.insert( t.end()-1, method( method_name, f, flag, doc ) );
163
    }
164
    else
165
    {
166
        throw RuntimeError( "Too late to add a module method!" );
167
    }
168
}
169

170
PyMethodDef *MethodTable::table()
171
{
172
    if( !mt )
173
    {
174
        Py_ssize_t t1size = t.size();
175
        mt = new PyMethodDef[ t1size ];
176
        int j = 0;
177
        for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
178
        {
179
            mt[ j++ ] = *i;
180
        }
181
    }
182
    return mt;
183
}
184

185
//================================================================================
186
//
187
//    Implementation of ExtensionModule
188
//
189
//================================================================================
190
ExtensionModuleBase::ExtensionModuleBase( const char *name )
191
: m_module_name( name )
192
, m_full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : m_module_name )
193
, m_method_table()
194
, m_module( NULL )
195
{}
196

197
ExtensionModuleBase::~ExtensionModuleBase()
198
{}
199

200
const std::string &ExtensionModuleBase::name() const
201
{
202
    return m_module_name;
203
}
204

205
const std::string &ExtensionModuleBase::fullName() const
206
{
207
    return m_full_module_name;
208
}
209

210
class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
211
{
212
public:
213
    ExtensionModuleBasePtr( ExtensionModuleBase *_module )
214
    : module( _module )
215
    {}
216

217
    virtual ~ExtensionModuleBasePtr()
218
    {}
219

220
    ExtensionModuleBase *module;
221
};
222

223
void ExtensionModuleBase::initialize( const char *module_doc )
224
{
225
    PyObject *module_ptr = new ExtensionModuleBasePtr( this );
226
    m_module = Py_InitModule4
227
    (
228
    const_cast<char *>( m_module_name.c_str() ),    // name
229
    m_method_table.table(),                         // methods
230
    const_cast<char *>( module_doc ),               // docs
231
    module_ptr,                                     // pass to functions as "self"
232
    PYTHON_API_VERSION                              // API version
233
    );
234
}
235

236
Py::Module ExtensionModuleBase::module( void ) const
237
{
238
    return Module( m_full_module_name );
239
}
240

241
Py::Dict ExtensionModuleBase::moduleDictionary( void ) const
242
{
243
    return module().getDict();
244
}
245

246
Object ExtensionModuleBase::moduleObject( void ) const
247
{
248
    return Object( m_module );
249
}
250

251
//================================================================================
252
//
253
//    Implementation of PythonType
254
//
255
//================================================================================
256
extern "C"
257
{
258
    static void standard_dealloc( PyObject *p );
259
    //
260
    // All the following functions redirect the call from Python
261
    // onto the matching virtual function in PythonExtensionBase
262
    //
263
#if defined( PYCXX_PYTHON_2TO3 )
264
    static int print_handler( PyObject *, FILE *, int );
265
#endif
266
    static PyObject *getattr_handler( PyObject *, char * );
267
    static int setattr_handler( PyObject *, char *, PyObject * );
268
    static PyObject *getattro_handler( PyObject *, PyObject * );
269
    static int setattro_handler( PyObject *, PyObject *, PyObject * );
270
#if defined( PYCXX_PYTHON_2TO3 )
271
    static int compare_handler( PyObject *, PyObject * );
272
#endif
273
    static PyObject *rich_compare_handler( PyObject *, PyObject *, int );
274
    static PyObject *repr_handler( PyObject * );
275
    static PyObject *str_handler( PyObject * );
276
    static long hash_handler( PyObject * );
277
    static PyObject *call_handler( PyObject *, PyObject *, PyObject * );
278
    static PyObject *iter_handler( PyObject * );
279
    static PyObject *iternext_handler( PyObject * );
280

281
    // Sequence methods
282
    static Py_ssize_t sequence_length_handler( PyObject * );
283
    static PyObject *sequence_concat_handler( PyObject *,PyObject * );
284
    static PyObject *sequence_repeat_handler( PyObject *, Py_ssize_t );
285
    static PyObject *sequence_item_handler( PyObject *, Py_ssize_t );
286
    static PyObject *sequence_slice_handler( PyObject *, Py_ssize_t, Py_ssize_t );
287
    static int sequence_ass_item_handler( PyObject *, Py_ssize_t, PyObject * );
288
    static int sequence_ass_slice_handler( PyObject *, Py_ssize_t, Py_ssize_t, PyObject * );
289

290
    // Mapping
291
    static Py_ssize_t mapping_length_handler( PyObject * );
292
    static PyObject *mapping_subscript_handler( PyObject *, PyObject * );
293
    static int mapping_ass_subscript_handler( PyObject *, PyObject *, PyObject * );
294

295
    // Numeric methods
296
    static int number_nonzero_handler( PyObject * );
297
    static PyObject *number_negative_handler( PyObject * );
298
    static PyObject *number_positive_handler( PyObject * );
299
    static PyObject *number_absolute_handler( PyObject * );
300
    static PyObject *number_invert_handler( PyObject * );
301
    static PyObject *number_int_handler( PyObject * );
302
    static PyObject *number_float_handler( PyObject * );
303
    static PyObject *number_long_handler( PyObject * );
304
    static PyObject *number_oct_handler( PyObject * );
305
    static PyObject *number_hex_handler( PyObject * );
306
    static PyObject *number_add_handler( PyObject *, PyObject * );
307
    static PyObject *number_subtract_handler( PyObject *, PyObject * );
308
    static PyObject *number_multiply_handler( PyObject *, PyObject * );
309
    static PyObject *number_divide_handler( PyObject *, PyObject * );
310
    static PyObject *number_remainder_handler( PyObject *, PyObject * );
311
    static PyObject *number_divmod_handler( PyObject *, PyObject * );
312
    static PyObject *number_lshift_handler( PyObject *, PyObject * );
313
    static PyObject *number_rshift_handler( PyObject *, PyObject * );
314
    static PyObject *number_and_handler( PyObject *, PyObject * );
315
    static PyObject *number_xor_handler( PyObject *, PyObject * );
316
    static PyObject *number_or_handler( PyObject *, PyObject * );
317
    static PyObject *number_power_handler( PyObject *, PyObject *, PyObject * );
318

319
    // Buffer
320
    static Py_ssize_t buffer_getreadbuffer_handler( PyObject *, Py_ssize_t, void ** );
321
    static Py_ssize_t buffer_getwritebuffer_handler( PyObject *, Py_ssize_t, void ** );
322
    static Py_ssize_t buffer_getsegcount_handler( PyObject *, Py_ssize_t * );
323
}
324

325
extern "C" void standard_dealloc( PyObject *p )
326
{
327
    PyMem_DEL( p );
328
}
329

330
bool PythonType::readyType()
331
{
332
    return PyType_Ready( table ) >= 0;
333
}
334

335
PythonType &PythonType::supportSequenceType()
336
{
337
    if( !sequence_table )
338
    {
339
        sequence_table = new PySequenceMethods;
340
        memset( sequence_table, 0, sizeof( PySequenceMethods ) );   // ensure new fields are 0
341
        table->tp_as_sequence = sequence_table;
342
        sequence_table->sq_length = sequence_length_handler;
343
        sequence_table->sq_concat = sequence_concat_handler;
344
        sequence_table->sq_repeat = sequence_repeat_handler;
345
        sequence_table->sq_item = sequence_item_handler;
346
        sequence_table->sq_slice = sequence_slice_handler;
347
        sequence_table->sq_ass_item = sequence_ass_item_handler;    // BAS setup separately?
348
        sequence_table->sq_ass_slice = sequence_ass_slice_handler;  // BAS setup separately?
349
    }
350
    return *this;
351
}
352

353
PythonType &PythonType::supportMappingType()
354
{
355
    if( !mapping_table )
356
    {
357
        mapping_table = new PyMappingMethods;
358
        memset( mapping_table, 0, sizeof( PyMappingMethods ) );   // ensure new fields are 0
359
        table->tp_as_mapping = mapping_table;
360
        mapping_table->mp_length = mapping_length_handler;
361
        mapping_table->mp_subscript = mapping_subscript_handler;
362
        mapping_table->mp_ass_subscript = mapping_ass_subscript_handler;    // BAS setup separately?
363
    }
364
    return *this;
365
}
366

367
PythonType &PythonType::supportNumberType()
368
{
369
    if( !number_table )
370
    {
371
        number_table = new PyNumberMethods;
372
        memset( number_table, 0, sizeof( PyNumberMethods ) );   // ensure new fields are 0
373
        table->tp_as_number = number_table;
374
        number_table->nb_add = number_add_handler;
375
        number_table->nb_subtract = number_subtract_handler;
376
        number_table->nb_multiply = number_multiply_handler;
377
        number_table->nb_divide = number_divide_handler;
378
        number_table->nb_remainder = number_remainder_handler;
379
        number_table->nb_divmod = number_divmod_handler;
380
        number_table->nb_power = number_power_handler;
381
        number_table->nb_negative = number_negative_handler;
382
        number_table->nb_positive = number_positive_handler;
383
        number_table->nb_absolute = number_absolute_handler;
384
        number_table->nb_nonzero = number_nonzero_handler;
385
        number_table->nb_invert = number_invert_handler;
386
        number_table->nb_lshift = number_lshift_handler;
387
        number_table->nb_rshift = number_rshift_handler;
388
        number_table->nb_and = number_and_handler;
389
        number_table->nb_xor = number_xor_handler;
390
        number_table->nb_or = number_or_handler;
391
        number_table->nb_coerce = 0;
392
        number_table->nb_int = number_int_handler;
393
        number_table->nb_long = number_long_handler;
394
        number_table->nb_float = number_float_handler;
395
        number_table->nb_oct = number_oct_handler;
396
        number_table->nb_hex = number_hex_handler;
397
    }
398
    return *this;
399
}
400

401
PythonType &PythonType::supportBufferType()
402
{
403
    if( !buffer_table )
404
    {
405
        buffer_table = new PyBufferProcs;
406
        memset( buffer_table, 0, sizeof( PyBufferProcs ) );   // ensure new fields are 0
407
        table->tp_as_buffer = buffer_table;
408
        buffer_table->bf_getreadbuffer = buffer_getreadbuffer_handler;
409
        buffer_table->bf_getwritebuffer = buffer_getwritebuffer_handler;
410
        buffer_table->bf_getsegcount = buffer_getsegcount_handler;
411
    }
412
    return *this;
413
}
414

415
// if you define one sequence method you must define
416
// all of them except the assigns
417

418
PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
419
: table( new PyTypeObject )
420
, sequence_table( NULL )
421
, mapping_table( NULL )
422
, number_table( NULL )
423
, buffer_table( NULL )
424
{
425
    // PyTypeObject is defined in <python-sources>/Include/object.h
426

427
    memset( table, 0, sizeof( PyTypeObject ) );   // ensure new fields are 0
428
    *reinterpret_cast<PyObject *>( table ) = py_object_initializer;
429
    table->ob_type = _Type_Type();
430
    table->ob_size = 0;
431

432
    table->tp_name = const_cast<char *>( default_name );
433
    table->tp_basicsize = basic_size;
434
    table->tp_itemsize = itemsize;
435

436
    // Methods to implement standard operations
437
    table->tp_dealloc = (destructor)standard_dealloc;
438
    table->tp_print = 0;
439
    table->tp_getattr = 0;
440
    table->tp_setattr = 0;
441
    table->tp_compare = 0;
442
    table->tp_repr = 0;
443

444
    // Method suites for standard classes
445
    table->tp_as_number = 0;
446
    table->tp_as_sequence = 0;
447
    table->tp_as_mapping =  0;
448

449
    // More standard operations (here for binary compatibility)
450
    table->tp_hash = 0;
451
    table->tp_call = 0;
452
    table->tp_str = 0;
453
    table->tp_getattro = 0;
454
    table->tp_setattro = 0;
455

456
    // Functions to access object as input/output buffer
457
    table->tp_as_buffer = 0;
458

459
    // Flags to define presence of optional/expanded features
460
    table->tp_flags = Py_TPFLAGS_DEFAULT;
461

462
    // Documentation string
463
    table->tp_doc = 0;
464

465
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 0)
466
    table->tp_traverse = 0L;
467

468
    // delete references to contained objects
469
    table->tp_clear = 0L;
470
#else
471
    table->tp_xxx5 = 0L;
472
    table->tp_xxx6 = 0L;
473
#endif
474
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
475
    // first defined in 2.1
476
    table->tp_richcompare = 0L;
477
    // weak reference enabler
478
    table->tp_weaklistoffset = 0L;
479
#else
480
    table->tp_xxx7 = 0L;
481
    table->tp_xxx8 = 0L;
482
#endif
483
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 2)
484
    // first defined in 2.3
485
    // Iterators
486
    table->tp_iter = 0L;
487
    table->tp_iternext = 0L;
488
#endif
489
#ifdef COUNT_ALLOCS
490
    table->tp_alloc = 0;
491
    table->tp_free = 0;
492
    table->tp_maxalloc = 0;
493
    table->tp_next = 0;
494
#endif
495
}
496

497
PythonType::~PythonType()
498
{
499
    delete table;
500
    delete sequence_table;
501
    delete mapping_table;
502
    delete number_table;
503
    delete buffer_table;
504
}
505

506
PyTypeObject *PythonType::type_object() const
507
{
508
    return table;
509
}
510

511
PythonType &PythonType::name( const char *nam )
512
{
513
    table->tp_name = const_cast<char *>( nam );
514
    return *this;
515
}
516

517
const char *PythonType::getName() const
518
{
519
    return table->tp_name;
520
}
521

522
PythonType &PythonType::doc( const char *d )
523
{
524
    table->tp_doc = const_cast<char *>( d );
525
    return *this;
526
}
527

528
const char *PythonType::getDoc() const
529
{
530
    return table->tp_doc;
531
}
532

533
PythonType &PythonType::set_tp_dealloc( void (*tp_dealloc)( PyObject *self ) )
534
{
535
    table->tp_dealloc = tp_dealloc;
536
    return *this;
537
}
538

539
PythonType &PythonType::set_tp_init( int (*tp_init)( PyObject *self, PyObject *args, PyObject *kwds ) )
540
{
541
    table->tp_init = tp_init;
542
    return *this;
543
}
544

545
PythonType &PythonType::set_tp_new( PyObject *(*tp_new)( PyTypeObject *subtype, PyObject *args, PyObject *kwds ) )
546
{
547
    table->tp_new = tp_new;
548
    return *this;
549
}
550

551
PythonType &PythonType::set_methods( PyMethodDef *methods )
552
{
553
    table->tp_methods = methods;
554
    return *this;
555
}
556

557
PythonType &PythonType::supportClass()
558
{
559
    table->tp_flags |= Py_TPFLAGS_BASETYPE;
560
    return *this;
561
}
562

563
PythonType &PythonType::dealloc( void( *f )( PyObject * ))
564
{
565
    table->tp_dealloc = f;
566
    return *this;
567
}
568

569
#if defined( PYCXX_PYTHON_2TO3 )
570
PythonType &PythonType::supportPrint()
571
{
572
    table->tp_print = print_handler;
573
    return *this;
574
}
575
#endif
576

577
PythonType &PythonType::supportGetattr()
578
{
579
    table->tp_getattr = getattr_handler;
580
    return *this;
581
}
582

583
PythonType &PythonType::supportSetattr()
584
{
585
    table->tp_setattr = setattr_handler;
586
    return *this;
587
}
588

589
PythonType &PythonType::supportGetattro()
590
{
591
    table->tp_getattro = getattro_handler;
592
    return *this;
593
}
594

595
PythonType &PythonType::supportSetattro()
596
{
597
    table->tp_setattro = setattro_handler;
598
    return *this;
599
}
600

601
#if defined( PYCXX_PYTHON_2TO3 )
602
PythonType &PythonType::supportCompare()
603
{
604
    table->tp_compare = compare_handler;
605
    return *this;
606
}
607
#endif
608

609
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
610
PythonType &PythonType::supportRichCompare()
611
{
612
    table->tp_richcompare = rich_compare_handler;
613
    return *this;
614
}
615
#endif
616

617
PythonType &PythonType::supportRepr()
618
{
619
    table->tp_repr = repr_handler;
620
    return *this;
621
}
622

623
PythonType &PythonType::supportStr()
624
{
625
    table->tp_str = str_handler;
626
    return *this;
627
}
628

629
PythonType &PythonType::supportHash()
630
{
631
    table->tp_hash = hash_handler;
632
    return *this;
633
}
634

635
PythonType &PythonType::supportCall()
636
{
637
    table->tp_call = call_handler;
638
    return *this;
639
}
640

641
PythonType &PythonType::supportIter()
642
{
643
    table->tp_iter = iter_handler;
644
    table->tp_iternext = iternext_handler;
645
    return *this;
646
}
647

648
//--------------------------------------------------------------------------------
649
//
650
//    Handlers
651
//
652
//--------------------------------------------------------------------------------
653
PYCXX_EXPORT PythonExtensionBase *getPythonExtensionBase( PyObject *self )
654
{
655
    if( self->ob_type->tp_flags&Py_TPFLAGS_BASETYPE )
656
    {
657
        PythonClassInstance *instance = reinterpret_cast<PythonClassInstance *>( self );
658
        return instance->m_pycxx_object;
659
    }
660
    else
661
    {
662
        return static_cast<PythonExtensionBase *>( self );
663
    }
664
}
665

666

667
#if defined( PYCXX_PYTHON_2TO3 )
668
extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
669
{
670
    try
671
    {
672
        PythonExtensionBase *p = getPythonExtensionBase( self );
673
        return p->print( fp, flags );
674
    }
675
    catch( Py::Exception &)
676
    {
677
        return -1;    // indicate error
678
    }
679
}
680
#endif
681

682
extern "C" PyObject *getattr_handler( PyObject *self, char *name )
683
{
684
    try
685
    {
686
        PythonExtensionBase *p = getPythonExtensionBase( self );
687
        return new_reference_to( p->getattr( name ) );
688
    }
689
    catch( Py::Exception &)
690
    {
691
        return NULL;    // indicate error
692
    }
693
}
694

695
extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
696
{
697
    try
698
    {
699
        PythonExtensionBase *p = getPythonExtensionBase( self );
700
        return p->setattr( name, Py::Object( value ) );
701
    }
702
    catch( Py::Exception &)
703
    {
704
        return -1;    // indicate error
705
    }
706
}
707

708
extern "C" PyObject *getattro_handler( PyObject *self, PyObject *name )
709
{
710
    try
711
    {
712
        PythonExtensionBase *p = getPythonExtensionBase( self );
713
        return new_reference_to( p->getattro( Py::String( name ) ) );
714
    }
715
    catch( Py::Exception &)
716
    {
717
        return NULL;    // indicate error
718
    }
719
}
720

721
extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
722
{
723
    try
724
    {
725
        PythonExtensionBase *p = getPythonExtensionBase( self );
726
        return p->setattro( Py::String( name ), Py::Object( value ) );
727
    }
728
    catch( Py::Exception &)
729
    {
730
        return -1;    // indicate error
731
    }
732
}
733

734
#if defined( PYCXX_PYTHON_2TO3 )
735
extern "C" int compare_handler( PyObject *self, PyObject *other )
736
{
737
    try
738
    {
739
        PythonExtensionBase *p = getPythonExtensionBase( self );
740
        return p->compare( Py::Object( other ) );
741
    }
742
    catch( Py::Exception &)
743
    {
744
        return -1;    // indicate error
745
    }
746
}
747
#endif
748

749
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
750
extern "C" PyObject *rich_compare_handler( PyObject *self, PyObject *other, int op )
751
{
752
    try
753
    {
754
        PythonExtensionBase *p = getPythonExtensionBase( self );
755
        return new_reference_to( p->rich_compare( Py::Object( other ), op ) );
756
    }
757
    catch( Py::Exception &)
758
    {
759
        return NULL;    // indicate error
760
    }
761
}
762
#endif
763

764
extern "C" PyObject *repr_handler( PyObject *self )
765
{
766
    try
767
    {
768
        PythonExtensionBase *p = getPythonExtensionBase( self );
769
        return new_reference_to( p->repr() );
770
    }
771
    catch( Py::Exception &)
772
    {
773
        return NULL;    // indicate error
774
    }
775
}
776

777
extern "C" PyObject *str_handler( PyObject *self )
778
{
779
    try
780
    {
781
        PythonExtensionBase *p = getPythonExtensionBase( self );
782
        return new_reference_to( p->str() );
783
    }
784
    catch( Py::Exception &)
785
    {
786
        return NULL;    // indicate error
787
    }
788
}
789

790
extern "C" long hash_handler( PyObject *self )
791
{
792
    try
793
    {
794
        PythonExtensionBase *p = getPythonExtensionBase( self );
795
        return p->hash();
796
    }
797
    catch( Py::Exception &)
798
    {
799
        return -1;    // indicate error
800
    }
801
}
802

803
extern "C" PyObject *call_handler( PyObject *self, PyObject *args, PyObject *kw )
804
{
805
    try
806
    {
807
        PythonExtensionBase *p = getPythonExtensionBase( self );
808
        if( kw != NULL )
809
            return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
810
        else
811
            return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
812
    }
813
    catch( Py::Exception &)
814
    {
815
        return NULL;    // indicate error
816
    }
817
}
818

819
extern "C" PyObject *iter_handler( PyObject *self )
820
{
821
    try
822
    {
823
        PythonExtensionBase *p = getPythonExtensionBase( self );
824
        return new_reference_to( p->iter() );
825
    }
826
    catch( Py::Exception &)
827
    {
828
        return NULL;    // indicate error
829
    }
830
}
831

832
extern "C" PyObject *iternext_handler( PyObject *self )
833
{
834
    try
835
    {
836
        PythonExtensionBase *p = getPythonExtensionBase( self );
837
        return p->iternext();  // might be a NULL ptr on end of iteration
838
    }
839
    catch( Py::Exception &)
840
    {
841
        return NULL;    // indicate error
842
    }
843
}
844

845
// Sequence methods
846
extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
847
{
848
    try
849
    {
850
        PythonExtensionBase *p = getPythonExtensionBase( self );
851
        return p->sequence_length();
852
    }
853
    catch( Py::Exception &)
854
    {
855
        return -1;    // indicate error
856
    }
857
}
858

859
extern "C" PyObject *sequence_concat_handler( PyObject *self, PyObject *other )
860
{
861
    try
862
    {
863
        PythonExtensionBase *p = getPythonExtensionBase( self );
864
        return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
865
    }
866
    catch( Py::Exception &)
867
    {
868
        return NULL;    // indicate error
869
    }
870
}
871

872
extern "C" PyObject *sequence_repeat_handler( PyObject *self, Py_ssize_t count )
873
{
874
    try
875
    {
876
        PythonExtensionBase *p = getPythonExtensionBase( self );
877
        return new_reference_to( p->sequence_repeat( count ) );
878
    }
879
    catch( Py::Exception &)
880
    {
881
        return NULL;    // indicate error
882
    }
883
}
884

885
extern "C" PyObject *sequence_item_handler( PyObject *self, Py_ssize_t index )
886
{
887
    try
888
    {
889
        PythonExtensionBase *p = getPythonExtensionBase( self );
890
        return new_reference_to( p->sequence_item( index ) );
891
    }
892
    catch( Py::Exception &)
893
    {
894
        return NULL;    // indicate error
895
    }
896
}
897

898
extern "C" PyObject *sequence_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last )
899
{
900
    try
901
    {
902
        PythonExtensionBase *p = getPythonExtensionBase( self );
903
        return new_reference_to( p->sequence_slice( first, last ) );
904
    }
905
    catch( Py::Exception &)
906
    {
907
        return NULL;    // indicate error
908
    }
909
}
910

911
extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
912
{
913
    try
914
    {
915
        PythonExtensionBase *p = getPythonExtensionBase( self );
916
        return p->sequence_ass_item( index, Py::Object( value ) );
917
    }
918
    catch( Py::Exception &)
919
    {
920
        return -1;    // indicate error
921
    }
922
}
923

924
extern "C" int sequence_ass_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *value )
925
{
926
    try
927
    {
928
        PythonExtensionBase *p = getPythonExtensionBase( self );
929
        return p->sequence_ass_slice( first, last, Py::Object( value ) );
930
    }
931
    catch( Py::Exception &)
932
    {
933
        return -1;    // indicate error
934
    }
935
}
936

937
// Mapping
938
extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
939
{
940
    try
941
    {
942
        PythonExtensionBase *p = getPythonExtensionBase( self );
943
        return p->mapping_length();
944
    }
945
    catch( Py::Exception &)
946
    {
947
        return -1;    // indicate error
948
    }
949
}
950

951
extern "C" PyObject *mapping_subscript_handler( PyObject *self, PyObject *key )
952
{
953
    try
954
    {
955
        PythonExtensionBase *p = getPythonExtensionBase( self );
956
        return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
957
    }
958
    catch( Py::Exception &)
959
    {
960
        return NULL;    // indicate error
961
    }
962
}
963

964
extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
965
{
966
    try
967
    {
968
        PythonExtensionBase *p = getPythonExtensionBase( self );
969
        return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
970
    }
971
    catch( Py::Exception &)
972
    {
973
        return -1;    // indicate error
974
    }
975
}
976

977
// Number
978
extern "C" int number_nonzero_handler( PyObject *self )
979
{
980
    try
981
    {
982
        PythonExtensionBase *p = getPythonExtensionBase( self );
983
        return p->number_nonzero();
984
    }
985
    catch( Py::Exception &)
986
    {
987
        return -1;    // indicate error
988
    }
989
}
990

991
extern "C" PyObject *number_negative_handler( PyObject *self )
992
{
993
    try
994
    {
995
        PythonExtensionBase *p = getPythonExtensionBase( self );
996
        return new_reference_to( p->number_negative() );
997
    }
998
    catch( Py::Exception &)
999
    {
1000
        return NULL;    // indicate error
1001
    }
1002
}
1003

1004
extern "C" PyObject *number_positive_handler( PyObject *self )
1005
{
1006
    try
1007
    {
1008
        PythonExtensionBase *p = getPythonExtensionBase( self );
1009
        return new_reference_to( p->number_positive() );
1010
    }
1011
    catch( Py::Exception &)
1012
    {
1013
        return NULL;    // indicate error
1014
    }
1015
}
1016

1017
extern "C" PyObject *number_absolute_handler( PyObject *self )
1018
{
1019
    try
1020
    {
1021
        PythonExtensionBase *p = getPythonExtensionBase( self );
1022
        return new_reference_to( p->number_absolute() );
1023
    }
1024
    catch( Py::Exception &)
1025
    {
1026
        return NULL;    // indicate error
1027
    }
1028
}
1029

1030
extern "C" PyObject *number_invert_handler( PyObject *self )
1031
{
1032
    try
1033
    {
1034
        PythonExtensionBase *p = getPythonExtensionBase( self );
1035
        return new_reference_to( p->number_invert() );
1036
    }
1037
    catch( Py::Exception &)
1038
    {
1039
        return NULL;    // indicate error
1040
    }
1041
}
1042

1043
extern "C" PyObject *number_int_handler( PyObject *self )
1044
{
1045
    try
1046
    {
1047
        PythonExtensionBase *p = getPythonExtensionBase( self );
1048
        return new_reference_to( p->number_int() );
1049
    }
1050
    catch( Py::Exception &)
1051
    {
1052
        return NULL;    // indicate error
1053
    }
1054
}
1055

1056
extern "C" PyObject *number_float_handler( PyObject *self )
1057
{
1058
    try
1059
    {
1060
        PythonExtensionBase *p = getPythonExtensionBase( self );
1061
        return new_reference_to( p->number_float() );
1062
    }
1063
    catch( Py::Exception &)
1064
    {
1065
        return NULL;    // indicate error
1066
    }
1067
}
1068

1069
extern "C" PyObject *number_long_handler( PyObject *self )
1070
{
1071
    try
1072
    {
1073
        PythonExtensionBase *p = getPythonExtensionBase( self );
1074
        return new_reference_to( p->number_long() );
1075
    }
1076
    catch( Py::Exception &)
1077
    {
1078
        return NULL;    // indicate error
1079
    }
1080
}
1081

1082
extern "C" PyObject *number_oct_handler( PyObject *self )
1083
{
1084
    try
1085
    {
1086
        PythonExtensionBase *p = getPythonExtensionBase( self );
1087
        return new_reference_to( p->number_oct() );
1088
    }
1089
    catch( Py::Exception &)
1090
    {
1091
        return NULL;    // indicate error
1092
    }
1093
}
1094

1095
extern "C" PyObject *number_hex_handler( PyObject *self )
1096
{
1097
    try
1098
    {
1099
        PythonExtensionBase *p = getPythonExtensionBase( self );
1100
        return new_reference_to( p->number_hex() );
1101
    }
1102
    catch( Py::Exception &)
1103
    {
1104
        return NULL;    // indicate error
1105
    }
1106
}
1107

1108
extern "C" PyObject *number_add_handler( PyObject *self, PyObject *other )
1109
{
1110
    try
1111
    {
1112
        PythonExtensionBase *p = getPythonExtensionBase( self );
1113
        return new_reference_to( p->number_add( Py::Object( other ) ) );
1114
    }
1115
    catch( Py::Exception &)
1116
    {
1117
        return NULL;    // indicate error
1118
    }
1119
}
1120

1121
extern "C" PyObject *number_subtract_handler( PyObject *self, PyObject *other )
1122
{
1123
    try
1124
    {
1125
        PythonExtensionBase *p = getPythonExtensionBase( self );
1126
        return new_reference_to( p->number_subtract( Py::Object( other ) ) );
1127
    }
1128
    catch( Py::Exception &)
1129
    {
1130
        return NULL;    // indicate error
1131
    }
1132
}
1133

1134
extern "C" PyObject *number_multiply_handler( PyObject *self, PyObject *other )
1135
{
1136
    try
1137
    {
1138
        PythonExtensionBase *p = getPythonExtensionBase( self );
1139
        return new_reference_to( p->number_multiply( Py::Object( other ) ) );
1140
    }
1141
    catch( Py::Exception &)
1142
    {
1143
        return NULL;    // indicate error
1144
    }
1145
}
1146

1147
extern "C" PyObject *number_divide_handler( PyObject *self, PyObject *other )
1148
{
1149
    try
1150
    {
1151
        PythonExtensionBase *p = getPythonExtensionBase( self );
1152
        return new_reference_to( p->number_divide( Py::Object( other ) ) );
1153
    }
1154
    catch( Py::Exception &)
1155
    {
1156
        return NULL;    // indicate error
1157
    }
1158
}
1159

1160
extern "C" PyObject *number_remainder_handler( PyObject *self, PyObject *other )
1161
{
1162
    try
1163
    {
1164
        PythonExtensionBase *p = getPythonExtensionBase( self );
1165
        return new_reference_to( p->number_remainder( Py::Object( other ) ) );
1166
    }
1167
    catch( Py::Exception &)
1168
    {
1169
        return NULL;    // indicate error
1170
    }
1171
}
1172

1173
extern "C" PyObject *number_divmod_handler( PyObject *self, PyObject *other )
1174
{
1175
    try
1176
    {
1177
        PythonExtensionBase *p = getPythonExtensionBase( self );
1178
        return new_reference_to( p->number_divmod( Py::Object( other ) ) );
1179
    }
1180
    catch( Py::Exception &)
1181
    {
1182
        return NULL;    // indicate error
1183
    }
1184
}
1185

1186
extern "C" PyObject *number_lshift_handler( PyObject *self, PyObject *other )
1187
{
1188
    try
1189
    {
1190
        PythonExtensionBase *p = getPythonExtensionBase( self );
1191
        return new_reference_to( p->number_lshift( Py::Object( other ) ) );
1192
    }
1193
    catch( Py::Exception &)
1194
    {
1195
        return NULL;    // indicate error
1196
    }
1197
}
1198

1199
extern "C" PyObject *number_rshift_handler( PyObject *self, PyObject *other )
1200
{
1201
    try
1202
    {
1203
        PythonExtensionBase *p = getPythonExtensionBase( self );
1204
        return new_reference_to( p->number_rshift( Py::Object( other ) ) );
1205
    }
1206
    catch( Py::Exception &)
1207
    {
1208
        return NULL;    // indicate error
1209
    }
1210
}
1211

1212
extern "C" PyObject *number_and_handler( PyObject *self, PyObject *other )
1213
{
1214
    try
1215
    {
1216
        PythonExtensionBase *p = getPythonExtensionBase( self );
1217
        return new_reference_to( p->number_and( Py::Object( other ) ) );
1218
    }
1219
    catch( Py::Exception &)
1220
    {
1221
        return NULL;    // indicate error
1222
    }
1223
}
1224

1225
extern "C" PyObject *number_xor_handler( PyObject *self, PyObject *other )
1226
{
1227
    try
1228
    {
1229
        PythonExtensionBase *p = getPythonExtensionBase( self );
1230
        return new_reference_to( p->number_xor( Py::Object( other ) ) );
1231
    }
1232
    catch( Py::Exception &)
1233
    {
1234
        return NULL;    // indicate error
1235
    }
1236
}
1237

1238
extern "C" PyObject *number_or_handler( PyObject *self, PyObject *other )
1239
{
1240
    try
1241
    {
1242
        PythonExtensionBase *p = getPythonExtensionBase( self );
1243
        return new_reference_to( p->number_or( Py::Object( other ) ) );
1244
    }
1245
    catch( Py::Exception &)
1246
    {
1247
        return NULL;    // indicate error
1248
    }
1249
}
1250

1251
extern "C" PyObject *number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
1252
{
1253
    try
1254
    {
1255
        PythonExtensionBase *p = getPythonExtensionBase( self );
1256
        return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
1257
    }
1258
    catch( Py::Exception &)
1259
    {
1260
        return NULL;    // indicate error
1261
    }
1262
}
1263

1264
// Buffer
1265
extern "C" Py_ssize_t buffer_getreadbuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1266
{
1267
    try
1268
    {
1269
        PythonExtensionBase *p = getPythonExtensionBase( self );
1270
        return p->buffer_getreadbuffer( index, pp );
1271
    }
1272
    catch( Py::Exception &)
1273
    {
1274
        return -1;    // indicate error
1275
    }
1276
}
1277

1278
extern "C" Py_ssize_t buffer_getwritebuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1279
{
1280
    try
1281
    {
1282
        PythonExtensionBase *p = getPythonExtensionBase( self );
1283
        return p->buffer_getwritebuffer( index, pp );
1284
    }
1285
    catch( Py::Exception &)
1286
    {
1287
        return -1;    // indicate error
1288
    }
1289
}
1290

1291
extern "C" Py_ssize_t buffer_getsegcount_handler( PyObject *self, Py_ssize_t *count )
1292
{
1293
    try
1294
    {
1295
        PythonExtensionBase *p = getPythonExtensionBase( self );
1296
        return p->buffer_getsegcount( count );
1297
    }
1298
    catch( Py::Exception &)
1299
    {
1300
        return -1;    // indicate error
1301
    }
1302
}
1303

1304
//================================================================================
1305
//
1306
//    Implementation of PythonExtensionBase
1307
//
1308
//================================================================================
1309
#define missing_method( method ) \
1310
    throw RuntimeError( "Extension object missing implement of " #method );
1311

1312
PythonExtensionBase::PythonExtensionBase()
1313
{
1314
    ob_refcnt = 0;
1315
}
1316

1317
PythonExtensionBase::~PythonExtensionBase()
1318
{
1319
    assert( ob_refcnt == 0 );
1320
}
1321

1322
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name )
1323
{
1324
    Py::TupleN args;
1325
    return  self().callMemberFunction( fn_name, args );
1326
}
1327

1328
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1329
                                            const Py::Object &arg1 )
1330
{
1331
    Py::TupleN args( arg1 );
1332
    return  self().callMemberFunction( fn_name, args );
1333
}
1334

1335
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1336
                                            const Py::Object &arg1, const Py::Object &arg2 )
1337
{
1338
    Py::TupleN args( arg1, arg2 );
1339
    return self().callMemberFunction( fn_name, args );
1340
}
1341

1342
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1343
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3 )
1344
{
1345
    Py::TupleN args( arg1, arg2, arg3 );
1346
    return self().callMemberFunction( fn_name, args );
1347
}
1348

1349
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1350
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1351
                                            const Py::Object &arg4 )
1352
{
1353
    Py::TupleN args( arg1, arg2, arg3, arg4 );
1354
    return self().callMemberFunction( fn_name, args );
1355
}
1356

1357
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1358
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1359
                                            const Py::Object &arg4, const Py::Object &arg5 )
1360
{
1361
    Py::TupleN args( arg1, arg2, arg3, arg4, arg5 );
1362
    return self().callMemberFunction( fn_name, args );
1363
}
1364

1365
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1366
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1367
                                            const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6 )
1368
{
1369
    Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6 );
1370
    return self().callMemberFunction( fn_name, args );
1371
}
1372

1373
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1374
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1375
                                            const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1376
                                            const Py::Object &arg7 )
1377
{
1378
    Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7 );
1379
    return self().callMemberFunction( fn_name, args );
1380
}
1381

1382
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1383
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1384
                                            const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1385
                                            const Py::Object &arg7, const Py::Object &arg8 )
1386
{
1387
    Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8 );
1388
    return self().callMemberFunction( fn_name, args );
1389
}
1390

1391
Py::Object PythonExtensionBase::callOnSelf( const std::string &fn_name,
1392
                                            const Py::Object &arg1, const Py::Object &arg2, const Py::Object &arg3,
1393
                                            const Py::Object &arg4, const Py::Object &arg5, const Py::Object &arg6,
1394
                                            const Py::Object &arg7, const Py::Object &arg8, const Py::Object &arg9 )
1395
{
1396
    Py::TupleN args( arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9 );
1397
    return self().callMemberFunction( fn_name, args );
1398
}
1399

1400
void PythonExtensionBase::reinit( Tuple & /*args*/, Dict & /*kwds*/ )
1401
{
1402
    throw RuntimeError( "Must not call __init__ twice on this class" );
1403
}
1404

1405
Py::Object PythonExtensionBase::genericGetAttro( const Py::String &name )
1406
{
1407
    return asObject( PyObject_GenericGetAttr( selfPtr(), name.ptr() ) );
1408
}
1409

1410
int PythonExtensionBase::genericSetAttro( const Py::String &name, const Py::Object &value )
1411
{
1412
    return PyObject_GenericSetAttr( selfPtr(), name.ptr(), value.ptr() );
1413
}
1414

1415
int PythonExtensionBase::print( FILE *, int )
1416
{
1417
    missing_method( print );
1418
    return -1;
1419
}
1420

1421
Py::Object PythonExtensionBase::getattr( const char * )
1422
{
1423
    missing_method( getattr );
1424
    return Py::None();
1425
}
1426

1427
int PythonExtensionBase::setattr( const char *, const Py::Object &)
1428
{
1429
    missing_method( setattr );
1430
    return -1;
1431
}
1432

1433
Py::Object PythonExtensionBase::getattro( const Py::String &name )
1434
{
1435
    return genericGetAttro( name );
1436
}
1437

1438
int PythonExtensionBase::setattro( const Py::String &name, const Py::Object &value )
1439
{
1440
    return genericSetAttro( name, value );
1441
}
1442

1443
int PythonExtensionBase::compare( const Py::Object &)
1444
{
1445
    missing_method( compare );
1446
    return -1;
1447
}
1448

1449
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
1450
Py::Object PythonExtensionBase::rich_compare( const Py::Object &, int /*op*/ )
1451
{
1452
    missing_method( rich_compare );
1453
    return Py::None();
1454
}
1455

1456
#endif
1457
Py::Object PythonExtensionBase::repr()
1458
{
1459
    missing_method( repr );
1460
    return Py::None();
1461
}
1462

1463
Py::Object PythonExtensionBase::str()
1464
{
1465
    missing_method( str );
1466
    return Py::None();
1467
}
1468

1469
long PythonExtensionBase::hash()
1470
{
1471
    missing_method( hash );
1472
    return -1;
1473
}
1474

1475
Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object &)
1476
{
1477
    missing_method( call );
1478
    return Py::None();
1479
}
1480

1481
Py::Object PythonExtensionBase::iter()
1482
{
1483
    missing_method( iter );
1484
    return Py::None();
1485
}
1486

1487
PyObject *PythonExtensionBase::iternext()
1488
{
1489
    missing_method( iternext );
1490
    return NULL;
1491
}
1492

1493
// Sequence methods
1494
int PythonExtensionBase::sequence_length()
1495
{
1496
    missing_method( sequence_length );
1497
    return -1;
1498
}
1499

1500
Py::Object PythonExtensionBase::sequence_concat( const Py::Object &)
1501
{
1502
    missing_method( sequence_concat );
1503
    return Py::None();
1504
}
1505

1506
Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t )
1507
{
1508
    missing_method( sequence_repeat );
1509
    return Py::None();
1510
}
1511

1512
Py::Object PythonExtensionBase::sequence_item( Py_ssize_t )
1513
{
1514
    missing_method( sequence_item );
1515
    return Py::None();
1516
}
1517

1518
Py::Object PythonExtensionBase::sequence_slice( Py_ssize_t, Py_ssize_t )
1519
{
1520
    missing_method( sequence_slice );
1521
    return Py::None();
1522
}
1523

1524
int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object &)
1525
{
1526
    missing_method( sequence_ass_item );
1527
    return -1;
1528
}
1529

1530
int PythonExtensionBase::sequence_ass_slice( Py_ssize_t, Py_ssize_t, const Py::Object &)
1531
{
1532
    missing_method( sequence_ass_slice );
1533
    return -1;
1534
}
1535

1536
// Mapping
1537
int PythonExtensionBase::mapping_length()
1538
{
1539
    missing_method( mapping_length );
1540
    return -1;
1541
}
1542

1543
Py::Object PythonExtensionBase::mapping_subscript( const Py::Object &)
1544
{
1545
    missing_method( mapping_subscript );
1546
    return Py::None();
1547
}
1548

1549
int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object &)
1550
{
1551
    missing_method( mapping_ass_subscript );
1552
    return -1;
1553
}
1554

1555
// Number
1556
int PythonExtensionBase::number_nonzero()
1557
{
1558
    missing_method( number_nonzero );
1559
    return -1;
1560
}
1561

1562
Py::Object PythonExtensionBase::number_negative()
1563
{
1564
    missing_method( number_negative );
1565
    return Py::None();
1566
}
1567

1568
Py::Object PythonExtensionBase::number_positive()
1569
{
1570
    missing_method( number_positive );
1571
    return Py::None();
1572
}
1573

1574
Py::Object PythonExtensionBase::number_absolute()
1575
{
1576
    missing_method( number_absolute );
1577
    return Py::None();
1578
}
1579

1580
Py::Object PythonExtensionBase::number_invert()
1581
{
1582
    missing_method( number_invert );
1583
    return Py::None();
1584
}
1585

1586
Py::Object PythonExtensionBase::number_int()
1587
{
1588
    missing_method( number_int );
1589
    return Py::None();
1590
}
1591

1592
Py::Object PythonExtensionBase::number_float()
1593
{
1594
    missing_method( number_float );
1595
    return Py::None();
1596
}
1597

1598
Py::Object PythonExtensionBase::number_long()
1599
{
1600
    missing_method( number_long );
1601
    return Py::None();
1602
}
1603

1604
Py::Object PythonExtensionBase::number_oct()
1605
{
1606
    missing_method( number_oct );
1607
    return Py::None();
1608
}
1609

1610
Py::Object PythonExtensionBase::number_hex()
1611
{
1612
    missing_method( number_hex );
1613
    return Py::None();
1614
}
1615

1616
Py::Object PythonExtensionBase::number_add( const Py::Object &)
1617
{
1618
    missing_method( number_add );
1619
    return Py::None();
1620
}
1621

1622
Py::Object PythonExtensionBase::number_subtract( const Py::Object &)
1623
{
1624
    missing_method( number_subtract );
1625
    return Py::None();
1626
}
1627

1628
Py::Object PythonExtensionBase::number_multiply( const Py::Object &)
1629
{
1630
    missing_method( number_multiply );
1631
    return Py::None();
1632
}
1633

1634
Py::Object PythonExtensionBase::number_divide( const Py::Object &)
1635
{
1636
    missing_method( number_divide );
1637
    return Py::None();
1638
}
1639

1640
Py::Object PythonExtensionBase::number_remainder( const Py::Object &)
1641
{
1642
    missing_method( number_remainder );
1643
    return Py::None();
1644
}
1645

1646
Py::Object PythonExtensionBase::number_divmod( const Py::Object &)
1647
{
1648
    missing_method( number_divmod );
1649
    return Py::None();
1650
}
1651

1652
Py::Object PythonExtensionBase::number_lshift( const Py::Object &)
1653
{
1654
    missing_method( number_lshift );
1655
    return Py::None();
1656
}
1657

1658
Py::Object PythonExtensionBase::number_rshift( const Py::Object &)
1659
{
1660
    missing_method( number_rshift );
1661
    return Py::None();
1662
}
1663

1664
Py::Object PythonExtensionBase::number_and( const Py::Object &)
1665
{
1666
    missing_method( number_and );
1667
    return Py::None();
1668
}
1669

1670
Py::Object PythonExtensionBase::number_xor( const Py::Object &)
1671
{
1672
    missing_method( number_xor );
1673
    return Py::None();
1674
}
1675

1676
Py::Object PythonExtensionBase::number_or( const Py::Object &)
1677
{
1678
    missing_method( number_or );
1679
    return Py::None();
1680
}
1681

1682
Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object &)
1683
{
1684
    missing_method( number_power );
1685
    return Py::None();
1686
}
1687

1688
// Buffer
1689
Py_ssize_t PythonExtensionBase::buffer_getreadbuffer( Py_ssize_t, void** )
1690
{
1691
    missing_method( buffer_getreadbuffer );
1692
    return -1;
1693
}
1694

1695
Py_ssize_t PythonExtensionBase::buffer_getwritebuffer( Py_ssize_t, void** )
1696
{
1697
    missing_method( buffer_getwritebuffer );
1698
    return -1;
1699
}
1700

1701
Py_ssize_t PythonExtensionBase::buffer_getsegcount( Py_ssize_t* )
1702
{
1703
    missing_method( buffer_getsegcount );
1704
    return -1;
1705
}
1706

1707
//--------------------------------------------------------------------------------
1708
//
1709
//    Method call handlers for
1710
//        PythonExtensionBase
1711
//        ExtensionModuleBase
1712
//
1713
//--------------------------------------------------------------------------------
1714
extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
1715
{
1716
    try
1717
    {
1718
        Tuple self_and_name_tuple( _self_and_name_tuple );
1719

1720
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1721
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1722
        if( self_as_void == NULL )
1723
            return NULL;
1724

1725
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1726

1727
        Tuple args( _args );
1728

1729
        if( _keywords == NULL )
1730
        {
1731
            Dict keywords;    // pass an empty dict
1732

1733
            Object result
1734
                    (
1735
                    self->invoke_method_keyword
1736
                        (
1737
                        PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1738
                        args,
1739
                        keywords
1740
                        )
1741
                    );
1742

1743
            return new_reference_to( result.ptr() );
1744
        }
1745
        else
1746
        {
1747
            Dict keywords( _keywords ); // make dict
1748

1749
            Object result
1750
                    (
1751
                    self->invoke_method_keyword
1752
                        (
1753
                        PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1754
                        args,
1755
                        keywords
1756
                        )
1757
                    );
1758

1759
            return new_reference_to( result.ptr() );
1760
        }
1761
    }
1762
    catch( Exception & )
1763
    {
1764
        return 0;
1765
    }
1766
}
1767

1768
//NOTE: This method is used in ExtensionModule.hxx but not provided by PyCXX.
1769
//However, it's required because without it we get linker errors.
1770
extern "C" PyObject *method_noargs_call_handler( PyObject *_self_and_name_tuple, PyObject * )
1771
{
1772
    try
1773
    {
1774
        Tuple self_and_name_tuple( _self_and_name_tuple );
1775

1776
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1777
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1778
        if( self_as_void == NULL )
1779
            return NULL;
1780

1781
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1782

1783
        Object result
1784
                (
1785
                self->invoke_method_noargs
1786
                    (
1787
                    PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() )
1788
                    )
1789
                );
1790

1791
        return new_reference_to( result.ptr() );
1792
    }
1793
    catch( Exception & )
1794
    {
1795
        return 0;
1796
    }
1797
#if 0
1798
    try
1799
    {
1800
        Tuple self_and_name_tuple( _self_and_name_tuple );
1801

1802
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1803
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1804
        if( self_as_void == NULL )
1805
            return NULL;
1806

1807
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1808

1809
        String py_name( self_and_name_tuple[1] );
1810
        std::string name( py_name.as_std_string( NULL ) );
1811

1812
        Object result( self->invoke_method_noargs( name ) );
1813

1814
        return new_reference_to( result.ptr() );
1815
    }
1816
    catch( Exception & )
1817
    {
1818
        return 0;
1819
    }
1820
#else
1821
    return 0;
1822
#endif
1823
}
1824

1825
extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
1826
{
1827
    try
1828
    {
1829
        Tuple self_and_name_tuple( _self_and_name_tuple );
1830

1831
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1832
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1833
        if( self_as_void == NULL )
1834
            return NULL;
1835

1836
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1837
        Tuple args( _args );
1838

1839
        Object result
1840
                (
1841
                self->invoke_method_varargs
1842
                    (
1843
                    PyCObject_AsVoidPtr( self_and_name_tuple[1].ptr() ),
1844
                    args
1845
                    )
1846
                );
1847

1848
        return new_reference_to( result.ptr() );
1849
    }
1850
    catch( Exception & )
1851
    {
1852
        return 0;
1853
    }
1854
}
1855

1856
extern "C" void do_not_dealloc( void * )
1857
{}
1858

1859
//--------------------------------------------------------------------------------
1860
//
1861
//    ExtensionExceptionType
1862
//
1863
//--------------------------------------------------------------------------------
1864
ExtensionExceptionType::ExtensionExceptionType()
1865
: Py::Object()
1866
{
1867
}
1868

1869
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
1870
{
1871
    std::string module_name( module.fullName() );
1872
    module_name += ".";
1873
    module_name += name;
1874
    set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
1875
}
1876

1877
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent)
1878
{
1879
     std::string module_name( module.fullName() );
1880
     module_name += ".";
1881
     module_name += name;
1882
     set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
1883
}
1884

1885
ExtensionExceptionType::~ExtensionExceptionType()
1886
{
1887
}
1888

1889
Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
1890
{
1891
    PyErr_SetString( exception.ptr(), reason.c_str() );
1892
}
1893

1894
Exception::Exception( ExtensionExceptionType &exception, Object &reason )
1895
{
1896
    PyErr_SetObject( exception.ptr(), reason.ptr() );
1897
}
1898

1899
Exception::Exception( PyObject *exception, Object &reason )
1900
{
1901
    PyErr_SetObject( exception, reason.ptr() );
1902
}
1903

1904
}    // end of namespace Py
1905

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.