cppyabm  1.0.17
An agent-based library to integrate C++ and Python
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 
46 /// Tag and check to identify a class which implements the Python object API
47 class pyobject_tag { };
48 template <typename T> using is_pyobject = std::is_base_of<pyobject_tag, remove_reference_t<T>>;
49 
50 /** \rst
51  A mixin class which adds common functions to `handle`, `object` and various accessors.
52  The only requirement for `Derived` is to implement ``PyObject *Derived::ptr() const``.
53 \endrst */
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:
59  /** \rst
60  Return an iterator equivalent to calling ``iter()`` in Python. The object
61  must be a collection which supports the iteration protocol.
62  \endrst */
63  iterator begin() const;
64  /// Return a sentinel which ends iteration.
65  iterator end() const;
66 
67  /** \rst
68  Return an internal functor to invoke the object's sequence protocol. Casting
69  the returned ``detail::item_accessor`` instance to a `handle` or `object`
70  subclass causes a corresponding call to ``__getitem__``. Assigning a `handle`
71  or `object` subclass causes a call to ``__setitem__``.
72  \endrst */
73  item_accessor operator[](handle key) const;
74  /// See above (the only difference is that they key is provided as a string literal)
75  item_accessor operator[](const char *key) const;
76 
77  /** \rst
78  Return an internal functor to access the object's attributes. Casting the
79  returned ``detail::obj_attr_accessor`` instance to a `handle` or `object`
80  subclass causes a corresponding call to ``getattr``. Assigning a `handle`
81  or `object` subclass causes a call to ``setattr``.
82  \endrst */
83  obj_attr_accessor attr(handle key) const;
84  /// See above (the only difference is that they key is provided as a string literal)
85  str_attr_accessor attr(const char *key) const;
86 
87  /** \rst
88  Matches * unpacking in Python, e.g. to unpack arguments out of a ``tuple``
89  or ``list`` for a function call. Applying another * to the result yields
90  ** unpacking, e.g. to unpack a dict as function keyword arguments.
91  See :ref:`calling_python_functions`.
92  \endrst */
93  args_proxy operator*() const;
94 
95  /// Check if the given item is contained within this object, i.e. ``item in obj``.
96  template <typename T> bool contains(T &&item) const;
97 
98  /** \rst
99  Assuming the Python object is a function or implements the ``__call__``
100  protocol, ``operator()`` invokes the underlying function, passing an
101  arbitrary set of parameters. The result is returned as a `object` and
102  may need to be converted back into a Python object using `handle::cast()`.
103 
104  When some of the arguments cannot be converted to Python objects, the
105  function will throw a `cast_error` exception. When the Python function
106  call fails, a `error_already_set` exception is thrown.
107  \endrst */
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 
114  /// Equivalent to ``obj is other`` in Python.
115  bool is(object_api const& other) const { return derived().ptr() == other.derived().ptr(); }
116  /// Equivalent to ``obj is None`` in Python.
117  bool is_none() const { return derived().ptr() == Py_None; }
118  /// Equivalent to obj == other in Python
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 
150  /// Get or set the object's docstring, i.e. ``obj.__doc__``.
151  str_attr_accessor doc() const;
152 
153  /// Return the object's current reference count
154  int ref_count() const { return static_cast<int>(Py_REFCNT(derived().ptr())); }
155 
156  // TODO 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 /** \rst
166  Holds a reference to a Python object (no reference counting)
167 
168  The `handle` class is a thin wrapper around an arbitrary Python object (i.e. a
169  ``PyObject *`` in Python's C API). It does not perform any automatic reference
170  counting and merely provides a basic C++ interface to various Python API functions.
171 
172  .. seealso::
173  The `object` class inherits from `handle` and adds automatic reference
174  counting features.
175 \endrst */
176 class handle : public detail::object_api<handle> {
177 public:
178  /// The default constructor creates a handle with a ``nullptr``-valued pointer
179  handle() = default;
180  /// Creates a ``handle`` from the given raw Python object pointer
181  handle(PyObject *ptr) : m_ptr(ptr) { } // Allow implicit conversion from PyObject*
182 
183  /// Return the underlying ``PyObject *`` pointer
184  PyObject *ptr() const { return m_ptr; }
185  PyObject *&ptr() { return m_ptr; }
186 
187  /** \rst
188  Manually increase the reference count of the Python object. Usually, it is
189  preferable to use the `object` class which derives from `handle` and calls
190  this function automatically. Returns a reference to itself.
191  \endrst */
192  const handle& inc_ref() const & { Py_XINCREF(m_ptr); return *this; }
193 
194  /** \rst
195  Manually decrease the reference count of the Python object. Usually, it is
196  preferable to use the `object` class which derives from `handle` and calls
197  this function automatically. Returns a reference to itself.
198  \endrst */
199  const handle& dec_ref() const & { Py_XDECREF(m_ptr); return *this; }
200 
201  /** \rst
202  Attempt to cast the Python object into the given C++ type. A `cast_error`
203  will be throw upon failure.
204  \endrst */
205  template <typename T> T cast() const;
206  /// Return ``true`` when the `handle` wraps a valid Python object
207  explicit operator bool() const { return m_ptr != nullptr; }
208  /** \rst
209  Deprecated: Check that the underlying pointers are the same.
210  Equivalent to ``obj1 is obj2`` in Python.
211  \endrst */
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 
222 /** \rst
223  Holds a reference to a Python object (with reference counting)
224 
225  Like `handle`, the `object` class is a thin wrapper around an arbitrary Python
226  object (i.e. a ``PyObject *`` in Python's C API). In contrast to `handle`, it
227  optionally increases the object's reference count upon construction, and it
228  *always* decreases the reference count when the `object` instance goes out of
229  scope and is destructed. When using `object` instances consistently, it is much
230  easier to get reference counting right at the first attempt.
231 \endrst */
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(); }
237  /// Copy constructor; always increases the reference count
238  object(const object &o) : handle(o) { inc_ref(); }
239  /// Move constructor; steals the object from ``other`` and preserves its reference count
240  object(object &&other) noexcept { m_ptr = other.m_ptr; other.m_ptr = nullptr; }
241  /// Destructor; automatically calls `handle::dec_ref()`
242  ~object() { dec_ref(); }
243 
244  /** \rst
245  Resets the internal pointer to ``nullptr`` without decreasing the
246  object's reference count. The function returns a raw handle to the original
247  Python object.
248  \endrst */
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 #ifndef DOXYGEN_SHOULD_SKIP_THIS // Issue in breathe 4.26.1
283  template <typename T> friend T reinterpret_borrow(handle);
284  template <typename T> friend T reinterpret_steal(handle);
285 #endif
286 
287 public:
288  // Only accessible from derived classes and the reinterpret_* functions
291 };
292 
293 /** \rst
294  Declare that a `handle` or ``PyObject *`` is a certain type and borrow the reference.
295  The target type ``T`` must be `object` or one of its derived classes. The function
296  doesn't do any conversions or checks. It's up to the user to make sure that the
297  target type is correct.
298 
299  .. code-block:: cpp
300 
301  PyObject *p = PyList_GetItem(obj, index);
302  py::object o = reinterpret_borrow<py::object>(p);
303  // or
304  py::tuple t = reinterpret_borrow<py::tuple>(p); // <-- `p` must be already be a `tuple`
305 \endrst */
306 template <typename T> T reinterpret_borrow(handle h) { return {h, object::borrowed_t{}}; }
307 
308 /** \rst
309  Like `reinterpret_borrow`, but steals the reference.
310 
311  .. code-block:: cpp
312 
313  PyObject *p = PyObject_Str(obj);
314  py::str s = reinterpret_steal<py::str>(p); // <-- `p` must be already be a `str`
315 \endrst */
316 template <typename T> T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; }
317 
319 inline std::string error_string();
321 
322 /// Fetch and hold an error which was already set in Python. An instance of this is typically
323 /// thrown to propagate python-side errors back through C++ which can either be caught manually or
324 /// else falls back to the function dispatcher (which then raises the captured error back to
325 /// python).
326 class error_already_set : public std::runtime_error {
327 public:
328  /// Constructs a new exception from the current Python error indicator, if any. The current
329  /// Python error indicator will be cleared.
330  error_already_set() : std::runtime_error(detail::error_string()) {
331  PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr());
332  }
333 
336 
337  inline ~error_already_set() override;
338 
339  /// Give the currently-held error back to Python, if any. If there is currently a Python error
340  /// already set it is cleared first. After this call, the current object no longer stores the
341  /// error variables (but the `.what()` string is still available).
342  void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); }
343 
344  /// If it is impossible to raise the currently-held error, such as in destructor, we can write
345  /// it out using Python's unraisable hook (sys.unraisablehook). The error context should be
346  /// some object whose repr() helps identify the location of the error. Python already knows the
347  /// type and value of the error, so there is no need to repeat that. For example, __func__ could
348  /// be helpful. After this call, the current object no longer stores the error variables,
349  /// and neither does Python.
350  void discard_as_unraisable(object err_context) {
351  restore();
352  PyErr_WriteUnraisable(err_context.ptr());
353  }
354  void discard_as_unraisable(const char *err_context) {
355  discard_as_unraisable(reinterpret_steal<object>(PYBIND11_FROM_STRING(err_context)));
356  }
357 
358  // Does nothing; provided for backwards compatibility.
359  PYBIND11_DEPRECATED("Use of error_already_set.clear() is deprecated")
360  void clear() {}
361 
362  /// Check if the currently trapped error type matches the given Python exception class (or a
363  /// subclass thereof). May also be passed a tuple to search for any exception class matches in
364  /// the given tuple.
365  bool matches(handle exc) const { return PyErr_GivenExceptionMatches(m_type.ptr(), exc.ptr()); }
366 
367  const object& type() const { return m_type; }
368  const object& value() const { return m_value; }
369  const object& trace() const { return m_trace; }
370 
371 private:
372  object m_type, m_value, m_trace;
373 };
374 
375 /** \defgroup python_builtins _
376  Unless stated otherwise, the following C++ functions behave the same
377  as their Python counterparts.
378  */
379 
380 /** \ingroup python_builtins
381  \rst
382  Return true if ``obj`` is an instance of ``T``. Type ``T`` must be a subclass of
383  `object` or a class which was exposed to Python as ``py::class_<T>``.
384 \endrst */
386 bool isinstance(handle obj) { return T::check_(obj); }
387 
389 bool isinstance(handle obj) { return detail::isinstance_generic(obj, typeid(T)); }
390 
391 template <> inline bool isinstance<handle>(handle) = delete;
392 template <> inline bool isinstance<object>(handle obj) { return obj.ptr() != nullptr; }
393 
394 /// \ingroup python_builtins
395 /// Return true if ``obj`` is an instance of the ``type``.
396 inline bool isinstance(handle obj, handle type) {
397  const auto result = PyObject_IsInstance(obj.ptr(), type.ptr());
398  if (result == -1)
399  throw error_already_set();
400  return result != 0;
401 }
402 
403 /// \addtogroup python_builtins
404 /// @{
405 inline bool hasattr(handle obj, handle name) {
406  return PyObject_HasAttr(obj.ptr(), name.ptr()) == 1;
407 }
408 
409 inline bool hasattr(handle obj, const char *name) {
410  return PyObject_HasAttrString(obj.ptr(), name) == 1;
411 }
412 
413 inline void delattr(handle obj, handle name) {
414  if (PyObject_DelAttr(obj.ptr(), name.ptr()) != 0) { throw error_already_set(); }
415 }
416 
417 inline void delattr(handle obj, const char *name) {
418  if (PyObject_DelAttrString(obj.ptr(), name) != 0) { throw error_already_set(); }
419 }
420 
421 inline object getattr(handle obj, handle name) {
422  PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr());
423  if (!result) { throw error_already_set(); }
424  return reinterpret_steal<object>(result);
425 }
426 
427 inline object getattr(handle obj, const char *name) {
428  PyObject *result = PyObject_GetAttrString(obj.ptr(), name);
429  if (!result) { throw error_already_set(); }
430  return reinterpret_steal<object>(result);
431 }
432 
433 inline object getattr(handle obj, handle name, handle default_) {
434  if (PyObject *result = PyObject_GetAttr(obj.ptr(), name.ptr())) {
435  return reinterpret_steal<object>(result);
436  } else {
437  PyErr_Clear();
438  return reinterpret_borrow<object>(default_);
439  }
440 }
441 
442 inline object getattr(handle obj, const char *name, handle default_) {
443  if (PyObject *result = PyObject_GetAttrString(obj.ptr(), name)) {
444  return reinterpret_steal<object>(result);
445  } else {
446  PyErr_Clear();
447  return reinterpret_borrow<object>(default_);
448  }
449 }
450 
451 inline void setattr(handle obj, handle name, handle value) {
452  if (PyObject_SetAttr(obj.ptr(), name.ptr(), value.ptr()) != 0) { throw error_already_set(); }
453 }
454 
455 inline void setattr(handle obj, const char *name, handle value) {
456  if (PyObject_SetAttrString(obj.ptr(), name, value.ptr()) != 0) { throw error_already_set(); }
457 }
458 
459 inline ssize_t hash(handle obj) {
460  auto h = PyObject_Hash(obj.ptr());
461  if (h == -1) { throw error_already_set(); }
462  return h;
463 }
464 
465 /// @} python_builtins
466 
469  if (value) {
470 #if PY_MAJOR_VERSION >= 3
471  if (PyInstanceMethod_Check(value.ptr()))
472  value = PyInstanceMethod_GET_FUNCTION(value.ptr());
473  else
474 #endif
475  if (PyMethod_Check(value.ptr()))
476  value = PyMethod_GET_FUNCTION(value.ptr());
477  }
478  return value;
479 }
480 
481 // Helper aliases/functions to support implicit casting of values given to python accessors/methods.
482 // When given a pyobject, this simply returns the pyobject as-is; for other C++ type, the value goes
483 // through pybind11::cast(obj) to convert it to an `object`.
485 auto object_or_cast(T &&o) -> decltype(std::forward<T>(o)) { return std::forward<T>(o); }
486 // The following casting version is implemented in cast.h:
488 object object_or_cast(T &&o);
489 // Match a PyObject*, which we want to convert directly to handle via its converting constructor
490 inline handle object_or_cast(PyObject *ptr) { return ptr; }
491 
492 template <typename Policy>
493 class accessor : public object_api<accessor<Policy>> {
494  using key_type = typename Policy::key_type;
495 
496 public:
497  accessor(handle obj, key_type key) : obj(obj), key(std::move(key)) { }
498  accessor(const accessor &) = default;
499  accessor(accessor &&) = default;
500 
501  // accessor overload required to override default assignment operator (templates are not allowed
502  // to replace default compiler-generated assignments).
503  void operator=(const accessor &a) && { std::move(*this).operator=(handle(a)); }
504  void operator=(const accessor &a) & { operator=(handle(a)); }
505 
506  template <typename T> void operator=(T &&value) && {
507  Policy::set(obj, key, object_or_cast(std::forward<T>(value)));
508  }
509  template <typename T> void operator=(T &&value) & {
510  get_cache() = reinterpret_borrow<object>(object_or_cast(std::forward<T>(value)));
511  }
512 
513  template <typename T = Policy>
514  PYBIND11_DEPRECATED("Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)")
515  explicit operator enable_if_t<std::is_same<T, accessor_policies::str_attr>::value ||
516  std::is_same<T, accessor_policies::obj_attr>::value, bool>() const {
517  return hasattr(obj, key);
518  }
519  template <typename T = Policy>
520  PYBIND11_DEPRECATED("Use of obj[key] as bool is deprecated in favor of obj.contains(key)")
521  explicit operator enable_if_t<std::is_same<T, accessor_policies::generic_item>::value, bool>() const {
522  return obj.contains(key);
523  }
524 
525  operator object() const { return get_cache(); }
526  PyObject *ptr() const { return get_cache().ptr(); }
527  template <typename T> T cast() const { return get_cache().template cast<T>(); }
528 
529 private:
530  object &get_cache() const {
531  if (!cache) { cache = Policy::get(obj, key); }
532  return cache;
533  }
534 
535 private:
536  handle obj;
537  key_type key;
538  mutable object cache;
539 };
540 
542 struct obj_attr {
543  using key_type = object;
544  static object get(handle obj, handle key) { return getattr(obj, key); }
545  static void set(handle obj, handle key, handle val) { setattr(obj, key, val); }
546 };
547 
548 struct str_attr {
549  using key_type = const char *;
550  static object get(handle obj, const char *key) { return getattr(obj, key); }
551  static void set(handle obj, const char *key, handle val) { setattr(obj, key, val); }
552 };
553 
554 struct generic_item {
555  using key_type = object;
556 
557  static object get(handle obj, handle key) {
558  PyObject *result = PyObject_GetItem(obj.ptr(), key.ptr());
559  if (!result) { throw error_already_set(); }
560  return reinterpret_steal<object>(result);
561  }
562 
563  static void set(handle obj, handle key, handle val) {
564  if (PyObject_SetItem(obj.ptr(), key.ptr(), val.ptr()) != 0) { throw error_already_set(); }
565  }
566 };
567 
569  using key_type = size_t;
570 
571  static object get(handle obj, size_t index) {
572  PyObject *result = PySequence_GetItem(obj.ptr(), static_cast<ssize_t>(index));
573  if (!result) { throw error_already_set(); }
574  return reinterpret_steal<object>(result);
575  }
576 
577  static void set(handle obj, size_t index, handle val) {
578  // PySequence_SetItem does not steal a reference to 'val'
579  if (PySequence_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.ptr()) != 0) {
580  throw error_already_set();
581  }
582  }
583 };
584 
585 struct list_item {
586  using key_type = size_t;
587 
588  static object get(handle obj, size_t index) {
589  PyObject *result = PyList_GetItem(obj.ptr(), static_cast<ssize_t>(index));
590  if (!result) { throw error_already_set(); }
591  return reinterpret_borrow<object>(result);
592  }
593 
594  static void set(handle obj, size_t index, handle val) {
595  // PyList_SetItem steals a reference to 'val'
596  if (PyList_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.inc_ref().ptr()) != 0) {
597  throw error_already_set();
598  }
599  }
600 };
601 
602 struct tuple_item {
603  using key_type = size_t;
604 
605  static object get(handle obj, size_t index) {
606  PyObject *result = PyTuple_GetItem(obj.ptr(), static_cast<ssize_t>(index));
607  if (!result) { throw error_already_set(); }
608  return reinterpret_borrow<object>(result);
609  }
610 
611  static void set(handle obj, size_t index, handle val) {
612  // PyTuple_SetItem steals a reference to 'val'
613  if (PyTuple_SetItem(obj.ptr(), static_cast<ssize_t>(index), val.inc_ref().ptr()) != 0) {
614  throw error_already_set();
615  }
616  }
617 };
619 
620 /// STL iterator template used for tuple, list, sequence and dict
621 template <typename Policy>
622 class generic_iterator : public Policy {
623  using It = generic_iterator;
624 
625 public:
627  using iterator_category = typename Policy::iterator_category;
628  using value_type = typename Policy::value_type;
629  using reference = typename Policy::reference;
630  using pointer = typename Policy::pointer;
631 
632  generic_iterator() = default;
633  generic_iterator(handle seq, ssize_t index) : Policy(seq, index) { }
634 
635  reference operator*() const { return Policy::dereference(); }
636  reference operator[](difference_type n) const { return *(*this + n); }
637  pointer operator->() const { return **this; }
638 
639  It &operator++() { Policy::increment(); return *this; }
640  It operator++(int) { auto copy = *this; Policy::increment(); return copy; }
641  It &operator--() { Policy::decrement(); return *this; }
642  It operator--(int) { auto copy = *this; Policy::decrement(); return copy; }
643  It &operator+=(difference_type n) { Policy::advance(n); return *this; }
644  It &operator-=(difference_type n) { Policy::advance(-n); return *this; }
645 
646  friend It operator+(const It &a, difference_type n) { auto copy = a; return copy += n; }
647  friend It operator+(difference_type n, const It &b) { return b + n; }
648  friend It operator-(const It &a, difference_type n) { auto copy = a; return copy -= n; }
649  friend difference_type operator-(const It &a, const It &b) { return a.distance_to(b); }
650 
651  friend bool operator==(const It &a, const It &b) { return a.equal(b); }
652  friend bool operator!=(const It &a, const It &b) { return !(a == b); }
653  friend bool operator< (const It &a, const It &b) { return b - a > 0; }
654  friend bool operator> (const It &a, const It &b) { return b < a; }
655  friend bool operator>=(const It &a, const It &b) { return !(a < b); }
656  friend bool operator<=(const It &a, const It &b) { return !(a > b); }
657 };
658 
659 PYBIND11_NAMESPACE_BEGIN(iterator_policies)
660 /// Quick proxy class needed to implement ``operator->`` for iterators which can't return pointers
661 template <typename T>
662 struct arrow_proxy {
663  T value;
664 
665  arrow_proxy(T &&value) : value(std::move(value)) { }
666  T *operator->() const { return &value; }
667 };
668 
669 /// Lightweight iterator policy using just a simple pointer: see ``PySequence_Fast_ITEMS``
671 protected:
672  using iterator_category = std::random_access_iterator_tag;
674  using reference = const handle;
676 
677  sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { }
678 
679  reference dereference() const { return *ptr; }
680  void increment() { ++ptr; }
681  void decrement() { --ptr; }
682  void advance(ssize_t n) { ptr += n; }
683  bool equal(const sequence_fast_readonly &b) const { return ptr == b.ptr; }
684  ssize_t distance_to(const sequence_fast_readonly &b) const { return ptr - b.ptr; }
685 
686 private:
687  PyObject **ptr;
688 };
689 
690 /// Full read and write access using the sequence protocol: see ``detail::sequence_accessor``
692 protected:
693  using iterator_category = std::random_access_iterator_tag;
697 
698  sequence_slow_readwrite(handle obj, ssize_t index) : obj(obj), index(index) { }
699 
700  reference dereference() const { return {obj, static_cast<size_t>(index)}; }
701  void increment() { ++index; }
702  void decrement() { --index; }
703  void advance(ssize_t n) { index += n; }
704  bool equal(const sequence_slow_readwrite &b) const { return index == b.index; }
705  ssize_t distance_to(const sequence_slow_readwrite &b) const { return index - b.index; }
706 
707 private:
708  handle obj;
709  ssize_t index;
710 };
711 
712 /// Python's dictionary protocol permits this to be a forward iterator
714 protected:
715  using iterator_category = std::forward_iterator_tag;
716  using value_type = std::pair<handle, handle>;
717  using reference = const value_type;
719 
720  dict_readonly() = default;
721  dict_readonly(handle obj, ssize_t pos) : obj(obj), pos(pos) { increment(); }
722 
723  reference dereference() const { return {key, value}; }
724  void increment() { if (!PyDict_Next(obj.ptr(), &pos, &key, &value)) { pos = -1; } }
725  bool equal(const dict_readonly &b) const { return pos == b.pos; }
726 
727 private:
728  handle obj;
729  PyObject *key = nullptr, *value = nullptr;
730  ssize_t pos = -1;
731 };
732 PYBIND11_NAMESPACE_END(iterator_policies)
733 
734 #if !defined(PYPY_VERSION)
737 #else
740 #endif
741 
744 
745 inline bool PyIterable_Check(PyObject *obj) {
746  PyObject *iter = PyObject_GetIter(obj);
747  if (iter) {
748  Py_DECREF(iter);
749  return true;
750  } else {
751  PyErr_Clear();
752  return false;
753  }
754 }
755 
756 inline bool PyNone_Check(PyObject *o) { return o == Py_None; }
757 inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; }
758 
759 #ifdef PYBIND11_STR_LEGACY_PERMISSIVE
760 inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); }
761 #define PYBIND11_STR_CHECK_FUN detail::PyUnicode_Check_Permissive
762 #else
763 #define PYBIND11_STR_CHECK_FUN PyUnicode_Check
764 #endif
765 
766 inline bool PyStaticMethod_Check(PyObject *o) { return o->ob_type == &PyStaticMethod_Type; }
767 
768 class kwargs_proxy : public handle {
769 public:
770  explicit kwargs_proxy(handle h) : handle(h) { }
771 };
772 
773 class args_proxy : public handle {
774 public:
775  explicit args_proxy(handle h) : handle(h) { }
776  kwargs_proxy operator*() const { return kwargs_proxy(*this); }
777 };
778 
779 /// Python argument categories (using PEP 448 terms)
780 template <typename T> using is_keyword = std::is_base_of<arg, T>;
781 template <typename T> using is_s_unpacking = std::is_same<args_proxy, T>; // * unpacking
782 template <typename T> using is_ds_unpacking = std::is_same<kwargs_proxy, T>; // ** unpacking
783 template <typename T> using is_positional = satisfies_none_of<T,
785 >;
787 
788 // Call argument collector forward declarations
789 template <return_value_policy policy = return_value_policy::automatic_reference>
790 class simple_collector;
791 template <return_value_policy policy = return_value_policy::automatic_reference>
792 class unpacking_collector;
793 
795 
796 // TODO: After the deprecated constructors are removed, this macro can be simplified by
797 // inheriting ctors: `using Parent::Parent`. It's not an option right now because
798 // the `using` statement triggers the parent deprecation warning even if the ctor
799 // isn't even used.
800 #define PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
801  public: \
802  PYBIND11_DEPRECATED("Use reinterpret_borrow<"#Name">() or reinterpret_steal<"#Name">()") \
803  Name(handle h, bool is_borrowed) : Parent(is_borrowed ? Parent(h, borrowed_t{}) : Parent(h, stolen_t{})) { } \
804  Name(handle h, borrowed_t) : Parent(h, borrowed_t{}) { } \
805  Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \
806  PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \
807  bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \
808  static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \
809  template <typename Policy_> \
810  Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
811 
812 #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
813  PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
814  /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
815  Name(const object &o) \
816  : Parent(check_(o) ? o.inc_ref().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
817  { if (!m_ptr) throw error_already_set(); } \
818  Name(object &&o) \
819  : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
820  { if (!m_ptr) throw error_already_set(); }
821 
822 #define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun) \
823  PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
824  Name() : Parent() { }
825 
826 #define PYBIND11_OBJECT_CHECK_FAILED(Name, o_ptr) \
827  ::pybind11::type_error("Object of type '" + \
828  ::pybind11::detail::get_fully_qualified_tp_name(Py_TYPE(o_ptr)) + \
829  "' is not an instance of '" #Name "'")
830 
831 #define PYBIND11_OBJECT(Name, Parent, CheckFun) \
832  PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
833  /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
834  Name(const object &o) : Parent(o) \
835  { if (m_ptr && !check_(m_ptr)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, m_ptr); } \
836  Name(object &&o) : Parent(std::move(o)) \
837  { if (m_ptr && !check_(m_ptr)) throw PYBIND11_OBJECT_CHECK_FAILED(Name, m_ptr); }
838 
839 #define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \
840  PYBIND11_OBJECT(Name, Parent, CheckFun) \
841  Name() : Parent() { }
842 
843 /// \addtogroup pytypes
844 /// @{
845 
846 /** \rst
847  Wraps a Python iterator so that it can also be used as a C++ input iterator
848 
849  Caveat: copying an iterator does not (and cannot) clone the internal
850  state of the Python iterable. This also applies to the post-increment
851  operator. This iterator should only be used to retrieve the current
852  value using ``operator*()``.
853 \endrst */
854 class iterator : public object {
855 public:
856  using iterator_category = std::input_iterator_tag;
859  using reference = const handle;
860  using pointer = const handle *;
861 
862  PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)
863 
864  iterator& operator++() {
865  advance();
866  return *this;
867  }
868 
870  auto rv = *this;
871  advance();
872  return rv;
873  }
874 
876  if (m_ptr && !value.ptr()) {
877  auto& self = const_cast<iterator &>(*this);
878  self.advance();
879  }
880  return value;
881  }
882 
883  pointer operator->() const { operator*(); return &value; }
884 
885  /** \rst
886  The value which marks the end of the iteration. ``it == iterator::sentinel()``
887  is equivalent to catching ``StopIteration`` in Python.
888 
889  .. code-block:: cpp
890 
891  void foo(py::iterator it) {
892  while (it != py::iterator::sentinel()) {
893  // use `*it`
894  ++it;
895  }
896  }
897  \endrst */
898  static iterator sentinel() { return {}; }
899 
900  friend bool operator==(const iterator &a, const iterator &b) { return a->ptr() == b->ptr(); }
901  friend bool operator!=(const iterator &a, const iterator &b) { return a->ptr() != b->ptr(); }
902 
903 private:
904  void advance() {
905  value = reinterpret_steal<object>(PyIter_Next(m_ptr));
906  if (PyErr_Occurred()) { throw error_already_set(); }
907  }
908 
909 private:
910  object value = {};
911 };
912 
913 
914 
915 class type : public object {
916 public:
917  PYBIND11_OBJECT(type, object, PyType_Check)
918 
919  /// Return a type handle from a handle or an object
920  static handle handle_of(handle h) { return handle((PyObject*) Py_TYPE(h.ptr())); }
921 
922  /// Return a type object from a handle or an object
923  static type of(handle h) { return type(type::handle_of(h), borrowed_t{}); }
924 
925  // Defined in pybind11/cast.h
926  /// Convert C++ type to handle if previously registered. Does not convert
927  /// standard types, like int, float. etc. yet.
928  /// See https://github.com/pybind/pybind11/issues/2486
929  template<typename T>
930  static handle handle_of();
931 
932  /// Convert C++ type to type if previously registered. Does not convert
933  /// standard types, like int, float. etc. yet.
934  /// See https://github.com/pybind/pybind11/issues/2486
935  template<typename T>
936  static type of() {return type(type::handle_of<T>(), borrowed_t{}); }
937 };
938 
939 class iterable : public object {
940 public:
942 };
943 
944 class bytes;
945 
946 class str : public object {
947 public:
949 
950  str(const char *c, size_t n)
951  : object(PyUnicode_FromStringAndSize(c, (ssize_t) n), stolen_t{}) {
952  if (!m_ptr) pybind11_fail("Could not allocate string object!");
953  }
954 
955  // 'explicit' is explicitly omitted from the following constructors to allow implicit conversion to py::str from C++ string-like objects
956  str(const char *c = "")
957  : object(PyUnicode_FromString(c), stolen_t{}) {
958  if (!m_ptr) pybind11_fail("Could not allocate string object!");
959  }
960 
961  str(const std::string &s) : str(s.data(), s.size()) { }
962 
963  explicit str(const bytes &b);
964 
965  /** \rst
966  Return a string representation of the object. This is analogous to
967  the ``str()`` function in Python.
968  \endrst */
969  explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { if (!m_ptr) throw error_already_set(); }
970 
971  operator std::string() const {
972  object temp = *this;
973  if (PyUnicode_Check(m_ptr)) {
974  temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(m_ptr));
975  if (!temp)
976  pybind11_fail("Unable to extract string contents! (encoding issue)");
977  }
978  char *buffer;
979  ssize_t length;
980  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
981  pybind11_fail("Unable to extract string contents! (invalid type)");
982  return std::string(buffer, (size_t) length);
983  }
984 
985  template <typename... Args>
986  str format(Args &&...args) const {
987  return attr("format")(std::forward<Args>(args)...);
988  }
989 
990 private:
991  /// Return string representation -- always returns a new reference, even if already a str
992  static PyObject *raw_str(PyObject *op) {
993  PyObject *str_value = PyObject_Str(op);
994 #if PY_MAJOR_VERSION < 3
995  if (!str_value) throw error_already_set();
996  PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
997  Py_XDECREF(str_value); str_value = unicode;
998 #endif
999  return str_value;
1000  }
1001 };
1002 /// @} pytypes
1003 
1004 inline namespace literals {
1005 /** \rst
1006  String literal version of `str`
1007  \endrst */
1008 inline str operator"" _s(const char *s, size_t size) { return {s, size}; }
1009 } // namespace literals
1010 
1011 /// \addtogroup pytypes
1012 /// @{
1013 class bytes : public object {
1014 public:
1016 
1017  // Allow implicit conversion:
1018  bytes(const char *c = "")
1019  : object(PYBIND11_BYTES_FROM_STRING(c), stolen_t{}) {
1020  if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
1021  }
1022 
1023  bytes(const char *c, size_t n)
1025  if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
1026  }
1027 
1028  // Allow implicit conversion:
1029  bytes(const std::string &s) : bytes(s.data(), s.size()) { }
1030 
1031  explicit bytes(const pybind11::str &s);
1032 
1033  operator std::string() const {
1034  char *buffer;
1035  ssize_t length;
1037  pybind11_fail("Unable to extract bytes contents!");
1038  return std::string(buffer, (size_t) length);
1039  }
1040 };
1041 // Note: breathe >= 4.17.0 will fail to build docs if the below two constructors
1042 // are included in the doxygen group; close here and reopen after as a workaround
1043 /// @} pytypes
1044 
1045 inline bytes::bytes(const pybind11::str &s) {
1046  object temp = s;
1047  if (PyUnicode_Check(s.ptr())) {
1048  temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
1049  if (!temp)
1050  pybind11_fail("Unable to extract string contents! (encoding issue)");
1051  }
1052  char *buffer;
1053  ssize_t length;
1054  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length))
1055  pybind11_fail("Unable to extract string contents! (invalid type)");
1056  auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length));
1057  if (!obj)
1058  pybind11_fail("Could not allocate bytes object!");
1059  m_ptr = obj.release().ptr();
1060 }
1061 
1062 inline str::str(const bytes& b) {
1063  char *buffer;
1064  ssize_t length;
1065  if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length))
1066  pybind11_fail("Unable to extract bytes contents!");
1067  auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, (ssize_t) length));
1068  if (!obj)
1069  pybind11_fail("Could not allocate string object!");
1070  m_ptr = obj.release().ptr();
1071 }
1072 
1073 /// \addtogroup pytypes
1074 /// @{
1075 class none : public object {
1076 public:
1078  none() : object(Py_None, borrowed_t{}) { }
1079 };
1080 
1081 class ellipsis : public object {
1082 public:
1084  ellipsis() : object(Py_Ellipsis, borrowed_t{}) { }
1085 };
1086 
1087 class bool_ : public object {
1088 public:
1089  PYBIND11_OBJECT_CVT(bool_, object, PyBool_Check, raw_bool)
1090  bool_() : object(Py_False, borrowed_t{}) { }
1091  // Allow implicit conversion from and to `bool`:
1092  bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { }
1093  operator bool() const { return m_ptr && PyLong_AsLong(m_ptr) != 0; }
1094 
1095 private:
1096  /// Return the truth value of an object -- always returns a new reference
1097  static PyObject *raw_bool(PyObject *op) {
1098  const auto value = PyObject_IsTrue(op);
1099  if (value == -1) return nullptr;
1100  return handle(value ? Py_True : Py_False).inc_ref().ptr();
1101  }
1102 };
1103 
1105 // Converts a value to the given unsigned type. If an error occurs, you get back (Unsigned) -1;
1106 // otherwise you get back the unsigned long or unsigned long long value cast to (Unsigned).
1107 // (The distinction is critically important when casting a returned -1 error value to some other
1108 // unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
1109 template <typename Unsigned>
1110 Unsigned as_unsigned(PyObject *o) {
1111  if (sizeof(Unsigned) <= sizeof(unsigned long)
1112 #if PY_VERSION_HEX < 0x03000000
1113  || PyInt_Check(o)
1114 #endif
1115  ) {
1116  unsigned long v = PyLong_AsUnsignedLong(o);
1117  return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1118  }
1119  else {
1120  unsigned long long v = PyLong_AsUnsignedLongLong(o);
1121  return v == (unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1122  }
1123 }
1124 PYBIND11_NAMESPACE_END(detail)
1125 
1126 class int_ : public object {
1127 public:
1128  PYBIND11_OBJECT_CVT(int_, object, PYBIND11_LONG_CHECK, PyNumber_Long)
1129  int_() : object(PyLong_FromLong(0), stolen_t{}) { }
1130  // Allow implicit conversion from C++ integral types:
1131  template <typename T,
1134  if (sizeof(T) <= sizeof(long)) {
1136  m_ptr = PyLong_FromLong((long) value);
1137  else
1138  m_ptr = PyLong_FromUnsignedLong((unsigned long) value);
1139  } else {
1141  m_ptr = PyLong_FromLongLong((long long) value);
1142  else
1143  m_ptr = PyLong_FromUnsignedLongLong((unsigned long long) value);
1144  }
1145  if (!m_ptr) pybind11_fail("Could not allocate int object!");
1146  }
1147 
1148  template <typename T,
1150  operator T() const {
1152  ? detail::as_unsigned<T>(m_ptr)
1153  : sizeof(T) <= sizeof(long)
1154  ? (T) PyLong_AsLong(m_ptr)
1155  : (T) PYBIND11_LONG_AS_LONGLONG(m_ptr);
1156  }
1157 };
1158 
1159 class float_ : public object {
1160 public:
1161  PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float)
1162  // Allow implicit conversion from float/double:
1163  float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1164  if (!m_ptr) pybind11_fail("Could not allocate float object!");
1165  }
1166  float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1167  if (!m_ptr) pybind11_fail("Could not allocate float object!");
1168  }
1169  operator float() const { return (float) PyFloat_AsDouble(m_ptr); }
1170  operator double() const { return (double) PyFloat_AsDouble(m_ptr); }
1171 };
1172 
1173 class weakref : public object {
1174 public:
1175  PYBIND11_OBJECT_CVT_DEFAULT(weakref, object, PyWeakref_Check, raw_weakref)
1176  explicit weakref(handle obj, handle callback = {})
1177  : object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), stolen_t{}) {
1178  if (!m_ptr) pybind11_fail("Could not allocate weak reference!");
1179  }
1180 
1181 private:
1182  static PyObject *raw_weakref(PyObject *o) {
1183  return PyWeakref_NewRef(o, nullptr);
1184  }
1185 };
1186 
1187 class slice : public object {
1188 public:
1189  PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
1190  slice(ssize_t start_, ssize_t stop_, ssize_t step_) {
1191  int_ start(start_), stop(stop_), step(step_);
1192  m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr());
1193  if (!m_ptr) pybind11_fail("Could not allocate slice object!");
1194  }
1195  bool compute(size_t length, size_t *start, size_t *stop, size_t *step,
1196  size_t *slicelength) const {
1197  return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr,
1198  (ssize_t) length, (ssize_t *) start,
1199  (ssize_t *) stop, (ssize_t *) step,
1200  (ssize_t *) slicelength) == 0;
1201  }
1202  bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step,
1203  ssize_t *slicelength) const {
1204  return PySlice_GetIndicesEx((PYBIND11_SLICE_OBJECT *) m_ptr,
1205  length, start,
1206  stop, step,
1207  slicelength) == 0;
1208  }
1209 };
1210 
1211 class capsule : public object {
1212 public:
1213  PYBIND11_OBJECT_DEFAULT(capsule, object, PyCapsule_CheckExact)
1215  capsule(PyObject *ptr, bool is_borrowed) : object(is_borrowed ? object(ptr, borrowed_t{}) : object(ptr, stolen_t{})) { }
1216 
1217  explicit capsule(const void *value, const char *name = nullptr, void (*destructor)(PyObject *) = nullptr)
1218  : object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
1219  if (!m_ptr)
1220  pybind11_fail("Could not allocate capsule object!");
1221  }
1222 
1223  PYBIND11_DEPRECATED("Please pass a destructor that takes a void pointer as input")
1224  capsule(const void *value, void (*destruct)(PyObject *))
1225  : object(PyCapsule_New(const_cast<void*>(value), nullptr, destruct), stolen_t{}) {
1226  if (!m_ptr)
1227  pybind11_fail("Could not allocate capsule object!");
1228  }
1229 
1230  capsule(const void *value, void (*destructor)(void *)) {
1231  m_ptr = PyCapsule_New(const_cast<void *>(value), nullptr, [](PyObject *o) {
1232  auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
1233  void *ptr = PyCapsule_GetPointer(o, nullptr);
1234  destructor(ptr);
1235  });
1236 
1237  if (!m_ptr)
1238  pybind11_fail("Could not allocate capsule object!");
1239 
1240  if (PyCapsule_SetContext(m_ptr, (void *) destructor) != 0)
1241  pybind11_fail("Could not set capsule context!");
1242  }
1243 
1244  capsule(void (*destructor)()) {
1245  m_ptr = PyCapsule_New(reinterpret_cast<void *>(destructor), nullptr, [](PyObject *o) {
1246  auto destructor = reinterpret_cast<void (*)()>(PyCapsule_GetPointer(o, nullptr));
1247  destructor();
1248  });
1249 
1250  if (!m_ptr)
1251  pybind11_fail("Could not allocate capsule object!");
1252  }
1253 
1254  template <typename T> operator T *() const {
1255  return get_pointer<T>();
1256  }
1257 
1258  /// Get the pointer the capsule holds.
1259  template<typename T = void>
1260  T* get_pointer() const {
1261  auto name = this->name();
1262  T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
1263  if (!result) pybind11_fail("Unable to extract capsule contents!");
1264  return result;
1265  }
1266 
1267  /// Replaces a capsule's pointer *without* calling the destructor on the existing one.
1268  void set_pointer(const void *value) {
1269  if (PyCapsule_SetPointer(m_ptr, const_cast<void *>(value)) != 0)
1270  pybind11_fail("Could not set capsule pointer");
1271  }
1272 
1273  const char *name() const { return PyCapsule_GetName(m_ptr); }
1274 };
1275 
1276 class tuple : public object {
1277 public:
1278  PYBIND11_OBJECT_CVT(tuple, object, PyTuple_Check, PySequence_Tuple)
1279  explicit tuple(size_t size = 0) : object(PyTuple_New((ssize_t) size), stolen_t{}) {
1280  if (!m_ptr) pybind11_fail("Could not allocate tuple object!");
1281  }
1282  size_t size() const { return (size_t) PyTuple_Size(m_ptr); }
1283  bool empty() const { return size() == 0; }
1284  detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
1285  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1286  detail::tuple_iterator begin() const { return {*this, 0}; }
1287  detail::tuple_iterator end() const { return {*this, PyTuple_GET_SIZE(m_ptr)}; }
1288 };
1289 
1290 // We need to put this into a separate function because the Intel compiler
1291 // fails to compile enable_if_t<all_of<is_keyword_or_ds<Args>...>::value> part below
1292 // (tested with ICC 2021.1 Beta 20200827).
1293 template <typename... Args>
1295 {
1296  return detail::all_of<detail::is_keyword_or_ds<Args>...>::value;
1297 }
1298 
1299 class dict : public object {
1300 public:
1301  PYBIND11_OBJECT_CVT(dict, object, PyDict_Check, raw_dict)
1302  dict() : object(PyDict_New(), stolen_t{}) {
1303  if (!m_ptr) pybind11_fail("Could not allocate dict object!");
1304  }
1305  template <typename... Args,
1306  typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>,
1307  // MSVC workaround: it can't compile an out-of-line definition, so defer the collector
1308  typename collector = detail::deferred_t<detail::unpacking_collector<>, Args...>>
1309  explicit dict(Args &&...args) : dict(collector(std::forward<Args>(args)...).kwargs()) { }
1310 
1311  size_t size() const { return (size_t) PyDict_Size(m_ptr); }
1312  bool empty() const { return size() == 0; }
1313  detail::dict_iterator begin() const { return {*this, 0}; }
1314  detail::dict_iterator end() const { return {}; }
1315  void clear() const { PyDict_Clear(ptr()); }
1316  template <typename T> bool contains(T &&key) const {
1317  return PyDict_Contains(m_ptr, detail::object_or_cast(std::forward<T>(key)).ptr()) == 1;
1318  }
1319 
1320 private:
1321  /// Call the `dict` Python type -- always returns a new reference
1322  static PyObject *raw_dict(PyObject *op) {
1323  if (PyDict_Check(op))
1324  return handle(op).inc_ref().ptr();
1325  return PyObject_CallFunctionObjArgs((PyObject *) &PyDict_Type, op, nullptr);
1326  }
1327 };
1328 
1329 class sequence : public object {
1330 public:
1331  PYBIND11_OBJECT_DEFAULT(sequence, object, PySequence_Check)
1332  size_t size() const {
1333  ssize_t result = PySequence_Size(m_ptr);
1334  if (result == -1)
1335  throw error_already_set();
1336  return (size_t) result;
1337  }
1338  bool empty() const { return size() == 0; }
1339  detail::sequence_accessor operator[](size_t index) const { return {*this, index}; }
1340  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1341  detail::sequence_iterator begin() const { return {*this, 0}; }
1342  detail::sequence_iterator end() const { return {*this, PySequence_Size(m_ptr)}; }
1343 };
1344 
1345 class list : public object {
1346 public:
1347  PYBIND11_OBJECT_CVT(list, object, PyList_Check, PySequence_List)
1348  explicit list(size_t size = 0) : object(PyList_New((ssize_t) size), stolen_t{}) {
1349  if (!m_ptr) pybind11_fail("Could not allocate list object!");
1350  }
1351  size_t size() const { return (size_t) PyList_Size(m_ptr); }
1352  bool empty() const { return size() == 0; }
1353  detail::list_accessor operator[](size_t index) const { return {*this, index}; }
1354  detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1355  detail::list_iterator begin() const { return {*this, 0}; }
1356  detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
1357  template <typename T> void append(T &&val) const {
1358  PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
1359  }
1360  template <typename T> void insert(size_t index, T &&val) const {
1361  PyList_Insert(m_ptr, static_cast<ssize_t>(index),
1362  detail::object_or_cast(std::forward<T>(val)).ptr());
1363  }
1364 };
1365 
1366 class args : public tuple { PYBIND11_OBJECT_DEFAULT(args, tuple, PyTuple_Check) };
1367 class kwargs : public dict { PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check) };
1368 
1369 class set : public object {
1370 public:
1371  PYBIND11_OBJECT_CVT(set, object, PySet_Check, PySet_New)
1372  set() : object(PySet_New(nullptr), stolen_t{}) {
1373  if (!m_ptr) pybind11_fail("Could not allocate set object!");
1374  }
1375  size_t size() const { return (size_t) PySet_Size(m_ptr); }
1376  bool empty() const { return size() == 0; }
1377  template <typename T> bool add(T &&val) const {
1378  return PySet_Add(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 0;
1379  }
1380  void clear() const { PySet_Clear(m_ptr); }
1381  template <typename T> bool contains(T &&val) const {
1382  return PySet_Contains(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 1;
1383  }
1384 };
1385 
1386 class function : public object {
1387 public:
1388  PYBIND11_OBJECT_DEFAULT(function, object, PyCallable_Check)
1390  handle fun = detail::get_function(m_ptr);
1391  if (fun && PyCFunction_Check(fun.ptr()))
1392  return fun;
1393  return handle();
1394  }
1395  bool is_cpp_function() const { return (bool) cpp_function(); }
1396 };
1397 
1398 class staticmethod : public object {
1399 public:
1400  PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New)
1401 };
1402 
1403 class buffer : public object {
1404 public:
1405  PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)
1406 
1407  buffer_info request(bool writable = false) const {
1408  int flags = PyBUF_STRIDES | PyBUF_FORMAT;
1409  if (writable) flags |= PyBUF_WRITABLE;
1410  auto *view = new Py_buffer();
1411  if (PyObject_GetBuffer(m_ptr, view, flags) != 0) {
1412  delete view;
1413  throw error_already_set();
1414  }
1415  return buffer_info(view);
1416  }
1417 };
1418 
1419 class memoryview : public object {
1420 public:
1421  PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject)
1422 
1423  /** \rst
1424  Creates ``memoryview`` from ``buffer_info``.
1425 
1426  ``buffer_info`` must be created from ``buffer::request()``. Otherwise
1427  throws an exception.
1428 
1429  For creating a ``memoryview`` from objects that support buffer protocol,
1430  use ``memoryview(const object& obj)`` instead of this constructor.
1431  \endrst */
1432  explicit memoryview(const buffer_info& info) {
1433  if (!info.view())
1434  pybind11_fail("Prohibited to create memoryview without Py_buffer");
1435  // Note: PyMemoryView_FromBuffer never increments obj reference.
1436  m_ptr = (info.view()->obj) ?
1437  PyMemoryView_FromObject(info.view()->obj) :
1438  PyMemoryView_FromBuffer(info.view());
1439  if (!m_ptr)
1440  pybind11_fail("Unable to create memoryview from buffer descriptor");
1441  }
1442 
1443  /** \rst
1444  Creates ``memoryview`` from static buffer.
1445 
1446  This method is meant for providing a ``memoryview`` for C/C++ buffer not
1447  managed by Python. The caller is responsible for managing the lifetime
1448  of ``ptr`` and ``format``, which MUST outlive the memoryview constructed
1449  here.
1450 
1451  See also: Python C API documentation for `PyMemoryView_FromBuffer`_.
1452 
1453  .. _PyMemoryView_FromBuffer: https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromBuffer
1454 
1455  :param ptr: Pointer to the buffer.
1456  :param itemsize: Byte size of an element.
1457  :param format: Pointer to the null-terminated format string. For
1458  homogeneous Buffers, this should be set to
1459  ``format_descriptor<T>::value``.
1460  :param shape: Shape of the tensor (1 entry per dimension).
1461  :param strides: Number of bytes between adjacent entries (for each
1462  per dimension).
1463  :param readonly: Flag to indicate if the underlying storage may be
1464  written to.
1465  \endrst */
1466  static memoryview from_buffer(
1467  void *ptr, ssize_t itemsize, const char *format,
1468  detail::any_container<ssize_t> shape,
1469  detail::any_container<ssize_t> strides, bool readonly = false);
1470 
1472  const void *ptr, ssize_t itemsize, const char *format,
1473  detail::any_container<ssize_t> shape,
1474  detail::any_container<ssize_t> strides) {
1475  return memoryview::from_buffer(
1476  const_cast<void*>(ptr), itemsize, format, shape, strides, true);
1477  }
1478 
1479  template<typename T>
1481  T *ptr, detail::any_container<ssize_t> shape,
1482  detail::any_container<ssize_t> strides, bool readonly = false) {
1483  return memoryview::from_buffer(
1484  reinterpret_cast<void*>(ptr), sizeof(T),
1485  format_descriptor<T>::value, shape, strides, readonly);
1486  }
1487 
1488  template<typename T>
1490  const T *ptr, detail::any_container<ssize_t> shape,
1491  detail::any_container<ssize_t> strides) {
1492  return memoryview::from_buffer(
1493  const_cast<T*>(ptr), shape, strides, true);
1494  }
1495 
1496 #if PY_MAJOR_VERSION >= 3
1497  /** \rst
1498  Creates ``memoryview`` from static memory.
1499 
1500  This method is meant for providing a ``memoryview`` for C/C++ buffer not
1501  managed by Python. The caller is responsible for managing the lifetime
1502  of ``mem``, which MUST outlive the memoryview constructed here.
1503 
1504  This method is not available in Python 2.
1505 
1506  See also: Python C API documentation for `PyMemoryView_FromBuffer`_.
1507 
1508  .. _PyMemoryView_FromMemory: https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromMemory
1509  \endrst */
1510  static memoryview from_memory(void *mem, ssize_t size, bool readonly = false) {
1511  PyObject* ptr = PyMemoryView_FromMemory(
1512  reinterpret_cast<char*>(mem), size,
1513  (readonly) ? PyBUF_READ : PyBUF_WRITE);
1514  if (!ptr)
1515  pybind11_fail("Could not allocate memoryview object!");
1516  return memoryview(object(ptr, stolen_t{}));
1517  }
1518 
1519  static memoryview from_memory(const void *mem, ssize_t size) {
1520  return memoryview::from_memory(const_cast<void*>(mem), size, true);
1521  }
1522 #endif
1523 };
1524 
1525 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1527  void *ptr, ssize_t itemsize, const char* format,
1528  detail::any_container<ssize_t> shape,
1529  detail::any_container<ssize_t> strides, bool readonly) {
1530  size_t ndim = shape->size();
1531  if (ndim != strides->size())
1532  pybind11_fail("memoryview: shape length doesn't match strides length");
1533  ssize_t size = ndim ? 1 : 0;
1534  for (size_t i = 0; i < ndim; ++i)
1535  size *= (*shape)[i];
1536  Py_buffer view;
1537  view.buf = ptr;
1538  view.obj = nullptr;
1539  view.len = size * itemsize;
1540  view.readonly = static_cast<int>(readonly);
1541  view.itemsize = itemsize;
1542  view.format = const_cast<char*>(format);
1543  view.ndim = static_cast<int>(ndim);
1544  view.shape = shape->data();
1545  view.strides = strides->data();
1546  view.suboffsets = nullptr;
1547  view.internal = nullptr;
1548  PyObject* obj = PyMemoryView_FromBuffer(&view);
1549  if (!obj)
1550  throw error_already_set();
1551  return memoryview(object(obj, stolen_t{}));
1552 }
1553 #endif // DOXYGEN_SHOULD_SKIP_THIS
1554 /// @} pytypes
1555 
1556 /// \addtogroup python_builtins
1557 /// @{
1558 
1559 /// Get the length of a Python object.
1560 inline size_t len(handle h) {
1561  ssize_t result = PyObject_Length(h.ptr());
1562  if (result < 0)
1563  throw error_already_set();
1564  return (size_t) result;
1565 }
1566 
1567 /// Get the length hint of a Python object.
1568 /// Returns 0 when this cannot be determined.
1569 inline size_t len_hint(handle h) {
1570 #if PY_VERSION_HEX >= 0x03040000
1571  ssize_t result = PyObject_LengthHint(h.ptr(), 0);
1572 #else
1573  ssize_t result = PyObject_Length(h.ptr());
1574 #endif
1575  if (result < 0) {
1576  // Sometimes a length can't be determined at all (eg generators)
1577  // In which case simply return 0
1578  PyErr_Clear();
1579  return 0;
1580  }
1581  return (size_t) result;
1582 }
1583 
1584 inline str repr(handle h) {
1585  PyObject *str_value = PyObject_Repr(h.ptr());
1586  if (!str_value) throw error_already_set();
1587 #if PY_MAJOR_VERSION < 3
1588  PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
1589  Py_XDECREF(str_value); str_value = unicode;
1590  if (!str_value) throw error_already_set();
1591 #endif
1592  return reinterpret_steal<str>(str_value);
1593 }
1594 
1595 inline iterator iter(handle obj) {
1596  PyObject *result = PyObject_GetIter(obj.ptr());
1597  if (!result) { throw error_already_set(); }
1598  return reinterpret_steal<iterator>(result);
1599 }
1600 /// @} python_builtins
1601 
1603 template <typename D> iterator object_api<D>::begin() const { return iter(derived()); }
1604 template <typename D> iterator object_api<D>::end() const { return iterator::sentinel(); }
1605 template <typename D> item_accessor object_api<D>::operator[](handle key) const {
1606  return {derived(), reinterpret_borrow<object>(key)};
1607 }
1608 template <typename D> item_accessor object_api<D>::operator[](const char *key) const {
1609  return {derived(), pybind11::str(key)};
1610 }
1611 template <typename D> obj_attr_accessor object_api<D>::attr(handle key) const {
1612  return {derived(), reinterpret_borrow<object>(key)};
1613 }
1614 template <typename D> str_attr_accessor object_api<D>::attr(const char *key) const {
1615  return {derived(), key};
1616 }
1617 template <typename D> args_proxy object_api<D>::operator*() const {
1618  return args_proxy(derived().ptr());
1619 }
1620 template <typename D> template <typename T> bool object_api<D>::contains(T &&item) const {
1621  return attr("__contains__")(std::forward<T>(item)).template cast<bool>();
1622 }
1623 
1624 template <typename D>
1625 pybind11::str object_api<D>::str() const { return pybind11::str(derived()); }
1626 
1627 template <typename D>
1628 str_attr_accessor object_api<D>::doc() const { return attr("__doc__"); }
1629 
1630 template <typename D>
1631 handle object_api<D>::get_type() const { return type::handle_of(derived()); }
1632 
1633 template <typename D>
1634 bool object_api<D>::rich_compare(object_api const &other, int value) const {
1635  int rv = PyObject_RichCompareBool(derived().ptr(), other.derived().ptr(), value);
1636  if (rv == -1)
1637  throw error_already_set();
1638  return rv == 1;
1639 }
1640 
1641 #define PYBIND11_MATH_OPERATOR_UNARY(op, fn) \
1642  template <typename D> object object_api<D>::op() const { \
1643  object result = reinterpret_steal<object>(fn(derived().ptr())); \
1644  if (!result.ptr()) \
1645  throw error_already_set(); \
1646  return result; \
1647  }
1648 
1649 #define PYBIND11_MATH_OPERATOR_BINARY(op, fn) \
1650  template <typename D> \
1651  object object_api<D>::op(object_api const &other) const { \
1652  object result = reinterpret_steal<object>( \
1653  fn(derived().ptr(), other.derived().ptr())); \
1654  if (!result.ptr()) \
1655  throw error_already_set(); \
1656  return result; \
1657  }
1658 
1659 PYBIND11_MATH_OPERATOR_UNARY (operator~, PyNumber_Invert)
1660 PYBIND11_MATH_OPERATOR_UNARY (operator-, PyNumber_Negative)
1661 PYBIND11_MATH_OPERATOR_BINARY(operator+, PyNumber_Add)
1662 PYBIND11_MATH_OPERATOR_BINARY(operator+=, PyNumber_InPlaceAdd)
1663 PYBIND11_MATH_OPERATOR_BINARY(operator-, PyNumber_Subtract)
1664 PYBIND11_MATH_OPERATOR_BINARY(operator-=, PyNumber_InPlaceSubtract)
1665 PYBIND11_MATH_OPERATOR_BINARY(operator*, PyNumber_Multiply)
1666 PYBIND11_MATH_OPERATOR_BINARY(operator*=, PyNumber_InPlaceMultiply)
1667 PYBIND11_MATH_OPERATOR_BINARY(operator/, PyNumber_TrueDivide)
1668 PYBIND11_MATH_OPERATOR_BINARY(operator/=, PyNumber_InPlaceTrueDivide)
1669 PYBIND11_MATH_OPERATOR_BINARY(operator|, PyNumber_Or)
1670 PYBIND11_MATH_OPERATOR_BINARY(operator|=, PyNumber_InPlaceOr)
1671 PYBIND11_MATH_OPERATOR_BINARY(operator&, PyNumber_And)
1672 PYBIND11_MATH_OPERATOR_BINARY(operator&=, PyNumber_InPlaceAnd)
1673 PYBIND11_MATH_OPERATOR_BINARY(operator^, PyNumber_Xor)
1674 PYBIND11_MATH_OPERATOR_BINARY(operator^=, PyNumber_InPlaceXor)
1675 PYBIND11_MATH_OPERATOR_BINARY(operator<<, PyNumber_Lshift)
1676 PYBIND11_MATH_OPERATOR_BINARY(operator<<=, PyNumber_InPlaceLshift)
1677 PYBIND11_MATH_OPERATOR_BINARY(operator>>, PyNumber_Rshift)
1678 PYBIND11_MATH_OPERATOR_BINARY(operator>>=, PyNumber_InPlaceRshift)
1679 
1680 #undef PYBIND11_MATH_OPERATOR_UNARY
1681 #undef PYBIND11_MATH_OPERATOR_BINARY
1682 
1683 PYBIND11_NAMESPACE_END(detail)
accessor::key
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:514
generic_iterator::operator>
friend bool operator>(const It &a, const It &b)
Definition: pytypes.h:654
sequence_fast_readonly::dereference
reference dereference() const
Definition: pytypes.h:679
PyNone_Check
bool PyNone_Check(PyObject *o)
Definition: pytypes.h:756
int_
Definition: pytypes.h:1126
sequence_item::get
static object get(handle obj, size_t index)
Definition: pytypes.h:571
str_attr
Definition: pytypes.h:548
enable_if_t
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Definition: common.h:504
sequence::operator[]
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1340
type::of
static type of(handle h)
Return a type object from a handle or an object.
Definition: pytypes.h:923
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
error_already_set::error_already_set
error_already_set()
Definition: pytypes.h:330
format_descriptor
Definition: common.h:754
generic_iterator::difference_type
ssize_t difference_type
Definition: pytypes.h:626
str::str
str(const std::string &s)
Definition: pytypes.h:961
generic_iterator::operator-
friend It operator-(const It &a, difference_type n)
Definition: pytypes.h:648
error_already_set::matches
bool matches(handle exc) const
Definition: pytypes.h:365
dict_readonly::increment
void increment()
Definition: pytypes.h:724
PYBIND11_OBJECT_CVT_DEFAULT
#define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun)
Definition: pytypes.h:822
object_api::PYBIND11_DEPRECATED
PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)") object call(Args &&... args) const
common.h
list::end
detail::list_iterator end() const
Definition: pytypes.h:1356
object::object
object(const object &o)
Copy constructor; always increases the reference count.
Definition: pytypes.h:238
name
Annotation for function names.
Definition: attr.h:36
object::reinterpret_borrow
friend T reinterpret_borrow(handle)
Definition: pytypes.h:306
generic_iterator::operator<=
friend bool operator<=(const It &a, const It &b)
Definition: pytypes.h:656
setattr
void setattr(handle obj, handle name, handle value)
Definition: pytypes.h:451
is_s_unpacking
std::is_same< args_proxy, T > is_s_unpacking
Definition: pytypes.h:781
cast
T cast(const handle &handle)
Definition: cast.h:1769
isinstance_generic
bool isinstance_generic(handle obj, const std::type_info &tp)
Definition: cast.h:416
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:16
accessor::operator=
void operator=(const accessor &a) &&
Definition: pytypes.h:503
generic_iterator::pointer
typename Policy::pointer pointer
Definition: pytypes.h:630
dict::dict
dict(Args &&...args)
Definition: pytypes.h:1309
bytes
Definition: pytypes.h:1013
deferred_t
typename deferred_type< T, Us... >::type deferred_t
Definition: common.h:638
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:17
set
Definition: pytypes.h:1369
generic_iterator::iterator_category
typename Policy::iterator_category iterator_category
Definition: pytypes.h:627
list_item::get
static object get(handle obj, size_t index)
Definition: pytypes.h:588
accessor::cache
object cache
Definition: pytypes.h:538
dict_readonly
Python's dictionary protocol permits this to be a forward iterator.
Definition: pytypes.h:713
PYBIND11_OBJECT
#define PYBIND11_OBJECT(Name, Parent, CheckFun)
Definition: pytypes.h:831
function::is_cpp_function
bool is_cpp_function() const
Definition: pytypes.h:1395
error_already_set
Definition: pytypes.h:326
object::stolen_t
Definition: pytypes.h:280
generic_iterator::operator+=
It & operator+=(difference_type n)
Definition: pytypes.h:643
object_api::operator&=
object operator&=(object_api const &other) const
memoryview::memoryview
memoryview(const buffer_info &info)
Definition: pytypes.h:1432
kwargs
Definition: pytypes.h:1367
PYBIND11_NAMESPACE
#define PYBIND11_NAMESPACE
Definition: common.h:26
object_api::operator<<
object operator<<(object_api const &other) const
sequence_slow_readwrite::increment
void increment()
Definition: pytypes.h:701
object_api::operator>>=
object operator>>=(object_api const &other) const
list_accessor
accessor< accessor_policies::list_item > list_accessor
Definition: pytypes.h:43
object_api::is_none
bool is_none() const
Equivalent to obj is None in Python.
Definition: pytypes.h:117
data
arr data(const arr &a, Ix... index)
Definition: test_numpy_array.cpp:82
tuple_item::key_type
size_t key_type
Definition: pytypes.h:603
error_already_set::trace
const object & trace() const
Definition: pytypes.h:369
staticmethod
Definition: pytypes.h:1398
str_attr::get
static object get(handle obj, const char *key)
Definition: pytypes.h:550
generic_item
Definition: pytypes.h:554
sequence_fast_readonly::distance_to
ssize_t distance_to(const sequence_fast_readonly &b) const
Definition: pytypes.h:684
list
Definition: pytypes.h:1345
object_api::operator*
object operator*(object_api const &other) const
generic_iterator::operator++
It & operator++()
Definition: pytypes.h:639
bytes::bytes
bytes(const char *c="")
Definition: pytypes.h:1018
error_string
std::string error_string()
Definition: cast.h:423
object::reinterpret_steal
friend T reinterpret_steal(handle)
Definition: pytypes.h:316
str_attr::set
static void set(handle obj, const char *key, handle val)
Definition: pytypes.h:551
object_api
Definition: pytypes.h:55
PYBIND11_DEPRECATED
#define PYBIND11_DEPRECATED(reason)
Definition: common.h:94
bytes::bytes
bytes(const char *c, size_t n)
Definition: pytypes.h:1023
str::format
str format(Args &&...args) const
Definition: pytypes.h:986
PYBIND11_OBJECT_CVT
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun)
Definition: pytypes.h:812
str::str
str(const char *c, size_t n)
Definition: pytypes.h:950
dict_readonly::value_type
std::pair< handle, handle > value_type
Definition: pytypes.h:716
type::of
static type of()
Definition: pytypes.h:936
PYBIND11_BYTES_CHECK
#define PYBIND11_BYTES_CHECK
Definition: common.h:216
literals
Definition: cast.h:1976
object_api::operator|=
object operator|=(object_api const &other) const
dict_readonly::equal
bool equal(const dict_readonly &b) const
Definition: pytypes.h:725
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:192
tuple::operator[]
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1285
sequence_accessor
accessor< accessor_policies::sequence_item > sequence_accessor
Definition: pytypes.h:42
set::add
bool add(T &&val) const
Definition: pytypes.h:1377
arrow_proxy::arrow_proxy
arrow_proxy(T &&value)
Definition: pytypes.h:665
object_api::operator>=
bool operator>=(object_api const &other) const
Definition: pytypes.h:124
obj_attr
Definition: pytypes.h:542
PYBIND11_STR_CHECK_FUN
#define PYBIND11_STR_CHECK_FUN
Definition: pytypes.h:763
hasattr
bool hasattr(handle obj, handle name)
Definition: pytypes.h:405
object_api::operator-=
object operator-=(object_api const &other) const
capsule
Definition: pytypes.h:1211
object_api::str
pybind11::str str() const
Definition: pytypes.h:1625
object_api::operator<
bool operator<(object_api const &other) const
Definition: pytypes.h:121
arrow_proxy
Quick proxy class needed to implement operator-> for iterators which can't return pointers.
Definition: pytypes.h:662
generic_iterator::reference
typename Policy::reference reference
Definition: pytypes.h:629
object_api::contains
bool contains(T &&item) const
Check if the given item is contained within this object, i.e. item in obj.
Definition: pytypes.h:1620
object_api::operator&
object operator&(object_api const &other) const
type
Definition: pytypes.h:915
dict::end
detail::dict_iterator end() const
Definition: pytypes.h:1314
getattr
object getattr(handle obj, handle name)
Definition: pytypes.h:421
iterator::difference_type
ssize_t difference_type
Definition: pytypes.h:857
handle::m_ptr
PyObject * m_ptr
Definition: pytypes.h:219
object_api::operator()
object operator()(Args &&...args) const
Definition: cast.h:2254
list::insert
void insert(size_t index, T &&val) const
Definition: pytypes.h:1360
generic_iterator::operator--
It operator--(int)
Definition: pytypes.h:642
object_api::not_equal
bool not_equal(object_api const &other) const
Definition: pytypes.h:120
buffer
Definition: pytypes.h:1403
sequence_slow_readwrite::iterator_category
std::random_access_iterator_tag iterator_category
Definition: pytypes.h:693
memoryview::from_buffer
static memoryview from_buffer(const T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides)
Definition: pytypes.h:1489
object_api::begin
iterator begin() const
Definition: pytypes.h:1603
iterator
Definition: pytypes.h:854
isinstance< handle >
bool isinstance< handle >(handle)=delete
error_already_set::restore
void restore()
Definition: pytypes.h:342
tuple::empty
bool empty() const
Definition: pytypes.h:1283
sequence_fast_readonly
Lightweight iterator policy using just a simple pointer: see PySequence_Fast_ITEMS
Definition: pytypes.h:670
object_api::operator/
object operator/(object_api const &other) const
weakref
Definition: pytypes.h:1173
object::PYBIND11_DEPRECATED
PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()") object(handle h
script.begin
begin
Definition: script.py:184
hash
ssize_t hash(handle obj)
Definition: pytypes.h:459
as_unsigned
Unsigned as_unsigned(PyObject *o)
Definition: pytypes.h:1110
set::size
size_t size() const
Definition: pytypes.h:1375
generic_iterator::operator==
friend bool operator==(const It &a, const It &b)
Definition: pytypes.h:651
list_item::key_type
size_t key_type
Definition: pytypes.h:586
object_api::equal
bool equal(object_api const &other) const
Equivalent to obj == other in Python.
Definition: pytypes.h:119
sequence_fast_readonly::equal
bool equal(const sequence_fast_readonly &b) const
Definition: pytypes.h:683
bool_
Definition: pytypes.h:1087
object_api::operator>
bool operator>(object_api const &other) const
Definition: pytypes.h:123
generic_iterator::operator-=
It & operator-=(difference_type n)
Definition: pytypes.h:644
object::~object
~object()
Destructor; automatically calls handle::dec_ref()
Definition: pytypes.h:242
set::clear
void clear() const
Definition: pytypes.h:1380
slice
Definition: pytypes.h:1187
object
Definition: pytypes.h:232
error_already_set::value
const object & value() const
Definition: pytypes.h:368
generic_item::get
static object get(handle obj, handle key)
Definition: pytypes.h:557
doc
Annotation for documentation.
Definition: attr.h:33
list::size
size_t size() const
Definition: pytypes.h:1351
error_already_set::discard_as_unraisable
void discard_as_unraisable(const char *err_context)
Definition: pytypes.h:354
generic_iterator
STL iterator template used for tuple, list, sequence and dict.
Definition: pytypes.h:622
tuple::begin
detail::tuple_iterator begin() const
Definition: pytypes.h:1286
generic_iterator::operator>=
friend bool operator>=(const It &a, const It &b)
Definition: pytypes.h:655
sequence::empty
bool empty() const
Definition: pytypes.h:1338
sequence_slow_readwrite::decrement
void decrement()
Definition: pytypes.h:702
dict
Definition: pytypes.h:1299
dict_readonly::reference
const value_type reference
Definition: pytypes.h:717
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:386
accessor::operator=
void operator=(T &&value) &
Definition: pytypes.h:509
sequence_fast_readonly::increment
void increment()
Definition: pytypes.h:680
set::empty
bool empty() const
Definition: pytypes.h:1376
sequence::begin
detail::sequence_iterator begin() const
Definition: pytypes.h:1341
handle
Definition: pytypes.h:176
generic_iterator::generic_iterator
generic_iterator(handle seq, ssize_t index)
Definition: pytypes.h:633
arrow_proxy::value
T value
Definition: pytypes.h:663
sequence
Definition: pytypes.h:1329
sequence_item::key_type
size_t key_type
Definition: pytypes.h:569
handle::cast
T cast() const
Definition: cast.h:1794
tuple_accessor
accessor< accessor_policies::tuple_item > tuple_accessor
Definition: pytypes.h:44
tuple_item::get
static object get(handle obj, size_t index)
Definition: pytypes.h:605
object_api::operator^=
object operator^=(object_api const &other) const
type::handle_of
static handle handle_of()
Definition: cast.h:2268
dict_readonly::dict_readonly
dict_readonly()=default
reinterpret_borrow
T reinterpret_borrow(handle h)
Definition: pytypes.h:306
delattr
void delattr(handle obj, handle name)
Definition: pytypes.h:413
arg_v
Definition: cast.h:1912
object_api::operator*=
object operator*=(object_api const &other) const
iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: pytypes.h:856
sequence_fast_readonly::decrement
void decrement()
Definition: pytypes.h:681
PyStaticMethod_Check
bool PyStaticMethod_Check(PyObject *o)
Definition: pytypes.h:766
args_are_all_keyword_or_ds
constexpr bool args_are_all_keyword_or_ds()
Definition: pytypes.h:1294
args_proxy::operator*
kwargs_proxy operator*() const
Definition: pytypes.h:776
object::object
object()=default
error_already_set::error_already_set
error_already_set(error_already_set &&)=default
object::release
handle release()
Definition: pytypes.h:249
list_item
Definition: pytypes.h:585
memoryview::from_buffer
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:1471
memoryview::from_buffer
static memoryview from_buffer(T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides, bool readonly=false)
Definition: pytypes.h:1480
list_item::set
static void set(handle obj, size_t index, handle val)
Definition: pytypes.h:594
get_function
handle get_function(handle value)
Definition: pytypes.h:468
str::str
str(const char *c="")
Definition: pytypes.h:956
generic_iterator::operator--
It & operator--()
Definition: pytypes.h:641
arg
Definition: cast.h:1895
iterator::operator!=
friend bool operator!=(const iterator &a, const iterator &b)
Definition: pytypes.h:901
accessor::accessor
accessor(accessor &&)=default
object_api::operator>>
object operator>>(object_api const &other) const
object_api::get_type
handle get_type() const
Definition: pytypes.h:1631
slice::compute
bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const
Definition: pytypes.h:1195
negation
Definition: common.h:529
policy
Definition: policy.py:1
set::contains
bool contains(T &&val) const
Definition: pytypes.h:1381
sequence_slow_readwrite::sequence_slow_readwrite
sequence_slow_readwrite(handle obj, ssize_t index)
Definition: pytypes.h:698
dict::clear
void clear() const
Definition: pytypes.h:1315
generic_iterator::operator<
friend bool operator<(const It &a, const It &b)
Definition: pytypes.h:653
PYBIND11_BYTES_FROM_STRING_AND_SIZE
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE
Definition: common.h:218
object_or_cast
auto object_or_cast(T &&o) -> decltype(std::forward< T >(o))
Definition: pytypes.h:485
dict::size
size_t size() const
Definition: pytypes.h:1311
object::borrowed_t
Definition: pytypes.h:279
object_api::operator~
object operator~() const
memoryview
Definition: pytypes.h:1419
ssize_t
Py_ssize_t ssize_t
Definition: common.h:353
is_ds_unpacking
std::is_same< kwargs_proxy, T > is_ds_unpacking
Definition: pytypes.h:782
object_api::is
bool is(object_api const &other) const
Equivalent to obj is other in Python.
Definition: pytypes.h:115
int_::int_
int_(T value)
Definition: pytypes.h:1133
cpp_function
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:62
args_proxy::args_proxy
args_proxy(handle h)
Definition: pytypes.h:775
simple_collector
Definition: cast.h:2077
kwargs_proxy
Definition: pytypes.h:768
size_t
std::size_t size_t
Definition: common.h:354
bool_::bool_
bool_(bool value)
Definition: pytypes.h:1092
buffer_info.h
memoryview::from_buffer
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:1526
tuple::operator[]
detail::tuple_accessor operator[](size_t index) const
Definition: pytypes.h:1284
generic_iterator::generic_iterator
generic_iterator()=default
object::object
object(object &&other) noexcept
Move constructor; steals the object from other and preserves its reference count.
Definition: pytypes.h:240
generic_iterator::operator->
pointer operator->() const
Definition: pytypes.h:637
accessor::operator=
void operator=(const accessor &a) &
Definition: pytypes.h:504
PYBIND11_MATH_OPERATOR_UNARY
#define PYBIND11_MATH_OPERATOR_UNARY(op, fn)
Definition: pytypes.h:1641
is_keyword
std::is_base_of< arg, T > is_keyword
Python argument categories (using PEP 448 terms)
Definition: pytypes.h:780
capsule::is_borrowed
bool is_borrowed
Definition: pytypes.h:1215
pybind11_fail
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
Definition: common.h:751
str
Definition: pytypes.h:946
PYBIND11_LONG_AS_LONGLONG
#define PYBIND11_LONG_AS_LONGLONG(o)
Definition: common.h:223
object::operator=
object & operator=(object &&other) noexcept
Definition: pytypes.h:262
tuple_item
Definition: pytypes.h:602
unpacking_collector
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
Definition: cast.h:2102
object_api::ref_count
int ref_count() const
Return the object's current reference count.
Definition: pytypes.h:154
isinstance< object >
bool isinstance< object >(handle obj)
Definition: pytypes.h:392
PYBIND11_OBJECT_DEFAULT
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
Definition: pytypes.h:839
object_api::operator-
object operator-() const
tuple_iterator
generic_iterator< iterator_policies::sequence_fast_readonly > tuple_iterator
Definition: pytypes.h:735
move
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1798
accessor
Definition: pytypes.h:493
tuple::end
detail::tuple_iterator end() const
Definition: pytypes.h:1287
iterator::operator->
pointer operator->() const
Definition: pytypes.h:883
list::append
void append(T &&val) const
Definition: pytypes.h:1357
generic_iterator::operator++
It operator++(int)
Definition: pytypes.h:640
accessor::operator=
void operator=(T &&value) &&
Definition: pytypes.h:506
dict_readonly::iterator_category
std::forward_iterator_tag iterator_category
Definition: pytypes.h:715
sequence_slow_readwrite::advance
void advance(ssize_t n)
Definition: pytypes.h:703
dict::contains
bool contains(T &&key) const
Definition: pytypes.h:1316
handle::ptr
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:184
tuple::size
size_t size() const
Definition: pytypes.h:1282
generic_iterator::operator+
friend It operator+(difference_type n, const It &b)
Definition: pytypes.h:647
benchmark.size
size
Definition: benchmark.py:90
dict_readonly::dict_readonly
dict_readonly(handle obj, ssize_t pos)
Definition: pytypes.h:721
sequence_fast_readonly::iterator_category
std::random_access_iterator_tag iterator_category
Definition: pytypes.h:672
iterator::operator==
friend bool operator==(const iterator &a, const iterator &b)
Definition: pytypes.h:900
error_already_set::type
const object & type() const
Definition: pytypes.h:367
sequence_item
Definition: pytypes.h:568
arrow_proxy::operator->
T * operator->() const
Definition: pytypes.h:666
sequence::operator[]
detail::sequence_accessor operator[](size_t index) const
Definition: pytypes.h:1339
handle::handle
handle()=default
The default constructor creates a handle with a nullptr-valued pointer.
dict::empty
bool empty() const
Definition: pytypes.h:1312
obj_attr::get
static object get(handle obj, handle key)
Definition: pytypes.h:544
iter
iterator iter(handle obj)
Definition: pytypes.h:1595
iterable
Definition: pytypes.h:939
object::object
object(handle h, borrowed_t)
Definition: pytypes.h:289
float_::float_
float_(double value=.0)
Definition: pytypes.h:1166
str::str
str(handle h)
Definition: pytypes.h:969
item_accessor
accessor< accessor_policies::generic_item > item_accessor
Definition: pytypes.h:41
dict::begin
detail::dict_iterator begin() const
Definition: pytypes.h:1313
args
Definition: pytypes.h:1366
buffer_info
Information record describing a Python buffer object.
Definition: buffer_info.h:40
args_proxy
Definition: pytypes.h:773
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: common.h:357
sequence_fast_readonly::sequence_fast_readonly
sequence_fast_readonly(handle obj, ssize_t n)
Definition: pytypes.h:677
generic_iterator::value_type
typename Policy::value_type value_type
Definition: pytypes.h:628
sequence_slow_readwrite
Full read and write access using the sequence protocol: see detail::sequence_accessor
Definition: pytypes.h:691
str_attr::key_type
const char * key_type
Definition: pytypes.h:549
tuple
Definition: pytypes.h:1276
generic_iterator::operator*
reference operator*() const
Definition: pytypes.h:635
is_pyobject
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
Definition: pytypes.h:48
list::operator[]
detail::list_accessor operator[](size_t index) const
Definition: pytypes.h:1353
object_api::doc
str_attr_accessor doc() const
Get or set the object's docstring, i.e. obj.__doc__.
Definition: pytypes.h:1628
float_
Definition: pytypes.h:1159
handle::dec_ref
const handle & dec_ref() const &
Definition: pytypes.h:199
object::object
object(handle h, stolen_t)
Definition: pytypes.h:290
object_api::operator<=
bool operator<=(object_api const &other) const
Definition: pytypes.h:122
iterator::operator++
iterator operator++(int)
Definition: pytypes.h:869
accessor::accessor
accessor(handle obj, key_type key)
Definition: pytypes.h:497
list_iterator
generic_iterator< iterator_policies::sequence_fast_readonly > list_iterator
Definition: pytypes.h:736
tuple_item::set
static void set(handle obj, size_t index, handle val)
Definition: pytypes.h:611
PyIterable_Check
bool PyIterable_Check(PyObject *obj)
Definition: pytypes.h:745
sequence_slow_readwrite::equal
bool equal(const sequence_slow_readwrite &b) const
Definition: pytypes.h:704
object_api::operator/=
object operator/=(object_api const &other) const
accessor_policies
Definition: pytypes.h:31
PYBIND11_MATH_OPERATOR_BINARY
#define PYBIND11_MATH_OPERATOR_BINARY(op, fn)
Definition: pytypes.h:1649
object_api::operator[]
item_accessor operator[](handle key) const
Definition: pytypes.h:1605
pybind11
Definition: __init__.py:1
function
Definition: pytypes.h:1386
generic_item::set
static void set(handle obj, handle key, handle val)
Definition: pytypes.h:563
object_api::attr
obj_attr_accessor attr(handle key) const
Definition: pytypes.h:1611
error_already_set::discard_as_unraisable
void discard_as_unraisable(object err_context)
Definition: pytypes.h:350
object::operator=
object & operator=(const object &other)
Definition: pytypes.h:255
sequence_item::set
static void set(handle obj, size_t index, handle val)
Definition: pytypes.h:577
len_hint
size_t len_hint(handle h)
Definition: pytypes.h:1569
error_already_set::error_already_set
error_already_set(const error_already_set &)=default
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:1560
object::cast
T cast() const &
handle::ptr
PyObject *& ptr()
Definition: pytypes.h:185
PYBIND11_LONG_CHECK
#define PYBIND11_LONG_CHECK(o)
Definition: common.h:222
object_api::operator|
object operator|(object_api const &other) const
none
Definition: pytypes.h:1075
pyobject_tag
Tag and check to identify a class which implements the Python object API.
Definition: pytypes.h:47
object_api::operator+
object operator+(object_api const &other) const
sequence_iterator
generic_iterator< iterator_policies::sequence_slow_readwrite > sequence_iterator
Definition: pytypes.h:742
handle::handle
handle(PyObject *ptr)
Creates a handle from the given raw Python object pointer.
Definition: pytypes.h:181
generic_iterator::operator!=
friend bool operator!=(const It &a, const It &b)
Definition: pytypes.h:652
object_api::operator^
object operator^(object_api const &other) const
object_api::operator<<=
object operator<<=(object_api const &other) const
object_api::operator-
object operator-(object_api const &other) const
dict_readonly::dereference
reference dereference() const
Definition: pytypes.h:723
reinterpret_steal
T reinterpret_steal(handle h)
Definition: pytypes.h:316
object_api::operator*
args_proxy operator*() const
Definition: pytypes.h:1617
dict_iterator
generic_iterator< iterator_policies::dict_readonly > dict_iterator
Definition: pytypes.h:743
kwargs_proxy::kwargs_proxy
kwargs_proxy(handle h)
Definition: pytypes.h:770
list::empty
bool empty() const
Definition: pytypes.h:1352
PyEllipsis_Check
bool PyEllipsis_Check(PyObject *o)
Definition: pytypes.h:757
capsule::PYBIND11_DEPRECATED
PYBIND11_DEPRECATED("Use reinterpret_borrow<capsule>() or reinterpret_steal<capsule>()") capsule(PyObject *ptr
slice::compute
bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step, ssize_t *slicelength) const
Definition: pytypes.h:1202
generic_iterator::operator[]
reference operator[](difference_type n) const
Definition: pytypes.h:636
generic_iterator::operator-
friend difference_type operator-(const It &a, const It &b)
Definition: pytypes.h:649
object_api::operator+=
object operator+=(object_api const &other) const
sequence_slow_readwrite::distance_to
ssize_t distance_to(const sequence_slow_readwrite &b) const
Definition: pytypes.h:705
test_callbacks.value
value
Definition: test_callbacks.py:126
object::is_borrowed
bool is_borrowed
Definition: pytypes.h:236
generic_iterator::operator+
friend It operator+(const It &a, difference_type n)
Definition: pytypes.h:646
sequence_slow_readwrite::dereference
reference dereference() const
Definition: pytypes.h:700
iterator::operator*
reference operator*() const
Definition: pytypes.h:875
PYBIND11_BYTES_AS_STRING_AND_SIZE
#define PYBIND11_BYTES_AS_STRING_AND_SIZE
Definition: common.h:219
obj_attr::set
static void set(handle obj, handle key, handle val)
Definition: pytypes.h:545
accessor::accessor
accessor(const accessor &)=default
iterator::sentinel
static iterator sentinel()
Definition: pytypes.h:898
ellipsis
Definition: pytypes.h:1081
sequence_fast_readonly::advance
void advance(ssize_t n)
Definition: pytypes.h:682
bytes::bytes
bytes(const std::string &s)
Definition: pytypes.h:1029
object_api::end
iterator end() const
Return a sentinel which ends iteration.
Definition: pytypes.h:1604
PYBIND11_FROM_STRING
#define PYBIND11_FROM_STRING
Definition: common.h:229
sequence::end
detail::sequence_iterator end() const
Definition: pytypes.h:1342
list::operator[]
detail::item_accessor operator[](handle h) const
Definition: pytypes.h:1354
PYBIND11_SLICE_OBJECT
#define PYBIND11_SLICE_OBJECT
Definition: common.h:228
repr
str repr(handle h)
Definition: pytypes.h:1584
list::begin
detail::list_iterator begin() const
Definition: pytypes.h:1355
PYBIND11_BYTES_FROM_STRING
#define PYBIND11_BYTES_FROM_STRING
Definition: common.h:217