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 PyTypeObject *type_incref(PyTypeObject *type) {
28  Py_INCREF(type);
29  return type;
30 }
31 
32 #if !defined(PYPY_VERSION)
33 
35 extern "C" inline PyObject *pybind11_static_get(PyObject *self, PyObject * /*ob*/, PyObject *cls) {
36  return PyProperty_Type.tp_descr_get(self, cls, cls);
37 }
38 
40 extern "C" inline int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value) {
41  PyObject *cls = PyType_Check(obj) ? obj : (PyObject *) Py_TYPE(obj);
42  return PyProperty_Type.tp_descr_set(self, cls, value);
43 }
44 
48 inline PyTypeObject *make_static_property_type() {
49  constexpr auto *name = "pybind11_static_property";
50  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
51 
52  /* Danger zone: from now (and until PyType_Ready), make sure to
53  issue no Python C API calls which could potentially invoke the
54  garbage collector (the GC will call type_traverse(), which will in
55  turn find the newly constructed type in an invalid state) */
56  auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
57  if (!heap_type)
58  pybind11_fail("make_static_property_type(): error allocating type!");
59 
60  heap_type->ht_name = name_obj.inc_ref().ptr();
61 #ifdef PYBIND11_BUILTIN_QUALNAME
62  heap_type->ht_qualname = name_obj.inc_ref().ptr();
63 #endif
64 
65  auto type = &heap_type->ht_type;
66  type->tp_name = name;
67  type->tp_base = type_incref(&PyProperty_Type);
68  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
69  type->tp_descr_get = pybind11_static_get;
70  type->tp_descr_set = pybind11_static_set;
71 
72  if (PyType_Ready(type) < 0)
73  pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
74 
75  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
77 
78  return type;
79 }
80 
81 #else // PYPY
82 
86 inline PyTypeObject *make_static_property_type() {
87  auto d = dict();
88  PyObject *result = PyRun_String(R"(\
89  class pybind11_static_property(property):
90  def __get__(self, obj, cls):
91  return property.__get__(self, cls, cls)
92 
93  def __set__(self, obj, value):
94  cls = obj if isinstance(obj, type) else type(obj)
95  property.__set__(self, cls, value)
96  )", Py_file_input, d.ptr(), d.ptr()
97  );
98  if (result == nullptr)
99  throw error_already_set();
100  Py_DECREF(result);
101  return (PyTypeObject *) d["pybind11_static_property"].cast<object>().release().ptr();
102 }
103 
104 #endif // PYPY
105 
110 extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyObject* value) {
111  // Use `_PyType_Lookup()` instead of `PyObject_GetAttr()` in order to get the raw
112  // descriptor (`property`) instead of calling `tp_descr_get` (`property.__get__()`).
113  PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
114 
115  // The following assignment combinations are possible:
116  // 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
117  // 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
118  // 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
119  const auto static_prop = (PyObject *) get_internals().static_property_type;
120  const auto call_descr_set = descr && PyObject_IsInstance(descr, static_prop)
121  && !PyObject_IsInstance(value, static_prop);
122  if (call_descr_set) {
123  // Call `static_property.__set__()` instead of replacing the `static_property`.
124 #if !defined(PYPY_VERSION)
125  return Py_TYPE(descr)->tp_descr_set(descr, obj, value);
126 #else
127  if (PyObject *result = PyObject_CallMethod(descr, "__set__", "OO", obj, value)) {
128  Py_DECREF(result);
129  return 0;
130  } else {
131  return -1;
132  }
133 #endif
134  } else {
135  // Replace existing attribute.
136  return PyType_Type.tp_setattro(obj, name, value);
137  }
138 }
139 
140 #if PY_MAJOR_VERSION >= 3
141 
147 extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name) {
148  PyObject *descr = _PyType_Lookup((PyTypeObject *) obj, name);
149  if (descr && PyInstanceMethod_Check(descr)) {
150  Py_INCREF(descr);
151  return descr;
152  }
153  else {
154  return PyType_Type.tp_getattro(obj, name);
155  }
156 }
157 #endif
158 
160 extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
161 
162  // use the default metaclass call to create/initialize the object
163  PyObject *self = PyType_Type.tp_call(type, args, kwargs);
164  if (self == nullptr) {
165  return nullptr;
166  }
167 
168  // This must be a pybind11 instance
169  auto instance = reinterpret_cast<detail::instance *>(self);
170 
171  // Ensure that the base __init__ function(s) were called
172  for (const auto &vh : values_and_holders(instance)) {
173  if (!vh.holder_constructed()) {
174  PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__",
175  vh.type->type->tp_name);
176  Py_DECREF(self);
177  return nullptr;
178  }
179  }
180 
181  return self;
182 }
183 
187 inline PyTypeObject* make_default_metaclass() {
188  constexpr auto *name = "pybind11_type";
189  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
190 
191  /* Danger zone: from now (and until PyType_Ready), make sure to
192  issue no Python C API calls which could potentially invoke the
193  garbage collector (the GC will call type_traverse(), which will in
194  turn find the newly constructed type in an invalid state) */
195  auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
196  if (!heap_type)
197  pybind11_fail("make_default_metaclass(): error allocating metaclass!");
198 
199  heap_type->ht_name = name_obj.inc_ref().ptr();
200 #ifdef PYBIND11_BUILTIN_QUALNAME
201  heap_type->ht_qualname = name_obj.inc_ref().ptr();
202 #endif
203 
204  auto type = &heap_type->ht_type;
205  type->tp_name = name;
206  type->tp_base = type_incref(&PyType_Type);
207  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
208 
209  type->tp_call = pybind11_meta_call;
210 
211  type->tp_setattro = pybind11_meta_setattro;
212 #if PY_MAJOR_VERSION >= 3
213  type->tp_getattro = pybind11_meta_getattro;
214 #endif
215 
216  if (PyType_Ready(type) < 0)
217  pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
218 
219  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
221 
222  return type;
223 }
224 
228 inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
229  bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
230  for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
231  if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
232  for (auto &c : parent_tinfo->implicit_casts) {
233  if (c.first == tinfo->cpptype) {
234  auto *parentptr = c.second(valueptr);
235  if (parentptr != valueptr)
236  f(parentptr, self);
237  traverse_offset_bases(parentptr, parent_tinfo, self, f);
238  break;
239  }
240  }
241  }
242  }
243 }
244 
245 inline bool register_instance_impl(void *ptr, instance *self) {
246  get_internals().registered_instances.emplace(ptr, self);
247  return true; // unused, but gives the same signature as the deregister func
248 }
249 inline bool deregister_instance_impl(void *ptr, instance *self) {
250  auto &registered_instances = get_internals().registered_instances;
251  auto range = registered_instances.equal_range(ptr);
252  for (auto it = range.first; it != range.second; ++it) {
253  if (Py_TYPE(self) == Py_TYPE(it->second)) {
254  registered_instances.erase(it);
255  return true;
256  }
257  }
258  return false;
259 }
260 
261 inline void register_instance(instance *self, void *valptr, const type_info *tinfo) {
262  register_instance_impl(valptr, self);
263  if (!tinfo->simple_ancestors)
264  traverse_offset_bases(valptr, tinfo, self, register_instance_impl);
265 }
266 
267 inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo) {
268  bool ret = deregister_instance_impl(valptr, self);
269  if (!tinfo->simple_ancestors)
270  traverse_offset_bases(valptr, tinfo, self, deregister_instance_impl);
271  return ret;
272 }
273 
277 inline PyObject *make_new_instance(PyTypeObject *type) {
278 #if defined(PYPY_VERSION)
279  // PyPy gets tp_basicsize wrong (issue 2482) under multiple inheritance when the first inherited
280  // object is a a plain Python type (i.e. not derived from an extension type). Fix it.
281  ssize_t instance_size = static_cast<ssize_t>(sizeof(instance));
282  if (type->tp_basicsize < instance_size) {
283  type->tp_basicsize = instance_size;
284  }
285 #endif
286  PyObject *self = type->tp_alloc(type, 0);
287  auto inst = reinterpret_cast<instance *>(self);
288  // Allocate the value/holder internals:
289  inst->allocate_layout();
290 
291  inst->owned = true;
292 
293  return self;
294 }
295 
298 extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
299  return make_new_instance(type);
300 }
301 
305 extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject *) {
306  PyTypeObject *type = Py_TYPE(self);
307  std::string msg;
308 #if defined(PYPY_VERSION)
309  msg += handle((PyObject *) type).attr("__module__").cast<std::string>() + ".";
310 #endif
311  msg += type->tp_name;
312  msg += ": No constructor defined!";
313  PyErr_SetString(PyExc_TypeError, msg.c_str());
314  return -1;
315 }
316 
317 inline void add_patient(PyObject *nurse, PyObject *patient) {
318  auto &internals = get_internals();
319  auto instance = reinterpret_cast<detail::instance *>(nurse);
320  instance->has_patients = true;
321  Py_INCREF(patient);
322  internals.patients[nurse].push_back(patient);
323 }
324 
325 inline void clear_patients(PyObject *self) {
326  auto instance = reinterpret_cast<detail::instance *>(self);
327  auto &internals = get_internals();
328  auto pos = internals.patients.find(self);
329  assert(pos != internals.patients.end());
330  // Clearing the patients can cause more Python code to run, which
331  // can invalidate the iterator. Extract the vector of patients
332  // from the unordered_map first.
333  auto patients = std::move(pos->second);
334  internals.patients.erase(pos);
335  instance->has_patients = false;
336  for (PyObject *&patient : patients)
337  Py_CLEAR(patient);
338 }
339 
342 inline void clear_instance(PyObject *self) {
343  auto instance = reinterpret_cast<detail::instance *>(self);
344 
345  // Deallocate any values/holders, if present:
346  for (auto &v_h : values_and_holders(instance)) {
347  if (v_h) {
348 
349  // We have to deregister before we call dealloc because, for virtual MI types, we still
350  // need to be able to get the parent pointers.
351  if (v_h.instance_registered() && !deregister_instance(instance, v_h.value_ptr(), v_h.type))
352  pybind11_fail("pybind11_object_dealloc(): Tried to deallocate unregistered instance!");
353 
354  if (instance->owned || v_h.holder_constructed())
355  v_h.type->dealloc(v_h);
356  }
357  }
358  // Deallocate the value/holder layout internals:
360 
361  if (instance->weakrefs)
362  PyObject_ClearWeakRefs(self);
363 
364  PyObject **dict_ptr = _PyObject_GetDictPtr(self);
365  if (dict_ptr)
366  Py_CLEAR(*dict_ptr);
367 
368  if (instance->has_patients)
369  clear_patients(self);
370 }
371 
374 extern "C" inline void pybind11_object_dealloc(PyObject *self) {
375  clear_instance(self);
376 
377  auto type = Py_TYPE(self);
378  type->tp_free(self);
379 
380 #if PY_VERSION_HEX < 0x03080000
381  // `type->tp_dealloc != pybind11_object_dealloc` means that we're being called
382  // as part of a derived type's dealloc, in which case we're not allowed to decref
383  // the type here. For cross-module compatibility, we shouldn't compare directly
384  // with `pybind11_object_dealloc`, but with the common one stashed in internals.
385  auto pybind11_object_type = (PyTypeObject *) get_internals().instance_base;
386  if (type->tp_dealloc == pybind11_object_type->tp_dealloc)
387  Py_DECREF(type);
388 #else
389  // This was not needed before Python 3.8 (Python issue 35810)
390  // https://github.com/pybind/pybind11/issues/1946
391  Py_DECREF(type);
392 #endif
393 }
394 
398 inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
399  constexpr auto *name = "pybind11_object";
400  auto name_obj = reinterpret_steal<object>(PYBIND11_FROM_STRING(name));
401 
402  /* Danger zone: from now (and until PyType_Ready), make sure to
403  issue no Python C API calls which could potentially invoke the
404  garbage collector (the GC will call type_traverse(), which will in
405  turn find the newly constructed type in an invalid state) */
406  auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
407  if (!heap_type)
408  pybind11_fail("make_object_base_type(): error allocating type!");
409 
410  heap_type->ht_name = name_obj.inc_ref().ptr();
411 #ifdef PYBIND11_BUILTIN_QUALNAME
412  heap_type->ht_qualname = name_obj.inc_ref().ptr();
413 #endif
414 
415  auto type = &heap_type->ht_type;
416  type->tp_name = name;
417  type->tp_base = type_incref(&PyBaseObject_Type);
418  type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
419  type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
420 
421  type->tp_new = pybind11_object_new;
422  type->tp_init = pybind11_object_init;
423  type->tp_dealloc = pybind11_object_dealloc;
424 
425  /* Support weak references (needed for the keep_alive feature) */
426  type->tp_weaklistoffset = offsetof(instance, weakrefs);
427 
428  if (PyType_Ready(type) < 0)
429  pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
430 
431  setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
433 
434  assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
435  return (PyObject *) heap_type;
436 }
437 
439 extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) {
440  PyObject *&dict = *_PyObject_GetDictPtr(self);
441  if (!dict)
442  dict = PyDict_New();
443  Py_XINCREF(dict);
444  return dict;
445 }
446 
448 extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) {
449  if (!PyDict_Check(new_dict)) {
450  PyErr_Format(PyExc_TypeError, "__dict__ must be set to a dictionary, not a '%.200s'",
451  Py_TYPE(new_dict)->tp_name);
452  return -1;
453  }
454  PyObject *&dict = *_PyObject_GetDictPtr(self);
455  Py_INCREF(new_dict);
456  Py_CLEAR(dict);
457  dict = new_dict;
458  return 0;
459 }
460 
462 extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
463  PyObject *&dict = *_PyObject_GetDictPtr(self);
464  Py_VISIT(dict);
465  return 0;
466 }
467 
469 extern "C" inline int pybind11_clear(PyObject *self) {
470  PyObject *&dict = *_PyObject_GetDictPtr(self);
471  Py_CLEAR(dict);
472  return 0;
473 }
474 
476 inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
477  auto type = &heap_type->ht_type;
478 #if defined(PYPY_VERSION) && (PYPY_VERSION_NUM < 0x06000000)
479  pybind11_fail(std::string(type->tp_name) + ": dynamic attributes are "
480  "currently not supported in "
481  "conjunction with PyPy!");
482 #endif
483  type->tp_flags |= Py_TPFLAGS_HAVE_GC;
484  type->tp_dictoffset = type->tp_basicsize; // place dict at the end
485  type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
486  type->tp_traverse = pybind11_traverse;
487  type->tp_clear = pybind11_clear;
488 
489  static PyGetSetDef getset[] = {
490  {const_cast<char*>("__dict__"), pybind11_get_dict, pybind11_set_dict, nullptr, nullptr},
491  {nullptr, nullptr, nullptr, nullptr, nullptr}
492  };
493  type->tp_getset = getset;
494 }
495 
497 extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags) {
498  // Look for a `get_buffer` implementation in this type's info or any bases (following MRO).
499  type_info *tinfo = nullptr;
500  for (auto type : reinterpret_borrow<tuple>(Py_TYPE(obj)->tp_mro)) {
501  tinfo = get_type_info((PyTypeObject *) type.ptr());
502  if (tinfo && tinfo->get_buffer)
503  break;
504  }
505  if (view == nullptr || !tinfo || !tinfo->get_buffer) {
506  if (view)
507  view->obj = nullptr;
508  PyErr_SetString(PyExc_BufferError, "pybind11_getbuffer(): Internal error");
509  return -1;
510  }
511  std::memset(view, 0, sizeof(Py_buffer));
512  buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
513  view->obj = obj;
514  view->ndim = 1;
515  view->internal = info;
516  view->buf = info->ptr;
517  view->itemsize = info->itemsize;
518  view->len = view->itemsize;
519  for (auto s : info->shape)
520  view->len *= s;
521  view->readonly = info->readonly;
522  if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE && info->readonly) {
523  if (view)
524  view->obj = nullptr;
525  PyErr_SetString(PyExc_BufferError, "Writable buffer requested for readonly storage");
526  return -1;
527  }
528  if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
529  view->format = const_cast<char *>(info->format.c_str());
530  if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
531  view->ndim = (int) info->ndim;
532  view->strides = &info->strides[0];
533  view->shape = &info->shape[0];
534  }
535  Py_INCREF(view->obj);
536  return 0;
537 }
538 
540 extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
541  delete (buffer_info *) view->internal;
542 }
543 
545 inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
546  heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
547 #if PY_MAJOR_VERSION < 3
548  heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
549 #endif
550 
551  heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
552  heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
553 }
554 
557 inline PyObject* make_new_python_type(const type_record &rec) {
558  auto name = reinterpret_steal<object>(PYBIND11_FROM_STRING(rec.name));
559 
560  auto qualname = name;
561  if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
562 #if PY_MAJOR_VERSION >= 3
563  qualname = reinterpret_steal<object>(
564  PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
565 #else
566  qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name);
567 #endif
568  }
569 
570  object module;
571  if (rec.scope) {
572  if (hasattr(rec.scope, "__module__"))
573  module = rec.scope.attr("__module__");
574  else if (hasattr(rec.scope, "__name__"))
575  module = rec.scope.attr("__name__");
576  }
577 
578  auto full_name = c_str(
579 #if !defined(PYPY_VERSION)
580  module ? str(module).cast<std::string>() + "." + rec.name :
581 #endif
582  rec.name);
583 
584  char *tp_doc = nullptr;
586  /* Allocate memory for docstring (using PyObject_MALLOC, since
587  Python will free this later on) */
588  size_t size = strlen(rec.doc) + 1;
589  tp_doc = (char *) PyObject_MALLOC(size);
590  memcpy((void *) tp_doc, rec.doc, size);
591  }
592 
593  auto &internals = get_internals();
594  auto bases = tuple(rec.bases);
595  auto base = (bases.empty()) ? internals.instance_base
596  : bases[0].ptr();
597 
598  /* Danger zone: from now (and until PyType_Ready), make sure to
599  issue no Python C API calls which could potentially invoke the
600  garbage collector (the GC will call type_traverse(), which will in
601  turn find the newly constructed type in an invalid state) */
602  auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr()
604 
605  auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
606  if (!heap_type)
607  pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
608 
609  heap_type->ht_name = name.release().ptr();
610 #ifdef PYBIND11_BUILTIN_QUALNAME
611  heap_type->ht_qualname = qualname.inc_ref().ptr();
612 #endif
613 
614  auto type = &heap_type->ht_type;
615  type->tp_name = full_name;
616  type->tp_doc = tp_doc;
617  type->tp_base = type_incref((PyTypeObject *)base);
618  type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
619  if (!bases.empty())
620  type->tp_bases = bases.release().ptr();
621 
622  /* Don't inherit base __init__ */
623  type->tp_init = pybind11_object_init;
624 
625  /* Supported protocols */
626  type->tp_as_number = &heap_type->as_number;
627  type->tp_as_sequence = &heap_type->as_sequence;
628  type->tp_as_mapping = &heap_type->as_mapping;
629 #if PY_VERSION_HEX >= 0x03050000
630  type->tp_as_async = &heap_type->as_async;
631 #endif
632 
633  /* Flags */
634  type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
635 #if PY_MAJOR_VERSION < 3
636  type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
637 #endif
638  if (!rec.is_final)
639  type->tp_flags |= Py_TPFLAGS_BASETYPE;
640 
641  if (rec.dynamic_attr)
642  enable_dynamic_attributes(heap_type);
643 
644  if (rec.buffer_protocol)
645  enable_buffer_protocol(heap_type);
646 
647  if (PyType_Ready(type) < 0)
648  pybind11_fail(std::string(rec.name) + ": PyType_Ready failed (" + error_string() + ")!");
649 
650  assert(rec.dynamic_attr ? PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC)
651  : !PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
652 
653  /* Register type with the parent scope */
654  if (rec.scope)
655  setattr(rec.scope, rec.name, (PyObject *) type);
656  else
657  Py_INCREF(type); // Keep it alive forever (reference leak)
658 
659  if (module) // Needed by pydoc
660  setattr((PyObject *) type, "__module__", module);
661 
663 
664  return (PyObject *) type;
665 }
666 
void * ptr
Definition: buffer_info.h:18
bool has_patients
If true, get_internals().patients has an entry for this object.
module_ module
Definition: pybind11.h:943
int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyObject *value)
Definition: class.h:110
PyObject * weakrefs
Weak references.
int pybind11_traverse(PyObject *self, visitproc visit, void *arg)
dynamic_attr: Allow the garbage collector to traverse the internal instance __dict__.
Definition: class.h:462
#define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
Definition: class.h:24
void deallocate_layout()
Destroys/deallocates all of the above.
Definition: cast.h:398
bool is_final
Is the class inheritable from python classes?
Definition: attr.h:272
bool hasattr(handle obj, handle name)
Definition: pytypes.h:403
return int(ret)+1
std::vector< ssize_t > strides
Definition: buffer_info.h:24
PyObject * pybind11_static_get(PyObject *self, PyObject *, PyObject *cls)
pybind11_static_property.__get__(): Always pass the class instead of the instance.
Definition: class.h:35
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
Annotation which requests that a special metaclass is created for a type.
Definition: attr.h:61
Definition: pytypes.h:1322
PyObject * instance_base
Definition: internals.h:109
bool readonly
Definition: buffer_info.h:25
PYBIND11_NOINLINE internals & get_internals()
Return a reference to the current internals data.
Definition: internals.h:245
void pybind11_object_dealloc(PyObject *self)
Definition: class.h:374
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
bool buffer_protocol
Does the class implement the buffer protocol?
Definition: attr.h:263
const char * doc
Optional docstring.
Definition: attr.h:251
void clear_instance(PyObject *self)
Definition: class.h:342
void * get_buffer_data
Definition: internals.h:139
std::unordered_multimap< const void *, instance * > registered_instances
Definition: internals.h:99
Definition: cast.h:1853
bool deregister_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:267
The &#39;instance&#39; type which needs to be standard layout (need to be able to use &#39;offsetof&#39;) ...
const char * c_str(Args &&...args)
Definition: internals.h:314
int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *)
dynamic_attr: Support for instance.__dict__ = dict().
Definition: class.h:448
void pybind11_releasebuffer(PyObject *, Py_buffer *view)
buffer_protocol: Release the resources of the buffer.
Definition: class.h:540
T cast() const
Definition: cast.h:1752
bool deregister_instance_impl(void *ptr, instance *self)
Definition: class.h:249
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
PyTypeObject * type_incref(PyTypeObject *type)
Definition: class.h:27
else if n * info
PyObject * make_new_python_type(const type_record &rec)
Definition: class.h:557
PyObject * pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs)
metaclass __call__ function that is used to create all pybind11 objects.
Definition: class.h:160
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
void add_patient(PyObject *nurse, PyObject *patient)
Definition: class.h:317
handle metaclass
Custom metaclass (optional)
Definition: attr.h:254
PyTypeObject * static_property_type
Definition: internals.h:107
void clear_patients(PyObject *self)
Definition: class.h:325
Definition: descr.h:25
handle scope
Handle to the parent scope.
Definition: attr.h:221
ssize_t itemsize
Definition: buffer_info.h:19
int pybind11_clear(PyObject *self)
dynamic_attr: Allow the GC to clear the dictionary.
Definition: class.h:469
Values result
int pybind11_static_set(PyObject *self, PyObject *obj, PyObject *value)
pybind11_static_property.__set__(): Just like the above __get__().
Definition: class.h:40
std::string format
Definition: buffer_info.h:21
const char * name
Name of the class.
Definition: attr.h:224
list bases
List of base classes of the newly created type.
Definition: attr.h:248
float * ptr
void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self, bool(*f)(void *, instance *))
Definition: class.h:228
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
RealScalar s
PyObject * pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *)
Definition: class.h:298
PyObject * pybind11_get_dict(PyObject *self, void *)
dynamic_attr: Support for d = instance.__dict__.
Definition: class.h:439
std::vector< ssize_t > shape
Definition: buffer_info.h:23
PyObject * make_new_instance(PyTypeObject *type)
Definition: class.h:277
PyObject * make_object_base_type(PyTypeObject *metaclass)
Definition: class.h:398
PyTypeObject * default_metaclass
Definition: internals.h:108
bool owned
If true, the pointer is owned which means we&#39;re free to manage it with a holder.
const double h
handle release()
Definition: pytypes.h:249
Special data structure which (temporarily) holds metadata about a bound class.
Definition: attr.h:215
PYBIND11_NOINLINE detail::type_info * get_type_info(PyTypeObject *type)
Definition: cast.h:164
bool simple_ancestors
Definition: internals.h:145
DenseIndex ret
Definition: level1_impl.h:59
bool register_instance_impl(void *ptr, instance *self)
Definition: class.h:245
int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int flags)
buffer_protocol: Fill in the view as specified by flags.
Definition: class.h:497
int pybind11_object_init(PyObject *self, PyObject *, PyObject *)
Definition: class.h:305
std::unordered_map< const PyObject *, std::vector< PyObject * > > patients
Definition: internals.h:102
static bool show_user_defined_docstrings()
Definition: options.h:43
void enable_buffer_protocol(PyHeapTypeObject *heap_type)
Give this type a buffer interface.
Definition: class.h:545
Definition: pytypes.h:1255
Annotation for function names.
Definition: attr.h:36
Annotation indicating that a class derives from another given type.
Definition: attr.h:42
PYBIND11_NOINLINE std::string error_string()
Definition: cast.h:410
Information record describing a Python buffer object.
Definition: buffer_info.h:17
ssize_t ndim
Definition: buffer_info.h:22
PyTypeObject * make_static_property_type()
Definition: class.h:48
buffer_info *(* get_buffer)(PyObject *, void *)
Definition: internals.h:138
void register_instance(instance *self, void *valptr, const type_info *tinfo)
Definition: class.h:261
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:449
PyTypeObject * make_default_metaclass()
Definition: class.h:187
#define PYBIND11_NAMESPACE_END(name)
bool dynamic_attr
Does the class manage a dict?
Definition: attr.h:260
Definition: pytypes.h:897
#define PYBIND11_NAMESPACE_BEGIN(name)
void enable_dynamic_attributes(PyHeapTypeObject *heap_type)
Give instances of this type a __dict__ and opt into garbage collection.
Definition: class.h:476


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:47