cppyabm  1.0.17
An agent-based library to integrate C++ and Python
class.h
Go to the documentation of this file.
1 /*
2  pybind11/detail/class.h: Python C API implementation details for py::class_
3 
4  Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #pragma once
11 
12 #include "../attr.h"
13 #include "../options.h"
14 
17 
18 #if PY_VERSION_HEX >= 0x03030000 && !defined(PYPY_VERSION)
19 # define PYBIND11_BUILTIN_QUALNAME
20 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
21 #else
22 // In pre-3.3 Python, we still set __qualname__ so that we can produce reliable function type
23 // signatures; in 3.3+ this macro expands to nothing:
24 # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj) setattr((PyObject *) obj, "__qualname__", nameobj)
25 #endif
26 
27 inline std::string get_fully_qualified_tp_name(PyTypeObject *type) {
28 #if !defined(PYPY_VERSION)
29  return type->tp_name;
30 #else
31  auto module_name = handle((PyObject *) type).attr("__module__").cast<std::string>();
32  if (module_name == PYBIND11_BUILTINS_MODULE)
33  return type->tp_name;
34  else
35  return std::move(module_name) + "." + type->tp_name;
36 #endif
37 }
38 
39 inline PyTypeObject *type_incref(PyTypeObject *type) {
40  Py_INCREF(type);
41  return type;
42 }
43 
44 #if !defined(PYPY_VERSION)
45 
46 /// `pybind11_static_property.__get__()`: Always pass the class instead of the instance.
47 extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) {
48  return PyProperty_Type.tp_descr_get(self, cls, cls);
49 }
50 
51 /// `pybind11_static_property.__set__()`: Just like the above `__get__()`.
52 extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) {
53  PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
54  return PyProperty_Type.tp_descr_set(self, cls, value);
55 }
56 
57 /** A `static_property` is the same as a `property` but the `__get__()` and `__set__()`
58  methods are modified to always use the object type instead of a concrete instance.
59  Return value: New reference. */
60 inline PyTypeObject *make_static_property_type() {
61  constexpr auto *name = "pybind11_static_property";
62  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
63 
64  /* Danger zone: from now (and until PyType_Ready), make sure to
65  issue no Python C API calls which could potentially invoke the
66  garbage collector (the GC will call type_traverse(), which will in
67  turn find the newly constructed type in an invalid state) */
68  auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
69  if (!heap_type)
70  pybind11_fail("make_static_property_type(): error allocating type!");
71 
72  heap_type->ht_name = name_obj.inc_ref().ptr();
73 #ifdef PYBIND11_BUILTIN_QUALNAME
74  heap_type->ht_qualname = name_obj.inc_ref().ptr();
75 #endif
76 
77  auto type = &heap_type->ht_type;
78  type->tp_name = name;
79  type->tp_base = type_incref(&PyProperty_Type);
80  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
81  type->tp_descr_get = pybind11_static_get;
82  type->tp_descr_set = pybind11_static_set;
83 
84  if (PyType_Ready(type) < 0)
85  pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
86 
87  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
89 
90  return type;
91 }
92 
93 #else // PYPY
94 
95 /** PyPy has some issues with the above C API, so we evaluate Python code instead.
96  This function will only be called once so performance isn't really a concern.
97  Return value: New reference. */
98 inline PyTypeObject *make_static_property_type() {
99  auto d = dict();
100  PyObject *result = PyRun_String(R"(\
101  class pybind11_static_property(property):
102  def __get__(self, obj, cls):
103  return property.__get__(self, cls, cls)
104 
105  def __set__(self, obj, value):
106  cls = obj if isinstance(obj, type) else type(obj)
107  property.__set__(self, cls, value)
108  )", Py_file_input, d.ptr(), d.ptr()
109  );
110  if (result == nullptr)
111  throw error_already_set();
112  Py_DECREF(result);
113  return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr();
114 }
115 
116 #endif // PYPY
117 
118 /** Types with static properties need to handle `Type.static_prop = x` in a specific way.
119  By default, Python replaces the `static_property` itself, but for wrapped C++ types
120  we need to call `static_property.__set__()` in order to propagate the new value to
121  the underlying C++ data structure. */
122 extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyObject* value) {
123  // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw
124  // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`).
125  PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
126 
127  // The following assignment combinations are possible:
128  // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
129  // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
130  // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
131  const auto static_prop = (PyObject *) get_internals().static_property_type;
132  const auto call_descr_set = descr && value && PyObject_IsInstance(descr, static_prop)
133  && !PyObject_IsInstance(value, static_prop);
134  if (call_descr_set) {
135  // Call `static_property.__set__()` instead of replacing the `static_property`.
136 #if !defined(PYPY_VERSION)
137  return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
138 #else
139  if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
140  Py_DECREF(result);
141  return 0;
142  } else {
143  return -1;
144  }
145 #endif
146  } else {
147  // Replace existing attribute.
148  return PyType_Type.tp_setattro(obj, name, value);
149  }
150 }
151 
152 #if PY_MAJOR_VERSION >= 3
153 /**
154  * Python 3's PyInstanceMethod_Type hides itself via its tp_descr_get, which prevents aliasing
155  * methods via cls.attr("m2") = cls.attr("m1"): instead the tp_descr_get returns a plain function,
156  * when called on a class, or a PyMethod, when called on an instance. Override that behaviour here
157  * to do a special case bypass for PyInstanceMethod_Types.
158  */
159 extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) {
160  PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
161  if (descr && PyInstanceMethod_Check(descr)) {
162  Py_INCREF(descr);
163  return descr;
164  }
165  else {
166  return PyType_Type.tp_getattro(obj, name);
167  }
168 }
169 #endif
170 
171 /// metaclass `__call__` function that is used to create all pybind11 objects.
172 extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
173 
174  // use the default metaclass call to create/initialize the object
175  PyObject *self = PyType_Type.tp_call(type, args, kwargs);
176  if (self == nullptr) {
177  return nullptr;
178  }
179 
180  // This must be a pybind11 instance
181  auto instance = reinterpret_cast<detail::instance *>(self);
182 
183  // Ensure that the base __init__ function(s) were called
184  for (const auto &vh : values_and_holders(instance)) {
185  if (!vh.holder_constructed()) {
186  PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__",
187  get_fully_qualified_tp_name(vh.type->type).c_str());
188  Py_DECREF(self);
189  return nullptr;
190  }
191  }
192 
193  return self;
194 }
195 
196 /// Cleanup the type-info for a pybind11-registered type.
197 extern "C" inline void pybind11_meta_dealloc(PyObject *obj) {
198  auto *type = (PyTypeObject *) obj;
199  auto &internals = get_internals();
200 
201  // A pybind11-registered type will:
202  // 1) be found in internals.registered_types_py
203  // 2) have exactly one associated `detail::type_info`
204  auto found_type = internals.registered_types_py.find(type);
205  if (found_type != internals.registered_types_py.end() &&
206  found_type->second.size() == 1 &&
207  found_type->second[0]->type == type) {
208 
209  auto *tinfo = found_type->second[0];
210  auto tindex = std::type_index(*tinfo->cpptype);
211  internals.direct_conversions.erase(tindex);
212 
213  if (tinfo->module_local)
214  registered_local_types_cpp().erase(tindex);
215  else
216  internals.registered_types_cpp.erase(tindex);
217  internals.registered_types_py.erase(tinfo->type);
218 
219  // Actually just `std::erase_if`, but that's only available in C++20
220  auto &cache = internals.inactive_override_cache;
221  for (auto it = cache.begin(), last = cache.end(); it != last; ) {
222  if (it->first == (PyObject *) tinfo->type)
223  it = cache.erase(it);
224  else
225  ++it;
226  }
227 
228  delete tinfo;
229  }
230 
231  PyType_Type.tp_dealloc(obj);
232 }
233 
234 /** This metaclass is assigned by default to all pybind11 types and is required in order
235  for static properties to function correctly. Users may override this using `py::metaclass`.
236  Return value: New reference. */
237 inline PyTypeObject* make_default_metaclass() {
238  constexpr auto *name = "pybind11_type";
239  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
240 
241  /* Danger zone: from now (and until PyType_Ready), make sure to
242  issue no Python C API calls which could potentially invoke the
243  garbage collector (the GC will call type_traverse(), which will in
244  turn find the newly constructed type in an invalid state) */
245  auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
246  if (!heap_type)
247  pybind11_fail("make_default_metaclass(): error allocating metaclass!");
248 
249  heap_type->ht_name = name_obj.inc_ref().ptr();
250 #ifdef PYBIND11_BUILTIN_QUALNAME
251  heap_type->ht_qualname = name_obj.inc_ref().ptr();
252 #endif
253 
254  auto type = &heap_type->ht_type;
255  type->tp_name = name;
256  type->tp_base = type_incref(&PyType_Type);
257  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
258 
259  type->tp_call = pybind11_meta_call;
260 
261  type->tp_setattro = pybind11_meta_setattro;
262 #if PY_MAJOR_VERSION >= 3
263  type->tp_getattro = pybind11_meta_getattro;
264 #endif
265 
266  type->tp_dealloc = pybind11_meta_dealloc;
267 
268  if (PyType_Ready(type) < 0)
269  pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
270 
271  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
273 
274  return type;
275 }
276 
277 /// For multiple inheritance types we need to recursively register/deregister base pointers for any
278 /// base classes with pointers that are difference from the instance value pointer so that we can
279 /// correctly recognize an offset base class pointer. This calls a function with any offset base ptrs.
280 inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
281  bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
282  for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
283  if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
284  for (auto &c : parent_tinfo->implicit_casts) {
285  if (c.first == tinfo->cpptype) {
286  auto *parentptr = c.second(valueptr);
287  if (parentptr != valueptr)
288  f(parentptr, self);
289  traverse_offset_bases(parentptr, parent_tinfo, self, f);
290  break;
291  }
292  }
293  }
294  }
295 }
296 
297 inline bool register_instance_impl(void *ptr, instance *self) {
298  get_internals().registered_instances.emplace(ptr, self);
299  return true; // unused, but gives the same signature as the deregister func
300 }
301 inline bool deregister_instance_impl(void *ptr, instance *self) {
302  auto &registered_instances = get_internals().registered_instances;
303  auto range = registered_instances.equal_range(ptr);
304  for (auto it = range.first; it != range.second; ++it) {
305  if (self == it->second) {
306  registered_instances.erase(it);
307  return true;
308  }
309  }
310  return false;
311 }
312 
313 inline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
314  register_instance_impl(valptr, self);
315  if (!tinfo->simple_ancestors)
316  traverse_offset_bases(valptr, tinfo, self, register_instance_impl);
317 }
318 
319 inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) {
320  bool ret = deregister_instance_impl(valptr, self);
321  if (!tinfo->simple_ancestors)
322  traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl);
323  return ret;
324 }
325 
326 /// Instance creation function for all pybind11 types. It allocates the internal instance layout for
327 /// holding C++ objects and holders. Allocation is done lazily (the first time the instance is cast
328 /// to a reference or pointer), and initialization is done by an `__init__` function.
329 inline PyObject *make_new_instance(PyTypeObject *type) {
330 #if defined(PYPY_VERSION)
331  // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first inherited
332  // object is a a plain Python type (i.e. not derived from an extension type). Fix it.
333  ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
334  if (type->tp_basicsize < instance_size) {
335  type->tp_basicsize = instance_size;
336  }
337 #endif
338  PyObject *self = type->tp_alloc(type, 0);
339  auto inst = reinterpret_cast<instance *>(self);
340  // Allocate the value/holder internals:
341  inst->allocate_layout();
342 
343  return self;
344 }
345 
346 /// Instance creation function for all pybind11 types. It only allocates space for the
347 /// C++ object, but doesn't call the constructor -- an `__init__` function must do that.
348 extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
349  return make_new_instance(type);
350 }
351 
352 /// An `__init__` function constructs the C++ object. Users should provide at least one
353 /// of these using `py::init` or directly with `.def(__init__, ...)`. Otherwise, the
354 /// following default function will be used which simply throws an exception.
355 extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) {
356  PyTypeObject *type = Py_TYPE(self);
357  std::string msg = get_fully_qualified_tp_name(type) + ": No constructor defined!";
358  PyErr_SetString(PyExc_TypeError, msg.c_str());
359  return -1;
360 }
361 
362 inline void add_patient(PyObject *nurse, PyObject *patient) {
363  auto &internals = get_internals();
364  auto instance = reinterpret_cast<detail::instance *>(nurse);
365  instance->has_patients = true;
366  Py_INCREF(patient);
367  internals.patients[nurse].push_back(patient);
368 }
369 
370 inline void clear_patients(PyObject *self) {
371  auto instance = reinterpret_cast<detail::instance *>(self);
372  auto &internals = get_internals();
373  auto pos = internals.patients.find(self);
374  assert(pos != internals.patients.end());
375  // Clearing the patients can cause more Python code to run, which
376  // can invalidate the iterator. Extract the vector of patients
377  // from the unordered_map first.
378  auto patients = std::move(pos->second);
379  internals.patients.erase(pos);
380  instance->has_patients = false;
381  for (PyObject *&patient : patients)
382  Py_CLEAR(patient);
383 }
384 
385 /// Clears all internal data from the instance and removes it from registered instances in
386 /// preparation for deallocation.
387 inline void clear_instance(PyObject *self) {
388  auto instance = reinterpret_cast<detail::instance *>(self);
389 
390  // Deallocate any values/holders, if present:
391  for (auto &v_h : values_and_holders(instance)) {
392  if (v_h) {
393 
394  // We have to deregister before we call dealloc because, for virtual MI types, we still
395  // need to be able to get the parent pointers.
396  if (v_h.instance_registered() && !deregister_instance(instance, v_h.value_ptr(), v_h.type))
397  pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
398 
399  if (instance->owned || v_h.holder_constructed())
400  v_h.type->dealloc(v_h);
401  }
402  }
403  // Deallocate the value/holder layout internals:
405 
406  if (instance->weakrefs)
407  PyObject_ClearWeakRefs(self);
408 
409  PyObject **dict_ptr = _PyObject_GetDictPtr(self);
410  if (dict_ptr)
411  Py_CLEAR(*dict_ptr);
412 
413  if (instance->has_patients)
414  clear_patients(self);
415 }
416 
417 /// Instance destructor function for all pybind11 types. It calls `type_info.dealloc`
418 /// to destroy the C++ object itself, while the rest is Python bookkeeping.
419 extern "C" inline void pybind11_object_dealloc(PyObject *self) {
420  clear_instance(self);
421 
422  auto type = Py_TYPE(self);
423  type->tp_free(self);
424 
425 #if PY_VERSION_HEX < 0x03080000
426  // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called
427  // as part of a derived type's dealloc, in which case we're not allowed to decref
428  // the type here. For cross-module compatibility, we shouldn't compare directly
429  // with `pybind11_object_dealloc`, but with the common one stashed in internals.
430  auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
431  if (type->tp_dealloc == pybind11_object_type->tp_dealloc)
432  Py_DECREF(type);
433 #else
434  // This was not needed before Python 3.8 (Python issue 35810)
435  // https://github.com/pybind/pybind11/issues/1946
436  Py_DECREF(type);
437 #endif
438 }
439 
440 /** Create the type which can be used as a common base for all classes. This is
441  needed in order to satisfy Python's requirements for multiple inheritance.
442  Return value: New reference. */
443 inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
444  constexpr auto *name = "pybind11_object";
445  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
446 
447  /* Danger zone: from now (and until PyType_Ready), make sure to
448  issue no Python C API calls which could potentially invoke the
449  garbage collector (the GC will call type_traverse(), which will in
450  turn find the newly constructed type in an invalid state) */
451  auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
452  if (!heap_type)
453  pybind11_fail("make_object_base_type(): error allocating type!");
454 
455  heap_type->ht_name = name_obj.inc_ref().ptr();
456 #ifdef PYBIND11_BUILTIN_QUALNAME
457  heap_type->ht_qualname = name_obj.inc_ref().ptr();
458 #endif
459 
460  auto type = &heap_type->ht_type;
461  type->tp_name = name;
462  type->tp_base = type_incref(&PyBaseObject_Type);
463  type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
464  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
465 
466  type->tp_new = pybind11_object_new;
467  type->tp_init = pybind11_object_init;
468  type->tp_dealloc = pybind11_object_dealloc;
469 
470  /* Support weak references (needed for the keep_alive feature) */
471  type->tp_weaklistoffset = offsetof(instance, weakrefs);
472 
473  if (PyType_Ready(type) < 0)
474  pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
475 
476  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
478 
479  assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
480  return (PyObject *) heap_type;
481 }
482 
483 /// dynamic_attr: Support for `d = instance.__dict__`.
484 extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) {
485  PyObject *&dict = *_PyObject_GetDictPtr(self);
486  if (!dict)
487  dict = PyDict_New();
488  Py_XINCREF(dict);
489  return dict;
490 }
491 
492 /// dynamic_attr: Support for `instance.__dict__ = dict()`.
493 extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) {
494  if (!PyDict_Check(new_dict)) {
495  PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'",
496  get_fully_qualified_tp_name(Py_TYPE(new_dict)).c_str());
497  return -1;
498  }
499  PyObject *&dict = *_PyObject_GetDictPtr(self);
500  Py_INCREF(new_dict);
501  Py_CLEAR(dict);
502  dict = new_dict;
503  return 0;
504 }
505 
506 /// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
507 extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
508  PyObject *&dict = *_PyObject_GetDictPtr(self);
509  Py_VISIT(dict);
510  return 0;
511 }
512 
513 /// dynamic_attr: Allow the GC to clear the dictionary.
514 extern "C" inline int pybind11_clear(PyObject *self) {
515  PyObject *&dict = *_PyObject_GetDictPtr(self);
516  Py_CLEAR(dict);
517  return 0;
518 }
519 
520 /// Give instances of this type a `__dict__` and opt into garbage collection.
521 inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
522  auto type = &heap_type->ht_type;
523  type->tp_flags |= Py_TPFLAGS_HAVE_GC;
524  type->tp_dictoffset = type->tp_basicsize; // place dict at the end
525  type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
526  type->tp_traverse = pybind11_traverse;
527  type->tp_clear = pybind11_clear;
528 
529  static PyGetSetDef getset[] = {
530  {const_cast<char*>("__dict__"), pybind11_get_dict, pybind11_set_dict, nullptr, nullptr},
531  {nullptr, nullptr, nullptr, nullptr, nullptr}
532  };
533  type->tp_getset = getset;
534 }
535 
536 /// buffer_protocol: Fill in the view as specified by flags.
537 extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
538  // Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
539  type_info *tinfo = nullptr;
540  for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
541  tinfo = get_type_info((PyTypeObject *) type.ptr());
542  if (tinfo && tinfo->get_buffer)
543  break;
544  }
545  if (view == nullptr || !tinfo || !tinfo->get_buffer) {
546  if (view)
547  view->obj = nullptr;
548  PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
549  return -1;
550  }
551  std::memset(view, 0, sizeof(Py_buffer));
552  buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
553  if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
554  delete info;
555  // view->obj = nullptr; // Was just memset to 0, so not necessary
556  PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage");
557  return -1;
558  }
559  view->obj = obj;
560  view->ndim = 1;
561  view->internal = info;
562  view->buf = info->ptr;
563  view->itemsize = info->itemsize;
564  view->len = view->itemsize;
565  for (auto s : info->shape)
566  view->len *= s;
567  view->readonly = info->readonly;
568  if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
569  view->format = const_cast<char *>(info->format.c_str());
570  if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
571  view->ndim = (int) info->ndim;
572  view->strides = &info->strides[0];
573  view->shape = &info->shape[0];
574  }
575  Py_INCREF(view->obj);
576  return 0;
577 }
578 
579 /// buffer_protocol: Release the resources of the buffer.
580 extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
581  delete (buffer_info *) view->internal;
582 }
583 
584 /// Give this type a buffer interface.
585 inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
586  heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
587 #if PY_MAJOR_VERSION < 3
588  heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
589 #endif
590 
591  heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
592  heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
593 }
594 
595 /** Create a brand new Python type according to the `type_record` specification.
596  Return value: New reference. */
597 inline PyObject* make_new_python_type(const type_record &rec) {
598  auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
599 
600  auto qualname = name;
601  if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
602 #if PY_MAJOR_VERSION >= 3
603  qualname = reinterpret_steal<object>(
604  PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
605 #else
606  qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name);
607 #endif
608  }
609 
610  object module_;
611  if (rec.scope) {
612  if (hasattr(rec.scope, "__module__"))
613  module_ = rec.scope.attr("__module__");
614  else if (hasattr(rec.scope, "__name__"))
615  module_ = rec.scope.attr("__name__");
616  }
617 
618  auto full_name = c_str(
619 #if !defined(PYPY_VERSION)
620  module_ ? str(module_).cast<std::string>() + "." + rec.name :
621 #endif
622  rec.name);
623 
624  char *tp_doc = nullptr;
626  /* Allocate memory for docstring (using PyObject_MALLOC, since
627  Python will free this later on) */
628  size_t size = strlen(rec.doc) + 1;
629  tp_doc = (char *) PyObject_MALLOC(size);
630  memcpy((void *) tp_doc, rec.doc, size);
631  }
632 
633  auto &internals = get_internals();
634  auto bases = tuple(rec.bases);
635  auto base = (bases.empty()) ? internals.instance_base
636  : bases[0].ptr();
637 
638  /* Danger zone: from now (and until PyType_Ready), make sure to
639  issue no Python C API calls which could potentially invoke the
640  garbage collector (the GC will call type_traverse(), which will in
641  turn find the newly constructed type in an invalid state) */
642  auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr()
644 
645  auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
646  if (!heap_type)
647  pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
648 
649  heap_type->ht_name = name.release().ptr();
650 #ifdef PYBIND11_BUILTIN_QUALNAME
651  heap_type->ht_qualname = qualname.inc_ref().ptr();
652 #endif
653 
654  auto type = &heap_type->ht_type;
655  type->tp_name = full_name;
656  type->tp_doc = tp_doc;
657  type->tp_base = type_incref((PyTypeObject *)base);
658  type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
659  if (!bases.empty())
660  type->tp_bases = bases.release().ptr();
661 
662  /* Don't inherit base __init__ */
663  type->tp_init = pybind11_object_init;
664 
665  /* Supported protocols */
666  type->tp_as_number = &heap_type->as_number;
667  type->tp_as_sequence = &heap_type->as_sequence;
668  type->tp_as_mapping = &heap_type->as_mapping;
669 #if PY_VERSION_HEX >= 0x03050000
670  type->tp_as_async = &heap_type->as_async;
671 #endif
672 
673  /* Flags */
674  type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
675 #if PY_MAJOR_VERSION < 3
676  type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
677 #endif
678  if (!rec.is_final)
679  type->tp_flags |= Py_TPFLAGS_BASETYPE;
680 
681  if (rec.dynamic_attr)
682  enable_dynamic_attributes(heap_type);
683 
684  if (rec.buffer_protocol)
685  enable_buffer_protocol(heap_type);
686 
687  if (PyType_Ready(type) < 0)
688  pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!");
689 
690  assert(rec.dynamic_attr ? PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)
691  : !PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
692 
693  /* Register type with the parent scope */
694  if (rec.scope)
695  setattr(rec.scope, rec.name, (PyObject *) type);
696  else
697  Py_INCREF(type); // Keep it alive forever (reference leak)
698 
699  if (module_) // Needed by pydoc
700  setattr((PyObject *) type, "__module__", module_);
701 
703 
704  return (PyObject *) type;
705 }
706 
type_record::doc
const char * doc
Optional docstring.
Definition: attr.h:257
deregister_instance
bool deregister_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:319
PYBIND11_BUILTINS_MODULE
#define PYBIND11_BUILTINS_MODULE
Definition: common.h:233
name
Annotation for function names.
Definition: attr.h:36
pybind11_object_init
int pybind11_object_init(PyObject *self, PyObject *, PyObject *)
Definition: class.h:355
setattr
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:451
pybind11_static_set
int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value)
pybind11_static_property.__set__(): Just like the above __get__().
Definition: class.h:52
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:16
internals
Definition: internals.h:96
base
Annotation indicating that a class derives from another given type.
Definition: attr.h:42
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:17
clear_patients
void clear_patients(PyObject *self)
Definition: class.h:370
add_patient
void add_patient(PyObject *nurse, PyObject *patient)
Definition: class.h:362
traverse_offset_bases
void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool(*f)(void *, instance *))
Definition: class.h:280
error_already_set
Definition: pytypes.h:326
type_info
Definition: internals.h:128
kwargs
Definition: pytypes.h:1367
PYBIND11_NAMESPACE
#define PYBIND11_NAMESPACE
Definition: common.h:26
type_record::dynamic_attr
bool dynamic_attr
Does the class manage a dict?
Definition: attr.h:266
internals::registered_types_cpp
type_map< type_info * > registered_types_cpp
Definition: internals.h:97
type_info::get_buffer_data
void * get_buffer_data
Definition: internals.h:139
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:405
internals::registered_instances
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:99
type
Definition: pytypes.h:915
enable_dynamic_attributes
void enable_dynamic_attributes(PyHeapTypeObject *heap_type)
Give instances of this type a __dict__ and opt into garbage collection.
Definition: class.h:521
internals::static_property_type
PyTypeObject * static_property_type
Definition: internals.h:107
type_record::name
const char * name
Name of the class.
Definition: attr.h:230
get_internals
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:256
make_static_property_type
PyTypeObject * make_static_property_type()
Definition: class.h:60
descr
Definition: descr.h:25
make_new_instance
PyObject * make_new_instance(PyTypeObject *type)
Definition: class.h:329
deregister_instance_impl
bool deregister_instance_impl(void *ptr, instance *self)
Definition: class.h:301
instance::allocate_layout
void allocate_layout()
Initializes all of the above type/values/holders data (but not the instance values themselves)
Definition: cast.h:363
make_object_base_type
PyObject * make_object_base_type(PyTypeObject *metaclass)
Definition: class.h:443
error_string
PYBIND11_NOINLINE std::string error_string()
Definition: cast.h:423
internals::instance_base
PyObject * instance_base
Definition: internals.h:109
PYBIND11_SET_OLDPY_QUALNAME
#define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
Definition: class.h:24
instance
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Definition: common.h:437
type_record::metaclass
handle metaclass
Custom metaclass (optional)
Definition: attr.h:260
registered_local_types_cpp
type_map< type_info * > & registered_local_types_cpp()
Works like internals.registered_types_cpp, but for module-local registered types:
Definition: internals.h:315
dict
Definition: pytypes.h:1299
get_fully_qualified_tp_name
std::string get_fully_qualified_tp_name(PyTypeObject *type)
Definition: class.h:27
pybind11_meta_dealloc
void pybind11_meta_dealloc(PyObject *obj)
Cleanup the type-info for a pybind11-registered type.
Definition: class.h:197
handle
Definition: pytypes.h:176
handle::cast
T cast() const
Definition: cast.h:1794
buffer_info::format
std::string format
Definition: buffer_info.h:44
internals::inactive_override_cache
std::unordered_set< std::pair< const PyObject *, const char * >, override_hash > inactive_override_cache
Definition: internals.h:100
pybind11_object_new
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
Definition: class.h:348
type_record::bases
list bases
List of base classes of the newly created type.
Definition: attr.h:254
object::release
handle release()
Definition: pytypes.h:249
metaclass
Annotation which requests that a special metaclass is created for a type.
Definition: attr.h:61
buffer_info::itemsize
ssize_t itemsize
Definition: buffer_info.h:42
type_record
Special data structure which (temporarily) holds metadata about a bound class.
Definition: attr.h:221
instance::deallocate_layout
void deallocate_layout()
Destroys/deallocates all of the above.
Definition: cast.h:411
buffer_info::shape
std::vector< ssize_t > shape
Definition: buffer_info.h:46
arg
Definition: cast.h:1895
pybind11_getbuffer
int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags)
buffer_protocol: Fill in the view as specified by flags.
Definition: class.h:537
buffer_info::readonly
bool readonly
Definition: buffer_info.h:48
instance::has_patients
bool has_patients
If true, get_internals().patients has an entry for this object.
Definition: common.h:477
buffer_info::ndim
ssize_t ndim
Definition: buffer_info.h:45
instance::owned
bool owned
If true, the pointer is owned which means we're free to manage it with a holder.
Definition: common.h:447
ssize_t
Py_ssize_t ssize_t
Definition: common.h:353
pybind11_clear
int pybind11_clear(PyObject *self)
dynamic_attr: Allow the GC to clear the dictionary.
Definition: class.h:514
make_default_metaclass
PyTypeObject * make_default_metaclass()
Definition: class.h:237
pybind11_object_dealloc
void pybind11_object_dealloc(PyObject *self)
Definition: class.h:419
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: common.h:751
str
Definition: pytypes.h:946
register_instance_impl
bool register_instance_impl(void *ptr, instance *self)
Definition: class.h:297
pybind11_set_dict
int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *)
dynamic_attr: Support for instance.__dict__ = dict().
Definition: class.h:493
pybind11_releasebuffer
void pybind11_releasebuffer(PyObject *, Py_buffer *view)
buffer_protocol: Release the resources of the buffer.
Definition: class.h:580
type_record::is_final
bool is_final
Is the class inheritable from python classes?
Definition: attr.h:278
move
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1798
pybind11_get_dict
PyObject * pybind11_get_dict(PyObject *self, void *)
dynamic_attr: Support for d = instance.__dict__.
Definition: class.h:484
internals::direct_conversions
type_map< std::vector< bool(*)(PyObject *, void *&)> > direct_conversions
Definition: internals.h:101
pybind11_meta_setattro
int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value)
Definition: class.h:122
enable_buffer_protocol
void enable_buffer_protocol(PyHeapTypeObject *heap_type)
Give this type a buffer interface.
Definition: class.h:585
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
internals::patients
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
Definition: internals.h:102
module_
Wrapper for Python extension modules.
Definition: pybind11.h:941
benchmark.size
size
Definition: benchmark.py:90
args
Definition: pytypes.h:1366
buffer_info
Information record describing a Python buffer object.
Definition: buffer_info.h:40
c_str
const char * c_str(Args &&...args)
Definition: internals.h:325
buffer_info::ptr
void * ptr
Definition: buffer_info.h:41
instance::weakrefs
PyObject * weakrefs
Weak references.
Definition: common.h:445
tuple
Definition: pytypes.h:1276
options::show_user_defined_docstrings
static bool show_user_defined_docstrings()
Definition: options.h:43
make_new_python_type
PyObject * make_new_python_type(const type_record &rec)
Definition: class.h:597
clear_instance
void clear_instance(PyObject *self)
Definition: class.h:387
type_record::scope
handle scope
Handle to the parent scope.
Definition: attr.h:227
internals::default_metaclass
PyTypeObject * default_metaclass
Definition: internals.h:108
internals::registered_types_py
std::unordered_map< PyTypeObject *, std::vector< type_info * > > registered_types_py
Definition: internals.h:98
type_record::buffer_protocol
bool buffer_protocol
Does the class implement the buffer protocol?
Definition: attr.h:269
last
constexpr int last(int, int result)
Definition: common.h:600
pybind11_static_get
PyObject * pybind11_static_get(PyObject *self, PyObject *, PyObject *cls)
pybind11_static_property.__get__(): Always pass the class instead of the instance.
Definition: class.h:47
buffer_info::strides
std::vector< ssize_t > strides
Definition: buffer_info.h:47
type_info::simple_ancestors
bool simple_ancestors
Definition: internals.h:145
pybind11_traverse
int pybind11_traverse(PyObject *self, visitproc visit, void *arg)
dynamic_attr: Allow the garbage collector to traverse the internal instance __dict__.
Definition: class.h:507
values_and_holders
Definition: cast.h:279
pybind11_meta_call
PyObject * pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs)
metaclass __call__ function that is used to create all pybind11 objects.
Definition: class.h:172
type_incref
PyTypeObject * type_incref(PyTypeObject *type)
Definition: class.h:39
test_callbacks.value
value
Definition: test_callbacks.py:126
type_info::get_buffer
buffer_info *(* get_buffer)(PyObject *, void *)
Definition: internals.h:138
register_instance
void register_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:313
PYBIND11_FROM_STRING
#define PYBIND11_FROM_STRING
Definition: common.h:229
get_type_info
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
Definition: cast.h:164
setup.msg
msg
Definition: setup.py:47