diff --git a/pybind11_abseil/absl_casters.h b/pybind11_abseil/absl_casters.h index fc2c9fa..0e57884 100644 --- a/pybind11_abseil/absl_casters.h +++ b/pybind11_abseil/absl_casters.h @@ -627,8 +627,13 @@ struct type_caster { } // Conversion part 2 (C++ -> Python) - static handle cast(const absl::Cord& src, return_value_policy /*policy*/, + static handle cast(const absl::Cord& src, return_value_policy policy, handle /*parent*/) { +#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC) + if (policy == return_value_policy::_clif_automatic) { + return str(std::string(src)).release(); + } +#endif return bytes(std::string(src)).release(); } }; diff --git a/pybind11_abseil/requirements/BUILD b/pybind11_abseil/requirements/BUILD index 23c97f9..4567a90 100644 --- a/pybind11_abseil/requirements/BUILD +++ b/pybind11_abseil/requirements/BUILD @@ -1,13 +1,13 @@ +load("@python//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements") +load("@python//3.11:defs.bzl", compile_pip_requirements_3_11 = "compile_pip_requirements") +load("@python//3.12:defs.bzl", compile_pip_requirements_3_12 = "compile_pip_requirements") +load("@python//3.8:defs.bzl", compile_pip_requirements_3_8 = "compile_pip_requirements") +load("@python//3.9:defs.bzl", compile_pip_requirements_3_9 = "compile_pip_requirements") + package( default_visibility = ["//visibility:private"], ) -load("@python//3.12:defs.bzl", compile_pip_requirements_3_12 = "compile_pip_requirements") -load("@python//3.11:defs.bzl", compile_pip_requirements_3_11 = "compile_pip_requirements") -load("@python//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements") -load("@python//3.9:defs.bzl", compile_pip_requirements_3_9 = "compile_pip_requirements") -load("@python//3.8:defs.bzl", compile_pip_requirements_3_8 = "compile_pip_requirements") - compile_pip_requirements_3_12( name = "requirements_3_12", src = "requirements.in", diff --git a/pybind11_abseil/tests/absl_example.cc b/pybind11_abseil/tests/absl_example.cc index 91fdec4..576e5ca 100644 --- a/pybind11_abseil/tests/absl_example.cc +++ b/pybind11_abseil/tests/absl_example.cc @@ -363,6 +363,13 @@ static_assert( !std::is_const::value); // int is const, pointer is not. PYBIND11_MODULE(absl_example, m) { + m.attr("PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC") = +#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC) + true; +#else + false; +#endif + // absl::Time/Duration bindings. m.def("make_duration", &MakeDuration, arg("secs")); m.def("make_infinite_duration", &MakeInfiniteDuration); @@ -440,6 +447,11 @@ PYBIND11_MODULE(absl_example, m) { // absl::Cord bindings. m.def("check_absl_cord", &CheckAbslCord, arg("view"), arg("values")); m.def("return_absl_cord", &ReturnAbslCord, arg("values")); +#if defined(PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC) + m.def("return_absl_cord_clif_automatic", [](const std::string& values) { + return cast(ReturnAbslCord(values), return_value_policy::_clif_automatic); + }); +#endif // absl::optional bindings. m.def("check_optional", &CheckOptional, arg("optional") = absl::nullopt, diff --git a/pybind11_abseil/tests/absl_test.py b/pybind11_abseil/tests/absl_test.py index 555ad87..404327d 100644 --- a/pybind11_abseil/tests/absl_test.py +++ b/pybind11_abseil/tests/absl_test.py @@ -487,12 +487,24 @@ class AbslCordTest(absltest.TestCase): TEST_STRING = 'absl_Cord' TEST_BYTES = b'absl_Cord' - def test_return_absl_cord(self): + def test_return_absl_cord_rvp_not_specified(self): self.assertSequenceEqual( absl_example.return_absl_cord(self.TEST_STRING), self.TEST_BYTES) self.assertSequenceEqual( absl_example.return_absl_cord(self.TEST_BYTES), self.TEST_BYTES) + def test_return_absl_cord_rvp_clif_automatic(self): + if not absl_example.PYBIND11_HAS_RETURN_VALUE_POLICY_CLIF_AUTOMATIC: + self.skipTest('return_value_policy::_clif_automatic not available') + self.assertSequenceEqual( + absl_example.return_absl_cord_clif_automatic(self.TEST_STRING), + self.TEST_STRING, + ) + self.assertSequenceEqual( + absl_example.return_absl_cord_clif_automatic(self.TEST_BYTES), + self.TEST_STRING, + ) + def test_pass_absl_cord(self): self.assertTrue( absl_example.check_absl_cord(self.TEST_STRING, self.TEST_STRING))