blob: 4df4344a8ba937d393607aadf8b129ddd5a274e1 [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _reference:
2
Wenzel Jakob93296692015-10-13 23:21:54 +02003.. warning::
4
5 Please be advised that the reference documentation discussing pybind11
6 internals is currently incomplete. Please refer to the previous sections
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02007 and the pybind11 header files for the nitty gritty details.
Wenzel Jakob93296692015-10-13 23:21:54 +02008
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02009Reference
10#########
11
12Macros
13======
14
Wenzel Jakobb1b71402015-10-18 16:48:30 +020015.. function:: PYBIND11_PLUGIN(const char *name)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020016
17 This macro creates the entry point that will be invoked when the Python
18 interpreter imports a plugin library. Please create a
19 :class:`module` in the function body and return the pointer to its
20 underlying Python object at the end.
21
22 .. code-block:: cpp
23
Wenzel Jakobb1b71402015-10-18 16:48:30 +020024 PYBIND11_PLUGIN(example) {
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020025 pybind11::module m("example", "pybind11 example plugin");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020026 /// Set up bindings here
27 return m.ptr();
28 }
29
30.. _core_types:
31
32Convenience classes for arbitrary Python types
33==============================================
34
35Without reference counting
36--------------------------
37
38.. class:: handle
39
40 The :class:`handle` class is a thin wrapper around an arbitrary Python
41 object (i.e. a ``PyObject *`` in Python's C API). It does not perform any
42 automatic reference counting and merely provides a basic C++ interface to
43 various Python API functions.
44
45.. seealso::
Wenzel Jakob93296692015-10-13 23:21:54 +020046
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020047 The :class:`object` class inherits from :class:`handle` and adds automatic
48 reference counting features.
49
50.. function:: handle::handle()
51
52 The default constructor creates a handle with a ``nullptr``-valued pointer.
53
54.. function:: handle::handle(const handle&)
55
56 Copy constructor
57
58.. function:: handle::handle(PyObject *)
59
60 Creates a :class:`handle` from the given raw Python object pointer.
61
Tomasz Miąskoca771302015-12-30 21:03:57 +010062.. function:: PyObject * handle::ptr() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020063
64 Return the ``PyObject *`` underlying a :class:`handle`.
65
Wenzel Jakob48548ea2016-01-17 22:36:44 +010066.. function:: const handle& handle::inc_ref() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020067
68 Manually increase the reference count of the Python object. Usually, it is
69 preferable to use the :class:`object` class which derives from
Wenzel Jakob48548ea2016-01-17 22:36:44 +010070 :class:`handle` and calls this function automatically. Returns a reference
71 to itself.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020072
Wenzel Jakob48548ea2016-01-17 22:36:44 +010073.. function:: const handle& handle::dec_ref() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020074
75 Manually decrease the reference count of the Python object. Usually, it is
76 preferable to use the :class:`object` class which derives from
Wenzel Jakob48548ea2016-01-17 22:36:44 +010077 :class:`handle` and calls this function automatically. Returns a reference
78 to itself.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020079
Tomasz Miąsko875df552015-12-28 08:11:16 +010080.. function:: void handle::ref_count() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020081
82 Return the object's current reference count
83
Tomasz Miąsko875df552015-12-28 08:11:16 +010084.. function:: handle handle::get_type() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020085
86 Return a handle to the Python type object underlying the instance
87
Tomasz Miąsko875df552015-12-28 08:11:16 +010088.. function detail::accessor handle::operator[](handle key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020089
90 Return an internal functor to invoke the object's sequence protocol.
91 Casting the returned ``detail::accessor`` instance to a :class:`handle` or
92 :class:`object` subclass causes a corresponding call to ``__getitem__``.
93 Assigning a :class:`handle` or :class:`object` subclass causes a call to
94 ``__setitem__``.
95
Tomasz Miąsko875df552015-12-28 08:11:16 +010096.. function detail::accessor handle::operator[](const char *key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020097
98 See the above function (the only difference is that they key is provided as
99 a string literal).
100
Tomasz Miąsko875df552015-12-28 08:11:16 +0100101.. function detail::accessor handle::attr(handle key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200102
103 Return an internal functor to access the object's attributes.
104 Casting the returned ``detail::accessor`` instance to a :class:`handle` or
105 :class:`object` subclass causes a corresponding call to ``__getattr``.
106 Assigning a :class:`handle` or :class:`object` subclass causes a call to
107 ``__setattr``.
108
Tomasz Miąsko875df552015-12-28 08:11:16 +0100109.. function detail::accessor handle::attr(const char *key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200110
111 See the above function (the only difference is that they key is provided as
112 a string literal).
113
114.. function operator handle::bool() const
115
116 Return ``true`` when the :class:`handle` wraps a valid Python object.
117
118.. function str handle::str() const
119
120 Return a string representation of the object. This is analogous to
121 the ``str()`` function in Python.
122
Tomasz Miąskocc39b2f2015-12-26 19:01:28 +0100123.. function:: template <typename T> T handle::cast() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200124
125 Attempt to cast the Python object into the given C++ type. A
126 :class:`cast_error` will be throw upon failure.
127
Tomasz Miąsko875df552015-12-28 08:11:16 +0100128.. function:: template <typename ... Args> object handle::call(Args&&... args) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200129
130 Assuming the Python object is a function or implements the ``__call__``
131 protocol, ``call()`` invokes the underlying function, passing an arbitrary
132 set of parameters. The result is returned as a :class:`object` and may need
Wenzel Jakob93296692015-10-13 23:21:54 +0200133 to be converted back into a Python object using :func:`handle::cast`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200134
135 When some of the arguments cannot be converted to Python objects, the
136 function will throw a :class:`cast_error` exception. When the Python
137 function call fails, a :class:`error_already_set` exception is thrown.
138
139With reference counting
140-----------------------
141
142.. class:: object : public handle
143
144 Like :class:`handle`, the object class is a thin wrapper around an
145 arbitrary Python object (i.e. a ``PyObject *`` in Python's C API). In
146 contrast to :class:`handle`, it optionally increases the object's reference
147 count upon construction, and it *always* decreases the reference count when
148 the :class:`object` instance goes out of scope and is destructed. When
149 using :class:`object` instances consistently, it is much easier to get
150 reference counting right at the first attempt.
151
152.. function:: object::object(const object &o)
153
154 Copy constructor; always increases the reference count
155
156.. function:: object::object(const handle &h, bool borrowed)
157
158 Creates a :class:`object` from the given :class:`handle`. The reference
159 count is only increased if the ``borrowed`` parameter is set to ``true``.
160
161.. function:: object::object(PyObject *ptr, bool borrowed)
162
163 Creates a :class:`object` from the given raw Python object pointer. The
164 reference count is only increased if the ``borrowed`` parameter is set to
165 ``true``.
166
167.. function:: object::object(object &&other)
168
169 Move constructor; steals the object from ``other`` and preserves its
170 reference count.
171
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100172.. function:: handle object::release()
Tomasz Miąskoca771302015-12-30 21:03:57 +0100173
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100174 Resets the internal pointer to ``nullptr`` without without decreasing the
175 object's reference count. The function returns a raw handle to the original
176 Python object.
Tomasz Miąskoca771302015-12-30 21:03:57 +0100177
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200178.. function:: object::~object()
179
Tomasz Miąsko875df552015-12-28 08:11:16 +0100180 Destructor, which automatically calls :func:`handle::dec_ref()`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200181
182Convenience classes for specific Python types
183=============================================
184
185
186.. class:: module : public object
187
188.. function:: module::module(const char *name, const char *doc = nullptr)
189
190 Create a new top-level Python module with the given name and docstring
191
192.. function:: module module::def_submodule(const char *name, const char *doc = nullptr)
193
194 Create and return a new Python submodule with the given name and docstring.
195 This also works recursively, i.e.
196
197 .. code-block:: cpp
198
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200199 pybind11::module m("example", "pybind11 example plugin");
200 pybind11::module m2 = m.def_submodule("sub", "A submodule of 'example'");
201 pybind11::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200202
203.. cpp:function:: template <typename Func, typename ... Extra> module& module::def(const char *name, Func && f, Extra && ... extra)
204
205 Create Python binding for a new function within the module scope. ``Func``
206 can be a plain C++ function, a function pointer, or a lambda function. For
207 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
208
209.. _extras:
210
211Passing extra arguments to the def function
212===========================================
213
214.. class:: arg
215
216.. function:: arg::arg(const char *name)
217
218.. function:: template <typename T> arg_t<T> arg::operator=(const T &value)
219
220.. class:: template <typename T> arg_t<T> : public arg
221
222 Represents a named argument with a default value
Wenzel Jakob93296692015-10-13 23:21:54 +0200223
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200224.. class:: sibling
225
226 Used to specify a handle to an existing sibling function; used internally
227 to implement function overloading in :func:`module::def` and
228 :func:`class_::def`.
229
230.. function:: sibling::sibling(handle handle)
231
232.. class doc
233
234 This is class is internally used by pybind11.
235
236.. function:: doc::doc(const char *value)
237
238 Create a new docstring with the specified value
239
240.. class name
241
242 This is class is internally used by pybind11.
243
244.. function:: name::name(const char *value)
245
246 Used to specify the function name