Skip to content

Commit 18b1a01

Browse files
committed
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.
1 parent a8865ee commit 18b1a01

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

include/pybind11/cast.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ template <typename T1, typename T2> struct is_copy_constructible<std::pair<T1, T
777777
template <typename type> class type_caster_base : public type_caster_generic {
778778
using itype = intrinsic_t<type>;
779779
public:
780-
static constexpr auto name = type_descr(_<type>());
780+
static constexpr auto name = _<type>();
781781

782782
type_caster_base() : type_caster_base(typeid(type)) { }
783783
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
@@ -899,7 +899,7 @@ template <typename type> class type_caster<std::reference_wrapper<type>> {
899899
protected: \
900900
type value; \
901901
public: \
902-
static constexpr auto name = type_descr(py_name); \
902+
static constexpr auto name = py_name; \
903903
template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0> \
904904
static handle cast(T_ *src, return_value_policy policy, handle parent) { \
905905
if (!src) return none().release(); \
@@ -1048,7 +1048,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
10481048

10491049
template <typename T> using cast_op_type = void*&;
10501050
operator void *&() { return value; }
1051-
static constexpr auto name = type_descr(_("capsule"));
1051+
static constexpr auto name = _("capsule");
10521052
private:
10531053
void *value = nullptr;
10541054
};
@@ -1288,7 +1288,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
12881288
return value[0];
12891289
}
12901290

1291-
static constexpr auto name = type_descr(_(PYBIND11_STRING_NAME));
1291+
static constexpr auto name = _(PYBIND11_STRING_NAME);
12921292
template <typename _T> using cast_op_type = remove_reference_t<pybind11::detail::cast_op_type<_T>>;
12931293
};
12941294

@@ -1313,9 +1313,7 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
13131313
return cast_impl(std::forward<T>(src), policy, parent, indices{});
13141314
}
13151315

1316-
static constexpr auto name = type_descr(
1317-
_("Tuple[") + detail::concat(make_caster<Ts>::name...) + _("]")
1318-
);
1316+
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
13191317

13201318
template <typename T> using cast_op_type = type;
13211319

@@ -1822,7 +1820,7 @@ class argument_loader {
18221820
static constexpr bool has_kwargs = kwargs_pos < 0;
18231821
static constexpr bool has_args = args_pos < 0;
18241822

1825-
static constexpr auto arg_names = detail::concat(make_caster<Args>::name...);
1823+
static constexpr auto arg_names = concat(type_descr(make_caster<Args>::name)...);
18261824

18271825
bool load_args(function_call &call) {
18281826
return load_impl_sequence(call, indices{});

include/pybind11/detail/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class type_caster<value_and_holder> {
2424

2525
template <typename> using cast_op_type = value_and_holder &;
2626
operator value_and_holder &() { return *value; }
27-
static constexpr auto name = type_descr(_<value_and_holder>());
27+
static constexpr auto name = _<value_and_holder>();
2828

2929
private:
3030
value_and_holder *value = nullptr;

include/pybind11/eigen.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ template <typename Type_> struct EigenProps {
185185
static constexpr bool show_c_contiguous = show_order && requires_row_major;
186186
static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major;
187187

188-
static constexpr auto descriptor = type_descr(
188+
static constexpr auto descriptor =
189189
_("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
190190
_("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) +
191191
_(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) +
@@ -199,8 +199,7 @@ template <typename Type_> struct EigenProps {
199199
_<show_writeable>(", flags.writeable", "") +
200200
_<show_c_contiguous>(", flags.c_contiguous", "") +
201201
_<show_f_contiguous>(", flags.f_contiguous", "") +
202-
_("]")
203-
);
202+
_("]");
204203
};
205204

206205
// Casts an Eigen type to numpy array. If given a base, the numpy array references the src data,

include/pybind11/functional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct type_caster<std::function<Return(Args...)>> {
7575
return cpp_function(std::forward<Func>(f_), policy).release();
7676
}
7777

78-
PYBIND11_TYPE_CASTER(type, _("Callable[[") + argument_loader<Args...>::arg_names + _("], ")
78+
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
7979
+ make_caster<retval_type>::name + _("]"));
8080
};
8181

include/pybind11/pybind11.h

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,34 +217,30 @@ class cpp_function : public function {
217217

218218
/* Generate a proper function signature */
219219
std::string signature;
220-
size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
221-
while (true) {
222-
char c = text[char_index++];
223-
if (c == '\0')
224-
break;
220+
size_t type_index = 0, arg_index = 0;
221+
for (auto *p = text; *p != '\0'; ++p) {
222+
const auto c = *p;
225223

226224
if (c == '{') {
227-
// Write arg name for everything except *args, **kwargs and return type.
228-
if (type_depth == 0 && text[char_index] != '*' && arg_index < args) {
229-
if (!rec->args.empty() && rec->args[arg_index].name) {
230-
signature += rec->args[arg_index].name;
231-
} else if (arg_index == 0 && rec->is_method) {
232-
signature += "self";
233-
} else {
234-
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
235-
}
236-
signature += ": ";
225+
// Write arg name for everything except *args and **kwargs.
226+
if (*(p + 1) == '*')
227+
continue;
228+
229+
if (arg_index < rec->args.size() && rec->args[arg_index].name) {
230+
signature += rec->args[arg_index].name;
231+
} else if (arg_index == 0 && rec->is_method) {
232+
signature += "self";
233+
} else {
234+
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
237235
}
238-
++type_depth;
236+
signature += ": ";
239237
} else if (c == '}') {
240-
--type_depth;
241-
if (type_depth == 0) {
242-
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
243-
signature += "=";
244-
signature += rec->args[arg_index].descr;
245-
}
246-
arg_index++;
238+
// Write default value if available.
239+
if (arg_index < rec->args.size() && rec->args[arg_index].descr) {
240+
signature += "=";
241+
signature += rec->args[arg_index].descr;
247242
}
243+
arg_index++;
248244
} else if (c == '%') {
249245
const std::type_info *t = types[type_index++];
250246
if (!t)
@@ -272,7 +268,7 @@ class cpp_function : public function {
272268
signature += c;
273269
}
274270
}
275-
if (type_depth != 0 || types[type_index] != nullptr)
271+
if (arg_index != args || types[type_index] != nullptr)
276272
pybind11_fail("Internal error while parsing type signature (2)");
277273

278274
#if PY_MAJOR_VERSION < 3

0 commit comments

Comments
 (0)