Skip to content

Commit 0de9906

Browse files
committed
Revert "[smart_holder] Add a new return value policy return_as_bytes (#3838)"
This reverts commit 7064d43. Conflicts resolved in: include/pybind11/eigen.h tests/test_builtin_casters.cpp
1 parent e16b9ea commit 0de9906

8 files changed

+4
-104
lines changed

include/pybind11/cast.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,11 @@ struct string_caster {
476476
return true;
477477
}
478478

479-
static handle cast(const StringType &src, return_value_policy policy, handle /* parent */) {
479+
static handle
480+
cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) {
480481
const char *buffer = reinterpret_cast<const char *>(src.data());
481482
auto nbytes = ssize_t(src.size() * sizeof(CharT));
482-
handle s;
483-
if (policy == return_value_policy::_return_as_bytes) {
484-
s = PyBytes_FromStringAndSize(buffer, nbytes);
485-
} else {
486-
s = decode_utfN(buffer, nbytes);
487-
}
483+
handle s = decode_utfN(buffer, nbytes);
488484
if (!s) {
489485
throw error_already_set();
490486
}

include/pybind11/detail/common.h

+1-13
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,7 @@ enum class return_value_policy : uint8_t {
542542
collected while Python is still using the child. More advanced
543543
variations of this scheme are also possible using combinations of
544544
return_value_policy::reference and the keep_alive call policy */
545-
reference_internal,
546-
547-
/** With this policy, C++ string types are converted to Python bytes,
548-
instead of str. This is most useful when a C++ function returns a
549-
container-like type with nested C++ string types, and `py::bytes` cannot
550-
be applied easily. Dictionary like types might not work, for example,
551-
`Dict[str, bytes]`, because this policy forces all string return values
552-
to be converted to bytes. Note that this return_value_policy is not
553-
concerned with lifetime/ownership semantics, like the other policies,
554-
but the purpose of _return_as_bytes is certain to be orthogonal, because
555-
C++ strings are always copied to Python `bytes` or `str`.
556-
NOTE: This policy is NOT available on master. */
557-
_return_as_bytes
545+
reference_internal
558546
};
559547

560548
#define PYBIND11_HAS_RETURN_VALUE_POLICY_RETURN_AS_BYTES

include/pybind11/detail/smart_holder_type_casters.h

-3
Original file line numberDiff line numberDiff line change
@@ -909,15 +909,12 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
909909
break;
910910
case return_value_policy::take_ownership:
911911
throw cast_error("Invalid return_value_policy for shared_ptr (take_ownership).");
912-
break;
913912
case return_value_policy::copy:
914913
case return_value_policy::move:
915914
break;
916915
case return_value_policy::reference:
917916
throw cast_error("Invalid return_value_policy for shared_ptr (reference).");
918-
break;
919917
case return_value_policy::reference_internal:
920-
case return_value_policy::_return_as_bytes:
921918
break;
922919
}
923920
if (!src) {

include/pybind11/detail/type_caster_base.h

-4
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,6 @@ class type_caster_generic {
625625
keep_alive_impl(inst, parent);
626626
break;
627627

628-
case return_value_policy::_return_as_bytes:
629-
pybind11_fail("return_value_policy::_return_as_bytes does not apply.");
630-
break;
631-
632628
default:
633629
throw cast_error("unhandled return_value_policy: should not happen!");
634630
}

tests/test_builtin_casters.cpp

-31
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@
1111

1212
#include "pybind11_tests.h"
1313

14-
#include <utility>
15-
1614
struct ConstRefCasted {
1715
int tag;
1816
};
1917

20-
struct StringAttr {
21-
explicit StringAttr(std::string v) : value(std::move(v)) {}
22-
std::string value;
23-
};
24-
2518
PYBIND11_NAMESPACE_BEGIN(pybind11)
2619
PYBIND11_NAMESPACE_BEGIN(detail)
2720
template <>
@@ -390,29 +383,5 @@ TEST_SUBMODULE(builtin_casters, m) {
390383
m.def("takes_const_ref_wrap",
391384
[](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; });
392385

393-
// test return_value_policy::_return_as_bytes
394-
m.def(
395-
"invalid_utf8_string_as_bytes",
396-
[]() { return std::string("\xba\xd0\xba\xd0"); },
397-
py::return_value_policy::_return_as_bytes);
398-
m.def("invalid_utf8_string_as_str", []() { return std::string("\xba\xd0\xba\xd0"); });
399-
m.def(
400-
"invalid_utf8_char_array_as_bytes",
401-
[]() { return "\xba\xd0\xba\xd0"; },
402-
py::return_value_policy::_return_as_bytes);
403-
py::class_<StringAttr>(m, "StringAttr")
404-
.def(py::init<std::string>())
405-
.def_property(
406-
"value",
407-
py::cpp_function([](StringAttr &self) { return self.value; },
408-
py::return_value_policy::_return_as_bytes),
409-
py::cpp_function([](StringAttr &self, std::string v) { self.value = std::move(v); }));
410-
#ifdef PYBIND11_HAS_STRING_VIEW
411-
m.def(
412-
"invalid_utf8_string_view_as_bytes",
413-
[]() { return std::string_view("\xba\xd0\xba\xd0"); },
414-
py::return_value_policy::_return_as_bytes);
415-
#endif
416-
417386
PYBIND11_WARNING_POP
418387
}

tests/test_builtin_casters.py

-14
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,3 @@ def test_const_ref_caster():
526526
assert m.takes_const_ptr(x) == 5
527527
assert m.takes_const_ref(x) == 4
528528
assert m.takes_const_ref_wrap(x) == 4
529-
530-
531-
def test_return_as_bytes_policy():
532-
expected_return_value = b"\xba\xd0\xba\xd0"
533-
assert m.invalid_utf8_string_as_bytes() == expected_return_value
534-
with pytest.raises(UnicodeDecodeError):
535-
m.invalid_utf8_string_as_str()
536-
assert m.invalid_utf8_char_array_as_bytes() == expected_return_value
537-
obj = m.StringAttr(expected_return_value)
538-
assert obj.value == expected_return_value
539-
obj.value = "123"
540-
assert obj.value == b"123"
541-
if hasattr(m, "has_string_view"):
542-
assert m.invalid_utf8_string_view_as_bytes() == expected_return_value

tests/test_stl.cpp

-21
Original file line numberDiff line numberDiff line change
@@ -546,25 +546,4 @@ TEST_SUBMODULE(stl, m) {
546546
[]() { return new std::vector<bool>(4513); },
547547
// Without explicitly specifying `take_ownership`, this function leaks.
548548
py::return_value_policy::take_ownership);
549-
550-
// test return_value_policy::_return_as_bytes
551-
m.def(
552-
"invalid_utf8_string_array_as_bytes",
553-
[]() { return std::array<std::string, 1>{{"\xba\xd0\xba\xd0"}}; },
554-
py::return_value_policy::_return_as_bytes);
555-
m.def("invalid_utf8_string_array_as_str",
556-
[]() { return std::array<std::string, 1>{{"\xba\xd0\xba\xd0"}}; });
557-
#ifdef PYBIND11_HAS_OPTIONAL
558-
m.def(
559-
"invalid_utf8_optional_string_as_bytes",
560-
[]() { return std::optional<std::string>{"\xba\xd0\xba\xd0"}; },
561-
py::return_value_policy::_return_as_bytes);
562-
#endif
563-
564-
#ifdef PYBIND11_TEST_VARIANT
565-
m.def(
566-
"invalid_utf8_variant_string_as_bytes",
567-
[]() { return variant<std::string, int>{"\xba\xd0\xba\xd0"}; },
568-
py::return_value_policy::_return_as_bytes);
569-
#endif
570549
}

tests/test_stl.py

-11
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,3 @@ def test_return_vector_bool_raw_ptr():
379379
v = m.return_vector_bool_raw_ptr()
380380
assert isinstance(v, list)
381381
assert len(v) == 4513
382-
383-
384-
def test_return_as_bytes_policy():
385-
expected_return_value = b"\xba\xd0\xba\xd0"
386-
assert m.invalid_utf8_string_array_as_bytes() == [expected_return_value]
387-
with pytest.raises(UnicodeDecodeError):
388-
m.invalid_utf8_string_array_as_str()
389-
if hasattr(m, "has_optional"):
390-
assert m.invalid_utf8_optional_string_as_bytes() == expected_return_value
391-
if hasattr(m, "load_variant"):
392-
assert m.invalid_utf8_variant_string_as_bytes() == expected_return_value

0 commit comments

Comments
 (0)