24.4. A CPython Extension containing C++ Objects¶
Here is an example of using C++ classes within a CPython extension type (Python object). It shows the various stages of construction and destruction.
Only the important code is shown here. The complete code is in src/cpy/cpp/placement_new.cpp and the tests are
in tests/unit/test_c_cpp.py
24.4.1. Allocation of C++ Objects and Placement new¶
In src/cpy/cpp/placement_new.cpp there is a C++ class Verbose which:
Reports on
stdoutconstruction and destruction events.Allocates an in-memory buffer of 256MB so that the memory usage, and any leaks, will show up in the process RSS.
We are going to create a Python extension that has a Python class that contains the C++ Verbose objects in two
ways:
Directly.
With a dynamically allocated pointer.
This will illustrate the different techniques needed for both. Here is the CPython structure:
typedef struct {
PyObject_HEAD
Verbose Attr;
Verbose *pAttr;
} CppCtorDtorInPyObject;
Here is the function to allocate a new CPython object. The important point here is that the line below:
self = (CppCtorDtorInPyObject *) type->tp_alloc(type, 0);
Allocates sufficient, uninitialised, space for the CppCtorDtorInPyObject object.
This will mean that both the Verbose Attr; and Verbose *pAttr; are uninitialised.
To initialise them two different techniques must be used:
For
Verbose Attr;this must be initialised with placement newnew(&self->Attr) Verbose;.For
Verbose *pAttr;this must be initialised with a dynamic new:self->pAttr = new Verbose("pAttr");.
Here is the complete code:
1static PyObject *
2CppCtorDtorInPyObject_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds)) {
3 printf("-- %s()\n", __FUNCTION__);
4 CppCtorDtorInPyObject *self;
5 self = (CppCtorDtorInPyObject *) type->tp_alloc(type, 0);
6 if (self != NULL) {
7 // Placement new used for direct allocation.
8 new(&self->Attr) Verbose;
9 self->Attr.print("Initial self->Attr");
10 // Dynamically allocated new.
11 self->pAttr = new Verbose("pAttr");
12 if (self->pAttr == NULL) {
13 Py_DECREF(self);
14 return NULL;
15 } else {
16 self->pAttr->print("Initial self->pAttr");
17 }
18 }
19 return (PyObject *) self;
20}
The complimentary de-allocation function uses different deletion techniques for the two objects.
1static void
2CppCtorDtorInPyObject_dealloc(CppCtorDtorInPyObject *self) {
3 printf("-- %s()\n", __FUNCTION__);
4 self->Attr.print("self->Attr before delete");
5 // For self->Attr call the destructor directly.
6 self->Attr.~Verbose();
7 self->pAttr->print("self->pAttr before delete");
8 // For self->pAttr use delete.
9 delete self->pAttr;
10 Py_TYPE(self)->tp_free((PyObject *) self);
11}
The C++ Verbose class writes to stdout the stages of construction and deletion, typically the output is:
1RSS start: 35,586,048
2-- CppCtorDtorInPyObject_new()
3Constructor at 0x102afa840 with argument "Default" buffer len: 268435456
4Default constructor at 0x102afa840 with argument "Default"
5Initial self->Attr: Verbose object at 0x102afa840 m_str: "Default"
6Constructor at 0x600003158000 with argument "pAttr" buffer len: 268435456
7Initial self->pAttr: Verbose object at 0x600003158000 m_str: "pAttr"
8-- CppCtorDtorInPyObject_buffer_size()
9Buffer size: 536,871,116
10 RSS new: 572,506,112 +536,920,064
11-- CppCtorDtorInPyObject_dealloc()
12self->Attr before delete: Verbose object at 0x102afa840 m_str: "Default"
13Destructor at 0x102afa840 m_str: "Default"
14self->pAttr before delete: Verbose object at 0x600003158000 m_str: "pAttr"
15Destructor at 0x600003158000 m_str: "pAttr"
16 RSS del: 35,602,432 +16,384
17 RSS end: 35,602,432 +16,384