Scarab  v2.9.1
Project 8 C++ Utility Library
Classes | Namespaces | Macros | Functions | Variables
pybind11.h File Reference
#include "attr.h"
#include "options.h"
#include "detail/class.h"
#include "detail/init.h"
Include dependency graph for pybind11.h:

Go to the source code of this file.

Classes

class  cpp_function
 Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object. More...
 
class  module
 Wrapper for Python extension modules. More...
 
class  generic_type
 Generic support for creating new Python heap types. More...
 
struct  has_operator_delete< T, SFINAE >
 
struct  has_operator_delete< T, void_t< decltype(static_cast< void(*)(void *)>(T::operator delete))> >
 
struct  has_operator_delete_size< T, SFINAE >
 
struct  has_operator_delete_size< T, void_t< decltype(static_cast< void(*)(void *, size_t)>(T::operator delete))> >
 
class  class_< type_, options >
 
struct  class_< type_, options >::is_valid_class_option< T >
 
struct  enum_base
 
class  enum_< Type >
 Binds C++ enumerations and enumeration classes to Python. More...
 
struct  iterator_state< Iterator, Sentinel, KeyIterator, Policy >
 
class  exception< type >
 
class  gil_scoped_acquire
 
class  gil_scoped_release
 

Namespaces

 pybind11
 glibc defines I as a macro which breaks things, e.g., boost template names
 
 pybind11::detail
 

Macros

#define PYBIND11_ENUM_OP_STRICT(op, expr, strict_behavior)
 
#define PYBIND11_ENUM_OP_CONV(op, expr)
 
#define PYBIND11_THROW   throw type_error("Expected an enumeration of matching type!");
 
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...)
 
#define PYBIND11_OVERLOAD_NAME(ret_type, cname, name, fn, ...)
 
#define PYBIND11_OVERLOAD_PURE_NAME(ret_type, cname, name, fn, ...)
 
#define PYBIND11_OVERLOAD(ret_type, cname, fn, ...)   PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
 
#define PYBIND11_OVERLOAD_PURE(ret_type, cname, fn, ...)   PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
 

Functions

dict globals ()
 
template<typename T , typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
void set_operator_new (type_record *r)
 Set the pointer to operator new if it exists. The cast is needed because it can be overloaded. More...
 
template<typename >
void set_operator_new (...)
 
template<typename T , enable_if_t< has_operator_delete< T >::value, int > = 0>
void call_operator_delete (T *p, size_t, size_t)
 Call class-specific delete if it exists or global otherwise. Can also be an overload set. More...
 
void call_operator_delete (void *p, size_t s, size_t a)
 
template<typename , typename F >
auto method_adaptor (F &&f) -> decltype(std::forward< F >(f))
 
template<typename Derived , typename Return , typename Class , typename... Args>
auto method_adaptor (Return(Class::*pmf)(Args...)) -> Return(Derived::*)(Args...)
 
template<typename... Args>
detail::initimpl::constructor< Args... > init ()
 Binds an existing constructor taking arguments Args... More...
 
template<typename... Args>
detail::initimpl::alias_constructor< Args... > init_alias ()
 
template<typename Func , typename Ret = detail::initimpl::factory<Func>>
Ret init (Func &&f)
 Binds a factory function as a constructor. More...
 
template<typename CFunc , typename AFunc , typename Ret = detail::initimpl::factory<CFunc, AFunc>>
Ret init (CFunc &&c, AFunc &&a)
 
template<typename GetState , typename SetState >
detail::initimpl::pickle_factory< GetState, SetState > pickle (GetState &&g, SetState &&s)
 
void keep_alive_impl (handle nurse, handle patient)
 
 __attribute__ ((noinline)) inline detail
 Return the type info for a given C++ type; on lookup failure can either throw or return nullptr. More...
 
 keep_alive_impl (get_arg(Nurse), get_arg(Patient))
 
std::pair< decltype(internals::registered_types_py)::iterator, bool > all_type_info_get_cache (PyTypeObject *type)
 
template<return_value_policy Policy = return_value_policy::reference_internal, typename Iterator , typename Sentinel , typename ValueType = decltype(*std::declval<Iterator>()), typename... Extra>
iterator make_iterator (Iterator first, Sentinel last, Extra &&... extra)
 Makes a python iterator from a first and past-the-end C++ InputIterator. More...
 
template<return_value_policy Policy = return_value_policy::reference_internal, typename Iterator , typename Sentinel , typename KeyType = decltype((*std::declval<Iterator>()).first), typename... Extra>
iterator make_key_iterator (Iterator first, Sentinel last, Extra &&... extra)
 
template<return_value_policy Policy = return_value_policy::reference_internal, typename Type , typename... Extra>
iterator make_iterator (Type &value, Extra &&... extra)
 
template<return_value_policy Policy = return_value_policy::reference_internal, typename Type , typename... Extra>
iterator make_key_iterator (Type &value, Extra &&... extra)
 
template<typename InputType , typename OutputType >
void implicitly_convertible ()
 
template<typename ExceptionTranslator >
void register_exception_translator (ExceptionTranslator &&translator)
 
template<typename CppException >
exception< CppException > & get_exception_object ()
 
template<typename CppException >
exception< CppException > & register_exception (handle scope, const char *name, PyObject *base=PyExc_Exception)
 
 for (size_t i=0;i< check.size();i++)
 
 if (kwargs.contains("file"))
 
 write (line)
 
 write (kwargs.contains("end") ? kwargs["end"] :cast("\))
 
 if (kwargs.contains("flush") &&kwargs["flush"].cast< bool >()) file.attr("flush")()
 
template<return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
void print (Args &&...args)
 
function get_type_overload (const void *this_ptr, const detail::type_info *this_type, const char *name)
 
template<class T >
function get_overload (const T *this_ptr, const char *name)
 

Variables

size_t Patient
 
size_t function_call & call
 
size_t function_call handle ret
 
dict kwargs
 
auto sep = kwargs.contains("sep") ? kwargs["sep"] : cast(" ")
 
auto line = sep.attr("join")(strings)
 
object file
 
 else
 

Macro Definition Documentation

◆ PYBIND11_ENUM_OP_CONV

#define PYBIND11_ENUM_OP_CONV (   op,
  expr 
)
Value:
m_base.attr(op) = cpp_function( \
[](object a_, object b_) { \
int_ a(a_), b(b_); \
return expr; \
}, \
is_method(m_base))

Definition at line 1464 of file pybind11.h.

◆ PYBIND11_ENUM_OP_STRICT

#define PYBIND11_ENUM_OP_STRICT (   op,
  expr,
  strict_behavior 
)
Value:
m_base.attr(op) = cpp_function( \
[](object a, object b) { \
if (!a.get_type().is(b.get_type())) \
strict_behavior; \
return expr; \
}, \
is_method(m_base))

Definition at line 1455 of file pybind11.h.

◆ PYBIND11_OVERLOAD

#define PYBIND11_OVERLOAD (   ret_type,
  cname,
  fn,
  ... 
)    PYBIND11_OVERLOAD_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)

Macro to populate the virtual method in the trampoline class. This macro tries to look up the method from the Python side, deals with the :ref:gil and necessary argument conversions to call this method and return the appropriate type. This macro should be used if the method name in C and in Python are identical. See :ref:overriding_virtuals for more information.

.. code-block:: cpp

class PyAnimal : public Animal { public: Inherit the constructors using Animal::Animal;

Trampoline (need one for each virtual function) std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE( std::string, // Return type (ret_type) Animal, // Parent class (cname) go, // Name of function in C++ (must match Python name) (fn) n_times // Argument(s) (...) ); } };

Definition at line 2145 of file pybind11.h.

◆ PYBIND11_OVERLOAD_INT

#define PYBIND11_OVERLOAD_INT (   ret_type,
  cname,
  name,
  ... 
)
Value:
{ \
pybind11::gil_scoped_acquire gil; \
pybind11::function overload = pybind11::get_overload(static_cast<const cname *>(this), name); \
if (overload) { \
auto o = overload(__VA_ARGS__); \
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
} \
else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
} \
}
bool_constant<(std::is_reference< type >::value||std::is_pointer< type >::value) &&!std::is_base_of< type_caster_generic, make_caster< type > >::value &&!std::is_same< intrinsic_t< type >, void >::value > cast_is_temporary_value_reference
Definition: cast.h:1618
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1685
function get_overload(const T *this_ptr, const char *name)
Definition: pybind11.h:2074
conditional_t< cast_is_temporary_value_reference< ret_type >::value, make_caster< ret_type >, overload_unused > overload_caster_t
Definition: cast.h:1731

Definition at line 2079 of file pybind11.h.

◆ PYBIND11_OVERLOAD_NAME

#define PYBIND11_OVERLOAD_NAME (   ret_type,
  cname,
  name,
  fn,
  ... 
)
Value:
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
return cname::fn(__VA_ARGS__)
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name,...)
Definition: pybind11.h:2079
#define PYBIND11_TYPE(...)
Definition: cast.h:2128

Macro to populate the virtual method in the trampoline class. This macro tries to look up a method named 'fn' from the Python side, deals with the :ref:gil and necessary argument conversions to call this method and return the appropriate type. See :ref:overriding_virtuals for more information. This macro should be used when the method name in C is not the same as the method name in Python. For example with __str__.

.. code-block:: cpp

std::string toString() override { PYBIND11_OVERLOAD_NAME( std::string, // Return type (ret_type) Animal, // Parent class (cname) toString, // Name of function in C++ (name) "__str__", // Name of method in Python (fn) ); }

Definition at line 2109 of file pybind11.h.

◆ PYBIND11_OVERLOAD_PURE

#define PYBIND11_OVERLOAD_PURE (   ret_type,
  cname,
  fn,
  ... 
)    PYBIND11_OVERLOAD_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)

Macro for pure virtual functions, this function is identical to :c:macro:PYBIND11_OVERLOAD, except that it throws if no overload can be found.

Definition at line 2152 of file pybind11.h.

◆ PYBIND11_OVERLOAD_PURE_NAME

#define PYBIND11_OVERLOAD_PURE_NAME (   ret_type,
  cname,
  name,
  fn,
  ... 
)
Value:
PYBIND11_OVERLOAD_INT(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__) \
pybind11::pybind11_fail("Tried to call pure virtual function \"" PYBIND11_STRINGIFY(cname) "::" name "\"");
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name,...)
Definition: pybind11.h:2079
#define PYBIND11_STRINGIFY(x)
#define PYBIND11_TYPE(...)
Definition: cast.h:2128

Macro for pure virtual functions, this function is identical to :c:macro:PYBIND11_OVERLOAD_NAME, except that it throws if no overload can be found.

Definition at line 2117 of file pybind11.h.

◆ PYBIND11_THROW

#define PYBIND11_THROW   throw type_error("Expected an enumeration of matching type!");