13#include "detail/common.h"
64 "base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
69template <
size_t Nurse,
size_t Patient>
85 PYBIND11_DEPRECATED(
"py::metaclass() is no longer required. It's turned on by default now.")
102 using callback = std::function<void(PyHeapTypeObject *heap_type)>;
139template <
typename... Ts>
144 using type = detail::void_type;
149 static_assert(std::is_default_constructible<T>::value,
150 "The guard type must be default constructible");
155template <
typename T,
typename... Ts>
170template <op_
id id, op_type ot,
typename L = undefined_t,
typename R = undefined_t>
195 char *
name =
nullptr;
201 char *signature =
nullptr;
204 std::vector<argument_record>
args;
219 bool is_constructor : 1;
225 bool is_stateless : 1;
247 std::uint16_t nargs_pos = 0;
250 std::uint16_t nargs_pos_only = 0;
253 PyMethodDef *def =
nullptr;
275 const char *
name =
nullptr;
278 const std::type_info *
type =
nullptr;
281 size_t type_size = 0;
284 size_t type_align = 0;
287 size_t holder_size = 0;
290 void *(*operator_new)(
size_t) =
nullptr;
293 void (*init_instance)(
instance *,
const void *) =
nullptr;
296 void (*dealloc)(detail::value_and_holder &) =
nullptr;
302 const char *
doc =
nullptr;
320 bool default_holder : 1;
329 auto *base_info = detail::get_type_info(
base,
false);
331 std::string tname(
base.name());
332 detail::clean_type_id(tname);
334 +
"\" referenced unknown base type \"" + tname +
"\"");
337 if (default_holder != base_info->default_holder) {
338 std::string tname(
base.name());
339 detail::clean_type_id(tname);
341 + (default_holder ?
"does not have" :
"has")
342 +
" a non-default holder type while its base \"" + tname +
"\" "
343 + (base_info->default_holder ?
"does not" :
"does"));
346 bases.append((PyObject *) base_info->type);
348#if PY_VERSION_HEX < 0x030B0000
351 dynamic_attr |= (base_info->type->tp_flags & Py_TPFLAGS_MANAGED_DICT) != 0;
355 base_info->implicit_casts.emplace_back(
type, caster);
362 args_convert.reserve(f.
nargs);
374template <
typename T,
typename SFINAE =
void>
451 pybind11_fail(
"arg(): cannot specify an unnamed argument after a kw_only() annotation or "
458 r->
args.emplace_back(
"self",
nullptr,
handle(),
true,
false);
478 r->
args.emplace_back(
479 "self",
nullptr,
handle(),
true,
false);
483#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
484 std::string
descr(
"'");
492 + (std::string) r->
name +
"'";
494 descr +=
" in method of '" + (std::string)
str(r->
scope) +
"'";
496 }
else if (r->
name) {
497 descr +=
" in function '" + (std::string) r->
name +
"'";
500 +
" into a Python object (type not registered yet?)");
503 "into a Python object (type not registered yet?). "
504 "#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for "
505 "more information.");
520 pybind11_fail(
"Mismatched args() and kw_only(): they must occur at the same relative "
521 "argument location (or omit kw_only() entirely)");
534 pybind11_fail(
"pos_only(): cannot follow a py::args() argument");
604template <
typename... Ts>
612template <
size_t Nurse,
size_t Patient>
615 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N != 0 && P != 0,
int> = 0>
619 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N != 0 && P != 0,
int> = 0>
621 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N == 0 || P == 0,
int> = 0>
623 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N == 0 || P == 0,
int> = 0>
630template <
typename... Args>
635 using expander =
int[];
642 using expander =
int[];
648 using expander =
int[];
655 using expander =
int[];
665template <
typename... Extra>
669template <
typename... Extra,
670 size_t named =
constexpr_sum(std::is_base_of<arg, Extra>::value...),
674 return named == 0 || (
self + named +
size_t(has_args) +
size_t(has_kwargs)) == nargs;
\rst Holds a reference to a Python object (no reference counting)
const handle & inc_ref() const &
\rst Manually increase the reference count of the Python object.
void append_self_arg_if_needed(function_record *r)
void check_kw_only_arg(const arg &a, function_record *r)
void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret)
typename exactly_one_t< is_call_guard, call_guard<>, Extra... >::type extract_guard_t
Extract the type from the first call_guard in Extras... (or void_type if none found)
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs)
Check the number of named arguments at compile time.
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
#define PYBIND11_NOINLINE
constexpr size_t constexpr_sum()
Compile-time integer sum.
#define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
#define PYBIND11_NAMESPACE_END(name)
typename exactly_one< Predicate, Default, Ts... >::type exactly_one_t
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
@ move
Use std::move to move the return value contents into a new instance that will be owned by Python.
#define PYBIND11_DEPRECATED(reason)
op_id
Enumeration with all supported operator types.
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
std::is_base_of< pyobject_tag, remove_reference_t< T > > is_pyobject
arr data(const arr &a, Ix... index)
void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret)
Annotation for arguments with values.
object value
The default value.
std::string type
The C++ type name of the default value (only available when compiled in debug mode)
const char * descr
The (optional) description of the default value.
Annotation for arguments.
const char * name
If non-null, this is a named kwargs argument.
bool flag_none
If set (the default), allow None to be passed to this argument.
bool flag_noconvert
If set, do not allow conversion (requires a supporting type caster!)
Internal data structure which holds metadata about a keyword argument.
argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
Annotation to mark enums as an arithmetic type.
Annotation indicating that a class derives from another given type.
PYBIND11_DEPRECATED("base<T>() was deprecated in favor of specifying 'T' as a template argument to class_") base()=default
Annotation which enables the buffer protocol for a type.
\rst A call policy which places one or more guard variables (Ts...) around the function call.
Specifies a custom callback with signature void (PyHeapTypeObject*) that may be used to customize the...
std::function< void(PyHeapTypeObject *heap_type)> callback
custom_type_setup(callback value)
Annotation for documentation.
Annotation which enables dynamic attributes, i.e. adds __dict__ to a class.
Internal data associated with a single function call.
function_call(const function_record &f, handle p)
Internal data structure which holds metadata about a bound function (signature, overloads,...
bool prepend
True if this function is to be inserted at the beginning of the overload resolution chain.
return_value_policy policy
Return value policy associated with this function.
bool is_new_style_constructor
True if this is a new-style __init__ defined in detail/init.h
std::uint16_t nargs_pos_only
Number of leading arguments (counted in nargs) that are positional-only.
handle sibling
Python handle to the sibling function representing an overload chain.
std::uint16_t nargs
Number of arguments (including py::args and/or py::kwargs, if present)
std::uint16_t nargs_pos
Number of leading positional arguments, which are terminated by a py::args or py::kwargs argument or ...
bool is_method
True if this is a method.
bool is_operator
True if this is an operator (add), etc.
std::vector< argument_record > args
List of registered keyword arguments.
char * name
Function name.
handle scope
Python handle to the parent scope (a class or a module)
bool has_args
True if the function has a '*args' argument.
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof')
Annotation for classes that cannot be subclassed.
Check if T is an instantiation of the template Class.
is_method(const handle &c)
Tag for a new-style __init__ defined in detail/init.h
Annotation for operators.
Keep patient alive while nurse lives.
Annotation indicating that all following arguments are keyword-only; the is the equivalent of an unna...
Annotation that marks a class as local to the module:
constexpr module_local(bool v=true)
Annotation indicating that a class is involved in a multiple inheritance relationship.
Annotation for function names.
Operator implementation generator.
Annotation indicating that all previous arguments are positional-only; the is the equivalent of an un...
Mark a function for addition at the beginning of the existing overload chain instead of the end.
static void init(const handle &h, type_record *r)
static void init(const arg &a, function_record *r)
static void init(const arg_v &a, function_record *r)
static void init(const base< T > &, type_record *r)
static void init(const buffer_protocol &, type_record *r)
static void init(const char *d, type_record *r)
static void init(const char *d, function_record *r)
static void init(const custom_type_setup &value, type_record *r)
static void init(const doc &n, function_record *r)
static void init(const dynamic_attr &, type_record *r)
static void init(const is_final &, type_record *r)
static void init(const is_method &s, function_record *r)
static void init(const is_new_style_constructor &, function_record *r)
static void init(const is_operator &, function_record *r)
static void precall(function_call &call)
static void postcall(function_call &call, handle ret)
static void postcall(function_call &, handle)
static void precall(function_call &)
static void init(const kw_only &, function_record *r)
static void init(const module_local &l, type_record *r)
static void init(const multiple_inheritance &, type_record *r)
static void init(const name &n, function_record *r)
static void init(const pos_only &, function_record *r)
static void init(const prepend &, function_record *r)
static void init(const return_value_policy &p, function_record *r)
static void init(const scope &s, function_record *r)
static void init(const sibling &s, function_record *r)
static void init(const T &, function_record *)
Default implementation: do nothing.
static void postcall(function_call &, handle)
static void precall(function_call &)
static void init(const T &, type_record *)
Partial template specializations to process custom attributes provided to cpp_function_ and class_.
Recursively iterate over variadic template arguments.
static void precall(function_call &call)
static void init(const Args &...args, function_record *r)
static void init(const Args &...args, type_record *r)
static void postcall(function_call &call, handle fn_ret)
Annotation for parent scope.
Annotation indicating that a function is an overload associated with a given "sibling".
sibling(const handle &value)
Special data structure which (temporarily) holds metadata about a bound class.
handle metaclass
Custom metaclass (optional)
bool multiple_inheritance
Multiple inheritance marker.
PYBIND11_NOINLINE type_record()
bool module_local
Is the class definition local to the module shared object?
bool is_final
Is the class inheritable from python classes?
bool buffer_protocol
Does the class implement the buffer protocol?
const char * doc
Optional docstring.
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *))
list bases
List of base classes of the newly created type.
custom_type_setup::callback custom_type_setup_callback
Custom type setup.
bool dynamic_attr
Does the class manage a dict?
Type for an unused type slot.