~ubuntu-branches/ubuntu/natty/pysvn/natty

« back to all changes in this revision

Viewing changes to Import/pycxx-5.4.2/Src/cxx_extensions.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-23 20:08:08 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223200808-t946skprxzf6vjqx
Tags: 1.6.3-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
40
 
#include <assert.h>
41
 
 
42
 
namespace Py 
43
 
{
44
 
 
45
 
void Object::validate()
46
 
{
47
 
    // release pointer if not the right type
48
 
    if( !accepts( p ) )
49
 
    {
50
 
#if defined( _CPPRTTI ) || defined( __GNUG__ )
51
 
        std::string s( "PyCXX: Error creating object of type " );
52
 
        s += (typeid( *this )).name();
53
 
 
54
 
        if( p != 0 )
55
 
        {
56
 
            String from_repr = repr();
57
 
            s += " from ";
58
 
            s += from_repr.as_std_string();
59
 
        }
60
 
        else
61
 
        {
62
 
            s += " from (nil)";
63
 
        }
64
 
#endif
65
 
        release();
66
 
        if( PyErr_Occurred() )
67
 
        { // Error message already set
68
 
            throw Exception();
69
 
        }
70
 
        // Better error message if RTTI available
71
 
#if defined( _CPPRTTI ) || defined( __GNUG__ )
72
 
        throw TypeError( s );
73
 
#else
74
 
        throw TypeError( "PyCXX: type error." );
75
 
#endif
76
 
    }
77
 
}
78
 
 
79
 
//================================================================================
80
 
//
81
 
//    Implementation of MethodTable
82
 
//
83
 
//================================================================================
84
 
 
85
 
PyMethodDef MethodTable::method( const char* method_name, PyCFunction f, int flags, const char* doc ) 
86
 
{
87
 
    PyMethodDef m;
88
 
    m.ml_name = const_cast<char*>( method_name );
89
 
    m.ml_meth = f;
90
 
    m.ml_flags = flags;
91
 
    m.ml_doc = const_cast<char*>( doc );
92
 
    return m;
93
 
}
94
 
 
95
 
MethodTable::MethodTable()
96
 
{
97
 
    t.push_back( method( 0, 0, 0, 0 ) );
98
 
    mt = 0;
99
 
}
100
 
 
101
 
MethodTable::~MethodTable()
102
 
{
103
 
    delete [] mt;
104
 
}
105
 
 
106
 
void MethodTable::add( const char* method_name, PyCFunction f, const char* doc, int flag )
107
 
{
108
 
    if( !mt )
109
 
    {
110
 
        t.insert( t.end()-1, method( method_name, f, flag, doc ) );
111
 
    }
112
 
    else
113
 
    {
114
 
        throw RuntimeError( "Too late to add a module method!" );
115
 
    }
116
 
}
117
 
 
118
 
PyMethodDef* MethodTable::table()
119
 
{    
120
 
    if( !mt )
121
 
    {
122
 
        Py_ssize_t t1size = t.size();
123
 
        mt = new PyMethodDef[t1size];
124
 
        int j = 0;
125
 
        for( std::vector<PyMethodDef>::iterator i = t.begin(); i != t.end(); i++ )
126
 
        {
127
 
            mt[j++] = *i;
128
 
        }
129
 
    }
130
 
    return mt;
131
 
}
132
 
 
133
 
//================================================================================
134
 
//
135
 
//    Implementation of ExtensionModule
136
 
//
137
 
//================================================================================
138
 
ExtensionModuleBase::ExtensionModuleBase( const char *name )
139
 
    : module_name( name )
140
 
    , full_module_name( __Py_PackageContext() != NULL ? std::string( __Py_PackageContext() ) : module_name )
141
 
    , method_table()
142
 
{}
143
 
 
144
 
ExtensionModuleBase::~ExtensionModuleBase()
145
 
{}
146
 
 
147
 
const std::string &ExtensionModuleBase::name() const
148
 
{
149
 
    return module_name;
150
 
}
151
 
 
152
 
const std::string &ExtensionModuleBase::fullName() const
153
 
{
154
 
    return full_module_name;
155
 
}
156
 
 
157
 
class ExtensionModuleBasePtr : public PythonExtension<ExtensionModuleBasePtr>
158
 
{
159
 
public:
160
 
    ExtensionModuleBasePtr( ExtensionModuleBase *_module )
161
 
        : module( _module )
162
 
    {}
163
 
    virtual ~ExtensionModuleBasePtr()
164
 
    {}
165
 
 
166
 
    ExtensionModuleBase *module;
167
 
};
168
 
 
169
 
 
170
 
void ExtensionModuleBase::initialize( const char *module_doc )
171
 
{
172
 
    PyObject *module_ptr = new ExtensionModuleBasePtr( this );
173
 
 
174
 
    Py_InitModule4
175
 
    (
176
 
    const_cast<char *>( module_name.c_str() ),    // name
177
 
    method_table.table(),                // methods
178
 
    const_cast<char *>( module_doc ),        // docs
179
 
    module_ptr,                    // pass to functions as "self"
180
 
    PYTHON_API_VERSION                // API version
181
 
    );
182
 
}
183
 
 
184
 
Py::Module ExtensionModuleBase::module(void) const
185
 
{
186
 
    return Module( full_module_name );
187
 
}
188
 
 
189
 
Py::Dict ExtensionModuleBase::moduleDictionary(void) const
190
 
{
191
 
    return module().getDict();
192
 
}
193
 
 
194
 
//--------------------------------------------------------------------------------
195
 
 
196
 
//================================================================================
197
 
//
198
 
//    Implementation of PythonType
199
 
//
200
 
//================================================================================
201
 
 
202
 
extern "C"
203
 
{
204
 
    static void standard_dealloc(PyObject* p);
205
 
    //
206
 
    // All the following functions redirect the call from Python
207
 
    // onto the matching virtual function in PythonExtensionBase
208
 
    //
209
 
    static int print_handler (PyObject*, FILE *, int);
210
 
    static PyObject* getattr_handler (PyObject*, char*);
211
 
    static int setattr_handler (PyObject*, char*, PyObject*);
212
 
    static PyObject* getattro_handler (PyObject*, PyObject*);
213
 
    static int setattro_handler (PyObject*, PyObject*, PyObject*);
214
 
    static int compare_handler (PyObject*, PyObject*);
215
 
    static PyObject* richcompare_handler (PyObject*, PyObject*, int op);
216
 
    static PyObject* repr_handler (PyObject*);
217
 
    static PyObject* str_handler (PyObject*);
218
 
    static long hash_handler (PyObject*);
219
 
    static PyObject* call_handler (PyObject*, PyObject*, PyObject*);
220
 
    static PyObject* iter_handler (PyObject*);
221
 
    static PyObject* iternext_handler (PyObject*);
222
 
 
223
 
    // Sequence methods
224
 
    static Py_ssize_t sequence_length_handler(PyObject*);
225
 
    static PyObject* sequence_concat_handler(PyObject*,PyObject*);
226
 
    static PyObject* sequence_repeat_handler(PyObject*, Py_ssize_t);
227
 
    static PyObject* sequence_item_handler(PyObject*, Py_ssize_t);
228
 
    static PyObject* sequence_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t);
229
 
    static int sequence_ass_item_handler(PyObject*, Py_ssize_t, PyObject*);
230
 
    static int sequence_ass_slice_handler(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*);
231
 
    // Mapping
232
 
    static Py_ssize_t mapping_length_handler(PyObject*);
233
 
    static PyObject* mapping_subscript_handler(PyObject*, PyObject*);
234
 
    static int mapping_ass_subscript_handler(PyObject*, PyObject*, PyObject*);
235
 
 
236
 
    // Numeric methods
237
 
    static int number_nonzero_handler (PyObject*);
238
 
    static PyObject* number_negative_handler (PyObject*);
239
 
    static PyObject* number_positive_handler (PyObject*);
240
 
    static PyObject* number_absolute_handler (PyObject*);
241
 
    static PyObject* number_invert_handler (PyObject*);
242
 
    static PyObject* number_int_handler (PyObject*);
243
 
    static PyObject* number_float_handler (PyObject*);
244
 
    static PyObject* number_long_handler (PyObject*);
245
 
    static PyObject* number_oct_handler (PyObject*);
246
 
    static PyObject* number_hex_handler (PyObject*);
247
 
    static PyObject* number_add_handler (PyObject*, PyObject*);
248
 
    static PyObject* number_subtract_handler (PyObject*, PyObject*);
249
 
    static PyObject* number_multiply_handler (PyObject*, PyObject*);
250
 
    static PyObject* number_divide_handler (PyObject*, PyObject*);
251
 
    static PyObject* number_remainder_handler (PyObject*, PyObject*);
252
 
    static PyObject* number_divmod_handler (PyObject*, PyObject*);
253
 
    static PyObject* number_lshift_handler (PyObject*, PyObject*);
254
 
    static PyObject* number_rshift_handler (PyObject*, PyObject*);
255
 
    static PyObject* number_and_handler (PyObject*, PyObject*);
256
 
    static PyObject* number_xor_handler (PyObject*, PyObject*);
257
 
    static PyObject* number_or_handler (PyObject*, PyObject*);
258
 
    static PyObject* number_power_handler(PyObject*, PyObject*, PyObject*);
259
 
 
260
 
    // Buffer
261
 
    static Py_ssize_t buffer_getreadbuffer_handler (PyObject*, Py_ssize_t, void**);
262
 
    static Py_ssize_t buffer_getwritebuffer_handler (PyObject*, Py_ssize_t, void**);
263
 
    static Py_ssize_t buffer_getsegcount_handler (PyObject*, Py_ssize_t*);
264
 
}
265
 
 
266
 
 
267
 
extern "C" void standard_dealloc( PyObject* p )
268
 
{
269
 
    PyMem_DEL( p );
270
 
}
271
 
 
272
 
PythonType & PythonType::supportSequenceType()
273
 
{
274
 
    if( !sequence_table )
275
 
    {
276
 
        sequence_table = new PySequenceMethods;
277
 
        memset( sequence_table, 0, sizeof( PySequenceMethods ) );   // ensure new fields are 0
278
 
        table->tp_as_sequence = sequence_table;
279
 
        sequence_table->sq_length = sequence_length_handler;
280
 
        sequence_table->sq_concat = sequence_concat_handler;
281
 
        sequence_table->sq_repeat = sequence_repeat_handler;
282
 
        sequence_table->sq_item = sequence_item_handler;
283
 
        sequence_table->sq_slice = sequence_slice_handler;
284
 
 
285
 
        sequence_table->sq_ass_item = sequence_ass_item_handler;    // BAS setup seperately?
286
 
        sequence_table->sq_ass_slice = sequence_ass_slice_handler;  // BAS setup seperately?
287
 
    }
288
 
    return *this;
289
 
}
290
 
 
291
 
PythonType & PythonType::supportMappingType()
292
 
{
293
 
    if( !mapping_table )
294
 
    {
295
 
        mapping_table = new PyMappingMethods;
296
 
        memset( mapping_table, 0, sizeof( PyMappingMethods ) );   // ensure new fields are 0
297
 
        table->tp_as_mapping = mapping_table;
298
 
        mapping_table->mp_length = mapping_length_handler;
299
 
        mapping_table->mp_subscript = mapping_subscript_handler;
300
 
        mapping_table->mp_ass_subscript = mapping_ass_subscript_handler;    // BAS setup seperately?
301
 
    }
302
 
    return *this;
303
 
}
304
 
 
305
 
PythonType & PythonType::supportNumberType()
306
 
{
307
 
    if( !number_table )
308
 
    {
309
 
        number_table = new PyNumberMethods;
310
 
        memset( number_table, 0, sizeof( PyNumberMethods ) );   // ensure new fields are 0
311
 
        table->tp_as_number = number_table;
312
 
        number_table->nb_add = number_add_handler;
313
 
        number_table->nb_subtract = number_subtract_handler;
314
 
        number_table->nb_multiply = number_multiply_handler;
315
 
        number_table->nb_divide = number_divide_handler;
316
 
        number_table->nb_remainder = number_remainder_handler;
317
 
        number_table->nb_divmod = number_divmod_handler;
318
 
        number_table->nb_power = number_power_handler;
319
 
        number_table->nb_negative = number_negative_handler;
320
 
        number_table->nb_positive = number_positive_handler;
321
 
        number_table->nb_absolute = number_absolute_handler;
322
 
        number_table->nb_nonzero = number_nonzero_handler;
323
 
        number_table->nb_invert = number_invert_handler;
324
 
        number_table->nb_lshift = number_lshift_handler;
325
 
        number_table->nb_rshift = number_rshift_handler;
326
 
        number_table->nb_and = number_and_handler;
327
 
        number_table->nb_xor = number_xor_handler;
328
 
        number_table->nb_or = number_or_handler;
329
 
        number_table->nb_coerce = 0;
330
 
        number_table->nb_int = number_int_handler;
331
 
        number_table->nb_long = number_long_handler;
332
 
        number_table->nb_float = number_float_handler;
333
 
        number_table->nb_oct = number_oct_handler;
334
 
        number_table->nb_hex = number_hex_handler;
335
 
    }
336
 
    return *this;
337
 
}
338
 
 
339
 
PythonType & PythonType::supportBufferType()
340
 
{
341
 
    if( !buffer_table )
342
 
    {
343
 
        buffer_table = new PyBufferProcs;
344
 
        memset( buffer_table, 0, sizeof( PyBufferProcs ) );   // ensure new fields are 0
345
 
        table->tp_as_buffer = buffer_table;
346
 
        buffer_table->bf_getreadbuffer = buffer_getreadbuffer_handler;
347
 
        buffer_table->bf_getwritebuffer = buffer_getwritebuffer_handler;
348
 
        buffer_table->bf_getsegcount = buffer_getsegcount_handler;
349
 
    }
350
 
    return *this;
351
 
}
352
 
 
353
 
// if you define one sequence method you must define 
354
 
// all of them except the assigns
355
 
 
356
 
PythonType::PythonType( size_t basic_size, int itemsize, const char *default_name )
357
 
    : table( new PyTypeObject )
358
 
    , sequence_table( NULL )
359
 
    , mapping_table( NULL )
360
 
    , number_table( NULL )
361
 
    , buffer_table( NULL )
362
 
{
363
 
    memset( table, 0, sizeof( PyTypeObject ) );   // ensure new fields are 0
364
 
    *reinterpret_cast<PyObject*>( table ) = py_object_initializer;
365
 
    table->ob_type = _Type_Type();
366
 
    table->ob_size = 0;
367
 
    table->tp_name = const_cast<char *>( default_name );
368
 
    table->tp_basicsize = basic_size;
369
 
    table->tp_itemsize = itemsize;
370
 
    table->tp_dealloc = ( destructor ) standard_dealloc;
371
 
    table->tp_print = 0;
372
 
    table->tp_getattr = 0;
373
 
    table->tp_setattr = 0;
374
 
    table->tp_compare = 0;
375
 
    table->tp_repr = 0;
376
 
    table->tp_as_number = 0;
377
 
    table->tp_as_sequence = 0;
378
 
    table->tp_as_mapping =  0;
379
 
    table->tp_hash = 0;
380
 
    table->tp_call = 0;
381
 
    table->tp_str = 0;
382
 
    table->tp_getattro = 0;
383
 
    table->tp_setattro = 0;
384
 
    table->tp_as_buffer = 0;
385
 
    table->tp_flags = Py_TPFLAGS_DEFAULT;
386
 
    table->tp_doc = 0;
387
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 0)
388
 
    // first use in 2.0
389
 
    table->tp_traverse = 0L;
390
 
    table->tp_clear = 0L;
391
 
#else
392
 
    table->tp_xxx5 = 0L;
393
 
    table->tp_xxx6 = 0L;
394
 
#endif
395
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
396
 
    // first defined in 2.1
397
 
    table->tp_richcompare = 0L;
398
 
    table->tp_weaklistoffset = 0L;
399
 
#else
400
 
    table->tp_xxx7 = 0L;
401
 
    table->tp_xxx8 = 0L;
402
 
#endif
403
 
 
404
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 2)
405
 
    // first defined in 2.3
406
 
    table->tp_iter = 0L;
407
 
    table->tp_iternext = 0L;
408
 
#endif
409
 
 
410
 
#ifdef COUNT_ALLOCS
411
 
    table->tp_alloc = 0;
412
 
    table->tp_free = 0;
413
 
    table->tp_maxalloc = 0;
414
 
    table->tp_next = 0;
415
 
#endif
416
 
}
417
 
 
418
 
PythonType::~PythonType( )
419
 
{
420
 
    delete table;
421
 
    delete sequence_table;
422
 
    delete mapping_table;
423
 
    delete number_table;
424
 
    delete buffer_table;
425
 
}
426
 
 
427
 
PyTypeObject* PythonType::type_object( ) const
428
 
{return table;}
429
 
 
430
 
PythonType & PythonType::name( const char* nam )
431
 
{
432
 
    table->tp_name = const_cast<char *>( nam );
433
 
    return *this;
434
 
}
435
 
 
436
 
const char *PythonType::getName() const
437
 
{
438
 
    return table->tp_name;
439
 
}
440
 
 
441
 
PythonType & PythonType::doc( const char* d )
442
 
{
443
 
    table->tp_doc = const_cast<char *>( d );
444
 
    return *this;
445
 
}
446
 
 
447
 
const char *PythonType::getDoc() const
448
 
{
449
 
    return table->tp_doc;
450
 
}
451
 
 
452
 
PythonType & PythonType::dealloc( void( *f )( PyObject* ))
453
 
{
454
 
    table->tp_dealloc = f;
455
 
    return *this;
456
 
}
457
 
 
458
 
PythonType & PythonType::supportPrint()
459
 
{
460
 
    table->tp_print = print_handler;
461
 
    return *this;
462
 
}
463
 
 
464
 
PythonType & PythonType::supportGetattr()
465
 
{
466
 
    table->tp_getattr = getattr_handler;
467
 
    return *this;
468
 
}
469
 
 
470
 
PythonType & PythonType::supportSetattr()
471
 
{
472
 
    table->tp_setattr = setattr_handler;
473
 
    return *this;
474
 
}
475
 
 
476
 
PythonType & PythonType::supportGetattro()
477
 
{
478
 
    table->tp_getattro = getattro_handler;
479
 
    return *this;
480
 
}
481
 
 
482
 
PythonType & PythonType::supportSetattro()
483
 
{
484
 
    table->tp_setattro = setattro_handler;
485
 
    return *this;
486
 
}
487
 
 
488
 
PythonType & PythonType::supportCompare()
489
 
{
490
 
    table->tp_compare = compare_handler;
491
 
    return *this;
492
 
}
493
 
 
494
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
495
 
PythonType & PythonType::supportRichCompare()
496
 
{
497
 
    table->tp_richcompare = richcompare_handler;
498
 
    return *this;
499
 
}
500
 
#endif
501
 
 
502
 
PythonType & PythonType::supportRepr()
503
 
{
504
 
    table->tp_repr = repr_handler;
505
 
    return *this;
506
 
}
507
 
 
508
 
PythonType & PythonType::supportStr()
509
 
{
510
 
    table->tp_str = str_handler;
511
 
    return *this;
512
 
}
513
 
 
514
 
PythonType & PythonType::supportHash()
515
 
{
516
 
    table->tp_hash = hash_handler;
517
 
    return *this;
518
 
}
519
 
 
520
 
PythonType & PythonType::supportCall()
521
 
{
522
 
    table->tp_call = call_handler;
523
 
    return *this;
524
 
}
525
 
 
526
 
PythonType & PythonType::supportIter()
527
 
{
528
 
    table->tp_iter = iter_handler;
529
 
    table->tp_iternext = iternext_handler;
530
 
    return *this;
531
 
}
532
 
 
533
 
//--------------------------------------------------------------------------------
534
 
//
535
 
//    Handlers
536
 
//
537
 
//--------------------------------------------------------------------------------
538
 
extern "C" int print_handler( PyObject *self, FILE *fp, int flags )
539
 
{
540
 
    try
541
 
    {
542
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
543
 
        return p->print( fp, flags );
544
 
    }
545
 
    catch( Py::Exception & )
546
 
    {
547
 
        return -1;    // indicate error
548
 
    }
549
 
}
550
 
 
551
 
extern "C" PyObject* getattr_handler( PyObject *self, char *name )
552
 
{
553
 
    try
554
 
    {
555
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
556
 
        return new_reference_to( p->getattr( name ) );
557
 
    }
558
 
    catch( Py::Exception & )
559
 
    {
560
 
        return NULL;    // indicate error
561
 
    }
562
 
}
563
 
 
564
 
extern "C" int setattr_handler( PyObject *self, char *name, PyObject *value )
565
 
{
566
 
    try
567
 
    {
568
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
569
 
        return p->setattr( name, Py::Object( value ) );
570
 
    }
571
 
    catch( Py::Exception & )
572
 
    {
573
 
        return -1;    // indicate error
574
 
    }
575
 
}
576
 
 
577
 
extern "C" PyObject* getattro_handler( PyObject *self, PyObject *name )
578
 
{
579
 
    try
580
 
    {
581
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
582
 
        return new_reference_to( p->getattro( Py::Object( name ) ) );
583
 
    }
584
 
    catch( Py::Exception & )
585
 
    {
586
 
        return NULL;    // indicate error
587
 
    }
588
 
}
589
 
 
590
 
extern "C" int setattro_handler( PyObject *self, PyObject *name, PyObject *value )
591
 
{
592
 
    try
593
 
    {
594
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
595
 
        return p->setattro( Py::Object( name ), Py::Object( value ) );
596
 
    }
597
 
    catch( Py::Exception & )
598
 
    {
599
 
        return -1;    // indicate error
600
 
    }
601
 
}
602
 
 
603
 
extern "C" int compare_handler( PyObject *self, PyObject *other )
604
 
{
605
 
    try
606
 
    {
607
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
608
 
        return p->compare( Py::Object( other ) );
609
 
    }
610
 
    catch( Py::Exception & )
611
 
    {
612
 
        return -1;    // indicate error
613
 
    }
614
 
}
615
 
 
616
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
617
 
extern "C" PyObject* richcompare_handler( PyObject *self, PyObject *other, int op )
618
 
{
619
 
    try
620
 
    {
621
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
622
 
        return new_reference_to( p->rich_compare( Py::Object( other ), op ) );
623
 
    }
624
 
    catch( Py::Exception & )
625
 
    {
626
 
        return NULL;    // indicate error
627
 
    }
628
 
}
629
 
#endif
630
 
 
631
 
extern "C" PyObject* repr_handler( PyObject *self )
632
 
{
633
 
    try
634
 
    {
635
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
636
 
        return new_reference_to( p->repr() );
637
 
    }
638
 
    catch( Py::Exception & )
639
 
    {
640
 
        return NULL;    // indicate error
641
 
    }
642
 
}
643
 
 
644
 
extern "C" PyObject* str_handler( PyObject *self )
645
 
{
646
 
    try
647
 
    {
648
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
649
 
        return new_reference_to( p->str() );
650
 
    }
651
 
    catch( Py::Exception & )
652
 
    {
653
 
        return NULL;    // indicate error
654
 
    }
655
 
}
656
 
 
657
 
extern "C" long hash_handler( PyObject *self )
658
 
{
659
 
    try
660
 
    {
661
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
662
 
        return p->hash();
663
 
    }
664
 
    catch( Py::Exception & )
665
 
    {
666
 
        return -1;    // indicate error
667
 
    }
668
 
}
669
 
 
670
 
extern "C" PyObject* call_handler( PyObject *self, PyObject *args, PyObject *kw )
671
 
{
672
 
    try
673
 
    {
674
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
675
 
        if( kw != NULL )
676
 
            return new_reference_to( p->call( Py::Object( args ), Py::Object( kw ) ) );
677
 
        else
678
 
            return new_reference_to( p->call( Py::Object( args ), Py::Object() ) );
679
 
    }
680
 
    catch( Py::Exception & )
681
 
    {
682
 
        return NULL;    // indicate error
683
 
    }
684
 
}
685
 
 
686
 
extern "C" PyObject* iter_handler( PyObject *self )
687
 
{
688
 
    try
689
 
    {
690
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
691
 
        return new_reference_to( p->iter() );
692
 
    }
693
 
    catch( Py::Exception & )
694
 
    {
695
 
        return NULL;    // indicate error
696
 
    }
697
 
}
698
 
 
699
 
extern "C" PyObject* iternext_handler( PyObject *self )
700
 
{
701
 
    try
702
 
    {
703
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
704
 
        return p->iternext();  // might be a NULL ptr on end of iteration
705
 
    }
706
 
    catch( Py::Exception & )
707
 
    {
708
 
        return NULL;    // indicate error
709
 
    }
710
 
}
711
 
 
712
 
 
713
 
// Sequence methods
714
 
extern "C" Py_ssize_t sequence_length_handler( PyObject *self )
715
 
{
716
 
    try
717
 
    {
718
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
719
 
        return p->sequence_length();
720
 
    }
721
 
    catch( Py::Exception & )
722
 
    {
723
 
        return -1;    // indicate error
724
 
    }
725
 
}
726
 
 
727
 
extern "C" PyObject* sequence_concat_handler( PyObject *self, PyObject *other )
728
 
{
729
 
    try
730
 
    {
731
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
732
 
        return new_reference_to( p->sequence_concat( Py::Object( other ) ) );
733
 
    }
734
 
    catch( Py::Exception & )
735
 
    {
736
 
        return NULL;    // indicate error
737
 
    }
738
 
}
739
 
 
740
 
extern "C" PyObject* sequence_repeat_handler( PyObject *self, Py_ssize_t count )
741
 
{
742
 
    try
743
 
    {
744
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
745
 
        return new_reference_to( p->sequence_repeat( count ) );
746
 
    }
747
 
    catch( Py::Exception & )
748
 
    {
749
 
        return NULL;    // indicate error
750
 
    }
751
 
}
752
 
 
753
 
extern "C" PyObject* sequence_item_handler( PyObject *self, Py_ssize_t index )
754
 
{
755
 
    try
756
 
    {
757
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
758
 
        return new_reference_to( p->sequence_item( index ) );
759
 
    }
760
 
    catch( Py::Exception & )
761
 
    {
762
 
        return NULL;    // indicate error
763
 
    }
764
 
}
765
 
 
766
 
extern "C" PyObject* sequence_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last )
767
 
{
768
 
    try
769
 
    {
770
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
771
 
        return new_reference_to( p->sequence_slice( first, last ) );
772
 
    }
773
 
    catch( Py::Exception & )
774
 
    {
775
 
        return NULL;    // indicate error
776
 
    }
777
 
}
778
 
 
779
 
extern "C" int sequence_ass_item_handler( PyObject *self, Py_ssize_t index, PyObject *value )
780
 
{
781
 
    try
782
 
    {
783
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
784
 
        return p->sequence_ass_item( index, Py::Object( value ) );
785
 
    }
786
 
    catch( Py::Exception & )
787
 
    {
788
 
        return -1;    // indicate error
789
 
    }
790
 
}
791
 
 
792
 
extern "C" int sequence_ass_slice_handler( PyObject *self, Py_ssize_t first, Py_ssize_t last, PyObject *value )
793
 
{
794
 
    try
795
 
    {
796
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
797
 
        return p->sequence_ass_slice( first, last, Py::Object( value ) );
798
 
    }
799
 
    catch( Py::Exception & )
800
 
    {
801
 
        return -1;    // indicate error
802
 
    }
803
 
}
804
 
 
805
 
// Mapping
806
 
extern "C" Py_ssize_t mapping_length_handler( PyObject *self )
807
 
{
808
 
    try
809
 
    {
810
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
811
 
        return p->mapping_length();
812
 
    }
813
 
    catch( Py::Exception & )
814
 
    {
815
 
        return -1;    // indicate error
816
 
    }
817
 
}
818
 
 
819
 
extern "C" PyObject* mapping_subscript_handler( PyObject *self, PyObject *key )
820
 
{
821
 
    try
822
 
    {
823
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
824
 
        return new_reference_to( p->mapping_subscript( Py::Object( key ) ) );
825
 
    }
826
 
    catch( Py::Exception & )
827
 
    {
828
 
        return NULL;    // indicate error
829
 
    }
830
 
}
831
 
 
832
 
extern "C" int mapping_ass_subscript_handler( PyObject *self, PyObject *key, PyObject *value )
833
 
{
834
 
    try
835
 
    {
836
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
837
 
        return p->mapping_ass_subscript( Py::Object( key ), Py::Object( value ) );
838
 
    }
839
 
    catch( Py::Exception & )
840
 
    {
841
 
        return -1;    // indicate error
842
 
    }
843
 
}
844
 
 
845
 
// Number
846
 
extern "C" int number_nonzero_handler( PyObject *self )
847
 
{
848
 
    try
849
 
    {
850
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
851
 
        return p->number_nonzero();
852
 
    }
853
 
    catch( Py::Exception & )
854
 
    {
855
 
        return -1;    // indicate error
856
 
    }
857
 
}
858
 
 
859
 
extern "C" PyObject* number_negative_handler( PyObject *self )
860
 
{
861
 
    try
862
 
    {
863
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
864
 
        return new_reference_to( p->number_negative() );
865
 
    }
866
 
    catch( Py::Exception & )
867
 
    {
868
 
        return NULL;    // indicate error
869
 
    }
870
 
}
871
 
 
872
 
extern "C" PyObject* number_positive_handler( PyObject *self )
873
 
{
874
 
    try
875
 
    {
876
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
877
 
        return new_reference_to( p->number_positive() );
878
 
    }
879
 
    catch( Py::Exception & )
880
 
    {
881
 
        return NULL;    // indicate error
882
 
    }
883
 
}
884
 
 
885
 
extern "C" PyObject* number_absolute_handler( PyObject *self )
886
 
{
887
 
    try
888
 
    {
889
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
890
 
        return new_reference_to( p->number_absolute() );
891
 
    }
892
 
    catch( Py::Exception & )
893
 
    {
894
 
        return NULL;    // indicate error
895
 
    }
896
 
}
897
 
 
898
 
extern "C" PyObject* number_invert_handler( PyObject *self )
899
 
{
900
 
    try
901
 
    {
902
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
903
 
        return new_reference_to( p->number_invert() );
904
 
    }
905
 
    catch( Py::Exception & )
906
 
    {
907
 
        return NULL;    // indicate error
908
 
    }
909
 
}
910
 
 
911
 
extern "C" PyObject* number_int_handler( PyObject *self )
912
 
{
913
 
    try
914
 
    {
915
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
916
 
        return new_reference_to( p->number_int() );
917
 
    }
918
 
    catch( Py::Exception & )
919
 
    {
920
 
        return NULL;    // indicate error
921
 
    }
922
 
}
923
 
 
924
 
extern "C" PyObject* number_float_handler( PyObject *self )
925
 
{
926
 
    try
927
 
    {
928
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
929
 
        return new_reference_to( p->number_float() );
930
 
    }
931
 
    catch( Py::Exception & )
932
 
    {
933
 
        return NULL;    // indicate error
934
 
    }
935
 
}
936
 
 
937
 
extern "C" PyObject* number_long_handler( PyObject *self )
938
 
{
939
 
    try
940
 
    {
941
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
942
 
        return new_reference_to( p->number_long() );
943
 
    }
944
 
    catch( Py::Exception & )
945
 
    {
946
 
        return NULL;    // indicate error
947
 
    }
948
 
}
949
 
 
950
 
extern "C" PyObject* number_oct_handler( PyObject *self )
951
 
{
952
 
    try
953
 
    {
954
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
955
 
        return new_reference_to( p->number_oct() );
956
 
    }
957
 
    catch( Py::Exception & )
958
 
    {
959
 
        return NULL;    // indicate error
960
 
    }
961
 
}
962
 
 
963
 
extern "C" PyObject* number_hex_handler( PyObject *self )
964
 
{
965
 
    try
966
 
    {
967
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
968
 
        return new_reference_to( p->number_hex() );
969
 
    }
970
 
    catch( Py::Exception & )
971
 
    {
972
 
        return NULL;    // indicate error
973
 
    }
974
 
}
975
 
 
976
 
extern "C" PyObject* number_add_handler( PyObject *self, PyObject *other )
977
 
{
978
 
    try
979
 
    {
980
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
981
 
        return new_reference_to( p->number_add( Py::Object( other ) ) );
982
 
    }
983
 
    catch( Py::Exception & )
984
 
    {
985
 
        return NULL;    // indicate error
986
 
    }
987
 
}
988
 
 
989
 
extern "C" PyObject* number_subtract_handler( PyObject *self, PyObject *other )
990
 
{
991
 
    try
992
 
    {
993
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
994
 
        return new_reference_to( p->number_subtract( Py::Object( other ) ) );
995
 
    }
996
 
    catch( Py::Exception & )
997
 
    {
998
 
        return NULL;    // indicate error
999
 
    }
1000
 
}
1001
 
 
1002
 
extern "C" PyObject* number_multiply_handler( PyObject *self, PyObject *other )
1003
 
{
1004
 
    try
1005
 
    {
1006
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1007
 
        return new_reference_to( p->number_multiply( Py::Object( other ) ) );
1008
 
    }
1009
 
    catch( Py::Exception & )
1010
 
    {
1011
 
        return NULL;    // indicate error
1012
 
    }
1013
 
}
1014
 
 
1015
 
extern "C" PyObject* number_divide_handler( PyObject *self, PyObject *other )
1016
 
{
1017
 
    try
1018
 
    {
1019
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1020
 
        return new_reference_to( p->number_divide( Py::Object( other ) ) );
1021
 
    }
1022
 
    catch( Py::Exception & )
1023
 
    {
1024
 
        return NULL;    // indicate error
1025
 
    }
1026
 
}
1027
 
 
1028
 
extern "C" PyObject* number_remainder_handler( PyObject *self, PyObject *other )
1029
 
{
1030
 
    try
1031
 
    {
1032
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1033
 
        return new_reference_to( p->number_remainder( Py::Object( other ) ) );
1034
 
    }
1035
 
    catch( Py::Exception & )
1036
 
    {
1037
 
        return NULL;    // indicate error
1038
 
    }
1039
 
}
1040
 
 
1041
 
extern "C" PyObject* number_divmod_handler( PyObject *self, PyObject *other )
1042
 
{
1043
 
    try
1044
 
    {
1045
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1046
 
        return new_reference_to( p->number_divmod( Py::Object( other ) ) );
1047
 
    }
1048
 
    catch( Py::Exception & )
1049
 
    {
1050
 
        return NULL;    // indicate error
1051
 
    }
1052
 
}
1053
 
 
1054
 
extern "C" PyObject* number_lshift_handler( PyObject *self, PyObject *other )
1055
 
{
1056
 
    try
1057
 
    {
1058
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1059
 
        return new_reference_to( p->number_lshift( Py::Object( other ) ) );
1060
 
    }
1061
 
    catch( Py::Exception & )
1062
 
    {
1063
 
        return NULL;    // indicate error
1064
 
    }
1065
 
}
1066
 
 
1067
 
extern "C" PyObject* number_rshift_handler( PyObject *self, PyObject *other )
1068
 
{
1069
 
    try
1070
 
    {
1071
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1072
 
        return new_reference_to( p->number_rshift( Py::Object( other ) ) );
1073
 
    }
1074
 
    catch( Py::Exception & )
1075
 
    {
1076
 
        return NULL;    // indicate error
1077
 
    }
1078
 
}
1079
 
 
1080
 
extern "C" PyObject* number_and_handler( PyObject *self, PyObject *other )
1081
 
{
1082
 
    try
1083
 
    {
1084
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1085
 
        return new_reference_to( p->number_and( Py::Object( other ) ) );
1086
 
    }
1087
 
    catch( Py::Exception & )
1088
 
    {
1089
 
        return NULL;    // indicate error
1090
 
    }
1091
 
}
1092
 
 
1093
 
extern "C" PyObject* number_xor_handler( PyObject *self, PyObject *other )
1094
 
{
1095
 
    try
1096
 
    {
1097
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1098
 
        return new_reference_to( p->number_xor( Py::Object( other ) ) );
1099
 
    }
1100
 
    catch( Py::Exception & )
1101
 
    {
1102
 
        return NULL;    // indicate error
1103
 
    }
1104
 
}
1105
 
 
1106
 
extern "C" PyObject* number_or_handler( PyObject *self, PyObject *other )
1107
 
{
1108
 
    try
1109
 
    {
1110
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1111
 
        return new_reference_to( p->number_or( Py::Object( other ) ) );
1112
 
    }
1113
 
    catch( Py::Exception & )
1114
 
    {
1115
 
        return NULL;    // indicate error
1116
 
    }
1117
 
}
1118
 
 
1119
 
extern "C" PyObject* number_power_handler( PyObject *self, PyObject *x1, PyObject *x2 )
1120
 
{
1121
 
    try
1122
 
    {
1123
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1124
 
        return new_reference_to( p->number_power( Py::Object( x1 ), Py::Object( x2 ) ) );
1125
 
    }
1126
 
    catch( Py::Exception & )
1127
 
    {
1128
 
        return NULL;    // indicate error
1129
 
    }
1130
 
}
1131
 
 
1132
 
// Buffer
1133
 
extern "C" Py_ssize_t buffer_getreadbuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1134
 
{
1135
 
    try
1136
 
    {
1137
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1138
 
        return p->buffer_getreadbuffer( index, pp );
1139
 
    }
1140
 
    catch( Py::Exception & )
1141
 
    {
1142
 
        return -1;    // indicate error
1143
 
    }
1144
 
}
1145
 
 
1146
 
extern "C" Py_ssize_t buffer_getwritebuffer_handler( PyObject *self, Py_ssize_t index, void **pp )
1147
 
{
1148
 
    try
1149
 
    {
1150
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1151
 
        return p->buffer_getwritebuffer( index, pp );
1152
 
    }
1153
 
    catch( Py::Exception & )
1154
 
    {
1155
 
        return -1;    // indicate error
1156
 
    }
1157
 
}
1158
 
 
1159
 
extern "C" Py_ssize_t buffer_getsegcount_handler( PyObject *self, Py_ssize_t *count )
1160
 
{
1161
 
    try
1162
 
    {
1163
 
        PythonExtensionBase *p = static_cast<PythonExtensionBase *>( self );
1164
 
        return p->buffer_getsegcount( count );
1165
 
    }
1166
 
    catch( Py::Exception & )
1167
 
    {
1168
 
        return -1;    // indicate error
1169
 
    }
1170
 
}
1171
 
 
1172
 
 
1173
 
//================================================================================
1174
 
//
1175
 
//    Implementation of PythonExtensionBase
1176
 
//
1177
 
//================================================================================
1178
 
#define missing_method( method ) \
1179
 
throw RuntimeError( "Extension object does not support method " #method );
1180
 
 
1181
 
PythonExtensionBase::PythonExtensionBase()
1182
 
{
1183
 
}
1184
 
 
1185
 
PythonExtensionBase::~PythonExtensionBase()
1186
 
{
1187
 
    assert( ob_refcnt == 0 );
1188
 
}
1189
 
 
1190
 
int PythonExtensionBase::print( FILE *, int )
1191
 
{ missing_method( print ); return -1; }
1192
 
 
1193
 
int PythonExtensionBase::setattr( const char*, const Py::Object & )
1194
 
{ missing_method( setattr ); return -1; }
1195
 
 
1196
 
Py::Object PythonExtensionBase::getattro( const Py::Object & )
1197
 
{ missing_method( getattro ); return Py::Nothing(); }
1198
 
 
1199
 
int PythonExtensionBase::setattro( const Py::Object &, const Py::Object & )
1200
 
{ missing_method( setattro ); return -1; }
1201
 
 
1202
 
int PythonExtensionBase::compare( const Py::Object & )
1203
 
{ missing_method( compare ); return -1; }
1204
 
 
1205
 
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 1)
1206
 
Py::Object PythonExtensionBase::rich_compare( const Py::Object &, int op )
1207
 
{ missing_method( rich_compare ); return Py::None(); }
1208
 
#endif
1209
 
 
1210
 
Py::Object PythonExtensionBase::repr()
1211
 
{ missing_method( repr ); return Py::Nothing(); }
1212
 
 
1213
 
Py::Object PythonExtensionBase::str()
1214
 
{ missing_method( str ); return Py::Nothing(); }
1215
 
 
1216
 
long PythonExtensionBase::hash()
1217
 
{ missing_method( hash ); return -1; }
1218
 
 
1219
 
Py::Object PythonExtensionBase::call( const Py::Object &, const Py::Object & )
1220
 
{ missing_method( call ); return Py::Nothing(); }
1221
 
 
1222
 
Py::Object PythonExtensionBase::iter()
1223
 
{ missing_method( iter ); return Py::Nothing(); }
1224
 
 
1225
 
PyObject* PythonExtensionBase::iternext()
1226
 
{ missing_method( iternext ); return NULL; }
1227
 
 
1228
 
 
1229
 
// Sequence methods
1230
 
int PythonExtensionBase::sequence_length()
1231
 
{ missing_method( sequence_length ); return -1; }
1232
 
 
1233
 
Py::Object PythonExtensionBase::sequence_concat( const Py::Object & )
1234
 
{ missing_method( sequence_concat ); return Py::Nothing(); }
1235
 
 
1236
 
Py::Object PythonExtensionBase::sequence_repeat( Py_ssize_t )
1237
 
{ missing_method( sequence_repeat ); return Py::Nothing(); }
1238
 
 
1239
 
Py::Object PythonExtensionBase::sequence_item( Py_ssize_t )
1240
 
{ missing_method( sequence_item ); return Py::Nothing(); }
1241
 
 
1242
 
Py::Object PythonExtensionBase::sequence_slice( Py_ssize_t, Py_ssize_t )
1243
 
{ missing_method( sequence_slice ); return Py::Nothing(); }
1244
 
 
1245
 
int PythonExtensionBase::sequence_ass_item( Py_ssize_t, const Py::Object & )
1246
 
{ missing_method( sequence_ass_item ); return -1; }
1247
 
 
1248
 
int PythonExtensionBase::sequence_ass_slice( Py_ssize_t, Py_ssize_t, const Py::Object & )
1249
 
{ missing_method( sequence_ass_slice ); return -1; }
1250
 
 
1251
 
 
1252
 
// Mapping
1253
 
int PythonExtensionBase::mapping_length()
1254
 
{ missing_method( mapping_length ); return -1; }
1255
 
 
1256
 
Py::Object PythonExtensionBase::mapping_subscript( const Py::Object & )
1257
 
{ missing_method( mapping_subscript ); return Py::Nothing(); }
1258
 
 
1259
 
int PythonExtensionBase::mapping_ass_subscript( const Py::Object &, const Py::Object & )
1260
 
{ missing_method( mapping_ass_subscript ); return -1; }
1261
 
 
1262
 
 
1263
 
// Number
1264
 
int PythonExtensionBase::number_nonzero()
1265
 
{ missing_method( number_nonzero ); return -1; }
1266
 
 
1267
 
Py::Object PythonExtensionBase::number_negative()
1268
 
{ missing_method( number_negative ); return Py::Nothing(); }
1269
 
 
1270
 
Py::Object PythonExtensionBase::number_positive()
1271
 
{ missing_method( number_positive ); return Py::Nothing(); }
1272
 
 
1273
 
Py::Object PythonExtensionBase::number_absolute()
1274
 
{ missing_method( number_absolute ); return Py::Nothing(); }
1275
 
 
1276
 
Py::Object PythonExtensionBase::number_invert()
1277
 
{ missing_method( number_invert ); return Py::Nothing(); }
1278
 
 
1279
 
Py::Object PythonExtensionBase::number_int()
1280
 
{ missing_method( number_int ); return Py::Nothing(); }
1281
 
 
1282
 
Py::Object PythonExtensionBase::number_float()
1283
 
{ missing_method( number_float ); return Py::Nothing(); }
1284
 
 
1285
 
Py::Object PythonExtensionBase::number_long()
1286
 
{ missing_method( number_long ); return Py::Nothing(); }
1287
 
 
1288
 
Py::Object PythonExtensionBase::number_oct()
1289
 
{ missing_method( number_oct ); return Py::Nothing(); }
1290
 
 
1291
 
Py::Object PythonExtensionBase::number_hex()
1292
 
{ missing_method( number_hex ); return Py::Nothing(); }
1293
 
 
1294
 
Py::Object PythonExtensionBase::number_add( const Py::Object & )
1295
 
{ missing_method( number_add ); return Py::Nothing(); }
1296
 
 
1297
 
Py::Object PythonExtensionBase::number_subtract( const Py::Object & )
1298
 
{ missing_method( number_subtract ); return Py::Nothing(); }
1299
 
 
1300
 
Py::Object PythonExtensionBase::number_multiply( const Py::Object & )
1301
 
{ missing_method( number_multiply ); return Py::Nothing(); }
1302
 
 
1303
 
Py::Object PythonExtensionBase::number_divide( const Py::Object & )
1304
 
{ missing_method( number_divide ); return Py::Nothing(); }
1305
 
 
1306
 
Py::Object PythonExtensionBase::number_remainder( const Py::Object & )
1307
 
{ missing_method( number_remainder ); return Py::Nothing(); }
1308
 
 
1309
 
Py::Object PythonExtensionBase::number_divmod( const Py::Object & )
1310
 
{ missing_method( number_divmod ); return Py::Nothing(); }
1311
 
 
1312
 
Py::Object PythonExtensionBase::number_lshift( const Py::Object & )
1313
 
{ missing_method( number_lshift ); return Py::Nothing(); }
1314
 
 
1315
 
Py::Object PythonExtensionBase::number_rshift( const Py::Object & )
1316
 
{ missing_method( number_rshift ); return Py::Nothing(); }
1317
 
 
1318
 
Py::Object PythonExtensionBase::number_and( const Py::Object & )
1319
 
{ missing_method( number_and ); return Py::Nothing(); }
1320
 
 
1321
 
Py::Object PythonExtensionBase::number_xor( const Py::Object & )
1322
 
{ missing_method( number_xor ); return Py::Nothing(); }
1323
 
 
1324
 
Py::Object PythonExtensionBase::number_or( const Py::Object & )
1325
 
{ missing_method( number_or ); return Py::Nothing(); }
1326
 
 
1327
 
Py::Object PythonExtensionBase::number_power( const Py::Object &, const Py::Object & )
1328
 
{ missing_method( number_power ); return Py::Nothing(); }
1329
 
 
1330
 
 
1331
 
// Buffer
1332
 
Py_ssize_t PythonExtensionBase::buffer_getreadbuffer( Py_ssize_t, void** )
1333
 
{ missing_method( buffer_getreadbuffer ); return -1; }
1334
 
 
1335
 
Py_ssize_t PythonExtensionBase::buffer_getwritebuffer( Py_ssize_t, void** )
1336
 
{ missing_method( buffer_getwritebuffer ); return -1; }
1337
 
 
1338
 
Py_ssize_t PythonExtensionBase::buffer_getsegcount( Py_ssize_t* )
1339
 
{ missing_method( buffer_getsegcount ); return -1; }
1340
 
 
1341
 
//--------------------------------------------------------------------------------
1342
 
//
1343
 
//    Method call handlers for
1344
 
//        PythonExtensionBase
1345
 
//        ExtensionModuleBase
1346
 
//
1347
 
//--------------------------------------------------------------------------------
1348
 
 
1349
 
extern "C" PyObject *method_keyword_call_handler( PyObject *_self_and_name_tuple, PyObject *_args, PyObject *_keywords )
1350
 
{
1351
 
    try
1352
 
    {
1353
 
        Tuple self_and_name_tuple( _self_and_name_tuple );
1354
 
 
1355
 
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1356
 
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1357
 
        if( self_as_void == NULL )
1358
 
            return NULL;
1359
 
 
1360
 
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1361
 
 
1362
 
        String py_name( self_and_name_tuple[1] );
1363
 
        std::string name( py_name.as_std_string() );
1364
 
 
1365
 
        Tuple args( _args );
1366
 
        if( _keywords == NULL )
1367
 
        {
1368
 
            Dict keywords;    // pass an empty dict
1369
 
 
1370
 
            Object result( self->invoke_method_keyword( name, args, keywords ) );
1371
 
            return new_reference_to( result.ptr() );
1372
 
        }
1373
 
 
1374
 
        Dict keywords( _keywords );
1375
 
 
1376
 
        Object result( self->invoke_method_keyword( name, args, keywords ) );
1377
 
        return new_reference_to( result.ptr() );
1378
 
    }
1379
 
    catch( Exception & )
1380
 
    {
1381
 
        return 0;
1382
 
    }
1383
 
}
1384
 
 
1385
 
extern "C" PyObject *method_varargs_call_handler( PyObject *_self_and_name_tuple, PyObject *_args )
1386
 
{
1387
 
    try
1388
 
    {
1389
 
        Tuple self_and_name_tuple( _self_and_name_tuple );
1390
 
 
1391
 
        PyObject *self_in_cobject = self_and_name_tuple[0].ptr();
1392
 
        void *self_as_void = PyCObject_AsVoidPtr( self_in_cobject );
1393
 
        if( self_as_void == NULL )
1394
 
        return NULL;
1395
 
 
1396
 
        ExtensionModuleBase *self = static_cast<ExtensionModuleBase *>( self_as_void );
1397
 
 
1398
 
        String py_name( self_and_name_tuple[1] );
1399
 
        std::string name( py_name.as_std_string() );
1400
 
 
1401
 
        Tuple args( _args );
1402
 
 
1403
 
        Object result( self->invoke_method_varargs( name, args ) );
1404
 
 
1405
 
        return new_reference_to( result.ptr() );
1406
 
    }
1407
 
    catch( Exception & )
1408
 
    {
1409
 
        return 0;
1410
 
    }
1411
 
}
1412
 
 
1413
 
extern "C" void do_not_dealloc( void * )
1414
 
{}
1415
 
 
1416
 
 
1417
 
//--------------------------------------------------------------------------------
1418
 
//
1419
 
//    ExtensionExceptionType
1420
 
//
1421
 
//--------------------------------------------------------------------------------
1422
 
ExtensionExceptionType::ExtensionExceptionType()
1423
 
    : Py::Object()
1424
 
{
1425
 
}
1426
 
 
1427
 
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name )
1428
 
{
1429
 
    std::string module_name( module.fullName() );
1430
 
    module_name += ".";
1431
 
    module_name += name;
1432
 
 
1433
 
    set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), NULL, NULL ), true );
1434
 
}
1435
 
 
1436
 
void ExtensionExceptionType::init( ExtensionModuleBase &module, const std::string& name, ExtensionExceptionType &parent)
1437
 
 {
1438
 
     std::string module_name( module.fullName() );
1439
 
     module_name += ".";
1440
 
     module_name += name;
1441
 
 
1442
 
    set( PyErr_NewException( const_cast<char *>( module_name.c_str() ), parent.ptr(), NULL ), true );
1443
 
}
1444
 
 
1445
 
ExtensionExceptionType::~ExtensionExceptionType()
1446
 
{
1447
 
}
1448
 
 
1449
 
Exception::Exception( ExtensionExceptionType &exception, const std::string& reason )
1450
 
{
1451
 
    PyErr_SetString (exception.ptr(), reason.c_str());
1452
 
}
1453
 
 
1454
 
Exception::Exception( ExtensionExceptionType &exception, Object &reason )
1455
 
{
1456
 
    PyErr_SetObject (exception.ptr(), reason.ptr());
1457
 
}
1458
 
 
1459
 
Exception::Exception( PyObject* exception, Object &reason )
1460
 
{
1461
 
    PyErr_SetObject (exception, reason.ptr());
1462
 
}        
1463
 
 
1464
 
}    // end of namespace Py