1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
/*-----------------------------------------------------------------------------
| Copyright (c) 2013-2017, Nucleic Development Team.
|
| Distributed under the terms of the Modified BSD License.
|
| The full license is in the file COPYING.txt, distributed with this software.
|----------------------------------------------------------------------------*/
#pragma once
#include <Python.h>
#include <structmember.h>
#include <string>
#if PY_MAJOR_VERSION >= 3
#define FROM_STRING PyUnicode_FromString
#define INITERROR return NULL
#define MOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name(void)
#else
#define FROM_STRING PyString_FromString
#define INITERROR return
#define MOD_INIT_FUNC(name) PyMODINIT_FUNC init##name(void)
#endif
#ifndef Py_RETURN_NOTIMPLEMENTED
#define Py_RETURN_NOTIMPLEMENTED \
return Py_INCREF(Py_NotImplemented), Py_NotImplemented
#endif
#define pyobject_cast( o ) ( reinterpret_cast<PyObject*>( o ) )
#define pytype_cast( o ) ( reinterpret_cast<PyTypeObject*>( o ) )
struct module_state {
PyObject *error;
};
namespace PythonHelpers
{
/*-----------------------------------------------------------------------------
| Exception Handling
|----------------------------------------------------------------------------*/
inline PyObject*
py_bad_internal_call( const char* message )
{
PyErr_SetString( PyExc_SystemError, message );
return 0;
}
inline PyObject*
py_type_fail( const char* message )
{
PyErr_SetString( PyExc_TypeError, message );
return 0;
}
inline PyObject*
py_expected_type_fail( PyObject* pyobj, const char* expected_type )
{
PyErr_Format(
PyExc_TypeError,
"Expected object of type `%s`. Got object of type `%s` instead.",
expected_type, pyobj->ob_type->tp_name
);
return 0;
}
inline PyObject*
py_value_fail( const char* message )
{
PyErr_SetString( PyExc_ValueError, message );
return 0;
}
inline PyObject*
py_runtime_fail( const char* message )
{
PyErr_SetString( PyExc_RuntimeError, message );
return 0;
}
inline PyObject*
py_attr_fail( const char* message )
{
PyErr_SetString( PyExc_AttributeError, message );
return 0;
}
inline PyObject*
py_no_attr_fail( PyObject* pyobj, const char* attr )
{
PyErr_Format(
PyExc_AttributeError,
"'%s' object has no attribute '%s'",
pyobj->ob_type->tp_name, attr
);
return 0;
}
/*-----------------------------------------------------------------------------
| Utilities
|----------------------------------------------------------------------------*/
inline PyObject*
newref( PyObject* pyobj )
{
Py_INCREF( pyobj );
return pyobj;
}
inline PyObject*
xnewref( PyObject* pyobj )
{
Py_XINCREF( pyobj );
return pyobj;
}
inline PyObject*
py_bool( bool val )
{
return newref( val ? Py_True : Py_False );
}
inline PyCFunction
lookup_method( PyTypeObject* type, const char* name )
{
PyMethodDef* method = type->tp_methods;
for( ; method->ml_name != 0; ++method )
{
if( strcmp( method->ml_name, name ) == 0 )
return method->ml_meth;
}
return 0;
}
/*-----------------------------------------------------------------------------
| Object Ptr
|----------------------------------------------------------------------------*/
class PyObjectPtr {
public:
PyObjectPtr() : m_pyobj( 0 ) {}
PyObjectPtr( const PyObjectPtr& objptr ) :
m_pyobj( PythonHelpers::xnewref( objptr.m_pyobj ) ) {}
PyObjectPtr( PyObject* pyobj ) : m_pyobj( pyobj ) {}
~PyObjectPtr()
{
xdecref_release();
}
PyObject* get() const
{
return m_pyobj;
}
void set( PyObject* pyobj )
{
PyObject* old = m_pyobj;
m_pyobj = pyobj;
Py_XDECREF( old );
}
PyObject* release()
{
PyObject* pyobj = m_pyobj;
m_pyobj = 0;
return pyobj;
}
PyObject* decref_release()
{
PyObject* pyobj = m_pyobj;
m_pyobj = 0;
Py_DECREF( pyobj );
return pyobj;
}
PyObject* xdecref_release()
{
PyObject* pyobj = m_pyobj;
m_pyobj = 0;
Py_XDECREF( pyobj );
return pyobj;
}
PyObject* incref_release()
{
PyObject* pyobj = m_pyobj;
m_pyobj = 0;
Py_INCREF( pyobj );
return pyobj;
}
PyObject* xincref_release()
{
PyObject* pyobj = m_pyobj;
m_pyobj = 0;
Py_XINCREF( pyobj );
return pyobj;
}
void incref() const
{
Py_INCREF( m_pyobj );
}
void decref() const
{
Py_DECREF( m_pyobj );
}
void xincref() const
{
Py_XINCREF( m_pyobj );
}
void xdecref() const
{
Py_XDECREF( m_pyobj );
}
PyObject* newref() const
{
Py_INCREF( m_pyobj );
return m_pyobj;
}
PyObject* xnewref() const
{
Py_XINCREF( m_pyobj );
return m_pyobj;
}
size_t refcount() const
{
if( m_pyobj )
return m_pyobj->ob_refcnt;
return 0;
}
bool is_true( bool clear_err=true ) const
{
int truth = PyObject_IsTrue( m_pyobj );
if( truth == 1 )
return true;
if( truth == 0 )
return false;
if( clear_err )
PyErr_Clear();
return false;
}
bool is_None() const
{
return m_pyobj == Py_None;
}
bool is_True() const
{
return m_pyobj == Py_True;
}
bool is_False() const
{
return m_pyobj == Py_False;
}
bool load_dict( PyObjectPtr& out, bool forcecreate=false )
{
PyObject** dict = _PyObject_GetDictPtr( m_pyobj );
if( !dict )
return false;
if( forcecreate && !*dict )
*dict = PyDict_New();
out = PythonHelpers::xnewref( *dict );
return true;
}
bool richcompare( PyObject* other, int opid, bool clear_err=true )
{
int r = PyObject_RichCompareBool( m_pyobj, other, opid );
if( r == 1 )
return true;
if( r == 0 )
return false;
if( clear_err && PyErr_Occurred() )
PyErr_Clear();
return false;
}
bool richcompare( PyObjectPtr& other, int opid, bool clear_err=true )
{
return richcompare( other.m_pyobj, opid, clear_err );
}
bool hasattr( PyObject* attr )
{
return PyObject_HasAttr( m_pyobj, attr ) == 1;
}
bool hasattr( PyObjectPtr& attr )
{
return PyObject_HasAttr( m_pyobj, attr.get() ) == 1;
}
bool hasattr( const char* attr )
{
return PyObject_HasAttrString( m_pyobj, attr ) == 1;
}
bool hasattr( std::string& attr )
{
return hasattr( attr.c_str() );
}
PyObjectPtr getattr( PyObject* attr )
{
return PyObjectPtr( PyObject_GetAttr( m_pyobj, attr ) );
}
PyObjectPtr getattr( PyObjectPtr& attr )
{
return PyObjectPtr( PyObject_GetAttr( m_pyobj, attr.get() ) );
}
PyObjectPtr getattr( const char* attr )
{
return PyObjectPtr( PyObject_GetAttrString( m_pyobj, attr ) );
}
PyObjectPtr getattr( std::string& attr )
{
return getattr( attr.c_str() );
}
bool setattr( PyObject* attr, PyObject* value )
{
return PyObject_SetAttr( m_pyobj, attr, value ) == 0;
}
bool setattr( PyObjectPtr& attr, PyObjectPtr& value )
{
return PyObject_SetAttr( m_pyobj, attr.get(), value.get() ) == 0;
}
PyObjectPtr operator()( PyObjectPtr& args ) const
{
return PyObjectPtr( PyObject_Call( m_pyobj, args.get(), 0 ) );
}
PyObjectPtr operator()( PyObjectPtr& args, PyObjectPtr& kwargs ) const
{
return PyObjectPtr( PyObject_Call( m_pyobj, args.get(), kwargs.get() ) );
}
operator void*() const
{
return static_cast<void*>( m_pyobj );
}
PyObjectPtr& operator=( const PyObjectPtr& rhs )
{
PyObject* old = m_pyobj;
m_pyobj = rhs.m_pyobj;
Py_XINCREF( m_pyobj );
Py_XDECREF( old );
return *this;
}
PyObjectPtr& operator=( PyObject* rhs )
{
PyObject* old = m_pyobj;
m_pyobj = rhs;
Py_XDECREF( old );
return *this;
}
protected:
PyObject* m_pyobj;
};
inline bool
operator!=( const PyObjectPtr& lhs, const PyObjectPtr& rhs )
{
return lhs.get() != rhs.get();
}
inline bool
operator!=( const PyObject* lhs, const PyObjectPtr& rhs )
{
return lhs != rhs.get();
}
inline bool
operator!=( const PyObjectPtr& lhs, const PyObject* rhs )
{
return lhs.get() != rhs;
}
inline bool
operator==( const PyObjectPtr& lhs, const PyObjectPtr& rhs )
{
return lhs.get() == rhs.get();
}
inline bool
operator==( const PyObject* lhs, const PyObjectPtr& rhs )
{
return lhs == rhs.get();
}
inline bool
operator==( const PyObjectPtr& lhs, const PyObject* rhs )
{
return lhs.get() == rhs;
}
/*-----------------------------------------------------------------------------
| Tuple Ptr
|----------------------------------------------------------------------------*/
class PyTuplePtr : public PyObjectPtr {
public:
PyTuplePtr() : PyObjectPtr() {}
PyTuplePtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {}
PyTuplePtr( PyObject* pytuple ) : PyObjectPtr( pytuple ) {}
bool check()
{
return PyTuple_Check( m_pyobj );
}
bool check_exact()
{
return PyTuple_CheckExact( m_pyobj );
}
Py_ssize_t size() const
{
return PyTuple_GET_SIZE( m_pyobj );
}
PyObjectPtr get_item( Py_ssize_t index ) const
{
return PyObjectPtr( PythonHelpers::newref( PyTuple_GET_ITEM( m_pyobj, index ) ) );
}
void set_item( Py_ssize_t index, PyObject* pyobj )
{
PyObject* old_item = PyTuple_GET_ITEM( m_pyobj, index );
PyTuple_SET_ITEM( m_pyobj, index, pyobj );
Py_XDECREF( old_item );
}
void set_item( Py_ssize_t index, PyObjectPtr& item )
{
PyObject* old_item = PyTuple_GET_ITEM( m_pyobj, index );
PyTuple_SET_ITEM( m_pyobj, index, item.get() );
Py_XINCREF( item.get() );
Py_XDECREF( old_item );
}
// pyobj must not be null, only use to fill a new empty tuple
void initialize( Py_ssize_t index, PyObject* pyobj )
{
PyTuple_SET_ITEM( m_pyobj, index, pyobj );
}
// ptr must not be empty, only use to fill a new empty tuple
void initialize( Py_ssize_t index, PyObjectPtr& item )
{
PyTuple_SET_ITEM( m_pyobj, index, item.get() );
Py_INCREF( item.get() );
}
};
/*-----------------------------------------------------------------------------
| List Ptr
|----------------------------------------------------------------------------*/
class PyListPtr : public PyObjectPtr {
public:
PyListPtr() : PyObjectPtr() {}
PyListPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {}
PyListPtr( PyObject* pylist ) : PyObjectPtr( pylist ) {}
bool check() const
{
return PyList_Check( m_pyobj );
}
bool check_exact() const
{
return PyList_CheckExact( m_pyobj );
}
Py_ssize_t size() const
{
return PyList_GET_SIZE( m_pyobj );
}
PyObject* borrow_item( Py_ssize_t index ) const
{
return PyList_GET_ITEM( m_pyobj, index );
}
PyObjectPtr get_item( Py_ssize_t index ) const
{
return PyObjectPtr( PythonHelpers::newref( PyList_GET_ITEM( m_pyobj, index ) ) );
}
void set_item( Py_ssize_t index, PyObject* pyobj ) const
{
PyObject* old_item = PyList_GET_ITEM( m_pyobj, index );
PyList_SET_ITEM( m_pyobj, index, pyobj );
Py_XDECREF( old_item );
}
void set_item( Py_ssize_t index, PyObjectPtr& item ) const
{
PyObject* old_item = PyList_GET_ITEM( m_pyobj, index );
PyList_SET_ITEM( m_pyobj, index, item.get() );
Py_XINCREF( item.get() );
Py_XDECREF( old_item );
}
bool del_item( Py_ssize_t index ) const
{
if( PySequence_DelItem( m_pyobj, index ) == -1 )
return false;
return true;
}
bool append( PyObjectPtr& pyobj ) const
{
if( PyList_Append( m_pyobj, pyobj.get() ) == 0 )
return true;
return false;
}
Py_ssize_t index( PyObjectPtr& item ) const
{
Py_ssize_t maxidx = size();
for( Py_ssize_t idx = 0; idx < maxidx; idx++ )
{
PyObjectPtr other( get_item( idx ) );
if( item.richcompare( other, Py_EQ ) )
return idx;
}
return -1;
}
};
/*-----------------------------------------------------------------------------
| Dict Ptr
|----------------------------------------------------------------------------*/
class PyDictPtr : public PyObjectPtr {
public:
PyDictPtr() : PyObjectPtr() {}
PyDictPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {}
PyDictPtr( PyObject* pydict ) : PyObjectPtr( pydict ) {}
bool check()
{
return PyDict_Check( m_pyobj );
}
bool check_exact()
{
return PyDict_CheckExact( m_pyobj );
}
Py_ssize_t size() const
{
return PyDict_Size( m_pyobj );
}
PyObjectPtr get_item( PyObject* key ) const
{
return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItem( m_pyobj, key ) ) ) ;
}
PyObjectPtr get_item( PyObjectPtr& key ) const
{
return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItem( m_pyobj, key.get() ) ) );
}
PyObjectPtr get_item( const char* key ) const
{
return PyObjectPtr( PythonHelpers::xnewref( PyDict_GetItemString( m_pyobj, key ) ) );
}
PyObjectPtr get_item( std::string& key ) const
{
return get_item( key.c_str() );
}
bool set_item( PyObject* key, PyObject* value ) const
{
if( PyDict_SetItem( m_pyobj, key, value ) == 0 )
return true;
return false;
}
bool set_item( PyObject* key, PyObjectPtr& value ) const
{
if( PyDict_SetItem( m_pyobj, key, value.get() ) == 0 )
return true;
return false;
}
bool set_item( PyObjectPtr& key, PyObject* value ) const
{
if( PyDict_SetItem( m_pyobj, key.get(), value ) == 0 )
return true;
return false;
}
bool set_item( PyObjectPtr& key, PyObjectPtr& value ) const
{
if( PyDict_SetItem( m_pyobj, key.get(), value.get() ) == 0 )
return true;
return false;
}
bool set_item( const char* key, PyObjectPtr& value ) const
{
if( PyDict_SetItemString( m_pyobj, key, value.get() ) == 0 )
return true;
return false;
}
bool set_item( const char* key, PyObject* value ) const
{
if( PyDict_SetItemString( m_pyobj, key, value ) == 0 )
return true;
return false;
}
bool set_item( std::string& key, PyObjectPtr& value ) const
{
return set_item( key.c_str(), value );
}
bool del_item( PyObjectPtr& key ) const
{
if( PyDict_DelItem( m_pyobj, key.get() ) == 0 )
return true;
return false;
}
bool del_item( const char* key ) const
{
if( PyDict_DelItemString( m_pyobj, key ) == 0 )
return true;
return false;
}
bool del_item( std::string& key ) const
{
return del_item( key.c_str() );
}
};
/*-----------------------------------------------------------------------------
| Method Ptr
|----------------------------------------------------------------------------*/
class PyMethodPtr : public PyObjectPtr {
public:
PyMethodPtr() : PyObjectPtr() {}
PyMethodPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {}
PyMethodPtr( PyObject* pymethod ) : PyObjectPtr( pymethod ) {}
bool check()
{
return PyMethod_Check( m_pyobj );
}
PyObjectPtr get_self() const
{
return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_SELF( m_pyobj ) ) );
}
PyObjectPtr get_function() const
{
return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_FUNCTION( m_pyobj ) ) );
}
#if PY_MAJOR_VERSION < 3
PyObjectPtr get_class() const
{
return PyObjectPtr( PythonHelpers::xnewref( PyMethod_GET_CLASS( m_pyobj ) ) );
}
#endif
};
/*-----------------------------------------------------------------------------
| Weakref Ptr
|----------------------------------------------------------------------------*/
class PyWeakrefPtr : public PyObjectPtr {
public:
PyWeakrefPtr() : PyObjectPtr() {}
PyWeakrefPtr( const PyObjectPtr& objptr ) : PyObjectPtr( objptr ) {}
PyWeakrefPtr( PyObject* pyweakref ) : PyObjectPtr( pyweakref ) {}
bool check()
{
return PyWeakref_CheckRef( m_pyobj );
}
bool check_exact()
{
return PyWeakref_CheckRefExact( m_pyobj );
}
PyObjectPtr get_object() const
{
return PyObjectPtr( PythonHelpers::newref( PyWeakref_GET_OBJECT( m_pyobj ) ) );
}
};
} // namespace PythonHelpers
|