mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Raise codec errors when casting to std::string (#2903)
* Raise codec errors when casting to std::string Allow the codec's exception to be raised instead of RuntimeError when casting from py::str to std::string. PY2 allows ucs surrogates in UTF-8 conversion Signed-off-by: Shane Loretz <sloretz@openrobotics.org> Signed-off-by: Shane Loretz <sloretz@osrfoundation.org> * Attempt to fix py2 error * Revert all unicode literals * Fixed Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
This commit is contained in:
@@ -133,6 +133,14 @@ def test_str(doc):
|
||||
else:
|
||||
assert m.str_from_handle(malformed_utf8) == "b'\\x80'"
|
||||
|
||||
assert m.str_from_string_from_str("this is a str") == "this is a str"
|
||||
ucs_surrogates_str = u"\udcc3"
|
||||
if env.PY2:
|
||||
assert u"\udcc3" == m.str_from_string_from_str(ucs_surrogates_str)
|
||||
else:
|
||||
with pytest.raises(UnicodeEncodeError):
|
||||
m.str_from_string_from_str(ucs_surrogates_str)
|
||||
|
||||
|
||||
def test_bytes(doc):
|
||||
assert m.bytes_from_string().decode() == "foo"
|
||||
|
||||
Reference in New Issue
Block a user