File tree 3 files changed +28
-0
lines changed 3 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -1432,6 +1432,9 @@ class str : public object {
1432
1432
str (const char *c, const SzType &n)
1433
1433
: object(PyUnicode_FromStringAndSize(c, ssize_t_cast(n)), stolen_t {}) {
1434
1434
if (!m_ptr) {
1435
+ if (PyErr_Occurred ()) {
1436
+ throw error_already_set ();
1437
+ }
1435
1438
pybind11_fail (" Could not allocate string object!" );
1436
1439
}
1437
1440
}
@@ -1441,6 +1444,9 @@ class str : public object {
1441
1444
// NOLINTNEXTLINE(google-explicit-constructor)
1442
1445
str (const char *c = " " ) : object(PyUnicode_FromString(c), stolen_t {}) {
1443
1446
if (!m_ptr) {
1447
+ if (PyErr_Occurred ()) {
1448
+ throw error_already_set ();
1449
+ }
1444
1450
pybind11_fail (" Could not allocate string object!" );
1445
1451
}
1446
1452
}
@@ -1598,6 +1604,9 @@ inline str::str(const bytes &b) {
1598
1604
}
1599
1605
auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize (buffer, length));
1600
1606
if (!obj) {
1607
+ if (PyErr_Occurred ()) {
1608
+ throw error_already_set ();
1609
+ }
1601
1610
pybind11_fail (" Could not allocate string object!" );
1602
1611
}
1603
1612
m_ptr = obj.release ().ptr ();
Original file line number Diff line number Diff line change @@ -206,7 +206,12 @@ TEST_SUBMODULE(pytypes, m) {
206
206
m.def (" str_from_char_ssize_t" , []() { return py::str{" red" , (py::ssize_t ) 3 }; });
207
207
m.def (" str_from_char_size_t" , []() { return py::str{" blue" , (py::size_t ) 4 }; });
208
208
m.def (" str_from_string" , []() { return py::str (std::string (" baz" )); });
209
+ m.def (" str_from_std_string_input" , [](const std::string &stri) { return py::str (stri); });
210
+ m.def (" str_from_cstr_input" , [](const char *c_str) { return py::str (c_str); });
209
211
m.def (" str_from_bytes" , []() { return py::str (py::bytes (" boo" , 3 )); });
212
+ m.def (" str_from_bytes_input" ,
213
+ [](const py::bytes &encoded_str) { return py::str (encoded_str); });
214
+
210
215
m.def (" str_from_object" , [](const py::object &obj) { return py::str (obj); });
211
216
m.def (" repr_from_object" , [](const py::object &obj) { return py::repr (obj); });
212
217
m.def (" str_from_handle" , [](py::handle h) { return py::str (h); });
Original file line number Diff line number Diff line change @@ -244,6 +244,20 @@ def __repr__(self):
244
244
m .str_from_string_from_str (ucs_surrogates_str )
245
245
246
246
247
+ @pytest .mark .parametrize (
248
+ "func" ,
249
+ [
250
+ m .str_from_bytes_input ,
251
+ m .str_from_cstr_input ,
252
+ m .str_from_std_string_input ,
253
+ ],
254
+ )
255
+ def test_surrogate_pairs_unicode_error (func ):
256
+ input_str = "\ud83d \ude4f " .encode ("utf-8" , "surrogatepass" )
257
+ with pytest .raises (UnicodeDecodeError ):
258
+ func (input_str )
259
+
260
+
247
261
def test_bytes (doc ):
248
262
assert m .bytes_from_char_ssize_t ().decode () == "green"
249
263
assert m .bytes_from_char_size_t ().decode () == "purple"
You can’t perform that action at this time.
0 commit comments