blob: 16d2a8d14559096185255f6f3416e0312eae7ea6 [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
62.. function:: PyObject * handle::ptr()
63
64 Return the ``PyObject *`` underlying a :class:`handle`.
65
66.. function:: void handle::inc_ref()
67
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
72.. function:: void handle::dec_ref()
73
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
78.. function:: void handle::ref_count()
79
80 Return the object's current reference count
81
82.. function:: handle handle::get_type()
83
84 Return a handle to the Python type object underlying the instance
85
86.. function detail::accessor handle::operator[](handle key)
87
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
94.. function detail::accessor handle::operator[](const char *key)
95
96 See the above function (the only difference is that they key is provided as
97 a string literal).
98
99.. function detail::accessor handle::attr(handle key)
100
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
107.. function detail::accessor handle::attr(const char *key)
108
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
121.. function:: template <typename T> T handle::cast()
122
123 Attempt to cast the Python object into the given C++ type. A
124 :class:`cast_error` will be throw upon failure.
125
126.. function:: template <typename ... Args> object handle::call(Args&&... args)
127
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
170.. function:: object::~object()
171
172 Constructor, which automatically calls :func:`handle::dec_ref()`.
173
174Convenience classes for specific Python types
175=============================================
176
177
178.. class:: module : public object
179
180.. function:: module::module(const char *name, const char *doc = nullptr)
181
182 Create a new top-level Python module with the given name and docstring
183
184.. function:: module module::def_submodule(const char *name, const char *doc = nullptr)
185
186 Create and return a new Python submodule with the given name and docstring.
187 This also works recursively, i.e.
188
189 .. code-block:: cpp
190
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200191 pybind11::module m("example", "pybind11 example plugin");
192 pybind11::module m2 = m.def_submodule("sub", "A submodule of 'example'");
193 pybind11::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200194
195.. cpp:function:: template <typename Func, typename ... Extra> module& module::def(const char *name, Func && f, Extra && ... extra)
196
197 Create Python binding for a new function within the module scope. ``Func``
198 can be a plain C++ function, a function pointer, or a lambda function. For
199 details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
200
201.. _extras:
202
203Passing extra arguments to the def function
204===========================================
205
206.. class:: arg
207
208.. function:: arg::arg(const char *name)
209
210.. function:: template <typename T> arg_t<T> arg::operator=(const T &value)
211
212.. class:: template <typename T> arg_t<T> : public arg
213
214 Represents a named argument with a default value
Wenzel Jakob93296692015-10-13 23:21:54 +0200215
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200216.. class:: sibling
217
218 Used to specify a handle to an existing sibling function; used internally
219 to implement function overloading in :func:`module::def` and
220 :func:`class_::def`.
221
222.. function:: sibling::sibling(handle handle)
223
224.. class doc
225
226 This is class is internally used by pybind11.
227
228.. function:: doc::doc(const char *value)
229
230 Create a new docstring with the specified value
231
232.. class name
233
234 This is class is internally used by pybind11.
235
236.. function:: name::name(const char *value)
237
238 Used to specify the function name