Instance creation function for all pybind11 types. It allocates the internal instance layout for holding C++ objects and holders. Allocation is done lazily (the first time the instance is cast to a reference or pointer), and initialization is done by an `__init__` function.
| 385 | /// for holding C++ objects and holders. Allocation is done lazily (the first time the instance is |
| 386 | /// cast to a reference or pointer), and initialization is done by an `__init__` function. |
| 387 | inline PyObject *make_new_instance(PyTypeObject *type) { |
| 388 | #if defined(PYPY_VERSION) |
| 389 | // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first |
| 390 | // inherited object is a plain Python type (i.e. not derived from an extension type). Fix it. |
| 391 | ssize_t instance_size = static_cast<ssize_t>(sizeof(instance)); |
| 392 | if (type->tp_basicsize < instance_size) { |
| 393 | type->tp_basicsize = instance_size; |
| 394 | } |
| 395 | #endif |
| 396 | PyObject *self = type->tp_alloc(type, 0); |
| 397 | auto *inst = reinterpret_cast<instance *>(self); |
| 398 | // Allocate the value/holder internals: |
| 399 | inst->allocate_layout(); |
| 400 | |
| 401 | return self; |
| 402 | } |
| 403 | |
| 404 | /// Instance creation function for all pybind11 types. It only allocates space for the |
| 405 | /// C++ object, but doesn't call the constructor -- an `__init__` function must do that. |
no test coverage detected