mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-26 08:01:48 +00:00
Merge branch 'master' into sh_merge_master
This commit is contained in:
@@ -174,7 +174,7 @@ struct function_record {
|
||||
function_record()
|
||||
: is_constructor(false), is_new_style_constructor(false), is_stateless(false),
|
||||
is_operator(false), is_method(false), has_args(false),
|
||||
has_kwargs(false), has_kw_only_args(false), prepend(false) { }
|
||||
has_kwargs(false), prepend(false) { }
|
||||
|
||||
/// Function name
|
||||
char *name = nullptr; /* why no C++ strings? They generate heavier code.. */
|
||||
@@ -221,17 +221,15 @@ struct function_record {
|
||||
/// True if the function has a '**kwargs' argument
|
||||
bool has_kwargs : 1;
|
||||
|
||||
/// True once a 'py::kw_only' is encountered (any following args are keyword-only)
|
||||
bool has_kw_only_args : 1;
|
||||
|
||||
/// True if this function is to be inserted at the beginning of the overload resolution chain
|
||||
bool prepend : 1;
|
||||
|
||||
/// Number of arguments (including py::args and/or py::kwargs, if present)
|
||||
std::uint16_t nargs;
|
||||
|
||||
/// Number of trailing arguments (counted in `nargs`) that are keyword-only
|
||||
std::uint16_t nargs_kw_only = 0;
|
||||
/// Number of leading positional arguments, which are terminated by a py::args or py::kwargs
|
||||
/// argument or by a py::kw_only annotation.
|
||||
std::uint16_t nargs_pos = 0;
|
||||
|
||||
/// Number of leading arguments (counted in `nargs`) that are positional-only
|
||||
std::uint16_t nargs_pos_only = 0;
|
||||
@@ -411,10 +409,9 @@ template <> struct process_attribute<is_new_style_constructor> : process_attribu
|
||||
static void init(const is_new_style_constructor &, function_record *r) { r->is_new_style_constructor = true; }
|
||||
};
|
||||
|
||||
inline void process_kw_only_arg(const arg &a, function_record *r) {
|
||||
if (!a.name || a.name[0] == '\0')
|
||||
pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation");
|
||||
++r->nargs_kw_only;
|
||||
inline void check_kw_only_arg(const arg &a, function_record *r) {
|
||||
if (r->args.size() > r->nargs_pos && (!a.name || a.name[0] == '\0'))
|
||||
pybind11_fail("arg(): cannot specify an unnamed argument after a kw_only() annotation or args() argument");
|
||||
}
|
||||
|
||||
/// Process a keyword argument attribute (*without* a default value)
|
||||
@@ -424,7 +421,7 @@ template <> struct process_attribute<arg> : process_attribute_default<arg> {
|
||||
r->args.emplace_back("self", nullptr, handle(), true /*convert*/, false /*none not allowed*/);
|
||||
r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
|
||||
|
||||
if (r->has_kw_only_args) process_kw_only_arg(a, r);
|
||||
check_kw_only_arg(a, r);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -457,14 +454,16 @@ template <> struct process_attribute<arg_v> : process_attribute_default<arg_v> {
|
||||
}
|
||||
r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
|
||||
|
||||
if (r->has_kw_only_args) process_kw_only_arg(a, r);
|
||||
check_kw_only_arg(a, r);
|
||||
}
|
||||
};
|
||||
|
||||
/// Process a keyword-only-arguments-follow pseudo argument
|
||||
template <> struct process_attribute<kw_only> : process_attribute_default<kw_only> {
|
||||
static void init(const kw_only &, function_record *r) {
|
||||
r->has_kw_only_args = true;
|
||||
if (r->has_args && r->nargs_pos != static_cast<std::uint16_t>(r->args.size()))
|
||||
pybind11_fail("Mismatched args() and kw_only(): they must occur at the same relative argument location (or omit kw_only() entirely)");
|
||||
r->nargs_pos = static_cast<std::uint16_t>(r->args.size());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -472,6 +471,9 @@ template <> struct process_attribute<kw_only> : process_attribute_default<kw_onl
|
||||
template <> struct process_attribute<pos_only> : process_attribute_default<pos_only> {
|
||||
static void init(const pos_only &, function_record *r) {
|
||||
r->nargs_pos_only = static_cast<std::uint16_t>(r->args.size());
|
||||
if (r->nargs_pos_only > r->nargs_pos)
|
||||
pybind11_fail("pos_only(): cannot follow a py::args() argument");
|
||||
// It also can't follow a kw_only, but a static_assert in pybind11.h checks that
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1159,6 +1159,9 @@ constexpr arg operator"" _a(const char *name, size_t) { return arg(name); }
|
||||
|
||||
PYBIND11_NAMESPACE_BEGIN(detail)
|
||||
|
||||
template <typename T> using is_kw_only = std::is_same<intrinsic_t<T>, kw_only>;
|
||||
template <typename T> using is_pos_only = std::is_same<intrinsic_t<T>, pos_only>;
|
||||
|
||||
// forward declaration (definition in attr.h)
|
||||
struct function_record;
|
||||
|
||||
@@ -1194,17 +1197,18 @@ class argument_loader {
|
||||
|
||||
template <typename Arg> using argument_is_args = std::is_same<intrinsic_t<Arg>, args>;
|
||||
template <typename Arg> using argument_is_kwargs = std::is_same<intrinsic_t<Arg>, kwargs>;
|
||||
// Get args/kwargs argument positions relative to the end of the argument list:
|
||||
static constexpr auto args_pos = constexpr_first<argument_is_args, Args...>() - (int) sizeof...(Args),
|
||||
kwargs_pos = constexpr_first<argument_is_kwargs, Args...>() - (int) sizeof...(Args);
|
||||
// Get kwargs argument position, or -1 if not present:
|
||||
static constexpr auto kwargs_pos = constexpr_last<argument_is_kwargs, Args...>();
|
||||
|
||||
static constexpr bool args_kwargs_are_last = kwargs_pos >= - 1 && args_pos >= kwargs_pos - 1;
|
||||
|
||||
static_assert(args_kwargs_are_last, "py::args/py::kwargs are only permitted as the last argument(s) of a function");
|
||||
static_assert(kwargs_pos == -1 || kwargs_pos == (int) sizeof...(Args) - 1, "py::kwargs is only permitted as the last argument of a function");
|
||||
|
||||
public:
|
||||
static constexpr bool has_kwargs = kwargs_pos < 0;
|
||||
static constexpr bool has_args = args_pos < 0;
|
||||
static constexpr bool has_kwargs = kwargs_pos != -1;
|
||||
|
||||
// py::args argument position; -1 if not present.
|
||||
static constexpr int args_pos = constexpr_last<argument_is_args, Args...>();
|
||||
|
||||
static_assert(args_pos == -1 || args_pos == constexpr_first<argument_is_args, Args...>(), "py::args cannot be specified more than once");
|
||||
|
||||
static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
template <typename Return, typename Class, typename... Arg, typename... Extra>
|
||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||
cpp_function(Return (Class::*f)(Arg...)&, const Extra&... extra) {
|
||||
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
|
||||
initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
|
||||
(Return (*) (Class *, Arg...)) nullptr, extra...);
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public:
|
||||
template <typename Return, typename Class, typename... Arg, typename... Extra>
|
||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||
cpp_function(Return (Class::*f)(Arg...) const&, const Extra&... extra) {
|
||||
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
|
||||
initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(std::forward<Arg>(args)...); },
|
||||
(Return (*)(const Class *, Arg ...)) nullptr, extra...);
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ protected:
|
||||
conditional_t<std::is_void<Return>::value, void_type, Return>
|
||||
>;
|
||||
|
||||
static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
|
||||
static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
|
||||
"The number of argument annotations does not match the number of function arguments");
|
||||
|
||||
/* Dispatch code which converts function arguments and performs the actual function call */
|
||||
@@ -240,17 +240,27 @@ protected:
|
||||
return result;
|
||||
};
|
||||
|
||||
rec->nargs_pos = cast_in::args_pos >= 0
|
||||
? static_cast<std::uint16_t>(cast_in::args_pos)
|
||||
: sizeof...(Args) - cast_in::has_kwargs; // Will get reduced more if we have a kw_only
|
||||
rec->has_args = cast_in::args_pos >= 0;
|
||||
rec->has_kwargs = cast_in::has_kwargs;
|
||||
|
||||
/* Process any user-provided function attributes */
|
||||
process_attributes<Extra...>::init(extra..., rec);
|
||||
|
||||
{
|
||||
constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value,
|
||||
has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value,
|
||||
has_args = any_of<std::is_same<args, Args>...>::value,
|
||||
has_arg_annotations = any_of<is_keyword<Extra>...>::value;
|
||||
static_assert(has_arg_annotations || !has_kw_only_args, "py::kw_only requires the use of argument annotations");
|
||||
static_assert(has_arg_annotations || !has_pos_only_args, "py::pos_only requires the use of argument annotations (for docstrings and aligning the annotations to the argument)");
|
||||
static_assert(!(has_args && has_kw_only_args), "py::kw_only cannot be combined with a py::args argument");
|
||||
|
||||
static_assert(constexpr_sum(is_kw_only<Extra>::value...) <= 1, "py::kw_only may be specified only once");
|
||||
static_assert(constexpr_sum(is_pos_only<Extra>::value...) <= 1, "py::pos_only may be specified only once");
|
||||
constexpr auto kw_only_pos = constexpr_first<is_kw_only, Extra...>();
|
||||
constexpr auto pos_only_pos = constexpr_first<is_pos_only, Extra...>();
|
||||
static_assert(!(has_kw_only_args && has_pos_only_args) || pos_only_pos < kw_only_pos, "py::pos_only must come before py::kw_only");
|
||||
}
|
||||
|
||||
/* Generate a readable signature describing the function's arguments and return value types */
|
||||
@@ -261,9 +271,6 @@ protected:
|
||||
// Pass on the ownership over the `unique_rec` to `initialize_generic`. `rec` stays valid.
|
||||
initialize_generic(std::move(unique_rec), signature.text, types.data(), sizeof...(Args));
|
||||
|
||||
if (cast_in::has_args) rec->has_args = true;
|
||||
if (cast_in::has_kwargs) rec->has_kwargs = true;
|
||||
|
||||
/* Stash some additional information used by an important optimization in 'functional.h' */
|
||||
using FunctionType = Return (*)(Args...);
|
||||
constexpr bool is_function_ptr =
|
||||
@@ -342,16 +349,18 @@ protected:
|
||||
/* Generate a proper function signature */
|
||||
std::string signature;
|
||||
size_t type_index = 0, arg_index = 0;
|
||||
bool is_starred = false;
|
||||
for (auto *pc = text; *pc != '\0'; ++pc) {
|
||||
const auto c = *pc;
|
||||
|
||||
if (c == '{') {
|
||||
// Write arg name for everything except *args and **kwargs.
|
||||
if (*(pc + 1) == '*')
|
||||
is_starred = *(pc + 1) == '*';
|
||||
if (is_starred)
|
||||
continue;
|
||||
// Separator for keyword-only arguments, placed before the kw
|
||||
// arguments start
|
||||
if (rec->nargs_kw_only > 0 && arg_index + rec->nargs_kw_only == args)
|
||||
// arguments start (unless we are already putting an *args)
|
||||
if (!rec->has_args && arg_index == rec->nargs_pos)
|
||||
signature += "*, ";
|
||||
if (arg_index < rec->args.size() && rec->args[arg_index].name) {
|
||||
signature += rec->args[arg_index].name;
|
||||
@@ -363,7 +372,7 @@ protected:
|
||||
signature += ": ";
|
||||
} else if (c == '}') {
|
||||
// Write default value if available.
|
||||
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
|
||||
if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) {
|
||||
signature += " = ";
|
||||
signature += rec->args[arg_index].descr;
|
||||
}
|
||||
@@ -371,7 +380,8 @@ protected:
|
||||
// argument, rather than before like *
|
||||
if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only)
|
||||
signature += ", /";
|
||||
arg_index++;
|
||||
if (!is_starred)
|
||||
arg_index++;
|
||||
} else if (c == '%') {
|
||||
const std::type_info *t = types[type_index++];
|
||||
if (!t)
|
||||
@@ -397,7 +407,7 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
if (arg_index != args || types[type_index] != nullptr)
|
||||
if (arg_index != args - rec->has_args - rec->has_kwargs || types[type_index] != nullptr)
|
||||
pybind11_fail("Internal error while parsing type signature (2)");
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
@@ -633,7 +643,7 @@ protected:
|
||||
named positional arguments weren't *also* specified via kwarg.
|
||||
2. If we weren't given enough, try to make up the omitted ones by checking
|
||||
whether they were provided by a kwarg matching the `py::arg("name")` name. If
|
||||
so, use it (and remove it from kwargs; if not, see if the function binding
|
||||
so, use it (and remove it from kwargs); if not, see if the function binding
|
||||
provided a default that we can use.
|
||||
3. Ensure that either all keyword arguments were "consumed", or that the function
|
||||
takes a kwargs argument to accept unconsumed kwargs.
|
||||
@@ -651,7 +661,7 @@ protected:
|
||||
size_t num_args = func.nargs; // Number of positional arguments that we need
|
||||
if (func.has_args) --num_args; // (but don't count py::args
|
||||
if (func.has_kwargs) --num_args; // or py::kwargs)
|
||||
size_t pos_args = num_args - func.nargs_kw_only;
|
||||
size_t pos_args = func.nargs_pos;
|
||||
|
||||
if (!func.has_args && n_args_in > pos_args)
|
||||
continue; // Too many positional arguments for this overload
|
||||
@@ -697,6 +707,10 @@ protected:
|
||||
if (bad_arg)
|
||||
continue; // Maybe it was meant for another overload (issue #688)
|
||||
|
||||
// Keep track of how many position args we copied out in case we need to come back
|
||||
// to copy the rest into a py::args argument.
|
||||
size_t positional_args_copied = args_copied;
|
||||
|
||||
// We'll need to copy this if we steal some kwargs for defaults
|
||||
dict kwargs = reinterpret_borrow<dict>(kwargs_in);
|
||||
|
||||
@@ -749,6 +763,10 @@ protected:
|
||||
}
|
||||
|
||||
if (value) {
|
||||
// If we're at the py::args index then first insert a stub for it to be replaced later
|
||||
if (func.has_args && call.args.size() == func.nargs_pos)
|
||||
call.args.push_back(none());
|
||||
|
||||
call.args.push_back(value);
|
||||
call.args_convert.push_back(arg_rec.convert);
|
||||
}
|
||||
@@ -771,16 +789,19 @@ protected:
|
||||
// We didn't copy out any position arguments from the args_in tuple, so we
|
||||
// can reuse it directly without copying:
|
||||
extra_args = reinterpret_borrow<tuple>(args_in);
|
||||
} else if (args_copied >= n_args_in) {
|
||||
} else if (positional_args_copied >= n_args_in) {
|
||||
extra_args = tuple(0);
|
||||
} else {
|
||||
size_t args_size = n_args_in - args_copied;
|
||||
size_t args_size = n_args_in - positional_args_copied;
|
||||
extra_args = tuple(args_size);
|
||||
for (size_t i = 0; i < args_size; ++i) {
|
||||
extra_args[i] = PyTuple_GET_ITEM(args_in, args_copied + i);
|
||||
extra_args[i] = PyTuple_GET_ITEM(args_in, positional_args_copied + i);
|
||||
}
|
||||
}
|
||||
call.args.push_back(extra_args);
|
||||
if (call.args.size() <= func.nargs_pos)
|
||||
call.args.push_back(extra_args);
|
||||
else
|
||||
call.args[func.nargs_pos] = extra_args;
|
||||
call.args_convert.push_back(false);
|
||||
call.args_ref = std::move(extra_args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user