@@ -1243,8 +1243,8 @@ class str : public object {
1243
1243
}
1244
1244
char *buffer = nullptr ;
1245
1245
ssize_t length = 0 ;
1246
- if (PYBIND11_BYTES_AS_STRING_AND_SIZE (temp.ptr (), &buffer, &length)) {
1247
- pybind11_fail ( " Unable to extract string contents! (invalid type) " );
1246
+ if (PyBytes_AsStringAndSize (temp.ptr (), &buffer, &length) != 0 ) {
1247
+ throw error_already_set ( );
1248
1248
}
1249
1249
return std::string (buffer, (size_t ) length);
1250
1250
}
@@ -1299,14 +1299,7 @@ class bytes : public object {
1299
1299
explicit bytes (const pybind11::str &s);
1300
1300
1301
1301
// NOLINTNEXTLINE(google-explicit-constructor)
1302
- operator std::string () const {
1303
- char *buffer = nullptr ;
1304
- ssize_t length = 0 ;
1305
- if (PYBIND11_BYTES_AS_STRING_AND_SIZE (m_ptr, &buffer, &length)) {
1306
- pybind11_fail (" Unable to extract bytes contents!" );
1307
- }
1308
- return std::string (buffer, (size_t ) length);
1309
- }
1302
+ operator std::string () const { return string_op<std::string>(); }
1310
1303
1311
1304
#ifdef PYBIND11_HAS_STRING_VIEW
1312
1305
// enable_if is needed to avoid "ambiguous conversion" errors (see PR #3521).
@@ -1318,15 +1311,18 @@ class bytes : public object {
1318
1311
// valid so long as the `bytes` instance remains alive and so generally should not outlive the
1319
1312
// lifetime of the `bytes` instance.
1320
1313
// NOLINTNEXTLINE(google-explicit-constructor)
1321
- operator std::string_view () const {
1314
+ operator std::string_view () const { return string_op<std::string_view>(); }
1315
+ #endif
1316
+ private:
1317
+ template <typename T>
1318
+ T string_op () const {
1322
1319
char *buffer = nullptr ;
1323
1320
ssize_t length = 0 ;
1324
- if (PYBIND11_BYTES_AS_STRING_AND_SIZE (m_ptr, &buffer, &length)) {
1325
- pybind11_fail ( " Unable to extract bytes contents! " );
1321
+ if (PyBytes_AsStringAndSize (m_ptr, &buffer, &length) != 0 ) {
1322
+ throw error_already_set ( );
1326
1323
}
1327
1324
return {buffer, static_cast <size_t >(length)};
1328
1325
}
1329
- #endif
1330
1326
};
1331
1327
// Note: breathe >= 4.17.0 will fail to build docs if the below two constructors
1332
1328
// are included in the doxygen group; close here and reopen after as a workaround
@@ -1337,13 +1333,13 @@ inline bytes::bytes(const pybind11::str &s) {
1337
1333
if (PyUnicode_Check (s.ptr ())) {
1338
1334
temp = reinterpret_steal<object>(PyUnicode_AsUTF8String (s.ptr ()));
1339
1335
if (!temp) {
1340
- pybind11_fail ( " Unable to extract string contents! (encoding issue) " );
1336
+ throw error_already_set ( );
1341
1337
}
1342
1338
}
1343
1339
char *buffer = nullptr ;
1344
1340
ssize_t length = 0 ;
1345
- if (PYBIND11_BYTES_AS_STRING_AND_SIZE (temp.ptr (), &buffer, &length)) {
1346
- pybind11_fail ( " Unable to extract string contents! (invalid type) " );
1341
+ if (PyBytes_AsStringAndSize (temp.ptr (), &buffer, &length) != 0 ) {
1342
+ throw error_already_set ( );
1347
1343
}
1348
1344
auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE (buffer, length));
1349
1345
if (!obj) {
@@ -1355,8 +1351,8 @@ inline bytes::bytes(const pybind11::str &s) {
1355
1351
inline str::str (const bytes &b) {
1356
1352
char *buffer = nullptr ;
1357
1353
ssize_t length = 0 ;
1358
- if (PYBIND11_BYTES_AS_STRING_AND_SIZE (b.ptr (), &buffer, &length)) {
1359
- pybind11_fail ( " Unable to extract bytes contents! " );
1354
+ if (PyBytes_AsStringAndSize (b.ptr (), &buffer, &length) != 0 ) {
1355
+ throw error_already_set ( );
1360
1356
}
1361
1357
auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize (buffer, length));
1362
1358
if (!obj) {
0 commit comments