Skip to content

Commit 40e0b9a

Browse files
committed
fix: avoid usage of _
1 parent 68f0ff7 commit 40e0b9a

17 files changed

+81
-74
lines changed

include/pybind11/cast.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
224224
return PyLong_FromUnsignedLongLong((unsigned long long) src);
225225
}
226226

227-
PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
227+
PYBIND11_TYPE_CASTER(T, const_str<std::is_integral<T>::value>("int", "float"));
228228
};
229229

230230
template<typename T> struct void_caster {
@@ -237,7 +237,7 @@ template<typename T> struct void_caster {
237237
static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
238238
return none().inc_ref();
239239
}
240-
PYBIND11_TYPE_CASTER(T, _("None"));
240+
PYBIND11_TYPE_CASTER(T, const_str("None"));
241241
};
242242

243243
template <> class type_caster<void_type> : public void_caster<void_type> {};
@@ -280,7 +280,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
280280

281281
template <typename T> using cast_op_type = void*&;
282282
explicit operator void *&() { return value; }
283-
static constexpr auto name = _("capsule");
283+
static constexpr auto name = const_str("capsule");
284284
private:
285285
void *value = nullptr;
286286
};
@@ -331,7 +331,7 @@ template <> class type_caster<bool> {
331331
static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
332332
return handle(src ? Py_True : Py_False).inc_ref();
333333
}
334-
PYBIND11_TYPE_CASTER(bool, _("bool"));
334+
PYBIND11_TYPE_CASTER(bool, const_str("bool"));
335335
};
336336

337337
// Helper class for UTF-{8,16,32} C++ stl strings:
@@ -421,7 +421,7 @@ template <typename StringType, bool IsView = false> struct string_caster {
421421
return s;
422422
}
423423

424-
PYBIND11_TYPE_CASTER(StringType, _(PYBIND11_STRING_NAME));
424+
PYBIND11_TYPE_CASTER(StringType, const_str(PYBIND11_STRING_NAME));
425425

426426
private:
427427
static handle decode_utfN(const char *buffer, ssize_t nbytes) {
@@ -558,7 +558,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
558558
return one_char;
559559
}
560560

561-
static constexpr auto name = _(PYBIND11_STRING_NAME);
561+
static constexpr auto name = const_str(PYBIND11_STRING_NAME);
562562
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
563563
};
564564

@@ -595,7 +595,7 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
595595
return cast(*src, policy, parent);
596596
}
597597

598-
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
598+
static constexpr auto name = const_str("Tuple[") + concat(make_caster<Ts>::name...) + const_str("]");
599599

600600
template <typename T> using cast_op_type = type;
601601

@@ -780,14 +780,14 @@ template <typename base, typename holder> struct is_holder_type :
780780
template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
781781
std::true_type {};
782782

783-
template <typename T> struct handle_type_name { static constexpr auto name = _<T>(); };
784-
template <> struct handle_type_name<bytes> { static constexpr auto name = _(PYBIND11_BYTES_NAME); };
785-
template <> struct handle_type_name<int_> { static constexpr auto name = _("int"); };
786-
template <> struct handle_type_name<iterable> { static constexpr auto name = _("Iterable"); };
787-
template <> struct handle_type_name<iterator> { static constexpr auto name = _("Iterator"); };
788-
template <> struct handle_type_name<none> { static constexpr auto name = _("None"); };
789-
template <> struct handle_type_name<args> { static constexpr auto name = _("*args"); };
790-
template <> struct handle_type_name<kwargs> { static constexpr auto name = _("**kwargs"); };
783+
template <typename T> struct handle_type_name { static constexpr auto name = const_str<T>(); };
784+
template <> struct handle_type_name<bytes> { static constexpr auto name = const_str(PYBIND11_BYTES_NAME); };
785+
template <> struct handle_type_name<int_> { static constexpr auto name = const_str("int"); };
786+
template <> struct handle_type_name<iterable> { static constexpr auto name = const_str("Iterable"); };
787+
template <> struct handle_type_name<iterator> { static constexpr auto name = const_str("Iterator"); };
788+
template <> struct handle_type_name<none> { static constexpr auto name = const_str("None"); };
789+
template <> struct handle_type_name<args> { static constexpr auto name = const_str("*args"); };
790+
template <> struct handle_type_name<kwargs> { static constexpr auto name = const_str("**kwargs"); };
791791

792792
template <typename type>
793793
struct pyobject_caster {

include/pybind11/chrono.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ template <typename type> class duration_caster {
9797
return PyDelta_FromDSU(dd.count(), ss.count(), us.count());
9898
}
9999

100-
PYBIND11_TYPE_CASTER(type, _("datetime.timedelta"));
100+
PYBIND11_TYPE_CASTER(type, const_str("datetime.timedelta"));
101101
};
102102

103103
inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) {
@@ -195,7 +195,7 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
195195
localtime.tm_sec,
196196
us.count());
197197
}
198-
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
198+
PYBIND11_TYPE_CASTER(type, const_str("datetime.datetime"));
199199
};
200200

201201
// Other clocks that are not the system clock are not measured as datetime.datetime objects

include/pybind11/complex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ template <typename T> class type_caster<std::complex<T>> {
5959
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
6060
}
6161

62-
PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
62+
PYBIND11_TYPE_CASTER(std::complex<T>, const_str("complex"));
6363
};
6464
PYBIND11_NAMESPACE_END(detail)
6565
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/detail/descr.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a, c
5353
return plus_impl(a, b, make_index_sequence<N1>(), make_index_sequence<N2>());
5454
}
5555

56+
template <size_t N>
57+
constexpr descr<N - 1> const_str(char const(&text)[N]) { return descr<N - 1>(text); }
58+
constexpr descr<0> const_str(char const(&)[1]) { return {}; }
59+
60+
// The "_" might be defined as a macro - don't define it if so.
61+
#ifndef _
5662
template <size_t N>
5763
constexpr descr<N - 1> _(char const(&text)[N]) { return descr<N - 1>(text); }
5864
constexpr descr<0> _(char const(&)[1]) { return {}; }
65+
#endif
5966

6067
template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
6168
template <size_t...Digits> struct int_to_str<0, Digits...> {
@@ -64,25 +71,25 @@ template <size_t...Digits> struct int_to_str<0, Digits...> {
6471

6572
// Ternary description (like std::conditional)
6673
template <bool B, size_t N1, size_t N2>
67-
constexpr enable_if_t<B, descr<N1 - 1>> _(char const(&text1)[N1], char const(&)[N2]) {
68-
return _(text1);
74+
constexpr enable_if_t<B, descr<N1 - 1>> const_str(char const(&text1)[N1], char const(&)[N2]) {
75+
return const_str(text1);
6976
}
7077
template <bool B, size_t N1, size_t N2>
71-
constexpr enable_if_t<!B, descr<N2 - 1>> _(char const(&)[N1], char const(&text2)[N2]) {
72-
return _(text2);
78+
constexpr enable_if_t<!B, descr<N2 - 1>> const_str(char const(&)[N1], char const(&text2)[N2]) {
79+
return const_str(text2);
7380
}
7481

7582
template <bool B, typename T1, typename T2>
76-
constexpr enable_if_t<B, T1> _(const T1 &d, const T2 &) { return d; }
83+
constexpr enable_if_t<B, T1> const_str(const T1 &d, const T2 &) { return d; }
7784
template <bool B, typename T1, typename T2>
78-
constexpr enable_if_t<!B, T2> _(const T1 &, const T2 &d) { return d; }
85+
constexpr enable_if_t<!B, T2> const_str(const T1 &, const T2 &d) { return d; }
7986

8087
template <size_t Size>
81-
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
88+
auto constexpr const_str() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
8289
return int_to_str<Size / 10, Size % 10>::digits;
8390
}
8491

85-
template <typename Type> constexpr descr<1, Type> _() { return {'%'}; }
92+
template <typename Type> constexpr descr<1, Type> const_str() { return {'%'}; }
8693

8794
constexpr descr<0> concat() { return {}; }
8895

@@ -92,12 +99,12 @@ constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) { return descr; }
9299
template <size_t N, typename... Ts, typename... Args>
93100
constexpr auto concat(const descr<N, Ts...> &d, const Args &...args)
94101
-> decltype(std::declval<descr<N + 2, Ts...>>() + concat(args...)) {
95-
return d + _(", ") + concat(args...);
102+
return d + const_str(", ") + concat(args...);
96103
}
97104

98105
template <size_t N, typename... Ts>
99106
constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
100-
return _("{") + descr + _("}");
107+
return const_str("{") + descr + const_str("}");
101108
}
102109

103110
PYBIND11_NAMESPACE_END(detail)

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
explicit operator value_and_holder &() { return *value; }
27-
static constexpr auto name = _<value_and_holder>();
27+
static constexpr auto name = const_str<value_and_holder>();
2828

2929
private:
3030
value_and_holder *value = nullptr;

include/pybind11/detail/type_caster_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
897897
using itype = intrinsic_t<type>;
898898

899899
public:
900-
static constexpr auto name = _<type>();
900+
static constexpr auto name = const_str<type>();
901901

902902
type_caster_base() : type_caster_base(typeid(type)) { }
903903
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }

include/pybind11/eigen.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,20 @@ template <typename Type_> struct EigenProps {
190190
static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major;
191191

192192
static constexpr auto descriptor =
193-
_("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
194-
_("[") + _<fixed_rows>(_<(size_t) rows>(), _("m")) +
195-
_(", ") + _<fixed_cols>(_<(size_t) cols>(), _("n")) +
196-
_("]") +
193+
const_str("numpy.ndarray[") + npy_format_descriptor<Scalar>::name +
194+
const_str("[") + const_str<fixed_rows>(const_str<(size_t) rows>(), const_str("m")) +
195+
const_str(", ") + const_str<fixed_cols>(const_str<(size_t) cols>(), const_str("n")) +
196+
const_str("]") +
197197
// For a reference type (e.g. Ref<MatrixXd>) we have other constraints that might need to be
198198
// satisfied: writeable=True (for a mutable reference), and, depending on the map's stride
199199
// options, possibly f_contiguous or c_contiguous. We include them in the descriptor output
200200
// to provide some hint as to why a TypeError is occurring (otherwise it can be confusing to
201201
// see that a function accepts a 'numpy.ndarray[float64[3,2]]' and an error message that you
202202
// *gave* a numpy.ndarray of the right type and dimensions.
203-
_<show_writeable>(", flags.writeable", "") +
204-
_<show_c_contiguous>(", flags.c_contiguous", "") +
205-
_<show_f_contiguous>(", flags.f_contiguous", "") +
206-
_("]");
203+
const_str<show_writeable>(", flags.writeable", "") +
204+
const_str<show_c_contiguous>(", flags.c_contiguous", "") +
205+
const_str<show_f_contiguous>(", flags.f_contiguous", "") +
206+
const_str("]");
207207
};
208208

209209
// Casts an Eigen type to numpy array. If given a base, the numpy array references the src data,
@@ -598,8 +598,8 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
598598
).release();
599599
}
600600

601-
PYBIND11_TYPE_CASTER(Type, _<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
602-
+ npy_format_descriptor<Scalar>::name + _("]"));
601+
PYBIND11_TYPE_CASTER(Type, const_str<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
602+
+ npy_format_descriptor<Scalar>::name + const_str("]"));
603603
};
604604

605605
PYBIND11_NAMESPACE_END(detail)

include/pybind11/functional.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ struct type_caster<std::function<Return(Args...)>> {
113113
return cpp_function(std::forward<Func>(f_), policy).release();
114114
}
115115

116-
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
117-
+ make_caster<retval_type>::name + _("]"));
116+
PYBIND11_TYPE_CASTER(type, const_str("Callable[[") + concat(make_caster<Args>::name...) + const_str("], ")
117+
+ make_caster<retval_type>::name + const_str("]"));
118118
};
119119

120120
PYBIND11_NAMESPACE_END(detail)

include/pybind11/numpy.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class array; // Forward declaration
3939

4040
PYBIND11_NAMESPACE_BEGIN(detail)
4141

42-
template <> struct handle_type_name<array> { static constexpr auto name = _("numpy.ndarray"); };
42+
template <> struct handle_type_name<array> { static constexpr auto name = const_str("numpy.ndarray"); };
4343

4444
template <typename type, typename SFINAE = void> struct npy_format_descriptor;
4545

@@ -290,7 +290,7 @@ template <typename T> struct array_info_scalar {
290290
using type = T;
291291
static constexpr bool is_array = false;
292292
static constexpr bool is_empty = false;
293-
static constexpr auto extents = _("");
293+
static constexpr auto extents = const_str("");
294294
static void append_extents(list& /* shape */) { }
295295
};
296296
// Computes underlying type and a comma-separated list of extents for array
@@ -309,8 +309,8 @@ template <typename T, size_t N> struct array_info<std::array<T, N>> {
309309
array_info<T>::append_extents(shape);
310310
}
311311

312-
static constexpr auto extents = _<array_info<T>::is_array>(
313-
concat(_<N>(), array_info<T>::extents), _<N>()
312+
static constexpr auto extents = const_str<array_info<T>::is_array>(
313+
concat(const_str<N>(), array_info<T>::extents), const_str<N>()
314314
);
315315
};
316316
// For numpy we have special handling for arrays of characters, so we don't include
@@ -1021,7 +1021,7 @@ template <typename T>
10211021
struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
10221022
static std::string format() {
10231023
using namespace detail;
1024-
static constexpr auto extents = _("(") + array_info<T>::extents + _(")");
1024+
static constexpr auto extents = const_str("(") + array_info<T>::extents + const_str(")");
10251025
return extents.text + format_descriptor<remove_all_extents_t<T>>::format();
10261026
}
10271027
};
@@ -1056,28 +1056,28 @@ struct npy_format_descriptor_name;
10561056

10571057
template <typename T>
10581058
struct npy_format_descriptor_name<T, enable_if_t<std::is_integral<T>::value>> {
1059-
static constexpr auto name = _<std::is_same<T, bool>::value>(
1060-
_("bool"), _<std::is_signed<T>::value>("numpy.int", "numpy.uint") + _<sizeof(T)*8>()
1059+
static constexpr auto name = const_str<std::is_same<T, bool>::value>(
1060+
const_str("bool"), const_str<std::is_signed<T>::value>("numpy.int", "numpy.uint") + const_str<sizeof(T)*8>()
10611061
);
10621062
};
10631063

10641064
template <typename T>
10651065
struct npy_format_descriptor_name<T, enable_if_t<std::is_floating_point<T>::value>> {
1066-
static constexpr auto name = _<std::is_same<T, float>::value
1066+
static constexpr auto name = const_str<std::is_same<T, float>::value
10671067
|| std::is_same<T, const float>::value
10681068
|| std::is_same<T, double>::value
10691069
|| std::is_same<T, const double>::value>(
1070-
_("numpy.float") + _<sizeof(T)*8>(), _("numpy.longdouble")
1070+
const_str("numpy.float") + const_str<sizeof(T)*8>(), const_str("numpy.longdouble")
10711071
);
10721072
};
10731073

10741074
template <typename T>
10751075
struct npy_format_descriptor_name<T, enable_if_t<is_complex<T>::value>> {
1076-
static constexpr auto name = _<std::is_same<typename T::value_type, float>::value
1076+
static constexpr auto name = const_str<std::is_same<typename T::value_type, float>::value
10771077
|| std::is_same<typename T::value_type, const float>::value
10781078
|| std::is_same<typename T::value_type, double>::value
10791079
|| std::is_same<typename T::value_type, const double>::value>(
1080-
_("numpy.complex") + _<sizeof(typename T::value_type)*16>(), _("numpy.longcomplex")
1080+
const_str("numpy.complex") + const_str<sizeof(typename T::value_type)*16>(), const_str("numpy.longcomplex")
10811081
);
10821082
};
10831083

@@ -1105,7 +1105,7 @@ struct npy_format_descriptor<T, enable_if_t<satisfies_any_of<T, std::is_arithmet
11051105
};
11061106

11071107
#define PYBIND11_DECL_CHAR_FMT \
1108-
static constexpr auto name = _("S") + _<N>(); \
1108+
static constexpr auto name = const_str("S") + const_str<N>(); \
11091109
static pybind11::dtype dtype() { return pybind11::dtype(std::string("S") + std::to_string(N)); }
11101110
template <size_t N> struct npy_format_descriptor<char[N]> { PYBIND11_DECL_CHAR_FMT };
11111111
template <size_t N> struct npy_format_descriptor<std::array<char, N>> { PYBIND11_DECL_CHAR_FMT };
@@ -1117,7 +1117,7 @@ template<typename T> struct npy_format_descriptor<T, enable_if_t<array_info<T>::
11171117
public:
11181118
static_assert(!array_info<T>::is_empty, "Zero-sized arrays are not supported");
11191119

1120-
static constexpr auto name = _("(") + array_info<T>::extents + _(")") + base_descr::name;
1120+
static constexpr auto name = const_str("(") + array_info<T>::extents + const_str(")") + base_descr::name;
11211121
static pybind11::dtype dtype() {
11221122
list shape;
11231123
array_info<T>::append_extents(shape);
@@ -1705,7 +1705,7 @@ vectorize_extractor(const Func &f, Return (*) (Args ...)) {
17051705
}
17061706

17071707
template <typename T, int Flags> struct handle_type_name<array_t<T, Flags>> {
1708-
static constexpr auto name = _("numpy.ndarray[") + npy_format_descriptor<T>::name + _("]");
1708+
static constexpr auto name = const_str("numpy.ndarray[") + npy_format_descriptor<T>::name + const_str("]");
17091709
};
17101710

17111711
PYBIND11_NAMESPACE_END(detail)

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class cpp_function : public function {
252252
}
253253

254254
/* Generate a readable signature describing the function's arguments and return value types */
255-
static constexpr auto signature = _("(") + cast_in::arg_names + _(") -> ") + cast_out::name;
255+
static constexpr auto signature = const_str("(") + cast_in::arg_names + const_str(") -> ") + cast_out::name;
256256
PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types();
257257

258258
/* Register the function with Python from generic (non-templated) code */

include/pybind11/pytypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ inline void raise_from(error_already_set& err, PyObject *type, const char *messa
431431

432432
#endif
433433

434-
/** \defgroup python_builtins _
434+
/** \defgroup python_builtins const_str
435435
Unless stated otherwise, the following C++ functions behave the same
436436
as their Python counterparts.
437437
*/

0 commit comments

Comments
 (0)