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:
Dean Moldovan 2017-08-31 14:38:23 +02:00
parent 56613945ae
commit 0aef6422a3
5 changed files with 30 additions and 37 deletions

View File

@ -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 { template <typename type> class type_caster_base : public type_caster_generic {
using itype = intrinsic_t<type>; using itype = intrinsic_t<type>;
public: public:
static constexpr auto name = type_descr(_<type>()); static constexpr auto name = _<type>();
type_caster_base() : type_caster_base(typeid(type)) { } type_caster_base() : type_caster_base(typeid(type)) { }
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { } explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
@ -900,7 +900,7 @@ public:
protected: \ protected: \
type value; \ type value; \
public: \ 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> \ 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) { \ static handle cast(T_ *src, return_value_policy policy, handle parent) { \
if (!src) return none().release(); \ if (!src) return none().release(); \
@ -1049,7 +1049,7 @@ public:
template <typename T> using cast_op_type = void*&; template <typename T> using cast_op_type = void*&;
operator void *&() { return value; } operator void *&() { return value; }
static constexpr auto name = type_descr(_("capsule")); static constexpr auto name = _("capsule");
private: private:
void *value = nullptr; void *value = nullptr;
}; };
@ -1289,7 +1289,7 @@ public:
return value[0]; 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>>; 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{}); return cast_impl(std::forward<T>(src), policy, parent, indices{});
} }
static constexpr auto name = type_descr( static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
_("Tuple[") + detail::concat(make_caster<Ts>::name...) + _("]")
);
template <typename T> using cast_op_type = type; 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_kwargs = kwargs_pos < 0;
static constexpr bool has_args = args_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) { bool load_args(function_call &call) {
return load_impl_sequence(call, indices{}); return load_impl_sequence(call, indices{});

View File

@ -24,7 +24,7 @@ public:
template <typename> using cast_op_type = value_and_holder &; template <typename> using cast_op_type = value_and_holder &;
operator value_and_holder &() { return *value; } operator value_and_holder &() { return *value; }
static constexpr auto name = type_descr(_<value_and_holder>()); static constexpr auto name = _<value_and_holder>();
private: private:
value_and_holder *value = nullptr; value_and_holder *value = nullptr;

View File

@ -185,7 +185,7 @@ template <typename Type_> struct EigenProps {
static constexpr bool show_c_contiguous = show_order && requires_row_major; 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 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 + _("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
_("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) + _("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) +
_(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) + _(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) +
@ -199,8 +199,7 @@ template <typename Type_> struct EigenProps {
_<show_writeable>(", flags.writeable", "") + _<show_writeable>(", flags.writeable", "") +
_<show_c_contiguous>(", flags.c_contiguous", "") + _<show_c_contiguous>(", flags.c_contiguous", "") +
_<show_f_contiguous>(", flags.f_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, // Casts an Eigen type to numpy array. If given a base, the numpy array references the src data,

View File

@ -75,7 +75,7 @@ public:
return cpp_function(std::forward<Func>(f_), policy).release(); 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 + _("]")); + make_caster<retval_type>::name + _("]"));
}; };

View File

@ -217,16 +217,16 @@ protected:
/* Generate a proper function signature */ /* Generate a proper function signature */
std::string signature; std::string signature;
size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0; size_t type_index = 0, arg_index = 0;
while (true) { for (auto *pc = text; *pc != '\0'; ++pc) {
char c = text[char_index++]; const auto c = *pc;
if (c == '\0')
break;
if (c == '{') { if (c == '{') {
// Write arg name for everything except *args, **kwargs and return type. // Write arg name for everything except *args and **kwargs.
if (type_depth == 0 && text[char_index] != '*' && arg_index < args) { if (*(pc + 1) == '*')
if (!rec->args.empty() && rec->args[arg_index].name) { continue;
if (arg_index < rec->args.size() && rec->args[arg_index].name) {
signature += rec->args[arg_index].name; signature += rec->args[arg_index].name;
} else if (arg_index == 0 && rec->is_method) { } else if (arg_index == 0 && rec->is_method) {
signature += "self"; signature += "self";
@ -234,17 +234,13 @@ protected:
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0)); signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
} }
signature += ": "; signature += ": ";
}
++type_depth;
} else if (c == '}') { } else if (c == '}') {
--type_depth; // Write default value if available.
if (type_depth == 0) {
if (arg_index < rec->args.size() && rec->args[arg_index].descr) { if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
signature += "="; signature += "=";
signature += rec->args[arg_index].descr; signature += rec->args[arg_index].descr;
} }
arg_index++; arg_index++;
}
} else if (c == '%') { } else if (c == '%') {
const std::type_info *t = types[type_index++]; const std::type_info *t = types[type_index++];
if (!t) if (!t)
@ -272,7 +268,7 @@ protected:
signature += c; 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)"); pybind11_fail("Internal error while parsing type signature (2)");
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3