mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
Simplify function signature annotation and parsing
`type_descr` is now applied only to the final signature so that it only marks the argument types, but not nested types (e.g. for tuples) or return types.
This commit is contained in:
parent
56613945ae
commit
0aef6422a3
@ -778,7 +778,7 @@ template <typename T1, typename T2> struct is_copy_constructible<std::pair<T1, T
|
||||
template <typename type> class type_caster_base : public type_caster_generic {
|
||||
using itype = intrinsic_t<type>;
|
||||
public:
|
||||
static constexpr auto name = type_descr(_<type>());
|
||||
static constexpr auto name = _<type>();
|
||||
|
||||
type_caster_base() : type_caster_base(typeid(type)) { }
|
||||
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
|
||||
@ -900,7 +900,7 @@ public:
|
||||
protected: \
|
||||
type value; \
|
||||
public: \
|
||||
static constexpr auto name = type_descr(py_name); \
|
||||
static constexpr auto name = py_name; \
|
||||
template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0> \
|
||||
static handle cast(T_ *src, return_value_policy policy, handle parent) { \
|
||||
if (!src) return none().release(); \
|
||||
@ -1049,7 +1049,7 @@ public:
|
||||
|
||||
template <typename T> using cast_op_type = void*&;
|
||||
operator void *&() { return value; }
|
||||
static constexpr auto name = type_descr(_("capsule"));
|
||||
static constexpr auto name = _("capsule");
|
||||
private:
|
||||
void *value = nullptr;
|
||||
};
|
||||
@ -1289,7 +1289,7 @@ public:
|
||||
return value[0];
|
||||
}
|
||||
|
||||
static constexpr auto name = type_descr(_(PYBIND11_STRING_NAME));
|
||||
static constexpr auto name = _(PYBIND11_STRING_NAME);
|
||||
template <typename _T> using cast_op_type = remove_reference_t<pybind11::detail::cast_op_type<_T>>;
|
||||
};
|
||||
|
||||
@ -1314,9 +1314,7 @@ public:
|
||||
return cast_impl(std::forward<T>(src), policy, parent, indices{});
|
||||
}
|
||||
|
||||
static constexpr auto name = type_descr(
|
||||
_("Tuple[") + detail::concat(make_caster<Ts>::name...) + _("]")
|
||||
);
|
||||
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
|
||||
|
||||
template <typename T> using cast_op_type = type;
|
||||
|
||||
@ -1826,7 +1824,7 @@ public:
|
||||
static constexpr bool has_kwargs = kwargs_pos < 0;
|
||||
static constexpr bool has_args = args_pos < 0;
|
||||
|
||||
static constexpr auto arg_names = detail::concat(make_caster<Args>::name...);
|
||||
static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
|
||||
|
||||
bool load_args(function_call &call) {
|
||||
return load_impl_sequence(call, indices{});
|
||||
|
@ -24,7 +24,7 @@ public:
|
||||
|
||||
template <typename> using cast_op_type = value_and_holder &;
|
||||
operator value_and_holder &() { return *value; }
|
||||
static constexpr auto name = type_descr(_<value_and_holder>());
|
||||
static constexpr auto name = _<value_and_holder>();
|
||||
|
||||
private:
|
||||
value_and_holder *value = nullptr;
|
||||
|
@ -185,7 +185,7 @@ template <typename Type_> struct EigenProps {
|
||||
static constexpr bool show_c_contiguous = show_order && requires_row_major;
|
||||
static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major;
|
||||
|
||||
static constexpr auto descriptor = type_descr(
|
||||
static constexpr auto descriptor =
|
||||
_("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
|
||||
_("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) +
|
||||
_(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) +
|
||||
@ -199,8 +199,7 @@ template <typename Type_> struct EigenProps {
|
||||
_<show_writeable>(", flags.writeable", "") +
|
||||
_<show_c_contiguous>(", flags.c_contiguous", "") +
|
||||
_<show_f_contiguous>(", flags.f_contiguous", "") +
|
||||
_("]")
|
||||
);
|
||||
_("]");
|
||||
};
|
||||
|
||||
// Casts an Eigen type to numpy array. If given a base, the numpy array references the src data,
|
||||
|
@ -75,7 +75,7 @@ public:
|
||||
return cpp_function(std::forward<Func>(f_), policy).release();
|
||||
}
|
||||
|
||||
PYBIND11_TYPE_CASTER(type, _("Callable[[") + argument_loader<Args...>::arg_names + _("], ")
|
||||
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
|
||||
+ make_caster<retval_type>::name + _("]"));
|
||||
};
|
||||
|
||||
|
@ -217,34 +217,30 @@ protected:
|
||||
|
||||
/* Generate a proper function signature */
|
||||
std::string signature;
|
||||
size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
|
||||
while (true) {
|
||||
char c = text[char_index++];
|
||||
if (c == '\0')
|
||||
break;
|
||||
size_t type_index = 0, arg_index = 0;
|
||||
for (auto *pc = text; *pc != '\0'; ++pc) {
|
||||
const auto c = *pc;
|
||||
|
||||
if (c == '{') {
|
||||
// Write arg name for everything except *args, **kwargs and return type.
|
||||
if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
|
||||
if (!rec->args.empty() && rec->args[arg_index].name) {
|
||||
signature += rec->args[arg_index].name;
|
||||
} else if (arg_index == 0 && rec->is_method) {
|
||||
signature += "self";
|
||||
} else {
|
||||
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
|
||||
}
|
||||
signature += ": ";
|
||||
// Write arg name for everything except *args and **kwargs.
|
||||
if (*(pc + 1) == '*')
|
||||
continue;
|
||||
|
||||
if (arg_index < rec->args.size() && rec->args[arg_index].name) {
|
||||
signature += rec->args[arg_index].name;
|
||||
} else if (arg_index == 0 && rec->is_method) {
|
||||
signature += "self";
|
||||
} else {
|
||||
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
|
||||
}
|
||||
++type_depth;
|
||||
signature += ": ";
|
||||
} else if (c == '}') {
|
||||
--type_depth;
|
||||
if (type_depth == 0) {
|
||||
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
|
||||
signature += "=";
|
||||
signature += rec->args[arg_index].descr;
|
||||
}
|
||||
arg_index++;
|
||||
// Write default value if available.
|
||||
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
|
||||
signature += "=";
|
||||
signature += rec->args[arg_index].descr;
|
||||
}
|
||||
arg_index++;
|
||||
} else if (c == '%') {
|
||||
const std::type_info *t = types[type_index++];
|
||||
if (!t)
|
||||
@ -272,7 +268,7 @@ protected:
|
||||
signature += c;
|
||||
}
|
||||
}
|
||||
if (type_depth != 0 || types[type_index] != nullptr)
|
||||
if (arg_index != args || types[type_index] != nullptr)
|
||||
pybind11_fail("Internal error while parsing type signature (2)");
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
|
Loading…
Reference in New Issue
Block a user