-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyhandtest_wrap.cpp
More file actions
616 lines (515 loc) · 17.7 KB
/
byhandtest_wrap.cpp
File metadata and controls
616 lines (515 loc) · 17.7 KB
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
/* This file was generated by PyBindGen 0.16.0 */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stddef.h>
#if PY_VERSION_HEX < 0x020400F0
#define PyEval_ThreadsInitialized() 1
#define Py_CLEAR(op) \
do { \
if (op) { \
PyObject *tmp = (PyObject *)(op); \
(op) = NULL; \
Py_DECREF(tmp); \
} \
} while (0)
#define Py_VISIT(op) \
do { \
if (op) { \
int vret = visit((PyObject *)(op), arg); \
if (vret) \
return vret; \
} \
} while (0)
#endif
#if PY_VERSION_HEX < 0x020500F0
typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
typedef inquiry lenfunc;
typedef intargfunc ssizeargfunc;
typedef intobjargproc ssizeobjargproc;
#endif
#if __GNUC__ > 2
# define PYBINDGEN_UNUSED(param) param __attribute__((__unused__))
#elif __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
# define PYBINDGEN_UNUSED(param) __attribute__((__unused__)) param
#else
# define PYBINDGEN_UNUSED(param) param
#endif /* !__GNUC__ */
typedef enum _PyBindGenWrapperFlags {
PYBINDGEN_WRAPPER_FLAG_NONE = 0,
PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED = (1<<0),
} PyBindGenWrapperFlags;
#include "autogentest.h"
#include <typeinfo>
/* --- forward declarations --- */
typedef struct {
PyObject_HEAD
A *obj;
PyObject *inst_dict;
PyBindGenWrapperFlags flags:8;
} PyA;
extern PyTypeObject PyA_Type;
class PyA__PythonHelper : public A
{
public:
PyObject *m_pyself;
PyA__PythonHelper()
: A(), m_pyself(NULL)
{}
void set_pyobj(PyObject *pyobj)
{
Py_XDECREF(m_pyself);
Py_INCREF(pyobj);
m_pyself = pyobj;
}
virtual ~PyA__PythonHelper()
{
Py_CLEAR(m_pyself);
}
virtual void virtualMethod1();
virtual void virtualMethod2();
};
#include <map>
#include <string>
#include <typeinfo>
#if defined(__GNUC__) && __GNUC__ >= 3
# include <cxxabi.h>
#endif
#define PBG_TYPEMAP_DEBUG 0
namespace pybindgen {
class TypeMap
{
std::map<std::string, PyTypeObject *> m_map;
public:
TypeMap() {}
void register_wrapper(const std::type_info &cpp_type_info, PyTypeObject *python_wrapper)
{
#if PBG_TYPEMAP_DEBUG
std::cerr << "register_wrapper(this=" << this << ", type_name=" << cpp_type_info.name()
<< ", python_wrapper=" << python_wrapper->tp_name << ")" << std::endl;
#endif
m_map[std::string(cpp_type_info.name())] = python_wrapper;
}
PyTypeObject * lookup_wrapper(const std::type_info &cpp_type_info, PyTypeObject *fallback_wrapper)
{
#if PBG_TYPEMAP_DEBUG
std::cerr << "lookup_wrapper(this=" << this << ", type_name=" << cpp_type_info.name() << ")" << std::endl;
#endif
PyTypeObject *python_wrapper = m_map[cpp_type_info.name()];
if (python_wrapper)
return python_wrapper;
else {
#if defined(__GNUC__) && __GNUC__ >= 3
// Get closest (in the single inheritance tree provided by cxxabi.h)
// registered python wrapper.
const abi::__si_class_type_info *_typeinfo =
dynamic_cast<const abi::__si_class_type_info*> (&cpp_type_info);
#if PBG_TYPEMAP_DEBUG
std::cerr << " -> looking at C++ type " << _typeinfo->name() << std::endl;
#endif
while (_typeinfo && (python_wrapper = m_map[std::string(_typeinfo->name())]) == 0) {
_typeinfo = dynamic_cast<const abi::__si_class_type_info*> (_typeinfo->__base_type);
#if PBG_TYPEMAP_DEBUG
std::cerr << " -> looking at C++ type " << _typeinfo->name() << std::endl;
#endif
}
#if PBG_TYPEMAP_DEBUG
if (python_wrapper) {
std::cerr << " -> found match " << std::endl;
} else {
std::cerr << " -> return fallback wrapper" << std::endl;
}
#endif
return python_wrapper? python_wrapper : fallback_wrapper;
#else // non gcc 3+ compilers can only match against explicitly registered classes, not hidden subclasses
return fallback_wrapper;
#endif
}
}
};
}
extern pybindgen::TypeMap PyA__typeid_map;
typedef struct {
PyObject_HEAD
B *obj;
PyObject *inst_dict;
PyBindGenWrapperFlags flags:8;
} PyB;
extern PyTypeObject PyB_Type;
static PyMethodDef byhandtest_functions[] = {
{NULL, NULL, 0, NULL}
};
/* --- classes --- */
pybindgen::TypeMap PyA__typeid_map;
void
PyA__PythonHelper::virtualMethod1()
{
PyGILState_STATE __py_gil_state;
PyObject *py_method;
A *self_obj_before;
PyObject *py_retval;
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0);
py_method = PyObject_GetAttrString(m_pyself, (char *) "virtualMethod1"); PyErr_Clear();
if (py_method == NULL || py_method->ob_type == &PyCFunction_Type) {
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
self_obj_before = reinterpret_cast< PyA* >(m_pyself)->obj;
reinterpret_cast< PyA* >(m_pyself)->obj = (A*) this;
py_retval = PyObject_CallMethod(m_pyself, (char *) "virtualMethod1", (char *) "");
if (py_retval == NULL) {
PyErr_Print();
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
if (py_retval != Py_None) {
PyErr_SetString(PyExc_TypeError, "function/method should return None");
Py_DECREF(py_retval);
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
Py_DECREF(py_retval);
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
void
PyA__PythonHelper::virtualMethod2()
{
PyGILState_STATE __py_gil_state;
PyObject *py_method;
A *self_obj_before;
PyObject *py_retval;
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0);
py_method = PyObject_GetAttrString(m_pyself, (char *) "virtualMethod2"); PyErr_Clear();
if (py_method == NULL || py_method->ob_type == &PyCFunction_Type) {
A::virtualMethod2();
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
self_obj_before = reinterpret_cast< PyA* >(m_pyself)->obj;
reinterpret_cast< PyA* >(m_pyself)->obj = (A*) this;
py_retval = PyObject_CallMethod(m_pyself, (char *) "virtualMethod2", (char *) "");
if (py_retval == NULL) {
PyErr_Print();
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
if (py_retval != Py_None) {
PyErr_SetString(PyExc_TypeError, "function/method should return None");
Py_DECREF(py_retval);
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
Py_DECREF(py_retval);
reinterpret_cast< PyA* >(m_pyself)->obj = self_obj_before;
Py_XDECREF(py_method);
if (PyEval_ThreadsInitialized())
PyGILState_Release(__py_gil_state);
return;
}
static int
_wrap_PyA__tp_init(PyA *self, PyObject *args, PyObject *kwargs)
{
const char *keywords[] = {NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "", (char **) keywords)) {
return -1;
}
if (self->ob_type != &PyA_Type)
{
self->obj = new PyA__PythonHelper();
self->flags = PYBINDGEN_WRAPPER_FLAG_NONE;
((PyA__PythonHelper*) self->obj)->set_pyobj((PyObject *)self);
} else {
// visibility: 'public'
PyErr_SetString(PyExc_TypeError, "class 'A' cannot be constructed");
return -1;
}
return 0;
}
PyObject *
_wrap_PyA_virtualMethod1(PyA *self)
{
PyObject *py_retval;
self->obj->virtualMethod1();
Py_INCREF(Py_None);
py_retval = Py_None;
return py_retval;
}
PyObject *
_wrap_PyA_virtualMethod2(PyA *self)
{
PyObject *py_retval;
PyA__PythonHelper *helper_class = dynamic_cast<PyA__PythonHelper*> (self->obj);
(helper_class == NULL)? (self->obj->virtualMethod2()) : (self->obj->A::virtualMethod2());
Py_INCREF(Py_None);
py_retval = Py_None;
return py_retval;
}
static PyMethodDef PyA_methods[] = {
{(char *) "virtualMethod1", (PyCFunction) _wrap_PyA_virtualMethod1, METH_NOARGS, NULL },
{(char *) "virtualMethod2", (PyCFunction) _wrap_PyA_virtualMethod2, METH_NOARGS, NULL },
{NULL, NULL, 0, NULL}
};
static void
PyA__tp_clear(PyA *self)
{
Py_CLEAR(self->inst_dict);
A *tmp = self->obj;
self->obj = NULL;
if (!(self->flags&PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED)) {
delete tmp;
}
}
static int
PyA__tp_traverse(PyA *self, visitproc visit, void *arg)
{
Py_VISIT(self->inst_dict);
if (self->obj && typeid(*self->obj).name() == typeid(PyA__PythonHelper).name() )
Py_VISIT((PyObject *) self);
return 0;
}
static void
_wrap_PyA__tp_dealloc(PyA *self)
{
PyA__tp_clear(self);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject*
_wrap_PyA__tp_richcompare (PyA *PYBINDGEN_UNUSED(self), PyA *other, int opid)
{
if (!PyObject_IsInstance((PyObject*) other, (PyObject*) &PyA_Type)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
switch (opid)
{
case Py_LT:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_LE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_EQ:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_NE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_GE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_GT:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
} /* closes switch (opid) */
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyTypeObject PyA_Type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
(char *) "byhandtest.A", /* tp_name */
sizeof(PyA), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)_wrap_PyA__tp_dealloc, /* tp_dealloc */
(printfunc)0, /* tp_print */
(getattrfunc)NULL, /* tp_getattr */
(setattrfunc)NULL, /* tp_setattr */
(cmpfunc)NULL, /* tp_compare */
(reprfunc)NULL, /* tp_repr */
(PyNumberMethods*)NULL, /* tp_as_number */
(PySequenceMethods*)NULL, /* tp_as_sequence */
(PyMappingMethods*)NULL, /* tp_as_mapping */
(hashfunc)NULL, /* tp_hash */
(ternaryfunc)NULL, /* tp_call */
(reprfunc)NULL, /* tp_str */
(getattrofunc)NULL, /* tp_getattro */
(setattrofunc)NULL, /* tp_setattro */
(PyBufferProcs*)NULL, /* tp_as_buffer */
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC, /* tp_flags */
NULL, /* Documentation string */
(traverseproc)PyA__tp_traverse, /* tp_traverse */
(inquiry)PyA__tp_clear, /* tp_clear */
(richcmpfunc)_wrap_PyA__tp_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)NULL, /* tp_iter */
(iternextfunc)NULL, /* tp_iternext */
(struct PyMethodDef*)PyA_methods, /* tp_methods */
(struct PyMemberDef*)0, /* tp_members */
0, /* tp_getset */
NULL, /* tp_base */
NULL, /* tp_dict */
(descrgetfunc)NULL, /* tp_descr_get */
(descrsetfunc)NULL, /* tp_descr_set */
offsetof(PyA, inst_dict), /* tp_dictoffset */
(initproc)_wrap_PyA__tp_init, /* tp_init */
(allocfunc)PyType_GenericAlloc, /* tp_alloc */
(newfunc)PyType_GenericNew, /* tp_new */
(freefunc)0, /* tp_free */
(inquiry)NULL, /* tp_is_gc */
NULL, /* tp_bases */
NULL, /* tp_mro */
NULL, /* tp_cache */
NULL, /* tp_subclasses */
NULL, /* tp_weaklist */
(destructor) NULL /* tp_del */
};
static int
_wrap_PyB__tp_init(void)
{
PyErr_SetString(PyExc_TypeError, "class 'B' cannot be constructed (have pure virtual methods but no helper class)");
return -1;
}
static PyMethodDef PyB_methods[] = {
{NULL, NULL, 0, NULL}
};
static void
PyB__tp_clear(PyB *self)
{
Py_CLEAR(self->inst_dict);
B *tmp = self->obj;
self->obj = NULL;
if (!(self->flags&PYBINDGEN_WRAPPER_FLAG_OBJECT_NOT_OWNED)) {
delete tmp;
}
}
static int
PyB__tp_traverse(PyB *self, visitproc visit, void *arg)
{
Py_VISIT(self->inst_dict);
return 0;
}
static void
_wrap_PyB__tp_dealloc(PyB *self)
{
PyB__tp_clear(self);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject*
_wrap_PyB__tp_richcompare (PyB *PYBINDGEN_UNUSED(self), PyB *other, int opid)
{
if (!PyObject_IsInstance((PyObject*) other, (PyObject*) &PyB_Type)) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
switch (opid)
{
case Py_LT:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_LE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_EQ:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_NE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_GE:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
case Py_GT:
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
} /* closes switch (opid) */
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
PyTypeObject PyB_Type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
(char *) "byhandtest.B", /* tp_name */
sizeof(PyB), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)_wrap_PyB__tp_dealloc, /* tp_dealloc */
(printfunc)0, /* tp_print */
(getattrfunc)NULL, /* tp_getattr */
(setattrfunc)NULL, /* tp_setattr */
(cmpfunc)NULL, /* tp_compare */
(reprfunc)NULL, /* tp_repr */
(PyNumberMethods*)NULL, /* tp_as_number */
(PySequenceMethods*)NULL, /* tp_as_sequence */
(PyMappingMethods*)NULL, /* tp_as_mapping */
(hashfunc)NULL, /* tp_hash */
(ternaryfunc)NULL, /* tp_call */
(reprfunc)NULL, /* tp_str */
(getattrofunc)NULL, /* tp_getattro */
(setattrofunc)NULL, /* tp_setattro */
(PyBufferProcs*)NULL, /* tp_as_buffer */
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_GC, /* tp_flags */
NULL, /* Documentation string */
(traverseproc)PyB__tp_traverse, /* tp_traverse */
(inquiry)PyB__tp_clear, /* tp_clear */
(richcmpfunc)_wrap_PyB__tp_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)NULL, /* tp_iter */
(iternextfunc)NULL, /* tp_iternext */
(struct PyMethodDef*)PyB_methods, /* tp_methods */
(struct PyMemberDef*)0, /* tp_members */
0, /* tp_getset */
NULL, /* tp_base */
NULL, /* tp_dict */
(descrgetfunc)NULL, /* tp_descr_get */
(descrsetfunc)NULL, /* tp_descr_set */
offsetof(PyB, inst_dict), /* tp_dictoffset */
(initproc)_wrap_PyB__tp_init, /* tp_init */
(allocfunc)PyType_GenericAlloc, /* tp_alloc */
(newfunc)PyType_GenericNew, /* tp_new */
(freefunc)0, /* tp_free */
(inquiry)NULL, /* tp_is_gc */
NULL, /* tp_bases */
NULL, /* tp_mro */
NULL, /* tp_cache */
NULL, /* tp_subclasses */
NULL, /* tp_weaklist */
(destructor) NULL /* tp_del */
};
PyMODINIT_FUNC
#if defined(__GNUC__) && __GNUC__ >= 4
__attribute__ ((visibility("default")))
#endif
initbyhandtest(void)
{
PyObject *m;
m = Py_InitModule3((char *) "byhandtest", byhandtest_functions, NULL);
if (m == NULL) {
return;
}
PyModule_AddObject(m, (char *) "_PyA__typeid_map", PyCObject_FromVoidPtr(&PyA__typeid_map, NULL));
PyA__typeid_map.register_wrapper(typeid(A), &PyA_Type);
/* Register the 'A' class */
if (PyType_Ready(&PyA_Type)) {
return;
}
PyModule_AddObject(m, (char *) "A", (PyObject *) &PyA_Type);
PyA__typeid_map.register_wrapper(typeid(B), &PyB_Type);
/* Register the 'B' class */
PyB_Type.tp_base = &PyA_Type;
if (PyType_Ready(&PyB_Type)) {
return;
}
PyModule_AddObject(m, (char *) "B", (PyObject *) &PyB_Type);
}