cppyabm
1.0.17
An agent-based library to integrate C++ and Python
|
|
Go to the documentation of this file.
15 #include <type_traits>
30 template <
typename Policy>
class accessor;
48 template <
typename T>
using is_pyobject = std::is_base_of<pyobject_tag, remove_reference_t<T>>;
54 template <
typename Derived>
56 const Derived &derived()
const {
return static_cast<const Derived &
>(*this); }
96 template <
typename T>
bool contains(T &&item)
const;
115 bool is(
object_api const& other)
const {
return derived().ptr() == other.derived().ptr(); }
117 bool is_none()
const {
return derived().ptr() == Py_None; }
154 int ref_count()
const {
return static_cast<int>(Py_REFCNT(derived().ptr())); }
184 PyObject *
ptr()
const {
return m_ptr; }
185 PyObject *&
ptr() {
return m_ptr; }
205 template <
typename T> T
cast()
const;
207 explicit operator bool()
const {
return m_ptr !=
nullptr; }
213 bool operator==(const
handle &h)
const {
return m_ptr == h.m_ptr; }
215 bool operator!=(const
handle &h)
const {
return m_ptr != h.m_ptr; }
217 bool check()
const {
return m_ptr !=
nullptr; }
219 PyObject *m_ptr =
nullptr;
240 object(
object &&other) noexcept {
m_ptr = other.m_ptr; other.m_ptr =
nullptr; }
250 PyObject *tmp =
m_ptr;
263 if (
this != &other) {
266 other.m_ptr =
nullptr;
273 template <
typename T> T
cast() const &;
275 template <typename T> T
cast() &&;
282 #ifndef DOXYGEN_SHOULD_SKIP_THIS // Issue in breathe 4.26.1
331 PyErr_Fetch(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr());
342 void restore() { PyErr_Restore(m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr()); }
352 PyErr_WriteUnraisable(err_context.
ptr());
365 bool matches(
handle exc)
const {
return PyErr_GivenExceptionMatches(m_type.ptr(), exc.
ptr()); }
367 const object&
type()
const {
return m_type; }
368 const object&
value()
const {
return m_value; }
369 const object&
trace()
const {
return m_trace; }
372 object m_type, m_value, m_trace;
397 const auto result = PyObject_IsInstance(obj.
ptr(),
type.
ptr());
406 return PyObject_HasAttr(obj.
ptr(),
name.ptr()) == 1;
410 return PyObject_HasAttrString(obj.
ptr(),
name) == 1;
422 PyObject *result = PyObject_GetAttr(obj.
ptr(),
name.ptr());
424 return reinterpret_steal<object>(result);
428 PyObject *result = PyObject_GetAttrString(obj.
ptr(),
name);
430 return reinterpret_steal<object>(result);
434 if (PyObject *result = PyObject_GetAttr(obj.
ptr(),
name.ptr())) {
435 return reinterpret_steal<object>(result);
438 return reinterpret_borrow<object>(default_);
443 if (PyObject *result = PyObject_GetAttrString(obj.
ptr(),
name)) {
444 return reinterpret_steal<object>(result);
447 return reinterpret_borrow<object>(default_);
460 auto h = PyObject_Hash(obj.
ptr());
470 #if PY_MAJOR_VERSION >= 3
471 if (PyInstanceMethod_Check(
value.ptr()))
472 value = PyInstanceMethod_GET_FUNCTION(
value.ptr());
475 if (PyMethod_Check(
value.ptr()))
485 auto object_or_cast(T &&o) -> decltype(std::forward<T>(o)) {
return std::forward<T>(o); }
492 template <
typename Policy>
494 using key_type =
typename Policy::key_type;
513 template <
typename T = Policy>
514 PYBIND11_DEPRECATED(
"Use of obj.attr(...) as bool is deprecated in favor of pybind11::hasattr(obj, ...)")
519 template <
typename T = Policy>
522 return obj.contains(
key);
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>(); }
530 object &get_cache()
const {
558 PyObject *result = PyObject_GetItem(obj.
ptr(), key.
ptr());
560 return reinterpret_steal<object>(result);
572 PyObject *result = PySequence_GetItem(obj.
ptr(),
static_cast<ssize_t>(index));
574 return reinterpret_steal<object>(result);
579 if (PySequence_SetItem(obj.
ptr(),
static_cast<ssize_t>(index), val.
ptr()) != 0) {
589 PyObject *result = PyList_GetItem(obj.
ptr(),
static_cast<ssize_t>(index));
591 return reinterpret_borrow<object>(result);
606 PyObject *result = PyTuple_GetItem(obj.
ptr(),
static_cast<ssize_t>(index));
608 return reinterpret_borrow<object>(result);
621 template <
typename Policy>
640 It operator++(
int) {
auto copy = *
this; Policy::increment();
return copy; }
642 It operator--(
int) {
auto copy = *
this; Policy::decrement();
return copy; }
661 template <
typename T>
724 void increment() {
if (!PyDict_Next(obj.
ptr(), &pos, &key, &value)) { pos = -1; } }
729 PyObject *key =
nullptr, *value =
nullptr;
734 #if !defined(PYPY_VERSION)
746 PyObject *
iter = PyObject_GetIter(obj);
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
763 #define PYBIND11_STR_CHECK_FUN PyUnicode_Check
780 template <
typename T>
using is_keyword = std::is_base_of<arg, T>;
789 template <return_value_policy policy = return_value_policy::automatic_reference>
791 template <return_value_policy policy = return_value_policy::automatic_reference>
800 #define PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
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)) { }
812 #define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
813 PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
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(); } \
819 : Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
820 { if (!m_ptr) throw error_already_set(); }
822 #define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun) \
823 PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
824 Name() : Parent() { }
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 "'")
831 #define PYBIND11_OBJECT(Name, Parent, CheckFun) \
832 PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
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); }
839 #define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \
840 PYBIND11_OBJECT(Name, Parent, CheckFun) \
841 Name() : Parent() { }
876 if (
m_ptr && !value.ptr()) {
877 auto&
self =
const_cast<iterator &
>(*this);
905 value = reinterpret_steal<object>(PyIter_Next(
m_ptr));
950 str(const
char *c,
size_t n)
971 operator std::string()
const {
973 if (PyUnicode_Check(
m_ptr)) {
974 temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(
m_ptr));
976 pybind11_fail(
"Unable to extract string contents! (encoding issue)");
981 pybind11_fail(
"Unable to extract string contents! (invalid type)");
982 return std::string(
buffer, (
size_t) length);
985 template <
typename... Args>
987 return attr(
"format")(std::forward<Args>(
args)...);
992 static PyObject *raw_str(PyObject *op) {
993 PyObject *str_value = PyObject_Str(op);
994 #if PY_MAJOR_VERSION < 3
996 PyObject *unicode = PyUnicode_FromEncodedObject(str_value,
"utf-8",
nullptr);
997 Py_XDECREF(str_value); str_value = unicode;
1008 inline str operator"" _s(
const char *s,
size_t size) {
return {s,
size}; }
1031 explicit bytes(
const pybind11::str &s);
1033 operator std::string()
const {
1038 return std::string(
buffer, (
size_t) length);
1047 if (PyUnicode_Check(s.ptr())) {
1048 temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
1050 pybind11_fail(
"Unable to extract string contents! (encoding issue)");
1055 pybind11_fail(
"Unable to extract string contents! (invalid type)");
1067 auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(
buffer, (
ssize_t) length));
1093 operator bool()
const {
return m_ptr && PyLong_AsLong(
m_ptr) != 0; }
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();
1109 template <
typename Un
signed>
1111 if (
sizeof(Unsigned) <=
sizeof(
unsigned long)
1112 #
if PY_VERSION_HEX < 0x03000000
1116 unsigned long v = PyLong_AsUnsignedLong(o);
1117 return v == (
unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1120 unsigned long long v = PyLong_AsUnsignedLongLong(o);
1121 return v == (
unsigned long long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
1131 template <
typename T,
1134 if (
sizeof(T) <=
sizeof(
long)) {
1136 m_ptr = PyLong_FromLong((
long)
value);
1138 m_ptr = PyLong_FromUnsignedLong((
unsigned long)
value);
1141 m_ptr = PyLong_FromLongLong((
long long)
value);
1143 m_ptr = PyLong_FromUnsignedLongLong((
unsigned long long)
value);
1145 if (!m_ptr)
pybind11_fail(
"Could not allocate int object!");
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)
1169 operator float()
const {
return (
float) PyFloat_AsDouble(
m_ptr); }
1170 operator double()
const {
return (
double) PyFloat_AsDouble(
m_ptr); }
1177 :
object(PyWeakref_NewRef(obj.
ptr(), callback.ptr()), stolen_t{}) {
1182 static PyObject *raw_weakref(PyObject *o) {
1183 return PyWeakref_NewRef(o,
nullptr);
1191 int_ start(start_), stop(stop_), step(step_);
1192 m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.
ptr());
1195 bool compute(
size_t length,
size_t *start,
size_t *stop,
size_t *step,
1196 size_t *slicelength)
const {
1200 (
ssize_t *) slicelength) == 0;
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{}) {
1224 capsule(const
void *
value,
void (*destruct)(PyObject *))
1225 :
object(PyCapsule_New(const_cast<
void*>(
value),
nullptr, destruct), stolen_t{}) {
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);
1240 if (PyCapsule_SetContext(
m_ptr, (
void *) destructor) != 0)
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));
1254 template <
typename T>
operator T *()
const {
1255 return get_pointer<T>();
1259 template<
typename T =
void>
1260 T* get_pointer()
const {
1262 T *result =
static_cast<T *
>(PyCapsule_GetPointer(
m_ptr,
name));
1263 if (!result)
pybind11_fail(
"Unable to extract capsule contents!");
1268 void set_pointer(
const void *
value) {
1269 if (PyCapsule_SetPointer(
m_ptr,
const_cast<void *
>(
value)) != 0)
1273 const char *
name()
const {
return PyCapsule_GetName(
m_ptr); }
1282 size_t size()
const {
return (
size_t) PyTuple_Size(
m_ptr); }
1293 template <
typename... Args>
1296 return detail::all_of<detail::is_keyword_or_ds<Args>...>
::value;
1305 template <
typename... Args,
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);
1333 ssize_t result = PySequence_Size(m_ptr);
1336 return (
size_t) result;
1349 if (!m_ptr)
pybind11_fail(
"Could not allocate list object!");
1351 size_t size()
const {
return (
size_t) PyList_Size(m_ptr); }
1357 template <
typename T>
void append(T &&val)
const {
1360 template <
typename T>
void insert(
size_t index, T &&val)
const {
1361 PyList_Insert(m_ptr,
static_cast<ssize_t>(index),
1373 if (!m_ptr)
pybind11_fail(
"Could not allocate set object!");
1375 size_t size()
const {
return (
size_t) PySet_Size(m_ptr); }
1377 template <
typename T>
bool add(T &&val)
const {
1380 void clear()
const { PySet_Clear(m_ptr); }
1391 if (fun && PyCFunction_Check(fun.
ptr()))
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) {
1434 pybind11_fail(
"Prohibited to create memoryview without Py_buffer");
1436 m_ptr = (info.view()->obj) ?
1437 PyMemoryView_FromObject(info.view()->obj) :
1438 PyMemoryView_FromBuffer(info.view());
1440 pybind11_fail(
"Unable to create memoryview from buffer descriptor");
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);
1472 const void *ptr,
ssize_t itemsize,
const char *format,
1473 detail::any_container<ssize_t> shape,
1474 detail::any_container<ssize_t> strides) {
1476 const_cast<void*
>(ptr), itemsize, format, shape, strides,
true);
1479 template<
typename T>
1481 T *ptr, detail::any_container<ssize_t> shape,
1482 detail::any_container<ssize_t> strides,
bool readonly =
false) {
1484 reinterpret_cast<void*
>(ptr),
sizeof(T),
1488 template<
typename T>
1490 const T *ptr, detail::any_container<ssize_t> shape,
1491 detail::any_container<ssize_t> strides) {
1493 const_cast<T*
>(ptr), shape, strides,
true);
1496 #if PY_MAJOR_VERSION >= 3
1511 PyObject* ptr = PyMemoryView_FromMemory(
1512 reinterpret_cast<char*
>(mem),
size,
1513 (readonly) ? PyBUF_READ : PyBUF_WRITE);
1520 return memoryview::from_memory(
const_cast<void*
>(mem),
size,
true);
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");
1534 for (
size_t i = 0;
i < ndim; ++
i)
1535 size *= (*shape)[
i];
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);
1553 #endif // DOXYGEN_SHOULD_SKIP_THIS
1564 return (
size_t) result;
1570 #if PY_VERSION_HEX >= 0x03040000
1571 ssize_t result = PyObject_LengthHint(h.
ptr(), 0);
1581 return (
size_t) result;
1585 PyObject *str_value = PyObject_Repr(h.
ptr());
1587 #if PY_MAJOR_VERSION < 3
1588 PyObject *unicode = PyUnicode_FromEncodedObject(str_value,
"utf-8",
nullptr);
1589 Py_XDECREF(str_value); str_value = unicode;
1592 return reinterpret_steal<str>(str_value);
1596 PyObject *result = PyObject_GetIter(obj.
ptr());
1598 return reinterpret_steal<iterator>(result);
1606 return {derived(), reinterpret_borrow<object>(key)};
1609 return {derived(), pybind11::str(key)};
1612 return {derived(), reinterpret_borrow<object>(key)};
1615 return {derived(), key};
1621 return attr(
"__contains__")(std::forward<T>(item)).
template cast<bool>();
1624 template <
typename D>
1627 template <
typename D>
1630 template <
typename D>
1633 template <
typename D>
1635 int rv = PyObject_RichCompareBool(derived().ptr(), other.derived().ptr(),
value);
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(); \
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(); \
1680 #undef PYBIND11_MATH_OPERATOR_UNARY
1681 #undef PYBIND11_MATH_OPERATOR_BINARY
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
friend bool operator>(const It &a, const It &b)
reference dereference() const
bool PyNone_Check(PyObject *o)
static object get(handle obj, size_t index)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
detail::item_accessor operator[](handle h) const
static type of(handle h)
Return a type object from a handle or an object.
str(const std::string &s)
friend It operator-(const It &a, difference_type n)
bool matches(handle exc) const
#define PYBIND11_OBJECT_CVT_DEFAULT(Name, Parent, CheckFun, ConvertFun)
PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)") object call(Args &&... args) const
detail::list_iterator end() const
object(const object &o)
Copy constructor; always increases the reference count.
Annotation for function names.
friend T reinterpret_borrow(handle)
friend bool operator<=(const It &a, const It &b)
void setattr(handle obj, handle name, handle value)
std::is_same< args_proxy, T > is_s_unpacking
T cast(const handle &handle)
bool isinstance_generic(handle obj, const std::type_info &tp)
#define PYBIND11_NAMESPACE_BEGIN(name)
void operator=(const accessor &a) &&
typename Policy::pointer pointer
typename deferred_type< T, Us... >::type deferred_t
#define PYBIND11_NAMESPACE_END(name)
typename Policy::iterator_category iterator_category
static object get(handle obj, size_t index)
Python's dictionary protocol permits this to be a forward iterator.
#define PYBIND11_OBJECT(Name, Parent, CheckFun)
bool is_cpp_function() const
It & operator+=(difference_type n)
object operator&=(object_api const &other) const
memoryview(const buffer_info &info)
#define PYBIND11_NAMESPACE
object operator<<(object_api const &other) const
object operator>>=(object_api const &other) const
accessor< accessor_policies::list_item > list_accessor
bool is_none() const
Equivalent to obj is None in Python.
arr data(const arr &a, Ix... index)
const object & trace() const
static object get(handle obj, const char *key)
ssize_t distance_to(const sequence_fast_readonly &b) const
object operator*(object_api const &other) const
std::string error_string()
friend T reinterpret_steal(handle)
static void set(handle obj, const char *key, handle val)
#define PYBIND11_DEPRECATED(reason)
bytes(const char *c, size_t n)
str format(Args &&...args) const
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun)
str(const char *c, size_t n)
std::pair< handle, handle > value_type
#define PYBIND11_BYTES_CHECK
object operator|=(object_api const &other) const
bool equal(const dict_readonly &b) const
const handle & inc_ref() const &
detail::item_accessor operator[](handle h) const
accessor< accessor_policies::sequence_item > sequence_accessor
bool operator>=(object_api const &other) const
#define PYBIND11_STR_CHECK_FUN
bool hasattr(handle obj, handle name)
object operator-=(object_api const &other) const
pybind11::str str() const
bool operator<(object_api const &other) const
Quick proxy class needed to implement operator-> for iterators which can't return pointers.
typename Policy::reference reference
bool contains(T &&item) const
Check if the given item is contained within this object, i.e. item in obj.
object operator&(object_api const &other) const
detail::dict_iterator end() const
object getattr(handle obj, handle name)
object operator()(Args &&...args) const
void insert(size_t index, T &&val) const
bool not_equal(object_api const &other) const
std::random_access_iterator_tag iterator_category
static memoryview from_buffer(const T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides)
bool isinstance< handle >(handle)=delete
Lightweight iterator policy using just a simple pointer: see PySequence_Fast_ITEMS
object operator/(object_api const &other) const
PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()") object(handle h
Unsigned as_unsigned(PyObject *o)
friend bool operator==(const It &a, const It &b)
bool equal(object_api const &other) const
Equivalent to obj == other in Python.
bool equal(const sequence_fast_readonly &b) const
bool operator>(object_api const &other) const
It & operator-=(difference_type n)
~object()
Destructor; automatically calls handle::dec_ref()
const object & value() const
static object get(handle obj, handle key)
Annotation for documentation.
void discard_as_unraisable(const char *err_context)
STL iterator template used for tuple, list, sequence and dict.
detail::tuple_iterator begin() const
friend bool operator>=(const It &a, const It &b)
const value_type reference
bool isinstance(handle obj)
void operator=(T &&value) &
detail::sequence_iterator begin() const
generic_iterator(handle seq, ssize_t index)
accessor< accessor_policies::tuple_item > tuple_accessor
static object get(handle obj, size_t index)
object operator^=(object_api const &other) const
static handle handle_of()
T reinterpret_borrow(handle h)
void delattr(handle obj, handle name)
object operator*=(object_api const &other) const
std::input_iterator_tag iterator_category
bool PyStaticMethod_Check(PyObject *o)
constexpr bool args_are_all_keyword_or_ds()
kwargs_proxy operator*() const
error_already_set(error_already_set &&)=default
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)
static memoryview from_buffer(T *ptr, detail::any_container< ssize_t > shape, detail::any_container< ssize_t > strides, bool readonly=false)
static void set(handle obj, size_t index, handle val)
handle get_function(handle value)
friend bool operator!=(const iterator &a, const iterator &b)
accessor(accessor &&)=default
object operator>>(object_api const &other) const
bool compute(size_t length, size_t *start, size_t *stop, size_t *step, size_t *slicelength) const
bool contains(T &&val) const
sequence_slow_readwrite(handle obj, ssize_t index)
friend bool operator<(const It &a, const It &b)
#define PYBIND11_BYTES_FROM_STRING_AND_SIZE
auto object_or_cast(T &&o) -> decltype(std::forward< T >(o))
std::is_same< kwargs_proxy, T > is_ds_unpacking
bool is(object_api const &other) const
Equivalent to obj is other in Python.
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
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)
detail::tuple_accessor operator[](size_t index) const
generic_iterator()=default
object(object &&other) noexcept
Move constructor; steals the object from other and preserves its reference count.
pointer operator->() const
void operator=(const accessor &a) &
#define PYBIND11_MATH_OPERATOR_UNARY(op, fn)
std::is_base_of< arg, T > is_keyword
Python argument categories (using PEP 448 terms)
PyExc_RuntimeError PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
#define PYBIND11_LONG_AS_LONGLONG(o)
object & operator=(object &&other) noexcept
Helper class which collects positional, keyword, * and ** arguments for a Python function call.
int ref_count() const
Return the object's current reference count.
bool isinstance< object >(handle obj)
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun)
generic_iterator< iterator_policies::sequence_fast_readonly > tuple_iterator
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
detail::tuple_iterator end() const
pointer operator->() const
void append(T &&val) const
void operator=(T &&value) &&
std::forward_iterator_tag iterator_category
bool contains(T &&key) const
PyObject * ptr() const
Return the underlying PyObject * pointer.
friend It operator+(difference_type n, const It &b)
dict_readonly(handle obj, ssize_t pos)
std::random_access_iterator_tag iterator_category
friend bool operator==(const iterator &a, const iterator &b)
const object & type() const
detail::sequence_accessor operator[](size_t index) const
handle()=default
The default constructor creates a handle with a nullptr-valued pointer.
static object get(handle obj, handle key)
iterator iter(handle obj)
object(handle h, borrowed_t)
accessor< accessor_policies::generic_item > item_accessor
detail::dict_iterator begin() const
Information record describing a Python buffer object.
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
sequence_fast_readonly(handle obj, ssize_t n)
typename Policy::value_type value_type
Full read and write access using the sequence protocol: see detail::sequence_accessor
reference operator*() const
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
detail::list_accessor operator[](size_t index) const
str_attr_accessor doc() const
Get or set the object's docstring, i.e. obj.__doc__.
const handle & dec_ref() const &
object(handle h, stolen_t)
bool operator<=(object_api const &other) const
accessor(handle obj, key_type key)
generic_iterator< iterator_policies::sequence_fast_readonly > list_iterator
static void set(handle obj, size_t index, handle val)
bool PyIterable_Check(PyObject *obj)
bool equal(const sequence_slow_readwrite &b) const
object operator/=(object_api const &other) const
#define PYBIND11_MATH_OPERATOR_BINARY(op, fn)
item_accessor operator[](handle key) const
static void set(handle obj, handle key, handle val)
obj_attr_accessor attr(handle key) const
void discard_as_unraisable(object err_context)
object & operator=(const object &other)
static void set(handle obj, size_t index, handle val)
size_t len_hint(handle h)
error_already_set(const error_already_set &)=default
size_t len(handle h)
Get the length of a Python object.
#define PYBIND11_LONG_CHECK(o)
object operator|(object_api const &other) const
Tag and check to identify a class which implements the Python object API.
object operator+(object_api const &other) const
generic_iterator< iterator_policies::sequence_slow_readwrite > sequence_iterator
handle(PyObject *ptr)
Creates a handle from the given raw Python object pointer.
friend bool operator!=(const It &a, const It &b)
object operator^(object_api const &other) const
object operator<<=(object_api const &other) const
object operator-(object_api const &other) const
reference dereference() const
T reinterpret_steal(handle h)
args_proxy operator*() const
generic_iterator< iterator_policies::dict_readonly > dict_iterator
bool PyEllipsis_Check(PyObject *o)
PYBIND11_DEPRECATED("Use reinterpret_borrow<capsule>() or reinterpret_steal<capsule>()") capsule(PyObject *ptr
bool compute(ssize_t length, ssize_t *start, ssize_t *stop, ssize_t *step, ssize_t *slicelength) const
reference operator[](difference_type n) const
friend difference_type operator-(const It &a, const It &b)
object operator+=(object_api const &other) const
ssize_t distance_to(const sequence_slow_readwrite &b) const
friend It operator+(const It &a, difference_type n)
reference dereference() const
reference operator*() const
#define PYBIND11_BYTES_AS_STRING_AND_SIZE
static void set(handle obj, handle key, handle val)
accessor(const accessor &)=default
static iterator sentinel()
bytes(const std::string &s)
iterator end() const
Return a sentinel which ends iteration.
#define PYBIND11_FROM_STRING
detail::sequence_iterator end() const
detail::item_accessor operator[](handle h) const
#define PYBIND11_SLICE_OBJECT
detail::list_iterator begin() const
#define PYBIND11_BYTES_FROM_STRING