mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
* init Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * Add constexpr to is_floating_point check This is known at compile time so it can be constexpr * Allow noconvert float to accept int * Update noconvert documentation * Allow noconvert complex to accept int and float * Add complex strict test * style: pre-commit fixes * Update unit tests so int, becomes double. * style: pre-commit fixes * remove if (constexpr) Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * fix spelling error * bump order in #else * Switch order in c++11 only section Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * ci: trigger build * ci: trigger build * Allow casting from float to int The int type caster allows anything that implements __int__ with explicit exception of the python float. I can't see any reason for this. This modifies the int casting behaviour to accept a float. If the argument is marked as noconvert() it will only accept int. * tests for py::float into int * Update complex_cast tests * Add SupportsIndex to int and float * style: pre-commit fixes * fix assert * Update docs to mention other conversions * fix pypy __index__ problems * style: pre-commit fixes * extract out PyLong_AsLong __index__ deprecation Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * style: pre-commit fixes * Add back env.deprecated_call Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * remove note Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * remove untrue comment Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * fix noconvert_args Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * resolve error Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * Add comment Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> * [skip ci] tests: Add overload resolution test for float/int breaking change Add test_overload_resolution_float_int() to explicitly test the breaking change where int arguments now match float overloads when registered first. The existing tests verify conversion behavior (int -> float, int/float -> complex) but do not test overload resolution when both float and int overloads exist. This test fills that gap by: - Testing that float overload registered before int overload matches int(42) - Testing strict mode (noconvert) overload resolution breaking change - Testing complex overload resolution with int/float/complex overloads - Documenting the breaking change explicitly This complements existing tests which verify 'can it convert?' by testing 'which overload wins when multiple can convert?' * Add test to verify that custom __index__ objects (not PyLong) work correctly with complex conversion. These should be consistent across CPython, PyPy, and GraalPy. * Improve comment clarity for PyPy __index__ handling Replace cryptic 'So: PYBIND11_INDEX_CHECK(src.ptr())' comment with clearer explanation of the logic: - Explains that we need to call PyNumber_Index explicitly on PyPy for non-PyLong objects - Clarifies the relationship to the outer condition: when convert is false, we only reach this point if PYBIND11_INDEX_CHECK passed above This makes the code more maintainable and easier to understand during review. * Undo inconsequential change to regex in test_enum.py During merge, HEAD's regex pattern was kept, but master's version is preferred. The order of ` ` and `\|` in the character class is arbitrary. Keep master's order (already fixed in PR #5891; sorry I missed looking back here when working on 5891). * test_methods_and_attributes.py: Restore existing `m.overload_order(1.1)` call and clearly explain the behavior change. * Reject float → int conversion even in convert mode Enabling implicit float → int conversion in convert mode causes silent truncation (e.g., 1.9 → 1). This is dangerous because: 1. It's implicit - users don't expect truncation when calling functions 2. It's silent - no warning or error 3. It can hide bugs - precision loss is hard to detect This change restores the explicit rejection of PyFloat_Check for integer casters, even in convert mode. This is more in line with Python's behavior where int(1.9) must be explicit. Note that the int → float conversion in noconvert mode is preserved, as that's a safe widening conversion. * Revert test changes that sidestepped implicit float→int conversion This reverts all test modifications that were made to accommodate implicit float→int conversion in convert mode. With the production code change that explicitly rejects float→int conversion even in convert mode, these test workarounds are no longer needed. Changes reverted: - test_builtin_casters.py: Restored cant_convert(3.14159) and np.float32 conversion with deprecated_call wrapper - test_custom_type_casters.py: Restored TypeError expectation for m.ints_preferred(4.0) - test_methods_and_attributes.py: Restored TypeError expectation for m.overload_order(1.1) - test_stl.py: Restored float literals (2.0) that were replaced with strings to avoid conversion - test_factory_constructors.py: Restored original constructor calls that were modified to avoid float→int conversion Also removes the unused avoid_PyLong_AsLong_deprecation fixture and related TypeVar imports, as all uses were removed. * Replace env.deprecated_call() with pytest.deprecated_call() The env.deprecated_call() function was removed, but two test cases still reference it. Replace with pytest.deprecated_call(), which is the standard pytest context manager for handling deprecation warnings. Since we already require pytest>=6 (see tests/requirements.txt), the compatibility function is obsolete and pytest.deprecated_call() is available. * Update test expectations for swapped NoisyAlloc overloads PR 5879 swapped the order of NoisyAlloc constructor overloads: - (int i, double) is now placement new (comes first) - (double d, double) is now factory pointer (comes second) This swap is necessary because pybind11 tries overloads in order until one matches. With int → float conversion now allowed: - create_and_destroy(4, 0.5): Without the swap, (double d, double) would match first (since int → double conversion is allowed), bypassing the more specific (int i, double) overload. With the swap, (int i, double) matches first (exact match), which is correct. - create_and_destroy(3.5, 4.5): (int i, double) fails (float → int is rejected), then (double d, double) matches, which is correct. The swap ensures exact int matches are preferred over double matches when an int is provided, which is the expected overload resolution behavior. Update the test expectations to match the new overload resolution order. * Resolve clang-tidy error: /__w/pybind11/pybind11/include/pybind11/cast.h:253:46: error: repeated branch body in conditional chain [bugprone-branch-clone,-warnings-as-errors] 253 | } else if (PyFloat_Check(src.ptr())) { | ^ /__w/pybind11/pybind11/include/pybind11/cast.h:258:10: note: end of the original 258 | } else if (convert || PYBIND11_LONG_CHECK(src.ptr()) || PYBIND11_INDEX_CHECK(src.ptr())) { | ^ /__w/pybind11/pybind11/include/pybind11/cast.h:283:16: note: clone 1 starts here 283 | } else { | ^ * Add test coverage for __index__ and __int__ edge cases: incorrectly returning float These tests ensure that: - Invalid return types (floats) are properly rejected - The fallback from __index__ to __int__ works correctly in convert mode - noconvert mode correctly prevents fallback when __index__ fails * Minor comment-only changes: add PR number, for easy future reference * Ensure we are not leaking a Python error is something is wrong elsewhere (e.g. UB, or bug in Python beta testing). See also: https://github.com/pybind/pybind11/pull/5879#issuecomment-3521099331 * [skip ci] Bump PYBIND11_INTERNALS_VERSION to 12 (for PRs 5879, 5887, 5960) --------- Signed-off-by: Michael Carlstrom <rmc@carlstrom.com> Co-authored-by: gentlegiantJGC <gentlegiantJGC@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
419 lines
17 KiB
C++
419 lines
17 KiB
C++
/*
|
||
tests/test_builtin_casters.cpp -- Casters available without any additional headers
|
||
|
||
Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
||
|
||
All rights reserved. Use of this source code is governed by a
|
||
BSD-style license that can be found in the LICENSE file.
|
||
*/
|
||
|
||
#include <pybind11/complex.h>
|
||
|
||
#include "pybind11_tests.h"
|
||
|
||
struct ConstRefCasted {
|
||
int tag;
|
||
};
|
||
|
||
PYBIND11_NAMESPACE_BEGIN(pybind11)
|
||
PYBIND11_NAMESPACE_BEGIN(detail)
|
||
template <>
|
||
class type_caster<ConstRefCasted> {
|
||
public:
|
||
static constexpr auto name = const_name<ConstRefCasted>();
|
||
|
||
// Input is unimportant, a new value will always be constructed based on the
|
||
// cast operator.
|
||
bool load(handle, bool) { return true; }
|
||
|
||
explicit operator ConstRefCasted &&() {
|
||
value = {1};
|
||
// NOLINTNEXTLINE(performance-move-const-arg)
|
||
return std::move(value);
|
||
}
|
||
explicit operator ConstRefCasted &() {
|
||
value = {2};
|
||
return value;
|
||
}
|
||
explicit operator ConstRefCasted *() {
|
||
value = {3};
|
||
return &value;
|
||
}
|
||
|
||
explicit operator const ConstRefCasted &() {
|
||
value = {4};
|
||
return value;
|
||
}
|
||
explicit operator const ConstRefCasted *() {
|
||
value = {5};
|
||
return &value;
|
||
}
|
||
|
||
// custom cast_op to explicitly propagate types to the conversion operators.
|
||
template <typename T_>
|
||
using cast_op_type =
|
||
/// const
|
||
conditional_t<
|
||
std::is_same<remove_reference_t<T_>, const ConstRefCasted *>::value,
|
||
const ConstRefCasted *,
|
||
conditional_t<
|
||
std::is_same<T_, const ConstRefCasted &>::value,
|
||
const ConstRefCasted &,
|
||
/// non-const
|
||
conditional_t<std::is_same<remove_reference_t<T_>, ConstRefCasted *>::value,
|
||
ConstRefCasted *,
|
||
conditional_t<std::is_same<T_, ConstRefCasted &>::value,
|
||
ConstRefCasted &,
|
||
/* else */ ConstRefCasted &&>>>>;
|
||
|
||
private:
|
||
ConstRefCasted value = {0};
|
||
};
|
||
PYBIND11_NAMESPACE_END(detail)
|
||
PYBIND11_NAMESPACE_END(pybind11)
|
||
|
||
TEST_SUBMODULE(builtin_casters, m) {
|
||
PYBIND11_WARNING_PUSH
|
||
PYBIND11_WARNING_DISABLE_MSVC(4127)
|
||
|
||
// test_simple_string
|
||
m.def("string_roundtrip", [](const char *s) { return s; });
|
||
|
||
// test_unicode_conversion
|
||
// Some test characters in utf16 and utf32 encodings. The last one (the 𝐀) contains a null
|
||
// byte
|
||
char32_t a32 = 0x61 /*a*/, z32 = 0x7a /*z*/, ib32 = 0x203d /*‽*/, cake32 = 0x1f382 /*🎂*/,
|
||
mathbfA32 = 0x1d400 /*𝐀*/;
|
||
char16_t b16 = 0x62 /*b*/, z16 = 0x7a, ib16 = 0x203d, cake16_1 = 0xd83c, cake16_2 = 0xdf82,
|
||
mathbfA16_1 = 0xd835, mathbfA16_2 = 0xdc00;
|
||
std::wstring wstr;
|
||
wstr.push_back(0x61); // a
|
||
wstr.push_back(0x2e18); // ⸘
|
||
if (sizeof(wchar_t) == 2) {
|
||
wstr.push_back(mathbfA16_1);
|
||
wstr.push_back(mathbfA16_2);
|
||
} // 𝐀, utf16
|
||
else {
|
||
wstr.push_back((wchar_t) mathbfA32);
|
||
} // 𝐀, utf32
|
||
wstr.push_back(0x7a); // z
|
||
|
||
m.def("good_utf8_string", []() {
|
||
return std::string((const char *) u8"Say utf8\u203d \U0001f382 \U0001d400");
|
||
}); // Say utf8‽ 🎂 𝐀
|
||
m.def("good_utf16_string", [=]() {
|
||
return std::u16string({b16, ib16, cake16_1, cake16_2, mathbfA16_1, mathbfA16_2, z16});
|
||
}); // b‽🎂𝐀z
|
||
m.def("good_utf32_string",
|
||
[=]() { return std::u32string({a32, mathbfA32, cake32, ib32, z32}); }); // a𝐀🎂‽z
|
||
m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
|
||
m.def("bad_utf8_string", []() {
|
||
return std::string("abc\xd0"
|
||
"def");
|
||
});
|
||
m.def("bad_utf16_string", [=]() { return std::u16string({b16, char16_t(0xd800), z16}); });
|
||
// Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger
|
||
// UnicodeDecodeError
|
||
m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); });
|
||
if (sizeof(wchar_t) == 2) {
|
||
m.def("bad_wchar_string",
|
||
[=]() { return std::wstring({wchar_t(0x61), wchar_t(0xd800)}); });
|
||
}
|
||
m.def("u8_Z", []() -> char { return 'Z'; });
|
||
m.def("u8_eacute", []() -> char { return '\xe9'; });
|
||
m.def("u16_ibang", [=]() -> char16_t { return ib16; });
|
||
m.def("u32_mathbfA", [=]() -> char32_t { return mathbfA32; });
|
||
m.def("wchar_heart", []() -> wchar_t { return 0x2665; });
|
||
|
||
// test_single_char_arguments
|
||
m.attr("wchar_size") = py::cast(sizeof(wchar_t));
|
||
m.def("ord_char", [](char c) -> int { return static_cast<unsigned char>(c); });
|
||
m.def("ord_char_lv", [](char &c) -> int { return static_cast<unsigned char>(c); });
|
||
m.def("ord_char16", [](char16_t c) -> uint16_t { return c; });
|
||
m.def("ord_char16_lv", [](char16_t &c) -> uint16_t { return c; });
|
||
m.def("ord_char32", [](char32_t c) -> uint32_t { return c; });
|
||
m.def("ord_wchar", [](wchar_t c) -> int { return c; });
|
||
|
||
// test_bytes_to_string
|
||
m.def("strlen", [](char *s) { return strlen(s); });
|
||
m.def("string_length", [](const std::string &s) { return s.length(); });
|
||
|
||
#ifdef PYBIND11_HAS_U8STRING
|
||
m.attr("has_u8string") = true;
|
||
m.def("good_utf8_u8string", []() {
|
||
return std::u8string(u8"Say utf8\u203d \U0001f382 \U0001d400");
|
||
}); // Say utf8‽ 🎂 𝐀
|
||
m.def("bad_utf8_u8string", []() {
|
||
return std::u8string((const char8_t *) "abc\xd0"
|
||
"def");
|
||
});
|
||
|
||
m.def("u8_char8_Z", []() -> char8_t { return u8'Z'; });
|
||
|
||
// test_single_char_arguments
|
||
m.def("ord_char8", [](char8_t c) -> int { return static_cast<unsigned char>(c); });
|
||
m.def("ord_char8_lv", [](char8_t &c) -> int { return static_cast<unsigned char>(c); });
|
||
#endif
|
||
|
||
// test_string_view
|
||
#ifdef PYBIND11_HAS_STRING_VIEW
|
||
m.attr("has_string_view") = true;
|
||
m.def("string_view_print", [](std::string_view s) { py::print(s, s.size()); });
|
||
m.def("string_view16_print", [](std::u16string_view s) { py::print(s, s.size()); });
|
||
m.def("string_view32_print", [](std::u32string_view s) { py::print(s, s.size()); });
|
||
m.def("string_view_chars", [](std::string_view s) {
|
||
py::list l;
|
||
for (auto c : s) {
|
||
l.append((std::uint8_t) c);
|
||
}
|
||
return l;
|
||
});
|
||
m.def("string_view16_chars", [](std::u16string_view s) {
|
||
py::list l;
|
||
for (auto c : s) {
|
||
l.append((int) c);
|
||
}
|
||
return l;
|
||
});
|
||
m.def("string_view32_chars", [](std::u32string_view s) {
|
||
py::list l;
|
||
for (auto c : s) {
|
||
l.append((int) c);
|
||
}
|
||
return l;
|
||
});
|
||
m.def("string_view_return",
|
||
[]() { return std::string_view((const char *) u8"utf8 secret \U0001f382"); });
|
||
m.def("string_view16_return",
|
||
[]() { return std::u16string_view(u"utf16 secret \U0001f382"); });
|
||
m.def("string_view32_return",
|
||
[]() { return std::u32string_view(U"utf32 secret \U0001f382"); });
|
||
|
||
// The inner lambdas here are to also test implicit conversion
|
||
using namespace std::literals;
|
||
m.def("string_view_bytes",
|
||
[]() { return [](py::bytes b) { return b; }("abc \x80\x80 def"sv); });
|
||
m.def("string_view_str",
|
||
[]() { return [](py::str s) { return s; }("abc \342\200\275 def"sv); });
|
||
m.def("string_view_from_bytes",
|
||
[](const py::bytes &b) { return [](std::string_view s) { return s; }(b); });
|
||
m.def("string_view_memoryview", []() {
|
||
static constexpr auto val = "Have some \360\237\216\202"sv;
|
||
return py::memoryview::from_memory(val);
|
||
});
|
||
|
||
# ifdef PYBIND11_HAS_U8STRING
|
||
m.def("string_view8_print", [](std::u8string_view s) { py::print(s, s.size()); });
|
||
m.def("string_view8_chars", [](std::u8string_view s) {
|
||
py::list l;
|
||
for (auto c : s)
|
||
l.append((std::uint8_t) c);
|
||
return l;
|
||
});
|
||
m.def("string_view8_return", []() { return std::u8string_view(u8"utf8 secret \U0001f382"); });
|
||
m.def("string_view8_str", []() { return py::str{std::u8string_view{u8"abc ‽ def"}}; });
|
||
# endif
|
||
|
||
struct TypeWithBothOperatorStringAndStringView {
|
||
// NOLINTNEXTLINE(google-explicit-constructor)
|
||
operator std::string() const { return "success"; }
|
||
// NOLINTNEXTLINE(google-explicit-constructor)
|
||
operator std::string_view() const { return "failure"; }
|
||
};
|
||
m.def("bytes_from_type_with_both_operator_string_and_string_view",
|
||
[]() { return py::bytes(TypeWithBothOperatorStringAndStringView()); });
|
||
m.def("str_from_type_with_both_operator_string_and_string_view",
|
||
[]() { return py::str(TypeWithBothOperatorStringAndStringView()); });
|
||
#endif
|
||
|
||
// test_integer_casting
|
||
m.def("i32_str", [](std::int32_t v) { return std::to_string(v); });
|
||
m.def("u32_str", [](std::uint32_t v) { return std::to_string(v); });
|
||
m.def("i64_str", [](std::int64_t v) { return std::to_string(v); });
|
||
m.def("u64_str", [](std::uint64_t v) { return std::to_string(v); });
|
||
|
||
// test_int_convert
|
||
m.def("int_passthrough", [](int arg) { return arg; });
|
||
m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());
|
||
|
||
// test_float_convert
|
||
m.def("float_passthrough", [](float arg) { return arg; });
|
||
m.def("float_passthrough_noconvert", [](float arg) { return arg; }, py::arg{}.noconvert());
|
||
|
||
// test_tuple
|
||
m.def(
|
||
"pair_passthrough",
|
||
[](const std::pair<bool, std::string> &input) {
|
||
return std::make_pair(input.second, input.first);
|
||
},
|
||
"Return a pair in reversed order");
|
||
m.def(
|
||
"tuple_passthrough",
|
||
[](std::tuple<bool, std::string, int> input) {
|
||
return std::make_tuple(std::get<2>(input), std::get<1>(input), std::get<0>(input));
|
||
},
|
||
"Return a triple in reversed order");
|
||
m.def("empty_tuple", []() { return std::tuple<>(); });
|
||
static std::pair<RValueCaster, RValueCaster> lvpair;
|
||
static std::tuple<RValueCaster, RValueCaster, RValueCaster> lvtuple;
|
||
static std::pair<RValueCaster, std::tuple<RValueCaster, std::pair<RValueCaster, RValueCaster>>>
|
||
lvnested;
|
||
m.def("rvalue_pair", []() { return std::make_pair(RValueCaster{}, RValueCaster{}); });
|
||
m.def("lvalue_pair", []() -> const decltype(lvpair) & { return lvpair; });
|
||
m.def("rvalue_tuple",
|
||
[]() { return std::make_tuple(RValueCaster{}, RValueCaster{}, RValueCaster{}); });
|
||
m.def("lvalue_tuple", []() -> const decltype(lvtuple) & { return lvtuple; });
|
||
m.def("rvalue_nested", []() {
|
||
return std::make_pair(
|
||
RValueCaster{},
|
||
std::make_tuple(RValueCaster{}, std::make_pair(RValueCaster{}, RValueCaster{})));
|
||
});
|
||
m.def("lvalue_nested", []() -> const decltype(lvnested) & { return lvnested; });
|
||
|
||
m.def(
|
||
"int_string_pair",
|
||
[]() {
|
||
// Using no-destructor idiom to side-step warnings from overzealous compilers.
|
||
static auto *int_string_pair = new std::pair<int, std::string>{2, "items"};
|
||
return int_string_pair;
|
||
},
|
||
py::return_value_policy::reference);
|
||
|
||
// test_builtins_cast_return_none
|
||
m.def("return_none_string", []() -> std::string * { return nullptr; });
|
||
m.def("return_none_char", []() -> const char * { return nullptr; });
|
||
m.def("return_none_bool", []() -> bool * { return nullptr; });
|
||
m.def("return_none_int", []() -> int * { return nullptr; });
|
||
m.def("return_none_float", []() -> float * { return nullptr; });
|
||
m.def("return_none_pair", []() -> std::pair<int, int> * { return nullptr; });
|
||
|
||
// test_none_deferred
|
||
m.def("defer_none_cstring", [](char *) { return false; });
|
||
m.def("defer_none_cstring", [](const py::none &) { return true; });
|
||
m.def("defer_none_custom", [](UserType *) { return false; });
|
||
m.def("defer_none_custom", [](const py::none &) { return true; });
|
||
m.def("nodefer_none_void", [](void *) { return true; });
|
||
m.def("nodefer_none_void", [](const py::none &) { return false; });
|
||
|
||
// test_void_caster
|
||
m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
|
||
m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
|
||
|
||
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
|
||
|
||
// test_bool_caster
|
||
m.def("bool_passthrough", [](bool arg) { return arg; });
|
||
m.def("bool_passthrough_noconvert", [](bool arg) { return arg; }, py::arg{}.noconvert());
|
||
|
||
// TODO: This should be disabled and fixed in future Intel compilers
|
||
#if !defined(__INTEL_COMPILER)
|
||
// Test "bool_passthrough_noconvert" again, but using () instead of {} to construct py::arg
|
||
// When compiled with the Intel compiler, this results in segmentation faults when importing
|
||
// the module. Tested with icc (ICC) 2021.1 Beta 20200827, this should be tested again when
|
||
// a newer version of icc is available.
|
||
m.def("bool_passthrough_noconvert2", [](bool arg) { return arg; }, py::arg().noconvert());
|
||
#endif
|
||
|
||
// test_reference_wrapper
|
||
m.def("refwrap_builtin", [](std::reference_wrapper<int> p) { return 10 * p.get(); });
|
||
m.def("refwrap_usertype", [](std::reference_wrapper<UserType> p) { return p.get().value(); });
|
||
m.def("refwrap_usertype_const",
|
||
[](std::reference_wrapper<const UserType> p) { return p.get().value(); });
|
||
|
||
m.def("refwrap_lvalue", []() -> std::reference_wrapper<UserType> {
|
||
static UserType x(1);
|
||
return std::ref(x);
|
||
});
|
||
m.def("refwrap_lvalue_const", []() -> std::reference_wrapper<const UserType> {
|
||
static UserType x(1);
|
||
return std::cref(x);
|
||
});
|
||
|
||
// Not currently supported (std::pair caster has return-by-value cast operator);
|
||
// triggers static_assert failure.
|
||
// m.def("refwrap_pair", [](std::reference_wrapper<std::pair<int, int>>) { });
|
||
|
||
m.def(
|
||
"refwrap_list",
|
||
[](bool copy) {
|
||
static IncType x1(1), x2(2);
|
||
py::list l;
|
||
for (const auto &f : {std::ref(x1), std::ref(x2)}) {
|
||
l.append(py::cast(
|
||
f, copy ? py::return_value_policy::copy : py::return_value_policy::reference));
|
||
}
|
||
return l;
|
||
},
|
||
"copy"_a);
|
||
|
||
m.def("refwrap_iiw", [](const IncType &w) { return w.value(); });
|
||
m.def("refwrap_call_iiw", [](IncType &w, const py::function &f) {
|
||
py::list l;
|
||
l.append(f(std::ref(w)));
|
||
l.append(f(std::cref(w)));
|
||
IncType x(w.value());
|
||
l.append(f(std::ref(x)));
|
||
IncType y(w.value());
|
||
auto r3 = std::ref(y);
|
||
l.append(f(r3));
|
||
return l;
|
||
});
|
||
|
||
// test_complex
|
||
m.def("complex_cast", [](float x) { return "{}"_s.format(x); });
|
||
m.def("complex_cast",
|
||
[](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); });
|
||
m.def(
|
||
"complex_cast_strict",
|
||
[](std::complex<float> x) { return "({}, {})"_s.format(x.real(), x.imag()); },
|
||
py::arg{}.noconvert());
|
||
|
||
m.def("complex_convert", [](std::complex<float> x) { return x; });
|
||
m.def("complex_noconvert", [](std::complex<float> x) { return x; }, py::arg{}.noconvert());
|
||
|
||
// test_overload_resolution_float_int
|
||
// Test that float overload registered before int overload gets selected when passing int
|
||
// This documents the breaking change: int can now match float in strict mode
|
||
m.def("overload_resolution_test", [](float x) { return "float: " + std::to_string(x); });
|
||
m.def("overload_resolution_test", [](int x) { return "int: " + std::to_string(x); });
|
||
|
||
// Test with noconvert (strict mode) - this is the key breaking change
|
||
m.def(
|
||
"overload_resolution_strict",
|
||
[](float x) { return "float_strict: " + std::to_string(x); },
|
||
py::arg{}.noconvert());
|
||
m.def("overload_resolution_strict", [](int x) { return "int_strict: " + std::to_string(x); });
|
||
|
||
// Test complex overload resolution: complex registered before float/int
|
||
m.def("overload_resolution_complex", [](std::complex<float> x) {
|
||
return "complex: (" + std::to_string(x.real()) + ", " + std::to_string(x.imag()) + ")";
|
||
});
|
||
m.def("overload_resolution_complex", [](float x) { return "float: " + std::to_string(x); });
|
||
m.def("overload_resolution_complex", [](int x) { return "int: " + std::to_string(x); });
|
||
|
||
// test int vs. long (Python 2)
|
||
m.def("int_cast", []() { return 42; });
|
||
m.def("long_cast", []() { return (long) 42; });
|
||
m.def("longlong_cast", []() { return ULLONG_MAX; });
|
||
|
||
/// test void* cast operator
|
||
m.def("test_void_caster", []() -> bool {
|
||
void *v = (void *) 0xabcd;
|
||
py::object o = py::cast(v);
|
||
return py::cast<void *>(o) == v;
|
||
});
|
||
|
||
// Tests const/non-const propagation in cast_op.
|
||
m.def("takes", [](ConstRefCasted x) { return x.tag; });
|
||
m.def("takes_move", [](ConstRefCasted &&x) { return x.tag; });
|
||
m.def("takes_ptr", [](ConstRefCasted *x) { return x->tag; });
|
||
m.def("takes_ref", [](ConstRefCasted &x) { return x.tag; });
|
||
m.def("takes_ref_wrap", [](std::reference_wrapper<ConstRefCasted> x) { return x.get().tag; });
|
||
m.def("takes_const_ptr", [](const ConstRefCasted *x) { return x->tag; });
|
||
m.def("takes_const_ref", [](const ConstRefCasted &x) { return x.tag; });
|
||
m.def("takes_const_ref_wrap",
|
||
[](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; });
|
||
|
||
PYBIND11_WARNING_POP
|
||
}
|