blob: ab5c11b68a1e9d4ad4f78b17f539d42c452d52ce [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
Tomasz Miąsko875df552015-12-28 08:11:16 +010066.. function:: void 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
70 :class:`handle` and calls this function automatically.
71
Tomasz Miąsko875df552015-12-28 08:11:16 +010072.. function:: void handle::dec_ref() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020073
74 Manually decrease the reference count of the Python object. Usually, it is
75 preferable to use the :class:`object` class which derives from
76 :class:`handle` and calls this function automatically.
77
Tomasz Miąsko875df552015-12-28 08:11:16 +010078.. function:: void handle::ref_count() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020079
80 Return the object's current reference count
81
Tomasz Miąsko875df552015-12-28 08:11:16 +010082.. function:: handle handle::get_type() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020083
84 Return a handle to the Python type object underlying the instance
85
Tomasz Miąsko875df552015-12-28 08:11:16 +010086.. function detail::accessor handle::operator[](handle key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020087
88 Return an internal functor to invoke the object's sequence protocol.
89 Casting the returned ``detail::accessor`` instance to a :class:`handle` or
90 :class:`object` subclass causes a corresponding call to ``__getitem__``.
91 Assigning a :class:`handle` or :class:`object` subclass causes a call to
92 ``__setitem__``.
93
Tomasz Miąsko875df552015-12-28 08:11:16 +010094.. function detail::accessor handle::operator[](const char *key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020095
96 See the above function (the only difference is that they key is provided as
97 a string literal).
98
Tomasz Miąsko875df552015-12-28 08:11:16 +010099.. function detail::accessor handle::attr(handle key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200100
101 Return an internal functor to access the object's attributes.
102 Casting the returned ``detail::accessor`` instance to a :class:`handle` or
103 :class:`object` subclass causes a corresponding call to ``__getattr``.
104 Assigning a :class:`handle` or :class:`object` subclass causes a call to
105 ``__setattr``.
106
Tomasz Miąsko875df552015-12-28 08:11:16 +0100107.. function detail::accessor handle::attr(const char *key) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200108
109 See the above function (the only difference is that they key is provided as
110 a string literal).
111
112.. function operator handle::bool() const
113
114 Return ``true`` when the :class:`handle` wraps a valid Python object.
115
116.. function str handle::str() const
117
118 Return a string representation of the object. This is analogous to
119 the ``str()`` function in Python.
120
Tomasz Miąskocc39b2f2015-12-26 19:01:28 +0100121.. function:: template <typename T> T handle::cast() const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200122
123 Attempt to cast the Python object into the given C++ type. A
124 :class:`cast_error` will be throw upon failure.
125
Tomasz Miąsko875df552015-12-28 08:11:16 +0100126.. function:: template <typename ... Args> object handle::call(Args&&... args) const
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200127
128 Assuming the Python object is a function or implements the ``__call__``
129 protocol, ``call()`` invokes the underlying function, passing an arbitrary
130 set of parameters. The result is returned as a :class:`object` and may need
Wenzel Jakob93296692015-10-13 23:21:54 +0200131 to be converted back into a Python object using :func:`handle::cast`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200132
133 When some of the arguments cannot be converted to Python objects, the
134 function will throw a :class:`cast_error` exception. When the Python
135 function call fails, a :class:`error_already_set` exception is thrown.
136
137With reference counting
138-----------------------
139
140.. class:: object : public handle
141
142 Like :class:`handle`, the object class is a thin wrapper around an
143 arbitrary Python object (i.e. a ``PyObject *`` in Python's C API). In
144 contrast to :class:`handle`, it optionally increases the object's reference
145 count upon construction, and it *always* decreases the reference count when
146 the :class:`object` instance goes out of scope and is destructed. When
147 using :class:`object` instances consistently, it is much easier to get
148 reference counting right at the first attempt.
149
150.. function:: object::object(const object &o)
151
152 Copy constructor; always increases the reference count
153
154.. function:: object::object(const handle &h, bool borrowed)
155
156 Creates a :class:`object` from the given :class:`handle`. The reference
157 count is only increased if the ``borrowed`` parameter is set to ``true``.
158
159.. function:: object::object(PyObject *ptr, bool borrowed)
160
161 Creates a :class:`object` from the given raw Python object pointer. The
162 reference count is only increased if the ``borrowed`` parameter is set to
163 ``true``.
164
165.. function:: object::object(object &&other)
166
167 Move constructor; steals the object from ``other`` and preserves its
168 reference count.
169
Tomasz Miąskoca771302015-12-30 21:03:57 +0100170.. function:: PyObject* object::release()
171
172 Release ownership of underlying ``PyObject *``. Returns raw Python object
173 pointer without decreasing its reference count and resets handle to
174 ``nullptr``-valued pointer.
175
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200176.. function:: object::~object()
177
Tomasz Miąsko875df552015-12-28 08:11:16 +0100178 Destructor, which automatically calls :func:`handle::dec_ref()`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200179
180Convenience classes for specific Python types
181=============================================
182
183
184.. class:: module : public object
185
186.. function:: module::module(const char *name, const char *doc = nullptr)
187
188 Create a new top-level Python module with the given name and docstring
189
190.. function:: module module::def_submodule(const char *name, const char *doc = nullptr)
191
192 Create and return a new Python submodule with the given name and docstring.
193 This also works recursively, i.e.
194
195 .. code-block:: cpp
196
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200197 pybind11::module m("example", "pybind11 example plugin");
198 pybind11::module m2 = m.def_submodule("sub", "A submodule of 'example'");
199 pybind11::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200200
201.. cpp:function:: template <typename Func, typename ... Extra> module& module::def(const char *name, Func && f, Extra && ... extra)
202
203 Create Python binding for a new function within the module scope. ``Func``
204 can be a plain C++ function, a function pointer, or a lambda function. For
205 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
206
207.. _extras:
208
209Passing extra arguments to the def function
210===========================================
211
212.. class:: arg
213
214.. function:: arg::arg(const char *name)
215
216.. function:: template <typename T> arg_t<T> arg::operator=(const T &value)
217
218.. class:: template <typename T> arg_t<T> : public arg
219
220 Represents a named argument with a default value
Wenzel Jakob93296692015-10-13 23:21:54 +0200221
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200222.. class:: sibling
223
224 Used to specify a handle to an existing sibling function; used internally
225 to implement function overloading in :func:`module::def` and
226 :func:`class_::def`.
227
228.. function:: sibling::sibling(handle handle)
229
230.. class doc
231
232 This is class is internally used by pybind11.
233
234.. function:: doc::doc(const char *value)
235
236 Create a new docstring with the specified value
237
238.. class name
239
240 This is class is internally used by pybind11.
241
242.. function:: name::name(const char *value)
243
244 Used to specify the function name