pytypes.h
Go to the documentation of this file.
1 /*
2  pybind11/pytypes.h: Convenience wrapper classes for basic Python types
3 
4  Copyright (c) 2016 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 "detail/common.h"
13 #include "buffer_info.h"
14 #include <utility>
15 #include <type_traits>
16 
18 
19 /* A few forward declarations */
20 class handle; class object;
21 class str; class iterator;
22 class type;
23 struct arg; struct arg_v;
24 
26 class args_proxy;
27 inline bool isinstance_generic(handle obj, const std::type_info &tp);
28 
29 // Accessor forward declarations
30 template <typename Policy> class accessor;
31 namespace accessor_policies {
32  struct obj_attr;
33  struct str_attr;
34  struct generic_item;
35  struct sequence_item;
36  struct list_item;
37  struct tuple_item;
38 } // namespace accessor_policies
45 
47 class pyobject_tag { };
48 template <typename T> using is_pyobject = std::is_base_of<pyobject_tag, remove_reference_t<T>>;
49 
54 template <typename Derived>
55 class object_api : public pyobject_tag {
56  const Derived &derived() const { return static_cast<const Derived &>(*this); }
57 
58 public:
63  iterator begin() const;
65  iterator end() const;
66 
73  item_accessor operator[](handle key) const;
75  item_accessor operator[](const char *key) const;
76 
83  obj_attr_accessor attr(handle key) const;
85  str_attr_accessor attr(const char *key) const;
86 
93  args_proxy operator*() const;
94 
96  template <typename T> bool contains(T &&item) const;
97 
108  template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
109  object operator()(Args &&...args) const;
110  template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
111  PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)")
112  object call(Args&&... args) const;
113 
115  bool is(object_api const& other) const { return derived().ptr() == other.derived().ptr(); }
117  bool is_none() const { return derived().ptr() == Py_None; }
119  bool equal(object_api const &other) const { return rich_compare(other, Py_EQ); }
120  bool not_equal(object_api const &other) const { return rich_compare(other, Py_NE); }
121  bool operator<(object_api const &other) const { return rich_compare(other, Py_LT); }
122  bool operator<=(object_api const &other) const { return rich_compare(other, Py_LE); }
123  bool operator>(object_api const &other) const { return rich_compare(other, Py_GT); }
124  bool operator>=(object_api const &other) const { return rich_compare(other, Py_GE); }
125 
126  object operator-() const;
127  object operator~() const;
128  object operator+(object_api const &other) const;
129  object operator+=(object_api const &other) const;
130  object operator-(object_api const &other) const;
131  object operator-=(object_api const &other) const;
132  object operator*(object_api const &other) const;
133  object operator*=(object_api const &other) const;
134  object operator/(object_api const &other) const;
135  object operator/=(object_api const &other) const;
136  object operator|(object_api const &other) const;
137  object operator|=(object_api const &other) const;
138  object operator&(object_api const &other) const;
139  object operator&=(object_api const &other) const;
140  object operator^(object_api const &other) const;
141  object operator^=(object_api const &other) const;
142  object operator<<(object_api const &other) const;
143  object operator<<=(object_api const &other) const;
144  object operator>>(object_api const &other) const;
145  object operator>>=(object_api const &other) const;
146 
147  PYBIND11_DEPRECATED("Use py::str(obj) instead")
148  pybind11::str str() const;
149 
151  str_attr_accessor doc() const;
152 
154  int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
155 
156  PYBIND11_DEPRECATED("Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
157  handle get_type() const;
158 
159 private:
160  bool rich_compare(object_api const &other, int value) const;
161 };
162 
164 
165 
176 class handle : public detail::object_api<handle> {
177 public:
179  handle() = default;
181  handle(PyObject *ptr) : m_ptr(ptr) { } // Allow implicit conversion from PyObject*
182 
184  PyObject *ptr() const { return m_ptr; }
185  PyObject *&ptr() { return m_ptr; }
186 
192  const handle& inc_ref() const & { Py_XINCREF(m_ptr); return *this; }
193 
199  const handle& dec_ref() const & { Py_XDECREF(m_ptr); return *this; }
200 
205  template <typename T> T cast() const;
207  explicit operator bool() const { return m_ptr != nullptr; }
212  PYBIND11_DEPRECATED("Use obj1.is(obj2) instead")
213  bool operator==(const handle &h) const { return m_ptr == h.m_ptr; }
214  PYBIND11_DEPRECATED("Use !obj1.is(obj2) instead")
215  bool operator!=(const handle &h) const { return m_ptr != h.m_ptr; }
216  PYBIND11_DEPRECATED("Use handle::operator bool() instead")
217  bool check() const { return m_ptr != nullptr; }
218 protected:
219  PyObject *m_ptr = nullptr;
220 };
221 
232 class object : public handle {
233 public:
234  object() = default;
235  PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()")
236  object(handle h, bool is_borrowed) : handle(h) { if (is_borrowed) inc_ref(); }
238  object(const object &o) : handle(o) { inc_ref(); }
240  object(object &&other) noexcept { m_ptr = other.m_ptr; other.m_ptr = nullptr; }
242  ~object() { dec_ref(); }
243 
250  PyObject *tmp = m_ptr;
251  m_ptr = nullptr;
252  return handle(tmp);
253  }
254 
255  object& operator=(const object &other) {
256  other.inc_ref();
257  dec_ref();
258  m_ptr = other.m_ptr;
259  return *this;
260  }
261 
262  object& operator=(object &&other) noexcept {
263  if (this != &other) {
264  handle temp(m_ptr);
265  m_ptr = other.m_ptr;
266  other.m_ptr = nullptr;
267  temp.dec_ref();
268  }
269  return *this;
270  }
271 
272  // Calling cast() on an object lvalue just copies (via handle::cast)
273  template <typename T> T cast() const &;
274  // Calling on an object rvalue does a move, if needed and/or possible
275  template <typename T> T cast() &&;
276 
277 protected:
278  // Tags for choosing constructors from raw PyObject *
279  struct borrowed_t { };
280  struct stolen_t { };
281 
282  template <typename T> friend T reinterpret_borrow(handle);
283  template <typename T> friend T reinterpret_steal(handle);
284 
285 public:
286  // Only accessible from derived classes and the reinterpret_* functions
287  object(handle h, borrowed_t) : handle(h) { inc_ref(); }
289 };
290 
304 template <typename T> T reinterpret_borrow(handle h) { return {h, object::borrowed_t{}}; }
305 
314 template <typename T> T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; }
315 
317 inline std::string error_string();
319 
320 class error_already_set : public std::runtime_error {
325 public:
328  error_already_set() : std::runtime_error(detail::error_string()) {
329  PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr());
330  }
331 
332  error_already_set(const error_already_set &) = default;
333  error_already_set(error_already_set &&) = default;
334 
335  inline ~error_already_set() override;
336 
340  void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); }
341 
348  void discard_as_unraisable(object err_context) {
349  restore();
350  PyErr_WriteUnraisable(err_context.ptr());
351  }
352  void discard_as_unraisable(const char *err_context) {
353  discard_as_unraisable(reinterpret_steal<object>(PYBIND11_FROM_STRING(err_context)));
354  }
355 
356  // Does nothing; provided for backwards compatibility.
357  PYBIND11_DEPRECATED("Use of error_already_set.clear() is deprecated")
358  void clear() {}
359 
363  bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); }
364 
365  const object& type() const { return m_type; }
366  const object& value() const { return m_value; }
367  const object& trace() const { return m_trace; }
368 
369 private:
370  object m_type, m_value, m_trace;
371 };
372 
384 bool isinstance(handle obj) { return T::check_(obj); }
385 
387 bool isinstance(handle obj) { return detail::isinstance_generic(obj, typeid(T)); }
388 
389 template <> inline bool isinstance<handle>(handle) = delete;
390 template <> inline bool isinstance<object>(handle obj) { return obj.ptr() != nullptr; }
391 
394 inline bool isinstance(handle obj, handle type) {
395  const auto result = PyObject_IsInstance(obj.ptr(), type.ptr());
396  if (result == -1)
397  throw error_already_set();
398  return result != 0;
399 }
400 
403 inline bool hasattr(handle obj, handle name) {
404  return PyObject_HasAttr(obj.ptr(), name.ptr()) == 1;
405 }
406 
407 inline bool hasattr(handle obj, const char *name) {
408  return PyObject_HasAttrString(obj.ptr(), name) == 1;
409 }
410 
411 inline void delattr(handle obj, handle name) {
412  if (PyObject_DelAttr(obj.ptr(), name.ptr()) != 0) { throw error_already_set(); }
413 }
414 
415 inline void delattr(handle obj, const char *name) {
416  if (PyObject_DelAttrString(obj.ptr(), name) != 0) { throw error_already_set(); }
417 }
418 
419 inline object getattr(handle obj, handle name) {
420  PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr());
421  if (!result) { throw error_already_set(); }
422  return reinterpret_steal<object>(result);
423 }
424 
425 inline object getattr(handle obj, const char *name) {
426  PyObject *result = PyObject_GetAttrString(obj.ptr(), name);
427  if (!result) { throw error_already_set(); }
428  return reinterpret_steal<object>(result);
429 }
430 
431 inline object getattr(handle obj, handle name, handle default_) {
432  if (PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr())) {
433  return reinterpret_steal<object>(result);
434  } else {
435  PyErr_Clear();
436  return reinterpret_borrow<object>(default_);
437  }
438 }
439 
440 inline object getattr(handle obj, const char *name, handle default_) {
441  if (PyObject *result = PyObject_GetAttrString(obj.ptr(), name)) {
442  return reinterpret_steal<object>(result);
443  } else {
444  PyErr_Clear();
445  return reinterpret_borrow<object>(default_);
446  }
447 }
448 
449 inline void setattr(handle obj, handle name, handle value) {
450  if (PyObject_SetAttr(obj.ptr(), name.ptr(), value.ptr()) != 0) { throw error_already_set(); }
451 }
452 
453 inline void setattr(handle obj, const char *name, handle value) {
454  if (PyObject_SetAttrString(obj.ptr(), name, value.ptr()) != 0) { throw error_already_set(); }
455 }
456 
457 inline ssize_t hash(handle obj) {
458  auto h = PyObject_Hash(obj.ptr());
459  if (h == -1) { throw error_already_set(); }
460  return h;
461 }
462 
464 
467  if (value) {
468 #if PY_MAJOR_VERSION >= 3
469  if (PyInstanceMethod_Check(value.ptr()))
470  value = PyInstanceMethod_GET_FUNCTION(value.ptr());
471  else
472 #endif
473  if (PyMethod_Check(value.ptr()))
474  value = PyMethod_GET_FUNCTION(value.ptr());
475  }
476  return value;
477 }
478 
479 // Helper aliases/functions to support implicit casting of values given to python accessors/methods.
480 // When given a pyobject, this simply returns the pyobject as-is; for other C++ type, the value goes
481 // through pybind11::cast(obj) to convert it to an `object`.
483 auto object_or_cast(T &&o) -> decltype(std::forward<T>(o)) { return std::forward<T>(o); }
484 // The following casting version is implemented in cast.h:
486 object object_or_cast(T &&o);
487 // Match a PyObject*, which we want to convert directly to handle via its converting constructor
488 inline handle object_or_cast(PyObject *ptr) { return ptr; }
489 
490 template <typename Policy>
491 class accessor : public object_api<accessor<Policy>> {
492  using key_type = typename Policy::key_type;
493 
494 public:
495  accessor(handle obj, key_type key) : obj(obj), key(std::move(key)) { }
496  accessor(const accessor &) = default;
497  accessor(accessor &&) = default;
498 
499  // accessor overload required to override default assignment operator (templates are not allowed
500  // to replace default compiler-generated assignments).
501  void operator=(const accessor &a) && { std::move(*this).operator=(handle(a)); }
502  void operator=(const accessor &a) & { operator=(handle(a)); }
503 
504  template <typename T> void operator=(T &&value) && {
505  Policy::set(obj, key, object_or_cast(std::forward<T>(value)));
506  }
507  template <typename T> void operator=(T &&value) & {
508  get_cache() = reinterpret_borrow<object>(object_or_cast(std::forward<T>(value)));
509  }
510 
511  template <typename T = Policy>
512  PYBIND11_DEPRECATED("Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)")
513  explicit operator enable_if_t<std::is_same<T, accessor_policies::str_attr>::value ||
514  std::is_same<T, accessor_policies::obj_attr>::value, bool>() const {
515  return hasattr(obj, key);
516  }
517  template <typename T = Policy>
518  PYBIND11_DEPRECATED("Use of obj[key] as bool is deprecated in favor of obj.contains(key)")
520  return obj.contains(key);
521  }
522 
523  operator object() const { return get_cache(); }
524  PyObject *ptr() const { return get_cache().ptr(); }
525  template <typename T> T cast() const { return get_cache().template cast<T>(); }
526 
527 private:
528  object &get_cache() const {
529  if (!cache) { cache = Policy::get(obj, key); }
530  return cache;
531  }
532 
533 private:
534  handle obj;
535  key_type key;
536  mutable object cache;
537 };
538 
540 struct obj_attr {
541  using key_type = object;
542  static object get(handle obj, handle key) { return getattr(obj, key); }
543  static void set(handle obj, handle key, handle val) { setattr(obj, key, val); }
544 };
545 
546 struct str_attr {
547  using key_type = const char *;
548  static object get(handle obj, const char *key) { return getattr(obj, key); }
549  static void set(handle obj, const char *key, handle val) { setattr(obj, key, val); }
550 };
551 
552 struct generic_item {
553  using key_type = object;
554 
555  static object get(handle obj, handle key) {
556  PyObject *result = PyObject_GetItem(obj.ptr(), key.ptr());
557  if (!result) { throw error_already_set(); }
558  return reinterpret_steal<object>(result);
559  }
560 
561  static void set(handle obj, handle key, handle val) {
562  if (PyObject_SetItem(obj.ptr(), key.ptr(), val.ptr()) != 0) { throw error_already_set(); }
563  }
564 };
565 
567  using key_type = size_t;
568 
569  static object get(handle obj, size_t index) {
570  PyObject *result = PySequence_GetItem(obj.ptr(), static_cast<ssize_t>(index));
571  if (!result) { throw error_already_set(); }
572  return reinterpret_steal<object>(result);
573  }
574 
575  static void set(handle obj, size_t index, handle val) {
576  // PySequence_SetItem does not steal a reference to 'val'
577  if (PySequence_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.ptr()) != 0) {
578  throw error_already_set();
579  }
580  }
581 };
582 
583 struct list_item {
584  using key_type = size_t;
585 
586  static object get(handle obj, size_t index) {
587  PyObject *result = PyList_GetItem(obj.ptr(), static_cast<ssize_t>(index));
588  if (!result) { throw error_already_set(); }
589  return reinterpret_borrow<object>(result);
590  }
591 
592  static void set(handle obj, size_t index, handle val) {
593  // PyList_SetItem steals a reference to 'val'
594  if (PyList_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.inc_ref().ptr()) != 0) {
595  throw error_already_set();
596  }
597  }
598 };
599 
600 struct tuple_item {
601  using key_type = size_t;
602 
603  static object get(handle obj, size_t index) {
604  PyObject *result = PyTuple_GetItem(obj.ptr(), static_cast<ssize_t>(index));
605  if (!result) { throw error_already_set(); }
606  return reinterpret_borrow<object>(result);
607  }
608 
609  static void set(handle obj, size_t index, handle val) {
610  // PyTuple_SetItem steals a reference to 'val'
611  if (PyTuple_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.inc_ref().ptr()) != 0) {
612  throw error_already_set();
613  }
614  }
615 };
617 
618 template <typename Policy>
620 class generic_iterator : public Policy {
622 
623 public:
625  using iterator_category = typename Policy::iterator_category;
626  using value_type = typename Policy::value_type;
627  using reference = typename Policy::reference;
628  using pointer = typename Policy::pointer;
629 
630  generic_iterator() = default;
631  generic_iterator(handle seq, ssize_t index) : Policy(seq, index) { }
632 
633  reference operator*() const { return Policy::dereference(); }
634  reference operator[](difference_type n) const { return *(*this + n); }
635  pointer operator->() const { return **this; }
636 
637  It &operator++() { Policy::increment(); return *this; }
638  It operator++(int) { auto copy = *this; Policy::increment(); return copy; }
639  It &operator--() { Policy::decrement(); return *this; }
640  It operator--(int) { auto copy = *this; Policy::decrement(); return copy; }
641  It &operator+=(difference_type n) { Policy::advance(n); return *this; }
642  It &operator-=(difference_type n) { Policy::advance(-n); return *this; }
643 
644  friend It operator+(const It &a, difference_type n) { auto copy = a; return copy += n; }
645  friend It operator+(difference_type n, const It &b) { return b + n; }
646  friend It operator-(const It &a, difference_type n) { auto copy = a; return copy -= n; }
647  friend difference_type operator-(const It &a, const It &b) { return a.distance_to(b); }
648 
649  friend bool operator==(const It &a, const It &b) { return a.equal(b); }
650  friend bool operator!=(const It &a, const It &b) { return !(a == b); }
651  friend bool operator< (const It &a, const It &b) { return b - a > 0; }
652  friend bool operator> (const It &a, const It &b) { return b < a; }
653  friend bool operator>=(const It &a, const It &b) { return !(a < b); }
654  friend bool operator<=(const It &a, const It &b) { return !(a > b); }
655 };
656 
657 PYBIND11_NAMESPACE_BEGIN(iterator_policies)
659 template <typename T>
660 struct arrow_proxy {
662 
663  arrow_proxy(T &&value) : value(std::move(value)) { }
664  T *operator->() const { return &value; }
665 };
666 
669 protected:
670  using iterator_category = std::random_access_iterator_tag;
672  using reference = const handle;
674 
675  sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { }
676 
677  reference dereference() const { return *ptr; }
678  void increment() { ++ptr; }
679  void decrement() { --ptr; }
680  void advance(ssize_t n) { ptr += n; }
681  bool equal(const sequence_fast_readonly &b) const { return ptr == b.ptr; }
682  ssize_t distance_to(const sequence_fast_readonly &b) const { return ptr - b.ptr; }
683 
684 private:
685  PyObject **ptr;
686 };
687 
690 protected:
691  using iterator_category = std::random_access_iterator_tag;
695 
696  sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) { }
697 
698  reference dereference() const { return {obj, static_cast<size_t>(index)}; }
699  void increment() { ++index; }
700  void decrement() { --index; }
701  void advance(ssize_t n) { index += n; }
702  bool equal(const sequence_slow_readwrite &b) const { return index == b.index; }
703  ssize_t distance_to(const sequence_slow_readwrite &b) const { return index - b.index; }
704 
705 private:
708 };
709 
712 protected:
713  using iterator_category = std::forward_iterator_tag;
714  using value_type = std::pair<handle, handle>;
715  using reference = const value_type;
717 
718  dict_readonly() = default;
719  dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); }
720 
721  reference dereference() const { return {key, value}; }
722  void increment() { if (!PyDict_Next(obj.ptr(), &pos, &key, &value)) { pos = -1; } }
723  bool equal(const dict_readonly &b) const { return pos == b.pos; }
724 
725 private:
727  PyObject *key = nullptr, *value = nullptr;
728  ssize_t pos = -1;
729 };
730 PYBIND11_NAMESPACE_END(iterator_policies)
731 
732 #if !defined(PYPY_VERSION)
735 #else
738 #endif
739 
742 
743 inline bool PyIterable_Check(PyObject *obj) {
744  PyObject *iter = PyObject_GetIter(obj);
745  if (iter) {
746  Py_DECREF(iter);
747  return true;
748  } else {
749  PyErr_Clear();
750  return false;
751  }
752 }
753 
754 inline bool PyNone_Check(PyObject *o) { return o == Py_None; }
755 inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; }
756 
757 inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); }
758 
759 inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; }
760 
761 class kwargs_proxy : public handle {
762 public:
763  explicit kwargs_proxy(handle h) : handle(h) { }
764 };
765 
766 class args_proxy : public handle {
767 public:
768  explicit args_proxy(handle h) : handle(h) { }
769  kwargs_proxy operator*() const { return kwargs_proxy(*this); }
770 };
771 
773 template <typename T> using is_keyword = std::is_base_of<arg, T>;
774 template <typename T> using is_s_unpacking = std::is_same<args_proxy, T>; // * unpacking
775 template <typename T> using is_ds_unpacking = std::is_same<kwargs_proxy, T>; // ** unpacking
776 template <typename T> using is_positional = satisfies_none_of<T,
778 >;
780 
781 // Call argument collector forward declarations
782 template <return_value_policy policy = return_value_policy::automatic_reference>
783 class simple_collector;
784 template <return_value_policy policy = return_value_policy::automatic_reference>
785 class unpacking_collector;
786 
788 
789 // TODO: After the deprecated constructors are removed, this macro can be simplified by
790 // inheriting ctors: `using Parent::Parent`. It's not an option right now because
791 // the `using` statement triggers the parent deprecation warning even if the ctor
792 // isn't even used.
793 #define PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
794  public: \
795  PYBIND11_DEPRECATED("Use reinterpret_borrow<"#Name">() or reinterpret_steal<"#Name">()") \
796  Name(handle h, bool is_borrowed) : Parent(is_borrowed ? Parent(h, borrowed_t{}) : Parent(h, stolen_t{})) { } \
797  Name(handle h, borrowed_t) : Parent(h, borrowed_t{}) { } \
798  Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \
799  PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \
800  bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \
801  static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \
802  template <typename Policy_> \
803  Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
804 
805 #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
806  PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
807  /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
808  Name(const object &o) \
809  : Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
810  { if (!m_ptr) throw error_already_set(); } \
811  Name(object &&o) \
812  : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
813  { if (!m_ptr) throw error_already_set(); }
814 
815 #define PYBIND11_OBJECT(Name, Parent, CheckFun) \
816  PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
817  /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
818  Name(const object &o) : Parent(o) { } \
819  Name(object &&o) : Parent(std::move(o)) { }
820 
821 #define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \
822  PYBIND11_OBJECT(Name, Parent, CheckFun) \
823  Name() : Parent() { }
824 
827 
836 class iterator : public object {
837 public:
838  using iterator_category = std::input_iterator_tag;
841  using reference = const handle;
842  using pointer = const handle *;
843 
844  PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)
845 
846  iterator& operator++() {
847  advance();
848  return *this;
849  }
850 
852  auto rv = *this;
853  advance();
854  return rv;
855  }
856 
858  if (m_ptr && !value.ptr()) {
859  auto& self = const_cast<iterator &>(*this);
860  self.advance();
861  }
862  return value;
863  }
864 
865  pointer operator->() const { operator*(); return &value; }
866 
880  static iterator sentinel() { return {}; }
881 
882  friend bool operator==(const iterator &a, const iterator &b) { return a->ptr() == b->ptr(); }
883  friend bool operator!=(const iterator &a, const iterator &b) { return a->ptr() != b->ptr(); }
884 
885 private:
886  void advance() {
887  value = reinterpret_steal<object>(PyIter_Next(m_ptr));
888  if (PyErr_Occurred()) { throw error_already_set(); }
889  }
890 
891 private:
892  object value = {};
893 };
894 
895 
896 
897 class type : public object {
898 public:
899  PYBIND11_OBJECT(type, object, PyType_Check)
900 
901 
902  static handle handle_of(handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); }
903 
905  static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); }
906 
907  // Defined in pybind11/cast.h
911  template<typename T>
912  static handle handle_of();
913 
917  template<typename T>
918  static type of() {return type(type::handle_of<T>(), borrowed_t{}); }
919 };
920 
921 class iterable : public object {
922 public:
924 };
925 
926 class bytes;
927 
928 class str : public object {
929 public:
931 
932  str(const char *c, size_t n)
933  : object(PyUnicode_FromStringAndSize(c, (ssize_t) n), stolen_t{}) {
934  if (!m_ptr) pybind11_fail("Could not allocate string object!");
935  }
936 
937  // 'explicit' is explicitly omitted from the following constructors to allow implicit conversion to py::str from C++ string-like objects
938  str(const char *c = "")
939  : object(PyUnicode_FromString(c), stolen_t{}) {
940  if (!m_ptr) pybind11_fail("Could not allocate string object!");
941  }
942 
943  str(const std::string &s) : str(s.data(), s.size()) { }
944 
945  explicit str(const bytes &b);
946 
951  explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); }
952 
953  operator std::string() const {
954  object temp = *this;
955  if (PyUnicode_Check(m_ptr)) {
956  temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(m_ptr));
957  if (!temp)
958  pybind11_fail("Unable to extract string contents! (encoding issue)");
959  }
960  char *buffer;
961  ssize_t length;
962  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
963  pybind11_fail("Unable to extract string contents! (invalid type)");
964  return std::string(buffer, (size_t) length);
965  }
966 
967  template <typename... Args>
968  str format(Args &&...args) const {
969  return attr("format")(std::forward<Args>(args)...);
970  }
971 
972 private:
974  static PyObject *raw_str(PyObject *op) {
975  PyObject *str_value = PyObject_Str(op);
976 #if PY_MAJOR_VERSION < 3
977  if (!str_value) throw error_already_set();
978  PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
979  Py_XDECREF(str_value); str_value = unicode;
980 #endif
981  return str_value;
982  }
983 };
985 
986 inline namespace literals {
990 inline str operator"" _s(const char *s, size_t size) { return {s, size}; }
991 } // namespace literals
992 
995 class bytes : public object {
996 public:
998 
999  // Allow implicit conversion:
1000  bytes(const char *c = "")
1001  : object(PYBIND11_BYTES_FROM_STRING(c), stolen_t{}) {
1002  if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
1003  }
1004 
1005  bytes(const char *c, size_t n)
1007  if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
1008  }
1009 
1010  // Allow implicit conversion:
1011  bytes(const std::string &s) : bytes(s.data(), s.size()) { }
1012 
1013  explicit bytes(const pybind11::str &s);
1014 
1015  operator std::string() const {
1016  char *buffer;
1017  ssize_t length;
1018  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length))
1019  pybind11_fail("Unable to extract bytes contents!");
1020  return std::string(buffer, (size_t) length);
1021  }
1022 };
1023 // Note: breathe >= 4.17.0 will fail to build docs if the below two constructors
1024 // are included in the doxygen group; close here and reopen after as a workaround
1026 
1027 inline bytes::bytes(const pybind11::str &s) {
1028  object temp = s;
1029  if (PyUnicode_Check(s.ptr())) {
1030  temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
1031  if (!temp)
1032  pybind11_fail("Unable to extract string contents! (encoding issue)");
1033  }
1034  char *buffer;
1035  ssize_t length;
1036  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
1037  pybind11_fail("Unable to extract string contents! (invalid type)");
1038  auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length));
1039  if (!obj)
1040  pybind11_fail("Could not allocate bytes object!");
1041  m_ptr = obj.release().ptr();
1042 }
1043 
1044 inline str::str(const bytes& b) {
1045  char *buffer;
1046  ssize_t length;
1047  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length))
1048  pybind11_fail("Unable to extract bytes contents!");
1049  auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, (ssize_t) length));
1050  if (!obj)
1051  pybind11_fail("Could not allocate string object!");
1052  m_ptr = obj.release().ptr();
1053 }
1054 
1057 class none : public object {
1058 public:
1060  none() : object(Py_None, borrowed_t{}) { }
1061 };
1062 
1063 class ellipsis : public object {
1064 public:
1066  ellipsis() : object(Py_Ellipsis, borrowed_t{}) { }
1067 };
1068 
1069 class bool_ : public object {
1070 public:
1071  PYBIND11_OBJECT_CVT(bool_, object, PyBool_Check, raw_bool)
1072  bool_() : object(Py_False, borrowed_t{}) { }
1073  // Allow implicit conversion from and to `bool`:
1074  bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { }
1075  operator bool() const { return m_ptr && PyLong_AsLong(m_ptr) != 0; }
1076 
1077 private:
1079  static PyObject *raw_bool(PyObject *op) {
1080  const auto value = PyObject_IsTrue(op);
1081  if (value == -1) return nullptr;
1082  return handle(value ? Py_True : Py_False).inc_ref().ptr();
1083  }
1084 };
1085 
1087 // Converts a value to the given unsigned type. If an error occurs, you get back (Unsigned) -1;
1088 // otherwise you get back the unsigned long or unsigned long long value cast to (Unsigned).
1089 // (The distinction is critically important when casting a returned -1 error value to some other
1090 // unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
1091 template <typename Unsigned>
1092 Unsigned as_unsigned(PyObject *o) {
1093  if (sizeof(Unsigned) <= sizeof(unsigned long)
1094 #if PY_VERSION_HEX < 0x03000000
1095  || PyInt_Check(o)
1096 #endif
1097  ) {
1098  unsigned long v = PyLong_AsUnsignedLong(o);
1099  return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1100  }
1101  else {
1102  unsigned long long v = PyLong_AsUnsignedLongLong(o);
1103  return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1104  }
1105 }
1107 
1108 class int_ : public object {
1109 public:
1110  PYBIND11_OBJECT_CVT(int_, object, PYBIND11_LONG_CHECK, PyNumber_Long)
1111  int_() : object(PyLong_FromLong(0), stolen_t{}) { }
1112  // Allow implicit conversion from C++ integral types:
1113  template <typename T,
1116  if (sizeof(T) <= sizeof(long)) {
1118  m_ptr = PyLong_FromLong((long) value);
1119  else
1120  m_ptr = PyLong_FromUnsignedLong((unsigned long) value);
1121  } else {
1123  m_ptr = PyLong_FromLongLong((long long) value);
1124  else
1125  m_ptr = PyLong_FromUnsignedLongLong((unsigned long long) value);
1126  }
1127  if (!m_ptr) pybind11_fail("Could not allocate int object!");
1128  }
1129 
1130  template <typename T,
1131  detail::enable_if_t<std::is_integral<T>::value, int> = 0>
1132  operator T() const {
1134  ? detail::as_unsigned<T>(m_ptr)
1135  : sizeof(T) <= sizeof(long)
1136  ? (T) PyLong_AsLong(m_ptr)
1137  : (T) PYBIND11_LONG_AS_LONGLONG(m_ptr);
1138  }
1139 };
1140 
1141 class float_ : public object {
1142 public:
1143  PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float)
1144  // Allow implicit conversion from float/double:
1145  float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1146  if (!m_ptr) pybind11_fail("Could not allocate float object!");
1147  }
1148  float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1149  if (!m_ptr) pybind11_fail("Could not allocate float object!");
1150  }
1151  operator float() const { return (float) PyFloat_AsDouble(m_ptr); }
1152  operator double() const { return (double) PyFloat_AsDouble(m_ptr); }
1153 };
1154 
1155 class weakref : public object {
1156 public:
1157  PYBIND11_OBJECT_DEFAULT(weakref, object, PyWeakref_Check)
1158  explicit weakref(handle obj, handle callback = {})
1159  : object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), stolen_t{}) {
1160  if (!m_ptr) pybind11_fail("Could not allocate weak reference!");
1161  }
1162 };
1163 
1164 class slice : public object {
1165 public:
1166  PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
1167  slice(ssize_t start_, ssize_t stop_, ssize_t step_) {
1168  int_ start(start_), stop(stop_), step(step_);
1169  m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr());
1170  if (!m_ptr) pybind11_fail("Could not allocate slice object!");
1171  }
1172  bool compute(size_t length, size_t *start, size_t *stop, size_t *step,
1173  size_t *slicelength) const {
1174  return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr,
1175  (ssize_t) length, (ssize_t *) start,
1176  (ssize_t *) stop, (ssize_t *) step,
1177  (ssize_t *) slicelength) == 0;
1178  }
1179  bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step,
1180  ssize_t *slicelength) const {
1181  return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr,
1182  length, start,
1183  stop, step,
1184  slicelength) == 0;
1185  }
1186 };
1187 
1188 class capsule : public object {
1189 public:
1190  PYBIND11_OBJECT_DEFAULT(capsule, object, PyCapsule_CheckExact)
1191  PYBIND11_DEPRECATED("Use reinterpret_borrow<capsule>() or reinterpret_steal<capsule>()")
1192  capsule(PyObject *ptr, bool is_borrowed) : object(is_borrowed ? object(ptr, borrowed_t{}) : object(ptr, stolen_t{})) { }
1193 
1194  explicit capsule(const void *value, const char *name = nullptr, void (*destructor)(PyObject *) = nullptr)
1195  : object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
1196  if (!m_ptr)
1197  pybind11_fail("Could not allocate capsule object!");
1198  }
1199 
1200  PYBIND11_DEPRECATED("Please pass a destructor that takes a void pointer as input")
1201  capsule(const void *value, void (*destruct)(PyObject *))
1202  : object(PyCapsule_New(const_cast<void*>(value), nullptr, destruct), stolen_t{}) {
1203  if (!m_ptr)
1204  pybind11_fail("Could not allocate capsule object!");
1205  }
1206 
1207  capsule(const void *value, void (*destructor)(void *)) {
1208  m_ptr = PyCapsule_New(const_cast<void *>(value), nullptr, [](PyObject *o) {
1209  auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
1210  void *ptr = PyCapsule_GetPointer(o, nullptr);
1211  destructor(ptr);
1212  });
1213 
1214  if (!m_ptr)
1215  pybind11_fail("Could not allocate capsule object!");
1216 
1217  if (PyCapsule_SetContext(m_ptr, (void *) destructor) != 0)
1218  pybind11_fail("Could not set capsule context!");
1219  }
1220 
1221  capsule(void (*destructor)()) {
1222  m_ptr = PyCapsule_New(reinterpret_cast<void *>(destructor), nullptr, [](PyObject *o) {
1223  auto destructor = reinterpret_cast<void (*)()>(PyCapsule_GetPointer(o, nullptr));
1224  destructor();
1225  });
1226 
1227  if (!m_ptr)
1228  pybind11_fail("Could not allocate capsule object!");
1229  }
1230 
1231  template <typename T> operator T *() const {
1232  auto name = this->name();
1233  T * result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
1234  if (!result) pybind11_fail("Unable to extract capsule contents!");
1235  return result;
1236  }
1237 
1238  const char *name() const { return PyCapsule_GetName(m_ptr); }
1239 };
1240 
1241 class tuple : public object {
1242 public:
1243  PYBIND11_OBJECT_CVT(tuple, object, PyTuple_Check, PySequence_Tuple)
1244  explicit tuple(size_t size = 0) : object(PyTuple_New((ssize_t) size), stolen_t{}) {
1245  if (!m_ptr) pybind11_fail("Could not allocate tuple object!");
1246  }
1247  size_t size() const { return (size_t) PyTuple_Size(m_ptr); }
1248  bool empty() const { return size() == 0; }
1249  detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
1250  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1251  detail::tuple_iterator begin() const { return {*this, 0}; }
1252  detail::tuple_iterator end() const { return {*this, PyTuple_GET_SIZE(m_ptr)}; }
1253 };
1254 
1255 class dict : public object {
1256 public:
1257  PYBIND11_OBJECT_CVT(dict, object, PyDict_Check, raw_dict)
1258  dict() : object(PyDict_New(), stolen_t{}) {
1259  if (!m_ptr) pybind11_fail("Could not allocate dict object!");
1260  }
1261  template <typename... Args,
1262  typename = detail::enable_if_t<detail::all_of<detail::is_keyword_or_ds<Args>...>::value>,
1263  // MSVC workaround: it can't compile an out-of-line definition, so defer the collector
1264  typename collector = detail::deferred_t<detail::unpacking_collector<>, Args...>>
1265  explicit dict(Args &&...args) : dict(collector(std::forward<Args>(args)...).kwargs()) { }
1266 
1267  size_t size() const { return (size_t) PyDict_Size(m_ptr); }
1268  bool empty() const { return size() == 0; }
1269  detail::dict_iterator begin() const { return {*this, 0}; }
1270  detail::dict_iterator end() const { return {}; }
1271  void clear() const { PyDict_Clear(ptr()); }
1272  template <typename T> bool contains(T &&key) const {
1273  return PyDict_Contains(m_ptr, detail::object_or_cast(std::forward<T>(key)).ptr()) == 1;
1274  }
1275 
1276 private:
1278  static PyObject *raw_dict(PyObject *op) {
1279  if (PyDict_Check(op))
1280  return handle(op).inc_ref().ptr();
1281  return PyObject_CallFunctionObjArgs((PyObject *) &PyDict_Type, op, nullptr);
1282  }
1283 };
1284 
1285 class sequence : public object {
1286 public:
1287  PYBIND11_OBJECT_DEFAULT(sequence, object, PySequence_Check)
1288  size_t size() const {
1289  ssize_t result = PySequence_Size(m_ptr);
1290  if (result == -1)
1291  throw error_already_set();
1292  return (size_t) result;
1293  }
1294  bool empty() const { return size() == 0; }
1295  detail::sequence_accessor operator[](size_t index) const { return {*this, index}; }
1296  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1297  detail::sequence_iterator begin() const { return {*this, 0}; }
1298  detail::sequence_iterator end() const { return {*this, PySequence_Size(m_ptr)}; }
1299 };
1300 
1301 class list : public object {
1302 public:
1303  PYBIND11_OBJECT_CVT(list, object, PyList_Check, PySequence_List)
1304  explicit list(size_t size = 0) : object(PyList_New((ssize_t) size), stolen_t{}) {
1305  if (!m_ptr) pybind11_fail("Could not allocate list object!");
1306  }
1307  size_t size() const { return (size_t) PyList_Size(m_ptr); }
1308  bool empty() const { return size() == 0; }
1309  detail::list_accessor operator[](size_t index) const { return {*this, index}; }
1310  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1311  detail::list_iterator begin() const { return {*this, 0}; }
1312  detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
1313  template <typename T> void append(T &&val) const {
1314  PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
1315  }
1316  template <typename T> void insert(size_t index, T &&val) const {
1317  PyList_Insert(m_ptr, static_cast<ssize_t>(index),
1318  detail::object_or_cast(std::forward<T>(val)).ptr());
1319  }
1320 };
1321 
1322 class args : public tuple { PYBIND11_OBJECT_DEFAULT(args, tuple, PyTuple_Check) };
1323 class kwargs : public dict { PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check) };
1324 
1325 class set : public object {
1326 public:
1327  PYBIND11_OBJECT_CVT(set, object, PySet_Check, PySet_New)
1328  set() : object(PySet_New(nullptr), stolen_t{}) {
1329  if (!m_ptr) pybind11_fail("Could not allocate set object!");
1330  }
1331  size_t size() const { return (size_t) PySet_Size(m_ptr); }
1332  bool empty() const { return size() == 0; }
1333  template <typename T> bool add(T &&val) const {
1334  return PySet_Add(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 0;
1335  }
1336  void clear() const { PySet_Clear(m_ptr); }
1337  template <typename T> bool contains(T &&val) const {
1338  return PySet_Contains(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 1;
1339  }
1340 };
1341 
1342 class function : public object {
1343 public:
1344  PYBIND11_OBJECT_DEFAULT(function, object, PyCallable_Check)
1346  handle fun = detail::get_function(m_ptr);
1347  if (fun && PyCFunction_Check(fun.ptr()))
1348  return fun;
1349  return handle();
1350  }
1351  bool is_cpp_function() const { return (bool) cpp_function(); }
1352 };
1353 
1354 class staticmethod : public object {
1355 public:
1356  PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New)
1357 };
1358 
1359 class buffer : public object {
1360 public:
1361  PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)
1362 
1363  buffer_info request(bool writable = false) const {
1364  int flags = PyBUF_STRIDES | PyBUF_FORMAT;
1365  if (writable) flags |= PyBUF_WRITABLE;
1366  auto *view = new Py_buffer();
1367  if (PyObject_GetBuffer(m_ptr, view, flags) != 0) {
1368  delete view;
1369  throw error_already_set();
1370  }
1371  return buffer_info(view);
1372  }
1373 };
1374 
1375 class memoryview : public object {
1376 public:
1377  PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject)
1378 
1379 
1388  explicit memoryview(const buffer_info& info) {
1389  if (!info.view())
1390  pybind11_fail("Prohibited to create memoryview without Py_buffer");
1391  // Note: PyMemoryView_FromBuffer never increments obj reference.
1392  m_ptr = (info.view()->obj) ?
1393  PyMemoryView_FromObject(info.view()->obj) :
1394  PyMemoryView_FromBuffer(info.view());
1395  if (!m_ptr)
1396  pybind11_fail("Unable to create memoryview from buffer descriptor");
1397  }
1398 
1422  static memoryview from_buffer(
1423  void *ptr, ssize_t itemsize, const char *format,
1424  detail::any_container<ssize_t> shape,
1425  detail::any_container<ssize_t> strides, bool readonly = false);
1426 
1428  const void *ptr, ssize_t itemsize, const char *format,
1429  detail::any_container<ssize_t> shape,
1430  detail::any_container<ssize_t> strides) {
1431  return memoryview::from_buffer(
1432  const_cast<void*>(ptr), itemsize, format, shape, strides, true);
1433  }
1434 
1435  template<typename T>
1437  T *ptr, detail::any_container<ssize_t> shape,
1438  detail::any_container<ssize_t> strides, bool readonly = false) {
1439  return memoryview::from_buffer(
1440  reinterpret_cast<void*>(ptr), sizeof(T),
1441  format_descriptor<T>::value, shape, strides, readonly);
1442  }
1443 
1444  template<typename T>
1446  const T *ptr, detail::any_container<ssize_t> shape,
1447  detail::any_container<ssize_t> strides) {
1448  return memoryview::from_buffer(
1449  const_cast<T*>(ptr), shape, strides, true);
1450  }
1451 
1452 #if PY_MAJOR_VERSION >= 3
1453 
1466  static memoryview from_memory(void *mem, ssize_t size, bool readonly = false) {
1467  PyObject* ptr = PyMemoryView_FromMemory(
1468  reinterpret_cast<char*>(mem), size,
1469  (readonly) ? PyBUF_READ : PyBUF_WRITE);
1470  if (!ptr)
1471  pybind11_fail("Could not allocate memoryview object!");
1472  return memoryview(object(ptr, stolen_t{}));
1473  }
1474 
1475  static memoryview from_memory(const void *mem, ssize_t size) {
1476  return memoryview::from_memory(const_cast<void*>(mem), size, true);
1477  }
1478 #endif
1479 };
1480 
1481 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1483  void *ptr, ssize_t itemsize, const char* format,
1484  detail::any_container<ssize_t> shape,
1485  detail::any_container<ssize_t> strides, bool readonly) {
1486  size_t ndim = shape->size();
1487  if (ndim != strides->size())
1488  pybind11_fail("memoryview: shape length doesn't match strides length");
1489  ssize_t size = ndim ? 1 : 0;
1490  for (size_t i = 0; i < ndim; ++i)
1491  size *= (*shape)[i];
1492  Py_buffer view;
1493  view.buf = ptr;
1494  view.obj = nullptr;
1495  view.len = size * itemsize;
1496  view.readonly = static_cast<int>(readonly);
1497  view.itemsize = itemsize;
1498  view.format = const_cast<char*>(format);
1499  view.ndim = static_cast<int>(ndim);
1500  view.shape = shape->data();
1501  view.strides = strides->data();
1502  view.suboffsets = nullptr;
1503  view.internal = nullptr;
1504  PyObject* obj = PyMemoryView_FromBuffer(&view);
1505  if (!obj)
1506  throw error_already_set();
1507  return memoryview(object(obj, stolen_t{}));
1508 }
1509 #endif // DOXYGEN_SHOULD_SKIP_THIS
1510 
1514 inline size_t len(handle h) {
1515  ssize_t result = PyObject_Length(h.ptr());
1516  if (result < 0)
1517  pybind11_fail("Unable to compute length of object");
1518  return (size_t) result;
1519 }
1520 
1521 inline size_t len_hint(handle h) {
1522 #if PY_VERSION_HEX >= 0x03040000
1523  ssize_t result = PyObject_LengthHint(h.ptr(), 0);
1524 #else
1525  ssize_t result = PyObject_Length(h.ptr());
1526 #endif
1527  if (result < 0) {
1528  // Sometimes a length can't be determined at all (eg generators)
1529  // In which case simply return 0
1530  PyErr_Clear();
1531  return 0;
1532  }
1533  return (size_t) result;
1534 }
1535 
1536 inline str repr(handle h) {
1537  PyObject *str_value = PyObject_Repr(h.ptr());
1538  if (!str_value) throw error_already_set();
1539 #if PY_MAJOR_VERSION < 3
1540  PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
1541  Py_XDECREF(str_value); str_value = unicode;
1542  if (!str_value) throw error_already_set();
1543 #endif
1544  return reinterpret_steal<str>(str_value);
1545 }
1546 
1547 inline iterator iter(handle obj) {
1548  PyObject *result = PyObject_GetIter(obj.ptr());
1549  if (!result) { throw error_already_set(); }
1550  return reinterpret_steal<iterator>(result);
1551 }
1553 
1555 template <typename D> iterator object_api<D>::begin() const { return iter(derived()); }
1556 template <typename D> iterator object_api<D>::end() const { return iterator::sentinel(); }
1557 template <typename D> item_accessor object_api<D>::operator[](handle key) const {
1558  return {derived(), reinterpret_borrow<object>(key)};
1559 }
1560 template <typename D> item_accessor object_api<D>::operator[](const char *key) const {
1561  return {derived(), pybind11::str(key)};
1562 }
1563 template <typename D> obj_attr_accessor object_api<D>::attr(handle key) const {
1564  return {derived(), reinterpret_borrow<object>(key)};
1565 }
1566 template <typename D> str_attr_accessor object_api<D>::attr(const char *key) const {
1567  return {derived(), key};
1568 }
1569 template <typename D> args_proxy object_api<D>::operator*() const {
1570  return args_proxy(derived().ptr());
1571 }
1572 template <typename D> template <typename T> bool object_api<D>::contains(T &&item) const {
1573  return attr("__contains__")(std::forward<T>(item)).template cast<bool>();
1574 }
1575 
1576 template <typename D>
1577 pybind11::str object_api<D>::str() const { return pybind11::str(derived()); }
1578 
1579 template <typename D>
1580 str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); }
1581 
1582 template <typename D>
1583 PYBIND11_DEPRECATED("Use py::type::of(h) instead of h.get_type()")
1584 handle object_api<D>::get_type() const { return type::handle_of(*this); }
1585 
1586 template <typename D>
1587 bool object_api<D>::rich_compare(object_api const &other, int value) const {
1588  int rv = PyObject_RichCompareBool(derived().ptr(), other.derived().ptr(), value);
1589  if (rv == -1)
1590  throw error_already_set();
1591  return rv == 1;
1592 }
1593 
1594 #define PYBIND11_MATH_OPERATOR_UNARY(op, fn) \
1595  template <typename D> object object_api<D>::op() const { \
1596  object result = reinterpret_steal<object>(fn(derived().ptr())); \
1597  if (!result.ptr()) \
1598  throw error_already_set(); \
1599  return result; \
1600  }
1601 
1602 #define PYBIND11_MATH_OPERATOR_BINARY(op, fn) \
1603  template <typename D> \
1604  object object_api<D>::op(object_api const &other) const { \
1605  object result = reinterpret_steal<object>( \
1606  fn(derived().ptr(), other.derived().ptr())); \
1607  if (!result.ptr()) \
1608  throw error_already_set(); \
1609  return result; \
1610  }
1611 
1612 PYBIND11_MATH_OPERATOR_UNARY (operator~, PyNumber_Invert)
1613 PYBIND11_MATH_OPERATOR_UNARY (operator-, PyNumber_Negative)
1614 PYBIND11_MATH_OPERATOR_BINARY(operator+, PyNumber_Add)
1615 PYBIND11_MATH_OPERATOR_BINARY(operator+=, PyNumber_InPlaceAdd)
1616 PYBIND11_MATH_OPERATOR_BINARY(operator-, PyNumber_Subtract)
1617 PYBIND11_MATH_OPERATOR_BINARY(operator-=, PyNumber_InPlaceSubtract)
1618 PYBIND11_MATH_OPERATOR_BINARY(operator*, PyNumber_Multiply)
1619 PYBIND11_MATH_OPERATOR_BINARY(operator*=, PyNumber_InPlaceMultiply)
1620 PYBIND11_MATH_OPERATOR_BINARY(operator/, PyNumber_TrueDivide)
1621 PYBIND11_MATH_OPERATOR_BINARY(operator/=, PyNumber_InPlaceTrueDivide)
1622 PYBIND11_MATH_OPERATOR_BINARY(operator|, PyNumber_Or)
1623 PYBIND11_MATH_OPERATOR_BINARY(operator|=, PyNumber_InPlaceOr)
1624 PYBIND11_MATH_OPERATOR_BINARY(operator&, PyNumber_And)
1625 PYBIND11_MATH_OPERATOR_BINARY(operator&=, PyNumber_InPlaceAnd)
1626 PYBIND11_MATH_OPERATOR_BINARY(operator^, PyNumber_Xor)
1627 PYBIND11_MATH_OPERATOR_BINARY(operator^=, PyNumber_InPlaceXor)
1628 PYBIND11_MATH_OPERATOR_BINARY(operator<<, PyNumber_Lshift)
1629 PYBIND11_MATH_OPERATOR_BINARY(operator<<=, PyNumber_InPlaceLshift)
1630 PYBIND11_MATH_OPERATOR_BINARY(operator>>, PyNumber_Rshift)
1631 PYBIND11_MATH_OPERATOR_BINARY(operator>>=, PyNumber_InPlaceRshift)
1632 
1633 #undef PYBIND11_MATH_OPERATOR_UNARY
1634 #undef PYBIND11_MATH_OPERATOR_BINARY
1635 
Quick proxy class needed to implement operator-> for iterators which can&#39;t return pointers...
Definition: pytypes.h:660
ssize_t hash(handle obj)
Definition: pytypes.h:457
Vector3_ operator*(const Double_ &s, const Vector3_ &v)
PyObject *& ptr()
Definition: pytypes.h:185
typename Policy::pointer pointer
Definition: pytypes.h:628
def step(data, isam, result, truth, currPoseIndex)
Definition: visual_isam.py:82
void advance(ssize_t n)
Definition: pytypes.h:701
static iterator sentinel()
Definition: pytypes.h:880
str format(Args &&...args) const
Definition: pytypes.h:968
static type of()
Definition: pytypes.h:918
generic_iterator< iterator_policies::sequence_slow_readwrite > sequence_iterator
Definition: pytypes.h:740
reference operator[](difference_type n) const
Definition: pytypes.h:634
static handle handle_of()
Definition: cast.h:2209
accessor< accessor_policies::list_item > list_accessor
Definition: pytypes.h:43
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1310
bool PyNone_Check(PyObject *o)
Definition: pytypes.h:754
typename Policy::reference reference
Definition: pytypes.h:627
args_proxy operator*() const
Definition: pytypes.h:1569
bool hasattr(handle obj, handle name)
Definition: pytypes.h:403
void discard_as_unraisable(const char *err_context)
Definition: pytypes.h:352
size_t size() const
Definition: pytypes.h:1307
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator*=(half &a, const half &b)
Definition: Half.h:289
handle obj
Definition: pytypes.h:726
friend It operator+(const It &a, difference_type n)
Definition: pytypes.h:644
Scalar * b
Definition: benchVecAdd.cpp:17
friend It operator-(const It &a, difference_type n)
Definition: pytypes.h:646
detail::sequence_accessor operator[](size_t index) const
Definition: pytypes.h:1295
ssize_t pos
Definition: pytypes.h:728
void append(T &&val) const
Definition: pytypes.h:1313
EIGEN_DEVICE_FUNC const CwiseBinaryOp< internal::scalar_boolean_xor_op, const Derived, const OtherDerived > operator^(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
bytes(const std::string &s)
Definition: pytypes.h:1011
std::is_same< args_proxy, T > is_s_unpacking
Definition: pytypes.h:774
bool operator>(object_api const &other) const
Definition: pytypes.h:123
bool operator<(const benchmark_t &b1, const benchmark_t &b2)
detail::tuple_iterator end() const
Definition: pytypes.h:1252
pointer operator->() const
Definition: pytypes.h:635
bool isinstance_generic(handle obj, const std::type_info &tp)
Definition: cast.h:403
bool isinstance< handle >(handle)=delete
static PyObject * raw_bool(PyObject *op)
Return the truth value of an object – always returns a new reference.
Definition: pytypes.h:1079
bool operator<=(object_api const &other) const
Definition: pytypes.h:122
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator-=(half &a, const half &b)
Definition: Half.h:293
object(handle h, stolen_t)
Definition: pytypes.h:288
bool equal(const dict_readonly &b) const
Definition: pytypes.h:723
accessor(handle obj, key_type key)
Definition: pytypes.h:495
size_t size() const
Definition: pytypes.h:1267
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseBinaryOp< internal::scalar_quotient_op< Scalar, typename OtherDerived::Scalar >, const Derived, const OtherDerived > operator/(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
object(handle h, borrowed_t)
Definition: pytypes.h:287
const mpreal operator>>(const mpreal &v, const unsigned long int k)
Definition: mpreal.h:1589
void restore()
Definition: pytypes.h:340
ssize_t difference_type
Definition: pytypes.h:839
bool equal(const sequence_slow_readwrite &b) const
Definition: pytypes.h:702
ArrayXcf v
Definition: Cwise_arg.cpp:1
reference dereference() const
Definition: pytypes.h:698
int ref_count() const
Return the object&#39;s current reference count.
Definition: pytypes.h:154
Definition: pytypes.h:1322
object(object &&other) noexcept
Move constructor; steals the object from other and preserves its reference count. ...
Definition: pytypes.h:240
Definition: cast.h:1870
std::string error_string()
Definition: cast.h:410
It operator--(int)
Definition: pytypes.h:640
int n
friend bool operator<=(const It &a, const It &b)
Definition: pytypes.h:654
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 contains(T &&key) const
Definition: pytypes.h:1272
bytes(const char *c="")
Definition: pytypes.h:1000
object m_value
Definition: pytypes.h:370
sequence_slow_readwrite(handle obj, ssize_t index)
Definition: pytypes.h:696
bool empty() const
Definition: pytypes.h:1332
bool add(T &&val) const
Definition: pytypes.h:1333
friend bool operator>=(const It &a, const It &b)
Definition: pytypes.h:653
typename Policy::iterator_category iterator_category
Definition: pytypes.h:625
Definition: Half.h:150
accessor< accessor_policies::sequence_item > sequence_accessor
Definition: pytypes.h:42
iterator iter(handle obj)
Definition: pytypes.h:1547
It & operator+=(difference_type n)
Definition: pytypes.h:641
bool rich_compare(object_api const &other, int value) const
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>(const half &a, const half &b)
Definition: Half.h:313
friend bool operator!=(const iterator &a, const iterator &b)
Definition: pytypes.h:883
bytes(const char *c, size_t n)
Definition: pytypes.h:1005
item_accessor operator[](handle key) const
Definition: pytypes.h:1557
It & operator++()
Definition: pytypes.h:637
STL iterator template used for tuple, list, sequence and dict.
Definition: pytypes.h:620
bool isinstance(handle obj)
Definition: pytypes.h:384
Definition: cast.h:1853
void delattr(handle obj, handle name)
Definition: pytypes.h:411
bool contains(T &&val) const
Definition: pytypes.h:1337
str_attr_accessor doc() const
Get or set the object&#39;s docstring, i.e. obj.__doc__.
Definition: pytypes.h:1580
str(const std::string &s)
Definition: pytypes.h:943
bool PyStaticMethod_Check(PyObject *o)
Definition: pytypes.h:759
friend bool operator==(const iterator &a, const iterator &b)
Definition: pytypes.h:882
It & operator-=(difference_type n)
Definition: pytypes.h:642
const Derived & derived() const
Definition: pytypes.h:56
Definition: pytypes.h:1057
Signature operator|(const DiscreteKey &key, const DiscreteKey &parent)
Definition: Signature.cpp:208
T * operator->() const
Definition: pytypes.h:664
size_t key_type
Definition: pytypes.h:601
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun)
Definition: pytypes.h:805
static memoryview from_buffer(void *ptr, ssize_t itemsize, const char *format, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides, bool readonly=false)
Definition: pytypes.h:1482
bool equal(object_api const &other) const
Equivalent to obj == other in Python.
Definition: pytypes.h:119
Array33i a
generic_iterator(handle seq, ssize_t index)
Definition: pytypes.h:631
bool is(object_api const &other) const
Equivalent to obj is other in Python.
Definition: pytypes.h:115
bool not_equal(object_api const &other) const
Definition: pytypes.h:120
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
obj_attr_accessor attr(handle key) const
Definition: pytypes.h:1563
bool is_none() const
Equivalent to obj is None in Python.
Definition: pytypes.h:117
detail::dict_iterator begin() const
Definition: pytypes.h:1269
DiscreteKeys operator&(const DiscreteKey &key1, const DiscreteKey &key2)
Create a list from two keys.
Definition: DiscreteKey.cpp:49
else if n * info
size_t key_type
Definition: pytypes.h:584
dict_readonly(handle obj, ssize_t pos)
Definition: pytypes.h:719
const handle & inc_ref() const &
Definition: pytypes.h:192
pybind11::str str() const
Definition: pytypes.h:1577
T reinterpret_borrow(handle h)
Definition: pytypes.h:304
auto object_or_cast(T &&o) -> decltype(std::forward< T >(o))
Definition: pytypes.h:483
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
accessor< accessor_policies::tuple_item > tuple_accessor
Definition: pytypes.h:44
ssize_t distance_to(const sequence_slow_readwrite &b) const
Definition: pytypes.h:703
std::ostream & operator<<(std::ostream &s, const Jet< T, N > &z)
Definition: jet.h:631
void operator=(T &&value)&&
Definition: pytypes.h:504
detail::sequence_iterator end() const
Definition: pytypes.h:1298
const char * key_type
Definition: pytypes.h:547
std::is_same< kwargs_proxy, T > is_ds_unpacking
Definition: pytypes.h:775
detail::list_iterator end() const
Definition: pytypes.h:1312
PyObject * m_ptr
Definition: pytypes.h:219
Values result
iterator end() const
Return a sentinel which ends iteration.
Definition: pytypes.h:1556
bool PyUnicode_Check_Permissive(PyObject *o)
Definition: pytypes.h:757
bool empty() const
Definition: pytypes.h:1294
const object & trace() const
Definition: pytypes.h:367
object & operator=(const object &other)
Definition: pytypes.h:255
Definition: pytypes.h:928
float * ptr
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1250
friend bool operator==(const It &a, const It &b)
Definition: pytypes.h:649
reference operator*() const
Definition: pytypes.h:857
float_(double value=.0)
Definition: pytypes.h:1148
std::random_access_iterator_tag iterator_category
Definition: pytypes.h:670
static PyObject * raw_str(PyObject *op)
Return string representation – always returns a new reference, even if already a str...
Definition: pytypes.h:974
pointer operator->() const
Definition: pytypes.h:865
object cache
Definition: pytypes.h:536
void discard_as_unraisable(object err_context)
Definition: pytypes.h:348
int_(T value)
Definition: pytypes.h:1115
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Eigen::Triplet< double > T
size_t size() const
Definition: pytypes.h:1331
bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step, ssize_t *slicelength) const
Definition: pytypes.h:1179
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:821
int data[]
detail::sequence_iterator begin() const
Definition: pytypes.h:1297
detail::tuple_accessor operator[](size_t index) const
Definition: pytypes.h:1249
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1296
RealScalar s
#define PYBIND11_DEPRECATED(reason)
str(handle h)
Definition: pytypes.h:951
std::random_access_iterator_tag iterator_category
Definition: pytypes.h:691
object(const object &o)
Copy constructor; always increases the reference count.
Definition: pytypes.h:238
std::is_base_of< arg, T > is_keyword
Python argument categories (using PEP 448 terms)
Definition: pytypes.h:773
bool is_cpp_function() const
Definition: pytypes.h:1351
const handle & dec_ref() const &
Definition: pytypes.h:199
detail::tuple_iterator begin() const
Definition: pytypes.h:1251
Unsigned as_unsigned(PyObject *o)
Definition: pytypes.h:1092
static PyObject * raw_dict(PyObject *op)
Call the dict Python type – always returns a new reference.
Definition: pytypes.h:1278
void set(Container &c, Position position, const Value &value)
bool empty() const
Definition: pytypes.h:1308
handle get_function(handle value)
Definition: pytypes.h:466
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator+=(half &a, const half &b)
Definition: Half.h:285
const object & type() const
Definition: pytypes.h:365
PyObject ** ptr
Definition: pytypes.h:685
It & operator--()
Definition: pytypes.h:639
iterator operator++(int)
Definition: pytypes.h:851
Python&#39;s dictionary protocol permits this to be a forward iterator.
Definition: pytypes.h:711
It operator++(int)
Definition: pytypes.h:638
bool_(bool value)
Definition: pytypes.h:1074
friend It operator+(difference_type n, const It &b)
Definition: pytypes.h:645
static memoryview from_buffer(const T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides)
Definition: pytypes.h:1445
bool matches(handle exc) const
Definition: pytypes.h:363
Full read and write access using the sequence protocol: see detail::sequence_accessor ...
Definition: pytypes.h:689
Definition: pytypes.h:1301
void operator=(T &&value)&
Definition: pytypes.h:507
str(const char *c="")
Definition: pytypes.h:938
static type of(handle h)
Return a type object from a handle or an object.
Definition: pytypes.h:905
void advance()
Definition: pytypes.h:886
Helper class which collects positional, keyword, * and ** arguments for a Python function call...
Definition: cast.h:2053
bool equal(const sequence_fast_readonly &b) const
Definition: pytypes.h:681
void insert(size_t index, T &&val) const
Definition: pytypes.h:1316
Tag and check to identify a class which implements the Python object API.
Definition: pytypes.h:47
const double h
const object & value() const
Definition: pytypes.h:366
void check(bool b, bool ref)
Definition: fastmath.cpp:12
handle release()
Definition: pytypes.h:249
std::input_iterator_tag iterator_category
Definition: pytypes.h:838
def doc()
Definition: conftest.py:157
Definition: pytypes.h:1108
void increment()
Definition: pytypes.h:722
bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const
Definition: pytypes.h:1172
kwargs_proxy(handle h)
Definition: pytypes.h:763
std::is_base_of< pyobject_tag, remove_reference_t< T >> is_pyobject
Definition: pytypes.h:48
void advance(ssize_t n)
Definition: pytypes.h:680
std::pair< handle, handle > value_type
Definition: pytypes.h:714
ssize_t distance_to(const sequence_fast_readonly &b) const
Definition: pytypes.h:682
size_t size() const
Definition: pytypes.h:1247
bool empty() const
Definition: pytypes.h:1248
reference operator*() const
Definition: pytypes.h:633
generic_iterator< iterator_policies::sequence_fast_readonly > tuple_iterator
Definition: pytypes.h:733
args_proxy(handle h)
Definition: pytypes.h:768
object getattr(handle obj, handle name)
Definition: pytypes.h:419
handle(PyObject *ptr)
Creates a handle from the given raw Python object pointer.
Definition: pytypes.h:181
bool PyEllipsis_Check(PyObject *o)
Definition: pytypes.h:755
bool PyIterable_Check(PyObject *obj)
Definition: pytypes.h:743
friend difference_type operator-(const It &a, const It &b)
Definition: pytypes.h:647
#define PYBIND11_MATH_OPERATOR_BINARY(op, fn)
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:56
size_t len_hint(handle h)
Definition: pytypes.h:1521
void operator=(const accessor &a)&
Definition: pytypes.h:502
kwargs_proxy operator*() const
Definition: pytypes.h:769
size_t key_type
Definition: pytypes.h:567
#define PYBIND11_MATH_OPERATOR_UNARY(op, fn)
static memoryview from_buffer(T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides, bool readonly=false)
Definition: pytypes.h:1436
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Jet< T, N > operator-(const Jet< T, N > &f)
Definition: jet.h:258
object & operator=(object &&other) noexcept
Definition: pytypes.h:262
typename Policy::value_type value_type
Definition: pytypes.h:626
Definition: pytypes.h:1255
std::forward_iterator_tag iterator_category
Definition: pytypes.h:713
T reinterpret_steal(handle h)
Definition: pytypes.h:314
bool isinstance< object >(handle obj)
Definition: pytypes.h:390
Annotation for function names.
Definition: attr.h:36
generic_iterator< iterator_policies::sequence_fast_readonly > list_iterator
Definition: pytypes.h:734
generic_iterator< iterator_policies::dict_readonly > dict_iterator
Definition: pytypes.h:741
str repr(handle h)
Definition: pytypes.h:1536
detail::dict_iterator end() const
Definition: pytypes.h:1270
Information record describing a Python buffer object.
Definition: buffer_info.h:17
bool contains(T &&item) const
Check if the given item is contained within this object, i.e. item in obj.
Definition: pytypes.h:1572
void clear() const
Definition: pytypes.h:1271
Container::iterator get(Container &c, Position position)
accessor< accessor_policies::generic_item > item_accessor
Definition: pytypes.h:41
PYBIND11_DEPRECATED("Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)") explicit operator enable_if_t< std key_type key
Definition: pytypes.h:512
ssize_t difference_type
Definition: pytypes.h:624
bool operator>=(object_api const &other) const
Definition: pytypes.h:124
size_t len(handle h)
Definition: pytypes.h:1514
const value_type reference
Definition: pytypes.h:715
typename Policy::key_type key_type
Definition: pytypes.h:492
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:449
reference dereference() const
Definition: pytypes.h:677
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator/=(half &a, const half &b)
Definition: Half.h:297
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
void clear() const
Definition: pytypes.h:1336
Definition: pytypes.h:1325
void operator=(const accessor &a)&&
Definition: pytypes.h:501
~object()
Destructor; automatically calls handle::dec_ref()
Definition: pytypes.h:242
Jet< T, N > const & operator+(const Jet< T, N > &f)
Definition: jet.h:249
#define PYBIND11_NAMESPACE_END(name)
static memoryview from_buffer(const void *ptr, ssize_t itemsize, const char *format, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides)
Definition: pytypes.h:1427
friend bool operator!=(const It &a, const It &b)
Definition: pytypes.h:650
#define PYBIND11_OBJECT(Name, Parent, CheckFun)
Definition: pytypes.h:815
dict(Args &&...args)
Definition: pytypes.h:1265
str(const char *c, size_t n)
Definition: pytypes.h:932
arrow_proxy(T &&value)
Definition: pytypes.h:663
Definition: pytypes.h:995
Definition: pytypes.h:897
#define PYBIND11_NAMESPACE_BEGIN(name)
bool empty() const
Definition: pytypes.h:1268
detail::list_iterator begin() const
Definition: pytypes.h:1311
sequence_fast_readonly(handle obj, ssize_t n)
Definition: pytypes.h:675
reference dereference() const
Definition: pytypes.h:721
bool operator<(object_api const &other) const
Definition: pytypes.h:121
detail::list_accessor operator[](size_t index) const
Definition: pytypes.h:1309
Lightweight iterator policy using just a simple pointer: see PySequence_Fast_ITEMS ...
Definition: pytypes.h:668


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:44