-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmy_class_py_type.cpp
More file actions
72 lines (57 loc) · 1.87 KB
/
my_class_py_type.cpp
File metadata and controls
72 lines (57 loc) · 1.87 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
#include <Python.h>
#include "my_class_py_type.h"
#include <exception>
PyObject *MyClass_new(PyTypeObject *type, PyObject *args, PyObject *kwds){
std::cout << "MtClass_new() called!" << std::endl;
MyClassObject *self;
self = (MyClassObject*) type->tp_alloc(type, 0);
if(self != NULL){ // -> allocation successfull
// assign initial values
self->m_value = 0;
self->m_myclass = NULL;
}
return (PyObject*) self;
}
int MyClass_init(PyObject *self, PyObject *args, PyObject *kwds){
std::cout << "MyClass_init() called!" << std::endl;
MyClassObject* m = (MyClassObject*)self;
m->m_value = 123;
m->m_myclass = (MyClass*)PyObject_Malloc(sizeof(MyClass));
if(!m->m_myclass){
PyErr_SetString(PyExc_RuntimeError, "Memory allocation failed");
return -1;
}
try {
new (m->m_myclass) MyClass();
} catch (const std::exception& ex) {
PyObject_Free(m->m_myclass);
m->m_myclass = NULL;
m->m_value = 0;
PyErr_SetString(PyExc_RuntimeError, ex.what());
return -1;
} catch(...) {
PyObject_Free(m->m_myclass);
m->m_myclass = NULL;
m->m_value = 0;
PyErr_SetString(PyExc_RuntimeError, "Initialization failed");
return -1;
}
return 0;
}
void MyClass_dealloc(MyClassObject *self){
std::cout << "MyClass_dealloc() called!" << std::endl;
PyTypeObject *tp = Py_TYPE(self);
MyClassObject* m = reinterpret_cast<MyClassObject*>(self);
if(m->m_myclass){
m->m_myclass->~MyClass();
PyObject_Free(m->m_myclass);
}
tp->tp_free(self);
Py_DECREF(tp);
};
PyObject* MyClass_addOne(PyObject *self, PyObject *args){
assert(self);
MyClassObject* _self = reinterpret_cast<MyClassObject*>(self);
unsigned long val = _self->m_myclass->addOne();
return PyLong_FromUnsignedLong(val);
}