| 1805 | /// @} pytypes |
| 1806 | |
| 1807 | inline bytes::bytes(const pybind11::str &s) { |
| 1808 | object temp = s; |
| 1809 | if (PyUnicode_Check(s.ptr())) { |
| 1810 | temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr())); |
| 1811 | if (!temp) { |
| 1812 | throw error_already_set(); |
| 1813 | } |
| 1814 | } |
| 1815 | char *buffer = nullptr; |
| 1816 | ssize_t length = 0; |
| 1817 | if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) { |
| 1818 | throw error_already_set(); |
| 1819 | } |
| 1820 | auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length)); |
| 1821 | if (!obj) { |
| 1822 | pybind11_fail("Could not allocate bytes object!"); |
| 1823 | } |
| 1824 | m_ptr = obj.release().ptr(); |
| 1825 | } |
| 1826 | |
| 1827 | inline str::str(const bytes &b) { |
| 1828 | char *buffer = nullptr; |