-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Experiments on top of PR #3643 (pybind11_select_caster) #3674
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
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a126d9b
Function pybind11_select_caster uses ADL to select the caster.
amauryfa 5cfd67b
Add documentation.:
amauryfa dfd4bf0
Use correct function name in docs.
amauryfa 43a065a
Attempt to fix compilation error on MSVC 2015
amauryfa 28cef75
Address rst conformance checks.
amauryfa 9100a05
Fix compilation.
amauryfa 13a63ad
Undoing std::string, std::string_view caster changes.
rwgk 222d867
Adding ReferenceSensitiveOptional copy/move/operator= as suggested by…
rwgk 59fadb3
noexcept to make clang-tidy happy; explicit py::return_value_policy::…
rwgk 74d9ee4
Disabling added copy/move/operator=
rwgk 09c664a
Trying pybind11_select_caster(IntrinsicType *);
rwgk f4e9248
make_caster_impl experiment
rwgk 1c73b00
test_make_caster_adl test_basic
rwgk 62ca662
Undoing all changes in tests/test_stl.cpp
rwgk 7d2e524
Adding test_minimal_real_caster. Keeping only one mock_caster.
rwgk 733d8b1
Python 2 compatibility.
rwgk 6a2ce0c
Make clang-tidy happy.
rwgk 9caa190
Adding dummy returned caster pointer to pybind11_select_caster() over…
rwgk f6f2ee2
Undoing previous commit.
rwgk bc4a241
Using workaround only compilers that need it.
rwgk 6f11212
Fixing silly oversight (missing parentheses).
rwgk 9865877
Using enable_if_t to make the workaround code a little more compact.
rwgk 4574aba
Additional mock_caster testing for global & unnamed namespaces.
rwgk dfd923d
Replacing non-portable warning-suppression pragma with a dummy implem…
rwgk 59c8c77
Adding test_make_caster_adl_alt
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// adl = Argument Dependent Lookup | ||
|
||
#include "pybind11_tests.h" | ||
|
||
namespace have_a_ns { | ||
struct type_mock {}; | ||
struct mock_caster { | ||
static int num() { return 101; } | ||
}; | ||
mock_caster pybind11_select_caster(type_mock *); | ||
} // namespace have_a_ns | ||
|
||
// namespace global { | ||
struct global_ns_type_mock {}; | ||
struct global_ns_mock_caster { | ||
static int num() { return 202; } | ||
}; | ||
global_ns_mock_caster pybind11_select_caster(global_ns_type_mock *); | ||
// } // namespace global | ||
|
||
namespace { | ||
struct unnamed_ns_type_mock {}; | ||
struct unnamed_ns_mock_caster { | ||
static int num() { return 303; } | ||
}; | ||
unnamed_ns_mock_caster pybind11_select_caster(unnamed_ns_type_mock *); | ||
#if !defined(_MSC_VER) | ||
// Dummy implementation, purely to avoid compiler warnings (unused function). | ||
// Easier than managing compiler-specific pragmas for warning suppression. | ||
// MSVC happens to not generate any warnings. Leveraging that to prove that | ||
// this test actually works without an implementation. | ||
unnamed_ns_mock_caster pybind11_select_caster(unnamed_ns_type_mock *) { | ||
return unnamed_ns_mock_caster{}; | ||
} | ||
#endif | ||
} // namespace | ||
|
||
namespace mrc_ns { // minimal real caster | ||
|
||
struct type_mrc { | ||
int value = -9999; | ||
}; | ||
|
||
struct minimal_real_caster { | ||
static constexpr auto name = py::detail::const_name<type_mrc>(); | ||
|
||
static py::handle | ||
cast(type_mrc const &src, py::return_value_policy /*policy*/, py::handle /*parent*/) { | ||
return py::int_(src.value + 1000).release(); | ||
} | ||
|
||
// Maximizing simplicity. This will go terribly wrong for other arg types. | ||
template <typename> | ||
using cast_op_type = const type_mrc &; | ||
|
||
// NOLINTNEXTLINE(google-explicit-constructor) | ||
operator type_mrc const &() { | ||
static type_mrc obj; | ||
obj.value = 404; | ||
return obj; | ||
} | ||
|
||
bool load(py::handle src, bool /*convert*/) { | ||
// Only accepts str, but the value is ignored. | ||
return py::isinstance<py::str>(src); | ||
} | ||
}; | ||
|
||
minimal_real_caster pybind11_select_caster(type_mrc *); | ||
|
||
} // namespace mrc_ns | ||
|
||
TEST_SUBMODULE(make_caster_adl, m) { | ||
m.def("have_a_ns_num", []() { return py::detail::make_caster<have_a_ns::type_mock>::num(); }); | ||
m.def("global_ns_num", []() { return py::detail::make_caster<global_ns_type_mock>::num(); }); | ||
m.def("unnamed_ns_num", []() { return py::detail::make_caster<unnamed_ns_type_mock>::num(); }); | ||
|
||
m.def("mrc_return", []() { | ||
mrc_ns::type_mrc obj; | ||
obj.value = 505; | ||
return obj; | ||
}); | ||
m.def("mrc_arg", [](mrc_ns::type_mrc const &obj) { return obj.value + 2000; }); | ||
|
||
#if !defined(_MSC_VER) | ||
// Dummy call, purely to avoid compiler warnings (unused function). | ||
(void) pybind11_select_caster(static_cast<unnamed_ns_type_mock *>(nullptr)); | ||
#endif | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from pybind11_tests import make_caster_adl as m | ||
from pybind11_tests import make_caster_adl_alt as m_alt | ||
|
||
|
||
def test_mock_casters(): | ||
assert m.have_a_ns_num() == 101 | ||
assert m.global_ns_num() == 202 | ||
assert m.unnamed_ns_num() == 303 | ||
|
||
|
||
def test_mock_casters_alt(): | ||
assert m_alt.have_a_ns_num() == 121 | ||
|
||
|
||
def test_minimal_real_caster(): | ||
assert m.mrc_return() == 1505 | ||
assert m.mrc_arg(u"ignored") == 2404 | ||
with pytest.raises(TypeError) as excinfo: | ||
m.mrc_arg(None) | ||
assert "(arg0: mrc_ns::type_mrc) -> int" in str(excinfo.value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "pybind11_tests.h" | ||
|
||
namespace have_a_ns { | ||
struct type_mock {}; | ||
struct mock_caster_alt { | ||
static int num() { return 121; } | ||
}; | ||
mock_caster_alt pybind11_select_caster(type_mock *); | ||
} // namespace have_a_ns | ||
|
||
TEST_SUBMODULE(make_caster_adl_alt, m) { | ||
m.def("have_a_ns_num", []() { return py::detail::make_caster<have_a_ns::type_mock>::num(); }); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from pybind11_tests import make_caster_adl_alt as m | ||
|
||
|
||
def test_mock_casters(): | ||
assert m.have_a_ns_num() == 121 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"When you own the namespace where the type is defined, the preferred method..."