15 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
33 struct doc {
const char *value;
doc(
const char *value) : value(value) { } };
36 struct name {
const char *value;
name(
const char *value) : value(value) { } };
42 template <
typename T>
struct base {
44 PYBIND11_DEPRECATED(
"base<T>() was deprecated in favor of specifying 'T' as a template argument to class_")
49 template <
size_t Nurse,
size_t Patient>
struct keep_alive { };
64 PYBIND11_DEPRECATED(
"py::metaclass() is no longer required. It's turned on by default now.")
102 template <
typename T>
104 static_assert(std::is_default_constructible<T>::value,
105 "The guard type must be default constructible");
110 template <
typename T,
typename... Ts>
120 PYBIND11_NAMESPACE_BEGIN(detail)
125 template <op_id
id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct
op_;
126 inline
void keep_alive_impl(
size_t Nurse,
size_t Patient, function_call &call,
handle ret);
136 argument_record(
const char *name,
const char *descr,
handle value,
bool convert,
bool none)
137 : name(name), descr(descr), value(value), convert(convert), none(none) { }
157 std::vector<argument_record>
args;
169 return_value_policy
policy = return_value_policy::automatic;
208 PyMethodDef *
def =
nullptr;
233 const std::type_info *
type =
nullptr;
245 void *(*operator_new)(size_t) =
nullptr;
251 void (*
dealloc)(detail::value_and_holder &) =
nullptr;
257 const char *
doc =
nullptr;
280 PYBIND11_NOINLINE
void add_base(
const std::type_info &
base,
void *(*caster)(
void *)) {
281 auto base_info = detail::get_type_info(base,
false);
283 std::string tname(base.name());
284 detail::clean_type_id(tname);
285 pybind11_fail(
"generic_type: type \"" + std::string(
name) +
286 "\" referenced unknown base type \"" + tname +
"\"");
290 std::string tname(base.name());
291 detail::clean_type_id(tname);
292 pybind11_fail(
"generic_type: type \"" + std::string(
name) +
"\" " +
294 " a non-default holder type while its base \"" + tname +
"\" " +
295 (base_info->default_holder ?
"does not" :
"does"));
298 bases.append((PyObject *) base_info->type);
300 if (base_info->type->tp_dictoffset != 0)
304 base_info->implicit_casts.emplace_back(
type, caster);
311 args_convert.reserve(f.
nargs);
328 static void init(
const T &, type_record *) { }
329 static void precall(function_call &) { }
330 static void postcall(function_call &,
handle) { }
340 static void init(
const doc &n,
function_record *r) { r->doc =
const_cast<char *
>(n.value); }
345 static void init(
const char *d,
function_record *r) { r->doc =
const_cast<char *
>(d); }
346 static void init(
const char *d, type_record *r) { r->
doc =
const_cast<char *
>(d); }
380 if (!a.name || strlen(a.name) == 0)
381 pybind11_fail(
"arg(): cannot specify an unnamed argument after an kw_only() annotation");
389 r->
args.emplace_back(
"self",
nullptr,
handle(),
true ,
false );
390 r->
args.emplace_back(a.name,
nullptr,
handle(), !a.flag_noconvert, a.flag_none);
400 r->
args.emplace_back(
"self",
nullptr ,
handle() ,
true ,
false );
404 std::string
descr(
"'");
405 if (a.name) descr += std::string(a.name) +
": ";
406 descr += a.type +
"'";
409 descr +=
" in method '" + (std::string)
str(r->
scope) +
"." + (std::string) r->
name +
"'";
411 descr +=
" in method of '" + (std::string)
str(r->
scope) +
"'";
412 }
else if (r->
name) {
413 descr +=
" in function '" + (std::string) r->
name +
"'";
415 pybind11_fail(
"arg(): could not convert default argument "
416 + descr +
" into a Python object (type not registered yet?)");
418 pybind11_fail(
"arg(): could not convert default argument "
419 "into a Python object (type not registered yet?). "
420 "Compile in debug mode for more information.");
423 r->
args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
444 template <
typename T>
446 static void init(
const handle &h, type_record *r) { r->
bases.append(h); }
450 template <
typename T>
452 static void init(
const base<T> &, type_record *r) { r->add_base(
typeid(T),
nullptr); }
496 template <
typename... Ts>
505 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N != 0 && P != 0,
int> = 0>
506 static void precall(function_call &call) { keep_alive_impl(Nurse, Patient, call,
handle()); }
507 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N != 0 && P != 0,
int> = 0>
508 static void postcall(function_call &,
handle) { }
509 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N == 0 || P == 0,
int> = 0>
510 static void precall(function_call &) { }
511 template <
size_t N = Nurse,
size_t P = Patient, enable_if_t<N == 0 || P == 0,
int> = 0>
512 static void postcall(function_call &call,
handle ret) { keep_alive_impl(Nurse, Patient, call, ret); }
519 ignore_unused(unused);
521 static void init(
const Args&... args, type_record *r) {
523 ignore_unused(unused);
525 static void precall(function_call &call) {
527 ignore_unused(unused);
529 static void postcall(function_call &call,
handle fn_ret) {
531 ignore_unused(unused);
535 template <
typename T>
539 template <
typename... Extra>
540 using extract_guard_t =
typename exactly_one_t<is_call_guard, call_guard<>, Extra...>
::type;
543 template <
typename... Extra,
544 size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
545 size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
546 constexpr
bool expected_num_args(
size_t nargs,
bool has_args,
bool has_kwargs) {
547 return named == 0 || (
self + named + has_args + has_kwargs) == nargs;
550 PYBIND11_NAMESPACE_END(detail)
551 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
list bases
List of base classes of the newly created type.
bool dynamic_attr
Does the class manage a dict?
bool multiple_inheritance
Multiple inheritance marker.
Annotation which enables dynamic attributes, i.e. adds __dict__ to a class.
Annotation for parent scope.
Operator implementation generator.
Keep patient alive while nurse lives.
Annotation for documentation.
Tag for a new-style __init__ defined in detail/init.h
Type for an unused type slot.
handle value
Associated Python object.
bool is_method
True if this is a method.
Annotation for operators.
void(* free_data)(function_record *ptr)
Pointer to custom destructor for 'data' (if needed)
PyMethodDef * def
Python method object.
return_value_policy policy
Return value policy associated with this function.
The 'instance' type which needs to be standard layout (need to be able to use 'offsetof') ...
size_t type_align
What is the alignment of the underlying C++ type?
Process an attribute specifying the function's docstring (provided as a C-style string) ...
bool is_constructor
True if name == 'init'.
std::vector< argument_record > args
List of registered keyword arguments.
function_record * next
Pointer to next overload.
bool has_args
True if the function has a '*args' argument.
const char * name
Argument name.
bool is_stateless
True if this is a stateless function pointer.
size_t holder_size
How large is the type's holder?
char * signature
Human-readable version of the function signature.
Annotation that marks a class as local to the module:
handle scope
Python handle to the parent scope (a class or a module)
PyObject * ptr() const
Return the underlying PyObject * pointer.
const char * doc
Optional docstring.
Recursively iterate over variadic template arguments.
void(* init_instance)(instance *, const void *)
Function pointer to class_<..>::init_instance.
Annotation which enables the buffer protocol for a type.
bool is_new_style_constructor
True if this is a new-style __init__ defined in detail/init.h
Annotation indicating that a class is involved in a multiple inheritance relationship.
bool is_operator
True if this is an operator (add), etc.
Annotation to mark enums as an arithmetic type.
char * name
Function name.
Annotation for classes that cannot be subclassed.
Special data structure which (temporarily) holds metadata about a bound class.
bool is_final
Is the class inheritable from python classes?
handle sibling
Python handle to the sibling function representing an overload chain.
std::uint16_t nargs_pos_only
Number of leading arguments (counted in nargs) that are positional-only.
handle metaclass
Custom metaclass (optional)
void * data[3]
Storage for the wrapped function pointer and captured data, if any.
handle(* impl)(function_call &)
Pointer to lambda function which converts arguments and performs the actual call. ...
bool has_kwargs
True if the function has a '**kwargs' argument.
bool default_holder
Is the default (unique_ptr) holder type used?
Annotation for function names.
bool prepend
True if this function is to be inserted at the beginning of the overload resolution chain...
Annotation indicating that a class derives from another given type.
bool buffer_protocol
Does the class implement the buffer protocol?
const char * descr
Human-readable version of the argument value.
std::uint16_t nargs
Number of arguments (including py::args and/or py::kwargs, if present)
std::uint16_t nargs_kw_only
Number of trailing arguments (counted in nargs) that are keyword-only.
bool has_kw_only_args
True once a 'py::kw_only' is encountered (any following args are keyword-only)
Mark a function for addition at the beginning of the existing overload chain instead of the end...
Annotation indicating that a function is an overload associated with a given "sibling".
static void init(const T &, function_record *)
Default implementation: do nothing.
Internal data structure which holds metadata about a bound function (signature, overloads, etc.)
handle scope
Handle to the parent scope.
Internal data structure which holds metadata about a keyword argument.
void(* dealloc)(detail::value_and_holder &)
Function pointer to class_<..>::dealloc.
size_t type_size
How large is the underlying C++ type?
bool module_local
Is the class definition local to the module shared object?