Skip to content

Commit da15bb2

Browse files
porrashuangpre-commit-ci[bot]Porras Huang
authored
Cast bytearray to string (#3707)
* Add bytearray to string cast, testcase and rename load_bytes to load_raw * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * New bytearray test case and convert failure to pybind11_fail * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix merge comments * Actually fix merge comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Assert early if AsString fails Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Porras Huang <[email protected]>
1 parent 91f597b commit da15bb2

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

include/pybind11/cast.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ struct string_caster {
380380
return false;
381381
}
382382
if (!PyUnicode_Check(load_src.ptr())) {
383-
return load_bytes(load_src);
383+
return load_raw(load_src);
384384
}
385385

386386
// For UTF-8 we avoid the need for a temporary `bytes` object by using
@@ -458,26 +458,37 @@ struct string_caster {
458458
#endif
459459
}
460460

461-
// When loading into a std::string or char*, accept a bytes object as-is (i.e.
461+
// When loading into a std::string or char*, accept a bytes/bytearray object as-is (i.e.
462462
// without any encoding/decoding attempt). For other C++ char sizes this is a no-op.
463463
// which supports loading a unicode from a str, doesn't take this path.
464464
template <typename C = CharT>
465-
bool load_bytes(enable_if_t<std::is_same<C, char>::value, handle> src) {
465+
bool load_raw(enable_if_t<std::is_same<C, char>::value, handle> src) {
466466
if (PYBIND11_BYTES_CHECK(src.ptr())) {
467467
// We were passed raw bytes; accept it into a std::string or char*
468468
// without any encoding attempt.
469469
const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
470-
if (bytes) {
471-
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
472-
return true;
470+
if (!bytes) {
471+
pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
472+
}
473+
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
474+
return true;
475+
}
476+
if (PyByteArray_Check(src.ptr())) {
477+
// We were passed a bytearray; accept it into a std::string or char*
478+
// without any encoding attempt.
479+
const char *bytearray = PyByteArray_AsString(src.ptr());
480+
if (!bytearray) {
481+
pybind11_fail("Unexpected PyByteArray_AsString() failure.");
473482
}
483+
value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
484+
return true;
474485
}
475486

476487
return false;
477488
}
478489

479490
template <typename C = CharT>
480-
bool load_bytes(enable_if_t<!std::is_same<C, char>::value, handle>) {
491+
bool load_raw(enable_if_t<!std::is_same<C, char>::value, handle>) {
481492
return false;
482493
}
483494
};

tests/test_builtin_casters.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def test_bytes_to_string():
133133
assert m.string_length("💩".encode()) == 4
134134

135135

136+
def test_bytearray_to_string():
137+
"""Tests the ability to pass bytearray to C++ string-accepting functions"""
138+
assert m.string_length(bytearray(b"Hi")) == 2
139+
assert m.strlen(bytearray(b"bytearray")) == 9
140+
assert m.string_length(bytearray()) == 0
141+
assert m.string_length(bytearray("🦜", "utf-8", "strict")) == 4
142+
assert m.string_length(bytearray(b"\x80")) == 1
143+
144+
136145
@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
137146
def test_string_view(capture):
138147
"""Tests support for C++17 string_view arguments and return values"""

0 commit comments

Comments
 (0)