Lines Matching refs:object

20 class handle; class object;
45 /// Tag and check to identify a class which implements the Python object API
50 A mixin class which adds common functions to `handle`, `object` and various accessors.
59 Return an iterator equivalent to calling ``iter()`` in Python. The object
67 Return an internal functor to invoke the object's sequence protocol. Casting
68 the returned ``detail::item_accessor`` instance to a `handle` or `object`
70 or `object` subclass causes a call to ``__setitem__``.
77 Return an internal functor to access the object's attributes. Casting the
78 returned ``detail::obj_attr_accessor`` instance to a `handle` or `object`
80 or `object` subclass causes a call to ``setattr``.
94 /// Check if the given item is contained within this object, i.e. ``item in obj``.
98 Assuming the Python object is a function or implements the ``__call__``
100 arbitrary set of parameters. The result is returned as a `object` and
101 may need to be converted back into a Python object using `handle::cast()`.
108 object operator()(Args &&...args) const;
111 object call(Args&&... args) const;
125 object operator-() const;
126 object operator~() const;
127 object operator+(object_api const &other) 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;
149 /// Get or set the object's docstring, i.e. ``obj.__doc__``.
152 /// Return the object's current reference count
154 /// Return a handle to the Python type object underlying the instance
164 Holds a reference to a Python object (no reference counting)
166 The `handle` class is a thin wrapper around an arbitrary Python object (i.e. a
171 The `object` class inherits from `handle` and adds automatic reference
178 /// Creates a ``handle`` from the given raw Python object pointer
186 Manually increase the reference count of the Python object. Usually, it is
187 preferable to use the `object` class which derives from `handle` and calls
193 Manually decrease the reference count of the Python object. Usually, it is
194 preferable to use the `object` class which derives from `handle` and calls
200 Attempt to cast the Python object into the given C++ type. A `cast_error`
204 /// Return ``true`` when the `handle` wraps a valid Python object
221 Holds a reference to a Python object (with reference counting)
223 Like `handle`, the `object` class is a thin wrapper around an arbitrary Python
224 object (i.e. a ``PyObject *`` in Python's C API). In contrast to `handle`, it
225 optionally increases the object's reference count upon construction, and it
226 *always* decreases the reference count when the `object` instance goes out of
227 scope and is destructed. When using `object` instances consistently, it is much
230 class object : public handle {
232 object() = default;
233 PYBIND11_DEPRECATED("Use reinterpret_borrow<object>() or reinterpret_steal<object>()")
234 object(handle h, bool is_borrowed) : handle(h) { if (is_borrowed) inc_ref(); }
236 object(const object &o) : handle(o) { inc_ref(); }
237 /// Move constructor; steals the object from ``other`` and preserves its reference count
238 object(object &&other) noexcept { m_ptr = other.m_ptr; other.m_ptr = nullptr; }
240 ~object() { dec_ref(); }
244 object's reference count. The function returns a raw handle to the original
245 Python object.
253 object& operator=(const object &other) {
260 object& operator=(object &&other) noexcept {
270 // Calling cast() on an object lvalue just copies (via handle::cast)
272 // Calling on an object rvalue does a move, if needed and/or possible
285 object(handle h, borrowed_t) : handle(h) { inc_ref(); }
286 object(handle h, stolen_t) : handle(h) { }
291 The target type ``T`` must be `object` or one of its derived classes. The function
298 py::object o = reinterpret_borrow<py::object>(p);
302 template <typename T> T reinterpret_borrow(handle h) { return {h, object::borrowed_t{}}; }
312 template <typename T> T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; }
336 /// already set it is cleared first. After this call, the current object no longer stores the
349 const object& type() const { return m_type; }
350 const object& value() const { return m_value; }
351 const object& trace() const { return m_trace; }
354 object m_type, m_value, m_trace;
365 `object` or a class which was exposed to Python as ``py::class_<T>``.
367 template <typename T, detail::enable_if_t<std::is_base_of<object, T>::value, int> = 0>
370 template <typename T, detail::enable_if_t<!std::is_base_of<object, T>::value, int> = 0>
374 template <> inline bool isinstance<object>(handle obj) { return obj.ptr() != nullptr; }
403 inline object getattr(handle obj, handle name) {
406 return reinterpret_steal<object>(result);
409 inline object getattr(handle obj, const char *name) {
412 return reinterpret_steal<object>(result);
415 inline object getattr(handle obj, handle name, handle default_) {
417 return reinterpret_steal<object>(result);
420 return reinterpret_borrow<object>(default_);
424 inline object getattr(handle obj, const char *name, handle default_) {
426 return reinterpret_steal<object>(result);
429 return reinterpret_borrow<object>(default_);
465 // through pybind11::cast(obj) to convert it to an `object`.
470 object object_or_cast(T &&o);
492 get_cache() = reinterpret_borrow<object>(object_or_cast(std::forward<T>(value)));
507 operator object() const { return get_cache(); }
512 object &get_cache() const {
520 mutable object cache;
525 using key_type = object;
526 static object get(handle obj, handle key) { return getattr(obj, key); }
532 static object get(handle obj, const char *key) { return getattr(obj, key); }
537 using key_type = object;
539 static object get(handle obj, handle key) {
542 return reinterpret_steal<object>(result);
553 static object get(handle obj, size_t index) {
556 return reinterpret_steal<object>(result);
570 static object get(handle obj, size_t index) {
573 return reinterpret_borrow<object>(result);
587 static object get(handle obj, size_t index) {
590 return reinterpret_borrow<object>(result);
676 using value_type = object;
791 /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
792 Name(const object &o) \
795 Name(object &&o) \
799 Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
803 /* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
804 Name(const object &o) : Parent(o) { } \
805 Name(object &&o) : Parent(std::move(o)) { }
822 class iterator : public object {
830 PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)
873 value = reinterpret_steal<object>(PyIter_Next(m_ptr));
878 object value = {};
881 class iterable : public object {
883 PYBIND11_OBJECT_DEFAULT(iterable, object, detail::PyIterable_Check)
888 class str : public object {
890 PYBIND11_OBJECT_CVT(str, object, detail::PyUnicode_Check_Permissive, raw_str)
893 : object(PyUnicode_FromStringAndSize(c, (ssize_t) n), stolen_t{}) {
894 if (!m_ptr) pybind11_fail("Could not allocate string object!");
899 : object(PyUnicode_FromString(c), stolen_t{}) {
900 if (!m_ptr) pybind11_fail("Could not allocate string object!");
908 Return a string representation of the object. This is analogous to
911 explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) { }
914 object temp = *this;
916 temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(m_ptr));
955 class bytes : public object {
957 PYBIND11_OBJECT(bytes, object, PYBIND11_BYTES_CHECK)
961 : object(PYBIND11_BYTES_FROM_STRING(c), stolen_t{}) {
962 if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
966 : object(PYBIND11_BYTES_FROM_STRING_AND_SIZE(c, (ssize_t) n), stolen_t{}) {
967 if (!m_ptr) pybind11_fail("Could not allocate bytes object!");
985 object temp = s;
987 temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
995 auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length));
997 pybind11_fail("Could not allocate bytes object!");
1006 auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, (ssize_t) length));
1008 pybind11_fail("Could not allocate string object!");
1012 class none : public object {
1014 PYBIND11_OBJECT(none, object, detail::PyNone_Check)
1015 none() : object(Py_None, borrowed_t{}) { }
1019 class ellipsis : public object {
1021 PYBIND11_OBJECT(ellipsis, object, detail::PyEllipsis_Check)
1022 ellipsis() : object(Py_Ellipsis, borrowed_t{}) { }
1026 class bool_ : public object {
1028 PYBIND11_OBJECT_CVT(bool_, object, PyBool_Check, raw_bool)
1029 bool_() : object(Py_False, borrowed_t{}) { }
1031 bool_(bool value) : object(value ? Py_True : Py_False, borrowed_t{}) { }
1035 /// Return the truth value of an object -- always returns a new reference
1065 class int_ : public object {
1067 PYBIND11_OBJECT_CVT(int_, object, PYBIND11_LONG_CHECK, PyNumber_Long)
1068 int_() : object(PyLong_FromLong(0), stolen_t{}) { }
1084 if (!m_ptr) pybind11_fail("Could not allocate int object!");
1098 class float_ : public object {
1100 PYBIND11_OBJECT_CVT(float_, object, PyFloat_Check, PyNumber_Float)
1102 float_(float value) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1103 if (!m_ptr) pybind11_fail("Could not allocate float object!");
1105 float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1106 if (!m_ptr) pybind11_fail("Could not allocate float object!");
1112 class weakref : public object {
1114 PYBIND11_OBJECT_DEFAULT(weakref, object, PyWeakref_Check)
1116 : object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), stolen_t{}) {
1121 class slice : public object {
1123 PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
1127 if (!m_ptr) pybind11_fail("Could not allocate slice object!");
1145 class capsule : public object {
1147 PYBIND11_OBJECT_DEFAULT(capsule, object, PyCapsule_CheckExact)
1149 capsule(PyObject *ptr, bool is_borrowed) : object(is_borrowed ? object(ptr, borrowed_t{}) : object(ptr, stolen_t{})) { }
1152 : object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
1154 pybind11_fail("Could not allocate capsule object!");
1159 : object(PyCapsule_New(const_cast<void*>(value), nullptr, destruct), stolen_t{}) {
1161 pybind11_fail("Could not allocate capsule object!");
1172 pybind11_fail("Could not allocate capsule object!");
1185 pybind11_fail("Could not allocate capsule object!");
1198 class tuple : public object {
1200 PYBIND11_OBJECT_CVT(tuple, object, PyTuple_Check, PySequence_Tuple)
1201 explicit tuple(size_t size = 0) : object(PyTuple_New((ssize_t) size), stolen_t{}) {
1202 if (!m_ptr) pybind11_fail("Could not allocate tuple object!");
1207 detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1212 class dict : public object {
1214 PYBIND11_OBJECT_CVT(dict, object, PyDict_Check, raw_dict)
1215 dict() : object(PyDict_New(), stolen_t{}) {
1216 if (!m_ptr) pybind11_fail("Could not allocate dict object!");
1242 class sequence : public object {
1244 PYBIND11_OBJECT_DEFAULT(sequence, object, PySequence_Check)
1248 detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1253 class list : public object {
1255 PYBIND11_OBJECT_CVT(list, object, PyList_Check, PySequence_List)
1256 explicit list(size_t size = 0) : object(PyList_New((ssize_t) size), stolen_t{}) {
1257 if (!m_ptr) pybind11_fail("Could not allocate list object!");
1262 detail::item_accessor operator[](handle h) const { return object::operator[](h); }
1277 class set : public object {
1279 PYBIND11_OBJECT_CVT(set, object, PySet_Check, PySet_New)
1280 set() : object(PySet_New(nullptr), stolen_t{}) {
1281 if (!m_ptr) pybind11_fail("Could not allocate set object!");
1294 class function : public object {
1296 PYBIND11_OBJECT_DEFAULT(function, object, PyCallable_Check)
1306 class staticmethod : public object {
1308 PYBIND11_OBJECT_CVT(staticmethod, object, detail::PyStaticMethod_Check, PyStaticMethod_New)
1311 class buffer : public object {
1313 PYBIND11_OBJECT_DEFAULT(buffer, object, PyObject_CheckBuffer)
1327 class memoryview : public object {
1356 PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject)
1365 pybind11_fail("Unable to compute length of object");
1406 return {derived(), reinterpret_borrow<object>(key)};
1412 return {derived(), reinterpret_borrow<object>(key)};
1442 template <typename D> object object_api<D>::op() const { \
1443 object result = reinterpret_steal<object>(fn(derived().ptr())); \
1451 object object_api<D>::op(object_api const &other) const { \
1452 object result = reinterpret_steal<object>( \