1 [section boost/python/object.hpp]
3 Exposes the generic Python object wrapper class object, and related classes. In order to avoid some potenential problems with argument-dependent lookup and the generalized operators defined on object, all these facilities are defined in namespace boost::python::api, and object is imported into namespace boost::python with a using-declaration.
5 [section Class `slice_nil`]
8 static const _ = slice_nil();
10 A type that can be used to get the effect of leaving out an index in a Python slice expression:
21 [section Class `const_attribute_policies`]
22 The policies which are used for proxies representing an attribute access to a const object.
24 namespace boost { namespace python { namespace api
26 struct const_attribute_policies
28 typedef char const* key_type;
29 static object get(object const& target, char const* key);
34 [section Class `const_attribute_policies` static functions]
36 static object get(object const& target, char const* key);
39 [[Requires][key is an [link ntbs].]]
40 [[Effects][accesses the attribute of target named by key.]]
41 [[Returns][An object managing the result of the attribute access.]]
42 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
45 [section Class `attribute_policies`]
46 The policies which are used for proxies representing an attribute access to a mutable object.
48 namespace boost { namespace python { namespace api
50 struct attribute_policies : const_attribute_policies
52 static object const& set(object const& target, char const* key, object const& value);
53 static void del(object const&target, char const* key);
58 [section Class `attribute_policies` static functions]
60 static object const& set(object const& target, char const* key, object const& value);
63 [[Requires][key is an [link ntbs].]]
64 [[Effects][sets the attribute of target named by key to value.]]
65 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
68 static void del(object const&target, char const* key);
71 [[Requires][key is an [link ntbs].]]
72 [[Effects][deletes the attribute of target named by key.]]
73 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
76 [section Class `const_objattribute_policies`]
77 The policies which are used for proxies representing an attribute access to a const object when the attribute name is given as a const object.
79 namespace boost { namespace python { namespace api
81 struct const_objattribute_policies
83 typedef object const& key_type;
84 static object get(object const& target, object const& key);
89 [section Class `const_objattribute_policies` static functions]
91 static object get(object const& target, object const& key);
94 [[Requires][key is an object holding a string.]]
95 [[Effects][accesses the attribute of target named by key.]]
96 [[Returns][An object managing the result of the attribute access.]]
97 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
100 [section Class `objattribute_policies`]
101 The policies which are used for proxies representing an attribute access to a mutable object when the attribute name is given as a const object.
103 namespace boost { namespace python { namespace api
105 struct objattribute_policies : const_objattribute_policies
107 static object const& set(object const& target, object const& key, object const& value);
108 static void del(object const&target, object const& key);
113 [section Class `objattribute_policies` static functions]
115 static object const& set(object const& target, object const& key, object const& value);
118 [[Requires][key is an object holding a string.]]
119 [[Effects][sets the attribute of target named by key to value.]]
120 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
123 static void del(object const&target, object const& key);
126 [[Requires][key is an object holding a string.]]
127 [[Effects][deletes the attribute of target named by key.]]
128 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
131 [section Class `const_item_policies`]
132 The policies which are used for proxies representing an item access (via the Python bracket operators []) to a const object.
134 namespace boost { namespace python { namespace api
136 struct const_item_policies
138 typedef object key_type;
139 static object get(object const& target, object const& key);
144 [section Class `const_item_policies` static functions]
146 static object get(object const& target, object const& key);
149 [[Effects][accesses the item of target specified by key.]]
150 [[Returns][An object managing the result of the item access.]]
151 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
154 [section Class `item_policies`]
155 The policies which are used for proxies representing an item access (via the Python bracket operators []) to a mutable object.
157 namespace boost { namespace python { namespace api
159 struct item_policies : const_item_policies
161 static object const& set(object const& target, object const& key, object const& value);
162 static void del(object const& target, object const& key);
167 [section Class `item_policies` static functions]
169 static object const& set(object const& target, object const& key, object const& value);
172 [[Effects][sets the item of target specified by key to value.]]
173 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
176 static void del(object const& target, object const& key);
179 [[Effects][deletes the item of target specified by key.]]
180 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
183 [section Class `const_slice_policies`]
184 The policies which are used for proxies representing an slice access (via the Python slice notation [x:y]) to a const object.
186 namespace boost { namespace python { namespace api
188 struct const_slice_policies
190 typedef std::pair<handle<>, handle<> > key_type;
191 static object get(object const& target, key_type const& key);
196 [section Class `const_slice_policies` static functions]
198 static object get(object const& target, key_type const& key);
201 [[Effects][accesses the slice of target specified by key.]]
202 [[Returns][An object managing the result of the slice access.]]
203 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
206 [section Class `slice_policies`]
207 The policies which are used for proxies representing an slice access to a mutable object.
209 namespace boost { namespace python { namespace api
211 struct slice_policies : const_slice_policies
213 static object const& set(object const& target, key_type const& key, object const& value);
214 static void del(object const& target, key_type const& key);
219 [section Class `slice_policies` static functions]
221 static object const& set(object const& target, key_type const& key, object const& value);
224 [[Effects][sets the slice of target specified by key to value.]]
225 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
228 static void del(object const& target, key_type const& key);
231 [[Effects][deletes the slice of target specified by key.]]
232 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] if a Python exception is raised.]]
235 [section Class template `object_operators`]
236 This is the base class of object and its proxy template used to supply common interface: member functions, and operators which must be defined within the class body. Its template parameter U is expected to be a class derived from object_operators<U>. In practice users should never use this class directly, but it is documented here because it supplies important interface to object and its proxies.
238 namespace boost { namespace python { namespace api
241 class object_operators
246 object operator()() const;
249 object operator()(A0 const&) const;
250 template <class A0, class A1>
251 object operator()(A0 const&, A1 const&) const;
253 template <class A0, class A1,...class An>
254 object operator()(A0 const&, A1 const&,...An const&) const;
256 detail::args_proxy operator* () const;
257 object operator()(detail::args_proxy const &args) const;
258 object operator()(detail::args_proxy const &args,
259 detail::kwds_proxy const &kwds) const;
261 // truth value testing
263 typedef unspecified bool_type;
264 operator bool_type() const;
268 proxy<const_object_attribute> attr(char const*) const;
269 proxy<object_attribute> attr(char const*);
270 proxy<const_object_objattribute> attr(object const&) const;
271 proxy<object_objattribute> attr(object const&);
276 proxy<const_object_item> operator[](T const& key) const;
279 proxy<object_item> operator[](T const& key);
283 template <class T, class V>
284 proxy<const_object_slice> slice(T const& start, V const& end) const
286 template <class T, class V>
287 proxy<object_slice> slice(T const& start, V const& end);
292 [section Class template `object_operators` observer functions]
294 object operator()() const;
296 object operator()(A0 const&) const;
297 template <class A0, class A1>
298 object operator()(A0 const&, A1 const&) const;
300 template <class A0, class A1,...class An>
301 object operator()(A0 const& a1, A1 const& a2,...An const& aN) const;
304 [[Effects][`call<object>(object(*static_cast<U*>(this)).ptr(), a1, a2,...aN)`]]
306 ``object operator()(detail::args_proxy const &args) const; ``
308 [[Effects][`call object with arguments given by the tuple args`]]
310 ``object operator()(detail::args_proxy const &args,
311 detail::kwds_proxy const &kwds) const;
315 [[Effects][`call object with arguments given by the tuple args, and named arguments given by the dictionary kwds`]]
317 ``operator bool_type() const;``
319 [[Effects][Tests truth value of `*this`.]]
320 [[Returns][`call<object>(object(*static_cast<U*>(this)).ptr(), a1, a2,...aN)`]]
323 proxy<const_object_attribute> attr(char const* name) const;
324 proxy<object_attribute> attr(char const* name);
327 [[Requires][name is an [link ntbs].]]
328 [[Effects][accesses the named attribute of *this.]]
329 [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and name as its key.]]
332 proxy<const_object_objattribute> attr(const object& name) const;
333 proxy<object_objattribute> attr(const object& name);
336 [[Requires][name is a object holding a string.]]
337 [[Effects][accesses the named attribute of `*this`.]]
338 [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and name as its key.]]
342 proxy<const_object_item> operator[](T const& key) const;
344 proxy<object_item> operator[](T const& key);
347 [[Effects][accesses the item of `*this` indicated by key.]]
348 [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and object(key) as its key.]]
351 template <class T, class V>
352 proxy<const_object_slice> slice(T const& start; start, V const& finish) const
353 template <class T, class V>
354 proxy<object_slice> slice(T const& start; start, V const& finish);
357 [[Effects][accesses the slice of `*this` indicated by `std::make_pair(object(start), object(finish))`.]]
358 [[Returns][a proxy object which binds `object(*static_cast<U*>(this))` as its target, and `std::make_pair(object(start), object(finish))` as its key.]]
361 [section Class `object`]
362 The intention is that object acts as much like a Python variable as possible. Thus expressions you'd expect to work in Python should generally work in the same way from C++. Most of object's interface is provided by its base class `object_operators<object>`, and the free functions defined in this header.
364 namespace boost { namespace python { namespace api
366 class object : public object_operators<object>
370 object(object const&);
372 explicit object(T const& x);
376 object& operator=(object const&);
377 PyObject* ptr() const;
378 bool is_none() const;
383 [section Class `object` constructors and destructor]
386 [[Effects][Constructs an object managing a reference to the Python None object.]]
390 explicit object(T const& x);
393 [[Effects][converts x to python and manages a reference to it.]]
394 [[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set error_already_set] and sets a Python TypeError exception if no such conversion is possible.]]
400 [[Effects][decrements the reference count of the internally-held object.]]
403 [section Class `object` modifiers]
404 ``PyObject* ptr() const;``
407 [[Returns] [a pointer to the internally-held Python object.]]
409 ``bool is_none() const;``
412 [[Returns] [result of `(ptr() == Py_None)`]]
415 [section Class template `proxy`]
416 This template is instantiated with various Policies described in this document in order to implement attribute, item, and slice access for object. It stores an object of type Policies::key_type.
418 namespace boost { namespace python { namespace api
420 template <class Policies>
421 class proxy : public object_operators<proxy<Policies> >
424 operator object() const;
426 proxy const& operator=(proxy const&) const;
428 inline proxy const& operator=(T const& rhs) const;
433 proxy operator+=(R const& rhs);
435 proxy operator-=(R const& rhs);
437 proxy operator*=(R const& rhs);
439 proxy operator/=(R const& rhs);
441 proxy operator%=(R const& rhs);
443 proxy operator<<=(R const& rhs);
445 proxy operator>>=(R const& rhs);
447 proxy operator&=(R const& rhs);
449 proxy operator|=(R const& rhs);
454 [section Class template `proxy` observer functions]
455 ``operator object() const;``
457 [[Effects][applies `Policies::get(target, key)` with the proxy's target and key objects.]]
460 [section Class template `proxy` modifier functions]
462 proxy const& operator=(proxy const& rhs) const;
464 inline proxy const& operator=(T const& rhs) const;
467 [[Effects][ `Policies::set(target, key , object(rhs))` with the proxy's target and key objects.]]
471 proxy operator+=(R const& rhs);
473 proxy operator-=(R const& rhs);
475 proxy operator*=(R const& rhs);
477 proxy operator/=(R const& rhs);
479 proxy operator%=(R const& rhs);
481 proxy operator<<=(R const& rhs);
483 proxy operator>>=(R const& rhs);
485 proxy operator&=(R const& rhs);
487 proxy operator|=(R const& rhs);
490 [[Effects][for a given `operator@=`, `object(*this) @= rhs;`]]
493 ``void del() const;``
495 [[Effects][Policies::del(target, key ) with the proxy's target and key objects.]]
501 void del(proxy<T> const& x);
504 [[Effects][`x.del()`]]
507 template<class L,class R> object operator>(L const&l,R const&r);
508 template<class L,class R> object operator>=(L const&l,R const&r);
509 template<class L,class R> object operator<(L const&l,R const&r);
510 template<class L,class R> object operator<=(L const&l,R const&r);
511 template<class L,class R> object operator==(L const&l,R const&r);
512 template<class L,class R> object operator!=(L const&l,R const&r);
515 [[Effects][returns the result of applying the operator to `object(l)` and `object(r)`, respectively, in Python.]]
518 template<class L,class R> object operator+(L const&l,R const&r);
519 template<class L,class R> object operator-(L const&l,R const&r);
520 template<class L,class R> object operator*(L const&l,R const&r);
521 template<class L,class R> object operator/(L const&l,R const&r);
522 template<class L,class R> object operator%(L const&l,R const&r);
523 template<class L,class R> object operator<<(L const&l,R const&r);
524 template<class L,class R> object operator>>(L const&l,R const&r);
525 template<class L,class R> object operator&(L const&l,R const&r);
526 template<class L,class R> object operator^(L const&l,R const&r);
527 template<class L,class R> object operator|(L const&l,R const&r);
530 [[Effects][returns the result of applying the operator to `object(l)` and `object(r)`, respectively, in Python.]]
533 template<class R> object& operator+=(object&l,R const&r);
534 template<class R> object& operator-=(object&l,R const&r);
535 template<class R> object& operator*=(object&l,R const&r);
536 template<class R> object& operator/=(object&l,R const&r);
537 template<class R> object& operator%=(object&l,R const&r);
538 template<class R> object& operator<<=(object&l,R const&r)
539 template<class R> object& operator>>=(object&l,R const&r);
540 template<class R> object& operator&=(object&l,R const&r);
541 template<class R> object& operator^=(object&l,R const&r);
542 template<class R> object& operator|=(object&l,R const&r);
545 [[Effects][assigns to `l` the result of applying the corresponding Python inplace operator to `l` and `object(r)`, respectively.]]
548 ``long len(object const& obj);``
550 [[Effects][`PyObject_Length(obj.ptr())`]]
551 [[Returns][`len()` of object.]]
565 object sum_items(object seq)
567 object result = object(0);
568 for (int i = 0; i < len(seq); ++i)