You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
8.1 KiB

  1. .. _reference:
  2. .. warning::
  3. Please be advised that the reference documentation discussing pybind11
  4. internals is currently incomplete. Please refer to the previous sections
  5. and the pybind11 header files for the nitty gritty details.
  6. Reference
  7. #########
  8. Macros
  9. ======
  10. .. function:: PYBIND11_PLUGIN(const char *name)
  11. This macro creates the entry point that will be invoked when the Python
  12. interpreter imports a plugin library. Please create a
  13. :class:`module` in the function body and return the pointer to its
  14. underlying Python object at the end.
  15. .. code-block:: cpp
  16. PYBIND11_PLUGIN(example) {
  17. pybind11::module m("example", "pybind11 example plugin");
  18. /// Set up bindings here
  19. return m.ptr();
  20. }
  21. .. _core_types:
  22. Convenience classes for arbitrary Python types
  23. ==============================================
  24. Without reference counting
  25. --------------------------
  26. .. class:: handle
  27. The :class:`handle` class is a thin wrapper around an arbitrary Python
  28. object (i.e. a ``PyObject *`` in Python's C API). It does not perform any
  29. automatic reference counting and merely provides a basic C++ interface to
  30. various Python API functions.
  31. .. seealso::
  32. The :class:`object` class inherits from :class:`handle` and adds automatic
  33. reference counting features.
  34. .. function:: handle::handle()
  35. The default constructor creates a handle with a ``nullptr``-valued pointer.
  36. .. function:: handle::handle(const handle&)
  37. Copy constructor
  38. .. function:: handle::handle(PyObject *)
  39. Creates a :class:`handle` from the given raw Python object pointer.
  40. .. function:: PyObject * handle::ptr() const
  41. Return the ``PyObject *`` underlying a :class:`handle`.
  42. .. function:: const handle& handle::inc_ref() const
  43. Manually increase the reference count of the Python object. Usually, it is
  44. preferable to use the :class:`object` class which derives from
  45. :class:`handle` and calls this function automatically. Returns a reference
  46. to itself.
  47. .. function:: const handle& handle::dec_ref() const
  48. Manually decrease the reference count of the Python object. Usually, it is
  49. preferable to use the :class:`object` class which derives from
  50. :class:`handle` and calls this function automatically. Returns a reference
  51. to itself.
  52. .. function:: void handle::ref_count() const
  53. Return the object's current reference count
  54. .. function:: handle handle::get_type() const
  55. Return a handle to the Python type object underlying the instance
  56. .. function detail::accessor handle::operator[](handle key) const
  57. Return an internal functor to invoke the object's sequence protocol.
  58. Casting the returned ``detail::accessor`` instance to a :class:`handle` or
  59. :class:`object` subclass causes a corresponding call to ``__getitem__``.
  60. Assigning a :class:`handle` or :class:`object` subclass causes a call to
  61. ``__setitem__``.
  62. .. function detail::accessor handle::operator[](const char *key) const
  63. See the above function (the only difference is that they key is provided as
  64. a string literal).
  65. .. function detail::accessor handle::attr(handle key) const
  66. Return an internal functor to access the object's attributes.
  67. Casting the returned ``detail::accessor`` instance to a :class:`handle` or
  68. :class:`object` subclass causes a corresponding call to ``__getattr``.
  69. Assigning a :class:`handle` or :class:`object` subclass causes a call to
  70. ``__setattr``.
  71. .. function detail::accessor handle::attr(const char *key) const
  72. See the above function (the only difference is that they key is provided as
  73. a string literal).
  74. .. function operator handle::bool() const
  75. Return ``true`` when the :class:`handle` wraps a valid Python object.
  76. .. function str handle::str() const
  77. Return a string representation of the object. This is analogous to
  78. the ``str()`` function in Python.
  79. .. function:: template <typename T> T handle::cast() const
  80. Attempt to cast the Python object into the given C++ type. A
  81. :class:`cast_error` will be throw upon failure.
  82. .. function:: template <typename ... Args> object handle::call(Args&&... args) const
  83. Assuming the Python object is a function or implements the ``__call__``
  84. protocol, ``call()`` invokes the underlying function, passing an arbitrary
  85. set of parameters. The result is returned as a :class:`object` and may need
  86. to be converted back into a Python object using :func:`handle::cast`.
  87. When some of the arguments cannot be converted to Python objects, the
  88. function will throw a :class:`cast_error` exception. When the Python
  89. function call fails, a :class:`error_already_set` exception is thrown.
  90. With reference counting
  91. -----------------------
  92. .. class:: object : public handle
  93. Like :class:`handle`, the object class is a thin wrapper around an
  94. arbitrary Python object (i.e. a ``PyObject *`` in Python's C API). In
  95. contrast to :class:`handle`, it optionally increases the object's reference
  96. count upon construction, and it *always* decreases the reference count when
  97. the :class:`object` instance goes out of scope and is destructed. When
  98. using :class:`object` instances consistently, it is much easier to get
  99. reference counting right at the first attempt.
  100. .. function:: object::object(const object &o)
  101. Copy constructor; always increases the reference count
  102. .. function:: object::object(const handle &h, bool borrowed)
  103. Creates a :class:`object` from the given :class:`handle`. The reference
  104. count is only increased if the ``borrowed`` parameter is set to ``true``.
  105. .. function:: object::object(PyObject *ptr, bool borrowed)
  106. Creates a :class:`object` from the given raw Python object pointer. The
  107. reference count is only increased if the ``borrowed`` parameter is set to
  108. ``true``.
  109. .. function:: object::object(object &&other)
  110. Move constructor; steals the object from ``other`` and preserves its
  111. reference count.
  112. .. function:: handle object::release()
  113. Resets the internal pointer to ``nullptr`` without without decreasing the
  114. object's reference count. The function returns a raw handle to the original
  115. Python object.
  116. .. function:: object::~object()
  117. Destructor, which automatically calls :func:`handle::dec_ref()`.
  118. Convenience classes for specific Python types
  119. =============================================
  120. .. class:: module : public object
  121. .. function:: module::module(const char *name, const char *doc = nullptr)
  122. Create a new top-level Python module with the given name and docstring
  123. .. function:: module module::def_submodule(const char *name, const char *doc = nullptr)
  124. Create and return a new Python submodule with the given name and docstring.
  125. This also works recursively, i.e.
  126. .. code-block:: cpp
  127. pybind11::module m("example", "pybind11 example plugin");
  128. pybind11::module m2 = m.def_submodule("sub", "A submodule of 'example'");
  129. pybind11::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
  130. .. cpp:function:: template <typename Func, typename ... Extra> module& module::def(const char *name, Func && f, Extra && ... extra)
  131. Create Python binding for a new function within the module scope. ``Func``
  132. can be a plain C++ function, a function pointer, or a lambda function. For
  133. details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
  134. .. _extras:
  135. Passing extra arguments to the def function
  136. ===========================================
  137. .. class:: arg
  138. .. function:: arg::arg(const char *name)
  139. .. function:: template <typename T> arg_v arg::operator=(T &&value)
  140. .. class:: arg_v : public arg
  141. Represents a named argument with a default value
  142. .. class:: sibling
  143. Used to specify a handle to an existing sibling function; used internally
  144. to implement function overloading in :func:`module::def` and
  145. :func:`class_::def`.
  146. .. function:: sibling::sibling(handle handle)
  147. .. class doc
  148. This is class is internally used by pybind11.
  149. .. function:: doc::doc(const char *value)
  150. Create a new docstring with the specified value
  151. .. class name
  152. This is class is internally used by pybind11.
  153. .. function:: name::name(const char *value)
  154. Used to specify the function name