MCPcopy Create free account
hub / github.com/pybind/pybind11 / make_new_instance

Function make_new_instance

include/pybind11/detail/class.h:387–402  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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.
387inline 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.

Callers 4

pybind11_object_newFunction · 0.85
castMethod · 0.85

Calls 1

allocate_layoutMethod · 0.80

Tested by

no test coverage detected