Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3480ce8
Introduce return_value_policy_pack
rwgk Feb 7, 2023
a7ef2da
Add return_value_policy_pack::override_policy() helpers.
rwgk Feb 7, 2023
3b885c6
PYBIND11_TYPE_CASTER_RVPP return_value_policy_pack
rwgk Feb 8, 2023
f668edd
Systematically use return_value_policy_pack in stl.h
rwgk Feb 8, 2023
2fd26d6
clang-tidy auto fix
rwgk Feb 9, 2023
75d801b
Fix MSVC warning C4458: declaration of 'policy' hides class member
rwgk Feb 9, 2023
c943407
Fix oversight (when renaming return_value_policy_opts to return_value…
rwgk Feb 9, 2023
f2414c2
Cover all changed casters in stl.h
rwgk Feb 9, 2023
f2dcfc6
WIP callbacks
rwgk Feb 10, 2023
cd0fb22
Explicitly specify return_value_policy::automatic_reference in functi…
rwgk Feb 10, 2023
4138bc9
WIP proof of concept manipulating the return value policy for callbacks.
rwgk Feb 11, 2023
0031428
Introduce from_python_policies as generalization of load bool convert
rwgk Feb 11, 2023
499da47
Change `std::vector<bool> args_convert;` to `std::vector<from_python_…
rwgk Feb 11, 2023
dc15431
clang-tidy auto fix
rwgk Feb 12, 2023
3aae97c
WIP: tests pass
rwgk Feb 12, 2023
208261c
Replace `argument_record.convert`,`none` with `from_python_policies`
rwgk Feb 12, 2023
e0d9c7b
functional.h using fpp.rvpp (instead of fpp.convert) but arg is non-c…
rwgk Feb 14, 2023
fb8edf8
Split out `detail::arg_literal` to make ALL tests work again.
rwgk Feb 14, 2023
e7486dd
clang-format auto fixes
rwgk Feb 14, 2023
a99c7e7
Quick workaround for MSVC private/friend issue.
rwgk Feb 14, 2023
0541ab5
Add test_nested_callbacks_rtn_string
rwgk Feb 14, 2023
09cdbe5
WIP: Add collect_arguments_rvpp() functions (unused).
rwgk Feb 14, 2023
7f0836b
Use collect_arguments_rvpp() from object_api<Derived>::operator()
rwgk Feb 15, 2023
b4cbcab
clang-format auto fixes
rwgk Feb 15, 2023
9570e45
Make test_return_value_policy_pack.cpp compatible with C++11 (skip op…
rwgk Feb 15, 2023
5a5d5d4
Add test_return_value_policy_pack to tests/CMakeLists.txt and clang-t…
rwgk Feb 15, 2023
dfef68f
Resolve `-Wmissing-braces` emitted by old clang versions (3.6, 3.7, 3…
rwgk Feb 15, 2023
9a63d77
Resolve "pointless comparison of unsigned integer with zero" warnings…
rwgk Feb 15, 2023
53ec32b
object_api<Derived>::call_with_policies()
rwgk Feb 15, 2023
763d254
Use object_api<Derived>::call_with_policies() in functional.h
rwgk Feb 15, 2023
d1c0963
test_call_callback_pass_pair_string
rwgk Feb 15, 2023
3319e6e
Systematically exercise nested callbacks to level 4.
rwgk Feb 16, 2023
a52921a
make_tuple -> make_tuple_rvpp
rwgk Feb 16, 2023
b7e9e23
simple_collector -> simple_collector_rvpp
rwgk Feb 16, 2023
c56698c
unpacking_collector -> unpacking_collector_rvpp
rwgk Feb 16, 2023
3dfae01
PYBIND11_TYPE_CASTER_IMPL
rwgk Feb 16, 2023
885325c
Universally pass return_value_policy_pack via const &
rwgk Feb 16, 2023
d086e80
Rename arg_literal to arg_base
rwgk Feb 16, 2023
0ff74b4
Fix git rebase accident.
rwgk Feb 17, 2023
2b24ede
Suppress MINGW `-Wmismatched-new-delete` (seen in mingw32 & mingw64 c…
rwgk Feb 17, 2023
cb51567
Fix copy-paste mishap (thanks @Lalaland for catching this).
rwgk Feb 18, 2023
094f0e4
style: pre-commit fixes
pre-commit-ci[bot] Mar 16, 2023
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
48 changes: 34 additions & 14 deletions include/pybind11/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ struct argument_record {
const char *name; ///< Argument name
const char *descr; ///< Human-readable version of the argument value
handle value; ///< Associated Python object
bool convert : 1; ///< True if the argument is allowed to convert when loading
bool none : 1; ///< True if None is allowed when loading
from_python_policies policies;

argument_record(const char *name, const char *descr, handle value, bool convert, bool none)
: name(name), descr(descr), value(value), convert(convert), none(none) {}
argument_record(const char *name,
const char *descr,
handle value,
const from_python_policies &policies)
: name(name), descr(descr), value(value), policies(policies) {}
};

/// Internal data structure which holds metadata about a bound function (signature, overloads,
Expand Down Expand Up @@ -212,8 +214,8 @@ struct function_record {
/// Pointer to custom destructor for 'data' (if needed)
void (*free_data)(function_record *ptr) = nullptr;

/// Return value policy associated with this function
return_value_policy policy = return_value_policy::automatic;
/// Return value policies associated with this function
return_value_policy_pack rvpp;

/// True if name == '__init__'
bool is_constructor : 1;
Expand Down Expand Up @@ -359,7 +361,7 @@ struct type_record {

inline function_call::function_call(const function_record &f, handle p) : func(f), parent(p) {
args.reserve(f.nargs);
args_convert.reserve(f.nargs);
args_policies.reserve(f.nargs);
}

/// Tag for a new-style `__init__` defined in `detail/init.h`
Expand Down Expand Up @@ -407,7 +409,12 @@ struct process_attribute<char *> : process_attribute<const char *> {};
/// Process an attribute indicating the function's return value policy
template <>
struct process_attribute<return_value_policy> : process_attribute_default<return_value_policy> {
static void init(const return_value_policy &p, function_record *r) { r->policy = p; }
static void init(const return_value_policy &p, function_record *r) { r->rvpp.policy = p; }
};
template <>
struct process_attribute<return_value_policy_pack>
: process_attribute_default<return_value_policy_pack> {
static void init(const return_value_policy_pack &rvpp, function_record *r) { r->rvpp = rvpp; }
};

/// Process an attribute which indicates that this is an overloaded function associated with a
Expand Down Expand Up @@ -455,7 +462,8 @@ inline void check_kw_only_arg(const arg &a, function_record *r) {

inline void append_self_arg_if_needed(function_record *r) {
if (r->is_method && r->args.empty()) {
r->args.emplace_back("self", nullptr, handle(), /*convert=*/true, /*none=*/false);
r->args.emplace_back(
"self", nullptr, handle(), from_python_policies(/*convert=*/true, /*none=*/false));
}
}

Expand All @@ -464,19 +472,27 @@ template <>
struct process_attribute<arg> : process_attribute_default<arg> {
static void init(const arg &a, function_record *r) {
append_self_arg_if_needed(r);
r->args.emplace_back(a.name, nullptr, handle(), !a.flag_noconvert, a.flag_none);
r->args.emplace_back(
a.name,
nullptr,
handle(),
from_python_policies(a.m_policies.rvpp, !a.flag_noconvert, a.flag_none));

check_kw_only_arg(a, r);
}
};
template <>
struct process_attribute<detail::arg_base> : process_attribute<arg> {};

/// Process a keyword argument attribute (*with* a default value)
template <>
struct process_attribute<arg_v> : process_attribute_default<arg_v> {
static void init(const arg_v &a, function_record *r) {
if (r->is_method && r->args.empty()) {
r->args.emplace_back(
"self", /*descr=*/nullptr, /*parent=*/handle(), /*convert=*/true, /*none=*/false);
r->args.emplace_back("self",
/*descr=*/nullptr,
/*parent=*/handle(),
from_python_policies(/*convert=*/true, /*none=*/false));
}

if (!a.value) {
Expand Down Expand Up @@ -505,7 +521,11 @@ struct process_attribute<arg_v> : process_attribute_default<arg_v> {
"more information.");
#endif
}
r->args.emplace_back(a.name, a.descr, a.value.inc_ref(), !a.flag_noconvert, a.flag_none);
r->args.emplace_back(
a.name,
a.descr,
a.value.inc_ref(),
from_python_policies(a.m_policies.rvpp, !a.flag_noconvert, a.flag_none));

check_kw_only_arg(a, r);
}
Expand Down Expand Up @@ -667,7 +687,7 @@ using extract_guard_t = typename exactly_one_t<is_call_guard, call_guard<>, Extr

/// Check the number of named arguments at compile time
template <typename... Extra,
size_t named = constexpr_sum(std::is_base_of<arg, Extra>::value...),
size_t named = constexpr_sum(std::is_base_of<arg_base, Extra>::value...),
size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
Expand Down
Loading