| 1843 | /// \addtogroup pytypes |
| 1844 | /// @{ |
| 1845 | class bytearray : public object { |
| 1846 | public: |
| 1847 | PYBIND11_OBJECT_CVT(bytearray, object, PyByteArray_Check, PyByteArray_FromObject) |
| 1848 | |
| 1849 | template <typename SzType, detail::enable_if_t<std::is_integral<SzType>::value, int> = 0> |
| 1850 | bytearray(const char *c, const SzType &n) |
| 1851 | : object(PyByteArray_FromStringAndSize(c, ssize_t_cast(n)), stolen_t{}) { |
| 1852 | if (!m_ptr) { |
| 1853 | pybind11_fail("Could not allocate bytearray object!"); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | bytearray() : bytearray("", 0) {} |
| 1858 | |
| 1859 | explicit bytearray(const std::string &s) : bytearray(s.data(), s.size()) {} |
| 1860 | |
| 1861 | size_t size() const { return static_cast<size_t>(PyByteArray_Size(m_ptr)); } |
| 1862 | |
| 1863 | explicit operator std::string() const { |
| 1864 | char *buffer = PyByteArray_AS_STRING(m_ptr); |
| 1865 | ssize_t size = PyByteArray_GET_SIZE(m_ptr); |
| 1866 | return std::string(buffer, static_cast<size_t>(size)); |
| 1867 | } |
| 1868 | }; |
| 1869 | // Note: breathe >= 4.17.0 will fail to build docs if the below two constructors |
| 1870 | // are included in the doxygen group; close here and reopen after as a workaround |
| 1871 | /// @} pytypes |
no outgoing calls