MCPcopy Create free account
hub / github.com/pybind/pybind11 / bytearray

Class bytearray

include/pybind11/pytypes.h:1845–1868  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1843/// \addtogroup pytypes
1844/// @{
1845class bytearray : public object {
1846public:
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

Callers 6

test_ctypes_from_bufferFunction · 0.85
TEST_SUBMODULEFunction · 0.85
test_constructorsFunction · 0.85
test_bytearray_to_stringFunction · 0.85
test_vector_bufferFunction · 0.85

Calls

no outgoing calls

Tested by 6

test_ctypes_from_bufferFunction · 0.68
TEST_SUBMODULEFunction · 0.68
test_constructorsFunction · 0.68
test_bytearray_to_stringFunction · 0.68
test_vector_bufferFunction · 0.68