Skip to content

fix: avoiding usage of _ if already defined #3423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
python: '3.6'
args: >
-DPYBIND11_FINDPYTHON=ON
-DCMAKE_CXX_FLAGS="-D_=1"
- runs-on: windows-latest
python: '3.6'
args: >
Expand All @@ -68,7 +69,8 @@ jobs:
python-version: ${{ matrix.python }}

- name: Setup Boost (Linux)
if: runner.os == 'Linux'
# Can't use boost + define _
if: runner.os == 'Linux' && matrix.python != '3.6'
run: sudo apt-get install libboost-dev

- name: Setup Boost (macOS)
Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/cast/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type is explicitly allowed.
* function signatures and declares a local variable
* 'value' of type inty
*/
PYBIND11_TYPE_CASTER(inty, _("inty"));
PYBIND11_TYPE_CASTER(inty, const_name("inty"));

/**
* Conversion part 1 (Python->C++): convert a PyObject into a inty
Expand Down
4 changes: 4 additions & 0 deletions docs/upgrade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ v2.9
converted to using ``py::module_::import("types").attr("SimpleNamespace")``
instead.

* The use of ``_`` in custom type casters can now be replaced with the more
readable ``const_name`` instead. The old ``_`` shortcut has been retained
unless it is being used as a macro (like for gettext).


.. _upgrade-guide-2.7:

Expand Down
30 changes: 15 additions & 15 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}

PYBIND11_TYPE_CASTER(T, _<std::is_integral<T>::value>("int", "float"));
PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));
};

template<typename T> struct void_caster {
Expand All @@ -221,7 +221,7 @@ template<typename T> struct void_caster {
static handle cast(T, return_value_policy /* policy */, handle /* parent */) {
return none().inc_ref();
}
PYBIND11_TYPE_CASTER(T, _("None"));
PYBIND11_TYPE_CASTER(T, const_name("None"));
};

template <> class type_caster<void_type> : public void_caster<void_type> {};
Expand Down Expand Up @@ -264,7 +264,7 @@ template <> class type_caster<void> : public type_caster<void_type> {

template <typename T> using cast_op_type = void*&;
explicit operator void *&() { return value; }
static constexpr auto name = _("capsule");
static constexpr auto name = const_name("capsule");
private:
void *value = nullptr;
};
Expand Down Expand Up @@ -315,7 +315,7 @@ template <> class type_caster<bool> {
static handle cast(bool src, return_value_policy /* policy */, handle /* parent */) {
return handle(src ? Py_True : Py_False).inc_ref();
}
PYBIND11_TYPE_CASTER(bool, _("bool"));
PYBIND11_TYPE_CASTER(bool, const_name("bool"));
};

// Helper class for UTF-{8,16,32} C++ stl strings:
Expand Down Expand Up @@ -405,7 +405,7 @@ template <typename StringType, bool IsView = false> struct string_caster {
return s;
}

PYBIND11_TYPE_CASTER(StringType, _(PYBIND11_STRING_NAME));
PYBIND11_TYPE_CASTER(StringType, const_name(PYBIND11_STRING_NAME));

private:
static handle decode_utfN(const char *buffer, ssize_t nbytes) {
Expand Down Expand Up @@ -542,7 +542,7 @@ template <typename CharT> struct type_caster<CharT, enable_if_t<is_std_char_type
return one_char;
}

static constexpr auto name = _(PYBIND11_STRING_NAME);
static constexpr auto name = const_name(PYBIND11_STRING_NAME);
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
};

Expand Down Expand Up @@ -579,7 +579,7 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
return cast(*src, policy, parent);
}

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

template <typename T> using cast_op_type = type;

Expand Down Expand Up @@ -764,14 +764,14 @@ template <typename base, typename holder> struct is_holder_type :
template <typename base, typename deleter> struct is_holder_type<base, std::unique_ptr<base, deleter>> :
std::true_type {};

template <typename T> struct handle_type_name { static constexpr auto name = _<T>(); };
template <> struct handle_type_name<bytes> { static constexpr auto name = _(PYBIND11_BYTES_NAME); };
template <> struct handle_type_name<int_> { static constexpr auto name = _("int"); };
template <> struct handle_type_name<iterable> { static constexpr auto name = _("Iterable"); };
template <> struct handle_type_name<iterator> { static constexpr auto name = _("Iterator"); };
template <> struct handle_type_name<none> { static constexpr auto name = _("None"); };
template <> struct handle_type_name<args> { static constexpr auto name = _("*args"); };
template <> struct handle_type_name<kwargs> { static constexpr auto name = _("**kwargs"); };
template <typename T> struct handle_type_name { static constexpr auto name = const_name<T>(); };
template <> struct handle_type_name<bytes> { static constexpr auto name = const_name(PYBIND11_BYTES_NAME); };
template <> struct handle_type_name<int_> { static constexpr auto name = const_name("int"); };
template <> struct handle_type_name<iterable> { static constexpr auto name = const_name("Iterable"); };
template <> struct handle_type_name<iterator> { static constexpr auto name = const_name("Iterator"); };
template <> struct handle_type_name<none> { static constexpr auto name = const_name("None"); };
template <> struct handle_type_name<args> { static constexpr auto name = const_name("*args"); };
template <> struct handle_type_name<kwargs> { static constexpr auto name = const_name("**kwargs"); };

template <typename type>
struct pyobject_caster {
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ template <typename type> class duration_caster {
return PyDelta_FromDSU(dd.count(), ss.count(), us.count());
}

PYBIND11_TYPE_CASTER(type, _("datetime.timedelta"));
PYBIND11_TYPE_CASTER(type, const_name("datetime.timedelta"));
};

inline std::tm *localtime_thread_safe(const std::time_t *time, std::tm *buf) {
Expand Down Expand Up @@ -195,7 +195,7 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
localtime.tm_sec,
us.count());
}
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
PYBIND11_TYPE_CASTER(type, const_name("datetime.datetime"));
};

// Other clocks that are not the system clock are not measured as datetime.datetime objects
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ template <typename T> class type_caster<std::complex<T>> {
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
}

PYBIND11_TYPE_CASTER(std::complex<T>, _("complex"));
PYBIND11_TYPE_CASTER(std::complex<T>, const_name("complex"));
};
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
28 changes: 18 additions & 10 deletions include/pybind11/detail/descr.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ constexpr descr<N1 + N2, Ts1..., Ts2...> operator+(const descr<N1, Ts1...> &a, c
return plus_impl(a, b, make_index_sequence<N1>(), make_index_sequence<N2>());
}

template <size_t N>
constexpr descr<N - 1> const_name(char const(&text)[N]) { return descr<N - 1>(text); }
constexpr descr<0> const_name(char const(&)[1]) { return {}; }

// The "_" might be defined as a macro - don't define it if so.
// Repeating the const_name code to avoid introducing a #define.
#ifndef _
template <size_t N>
constexpr descr<N - 1> _(char const(&text)[N]) { return descr<N - 1>(text); }
constexpr descr<0> _(char const(&)[1]) { return {}; }
#endif

template <size_t Rem, size_t... Digits> struct int_to_str : int_to_str<Rem/10, Rem%10, Digits...> { };
template <size_t...Digits> struct int_to_str<0, Digits...> {
Expand All @@ -64,25 +72,25 @@ template <size_t...Digits> struct int_to_str<0, Digits...> {

// Ternary description (like std::conditional)
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<B, descr<N1 - 1>> _(char const(&text1)[N1], char const(&)[N2]) {
return _(text1);
constexpr enable_if_t<B, descr<N1 - 1>> const_name(char const(&text1)[N1], char const(&)[N2]) {
return const_name(text1);
}
template <bool B, size_t N1, size_t N2>
constexpr enable_if_t<!B, descr<N2 - 1>> _(char const(&)[N1], char const(&text2)[N2]) {
return _(text2);
constexpr enable_if_t<!B, descr<N2 - 1>> const_name(char const(&)[N1], char const(&text2)[N2]) {
return const_name(text2);
}

template <bool B, typename T1, typename T2>
constexpr enable_if_t<B, T1> _(const T1 &d, const T2 &) { return d; }
constexpr enable_if_t<B, T1> const_name(const T1 &d, const T2 &) { return d; }
template <bool B, typename T1, typename T2>
constexpr enable_if_t<!B, T2> _(const T1 &, const T2 &d) { return d; }
constexpr enable_if_t<!B, T2> const_name(const T1 &, const T2 &d) { return d; }

template <size_t Size>
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
auto constexpr const_name() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
return int_to_str<Size / 10, Size % 10>::digits;
}

template <typename Type> constexpr descr<1, Type> _() { return {'%'}; }
template <typename Type> constexpr descr<1, Type> const_name() { return {'%'}; }

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

Expand All @@ -92,12 +100,12 @@ constexpr descr<N, Ts...> concat(const descr<N, Ts...> &descr) { return descr; }
template <size_t N, typename... Ts, typename... Args>
constexpr auto concat(const descr<N, Ts...> &d, const Args &...args)
-> decltype(std::declval<descr<N + 2, Ts...>>() + concat(args...)) {
return d + _(", ") + concat(args...);
return d + const_name(", ") + concat(args...);
}

template <size_t N, typename... Ts>
constexpr descr<N + 2, Ts...> type_descr(const descr<N, Ts...> &descr) {
return _("{") + descr + _("}");
return const_name("{") + descr + const_name("}");
}

PYBIND11_NAMESPACE_END(detail)
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class type_caster<value_and_holder> {

template <typename> using cast_op_type = value_and_holder &;
explicit operator value_and_holder &() { return *value; }
static constexpr auto name = _<value_and_holder>();
static constexpr auto name = const_name<value_and_holder>();

private:
value_and_holder *value = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
using itype = intrinsic_t<type>;

public:
static constexpr auto name = _<type>();
static constexpr auto name = const_name<type>();

type_caster_base() : type_caster_base(typeid(type)) { }
explicit type_caster_base(const std::type_info &info) : type_caster_generic(info) { }
Expand Down
20 changes: 10 additions & 10 deletions include/pybind11/eigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,20 @@ template <typename Type_> struct EigenProps {
static constexpr bool show_f_contiguous = !show_c_contiguous && show_order && requires_col_major;

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

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

PYBIND11_TYPE_CASTER(Type, _<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
+ npy_format_descriptor<Scalar>::name + _("]"));
PYBIND11_TYPE_CASTER(Type, const_name<(Type::IsRowMajor) != 0>("scipy.sparse.csr_matrix[", "scipy.sparse.csc_matrix[")
+ npy_format_descriptor<Scalar>::name + const_name("]"));
};

PYBIND11_NAMESPACE_END(detail)
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ struct type_caster<std::function<Return(Args...)>> {
return cpp_function(std::forward<Func>(f_), policy).release();
}

PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
+ make_caster<retval_type>::name + _("]"));
PYBIND11_TYPE_CASTER(type, const_name("Callable[[") + concat(make_caster<Args>::name...) + const_name("], ")
+ make_caster<retval_type>::name + const_name("]"));
};

PYBIND11_NAMESPACE_END(detail)
Expand Down
28 changes: 14 additions & 14 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class array; // Forward declaration

PYBIND11_NAMESPACE_BEGIN(detail)

template <> struct handle_type_name<array> { static constexpr auto name = _("numpy.ndarray"); };
template <> struct handle_type_name<array> { static constexpr auto name = const_name("numpy.ndarray"); };

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

Expand Down Expand Up @@ -290,7 +290,7 @@ template <typename T> struct array_info_scalar {
using type = T;
static constexpr bool is_array = false;
static constexpr bool is_empty = false;
static constexpr auto extents = _("");
static constexpr auto extents = const_name("");
static void append_extents(list& /* shape */) { }
};
// Computes underlying type and a comma-separated list of extents for array
Expand All @@ -309,8 +309,8 @@ template <typename T, size_t N> struct array_info<std::array<T, N>> {
array_info<T>::append_extents(shape);
}

static constexpr auto extents = _<array_info<T>::is_array>(
concat(_<N>(), array_info<T>::extents), _<N>()
static constexpr auto extents = const_name<array_info<T>::is_array>(
concat(const_name<N>(), array_info<T>::extents), const_name<N>()
);
};
// For numpy we have special handling for arrays of characters, so we don't include
Expand Down Expand Up @@ -1021,7 +1021,7 @@ template <typename T>
struct format_descriptor<T, detail::enable_if_t<detail::array_info<T>::is_array>> {
static std::string format() {
using namespace detail;
static constexpr auto extents = _("(") + array_info<T>::extents + _(")");
static constexpr auto extents = const_name("(") + array_info<T>::extents + const_name(")");
return extents.text + format_descriptor<remove_all_extents_t<T>>::format();
}
};
Expand Down Expand Up @@ -1056,28 +1056,28 @@ struct npy_format_descriptor_name;

template <typename T>
struct npy_format_descriptor_name<T, enable_if_t<std::is_integral<T>::value>> {
static constexpr auto name = _<std::is_same<T, bool>::value>(
_("bool"), _<std::is_signed<T>::value>("numpy.int", "numpy.uint") + _<sizeof(T)*8>()
static constexpr auto name = const_name<std::is_same<T, bool>::value>(
const_name("bool"), const_name<std::is_signed<T>::value>("numpy.int", "numpy.uint") + const_name<sizeof(T)*8>()
);
};

template <typename T>
struct npy_format_descriptor_name<T, enable_if_t<std::is_floating_point<T>::value>> {
static constexpr auto name = _<std::is_same<T, float>::value
static constexpr auto name = const_name<std::is_same<T, float>::value
|| std::is_same<T, const float>::value
|| std::is_same<T, double>::value
|| std::is_same<T, const double>::value>(
_("numpy.float") + _<sizeof(T)*8>(), _("numpy.longdouble")
const_name("numpy.float") + const_name<sizeof(T)*8>(), const_name("numpy.longdouble")
);
};

template <typename T>
struct npy_format_descriptor_name<T, enable_if_t<is_complex<T>::value>> {
static constexpr auto name = _<std::is_same<typename T::value_type, float>::value
static constexpr auto name = const_name<std::is_same<typename T::value_type, float>::value
|| std::is_same<typename T::value_type, const float>::value
|| std::is_same<typename T::value_type, double>::value
|| std::is_same<typename T::value_type, const double>::value>(
_("numpy.complex") + _<sizeof(typename T::value_type)*16>(), _("numpy.longcomplex")
const_name("numpy.complex") + const_name<sizeof(typename T::value_type)*16>(), const_name("numpy.longcomplex")
);
};

Expand Down Expand Up @@ -1105,7 +1105,7 @@ struct npy_format_descriptor<T, enable_if_t<satisfies_any_of<T, std::is_arithmet
};

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

static constexpr auto name = _("(") + array_info<T>::extents + _(")") + base_descr::name;
static constexpr auto name = const_name("(") + array_info<T>::extents + const_name(")") + base_descr::name;
static pybind11::dtype dtype() {
list shape;
array_info<T>::append_extents(shape);
Expand Down Expand Up @@ -1705,7 +1705,7 @@ vectorize_extractor(const Func &f, Return (*) (Args ...)) {
}

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

PYBIND11_NAMESPACE_END(detail)
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class cpp_function : public function {
}

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

/* Register the function with Python from generic (non-templated) code */
Expand Down
Loading