~ubuntu-branches/debian/squeeze/inkscape/squeeze

« back to all changes in this revision

Viewing changes to src/extension/script/CXX/cxx_extensions.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-09-09 23:29:02 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080909232902-c50iujhk1w79u8e7
Tags: 0.46-2.1
* Non-maintainer upload.
* Add upstream patch fixing a crash in the open dialog
  in the zh_CN.utf8 locale. Closes: #487623.
  Thanks to Luca Bruno for the patch.

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