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

Class memoryview

include/pybind11/pytypes.h:2393–2509  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2391};
2392
2393class memoryview : public object {
2394public:
2395 PYBIND11_OBJECT_CVT(memoryview, object, PyMemoryView_Check, PyMemoryView_FromObject)
2396
2397 /** \rst
2398 Creates ``memoryview`` from ``buffer_info``.
2399
2400 ``buffer_info`` must be created from ``buffer::request()``. Otherwise
2401 throws an exception.
2402
2403 For creating a ``memoryview`` from objects that support buffer protocol,
2404 use ``memoryview(const object& obj)`` instead of this constructor.
2405 \endrst */
2406 explicit memoryview(const buffer_info &info) {
2407 if (!info.view()) {
2408 pybind11_fail("Prohibited to create memoryview without Py_buffer");
2409 }
2410 // Note: PyMemoryView_FromBuffer never increments obj reference.
2411 m_ptr = (info.view()->obj) ? PyMemoryView_FromObject(info.view()->obj)
2412 : PyMemoryView_FromBuffer(info.view());
2413 if (!m_ptr) {
2414 pybind11_fail("Unable to create memoryview from buffer descriptor");
2415 }
2416 }
2417
2418 /** \rst
2419 Creates ``memoryview`` from static buffer.
2420
2421 This method is meant for providing a ``memoryview`` for C/C++ buffer not
2422 managed by Python. The caller is responsible for managing the lifetime
2423 of ``ptr`` and ``format``, which MUST outlive the memoryview constructed
2424 here.
2425
2426 See also: Python C API documentation for `PyMemoryView_FromBuffer`_.
2427
2428 .. _PyMemoryView_FromBuffer:
2429 https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromBuffer
2430
2431 :param ptr: Pointer to the buffer.
2432 :param itemsize: Byte size of an element.
2433 :param format: Pointer to the null-terminated format string. For
2434 homogeneous Buffers, this should be set to
2435 ``format_descriptor<T>::value``.
2436 :param shape: Shape of the tensor (1 entry per dimension).
2437 :param strides: Number of bytes between adjacent entries (for each
2438 per dimension).
2439 :param readonly: Flag to indicate if the underlying storage may be
2440 written to.
2441 \endrst */
2442 static memoryview from_buffer(void *ptr,
2443 ssize_t itemsize,
2444 const char *format,
2445 detail::any_container<ssize_t> shape,
2446 detail::any_container<ssize_t> strides,
2447 bool readonly = false);
2448
2449 static memoryview from_buffer(const void *ptr,
2450 ssize_t itemsize,

Callers 9

test_to_pythonFunction · 0.85
test_inherited_protocolFunction · 0.85
test_readonly_bufferFunction · 0.85
test_buffer_exceptionFunction · 0.85
TEST_SUBMODULEFunction · 0.85
test_vector_bufferFunction · 0.85
from_memoryMethod · 0.85
pytypes.hFile · 0.85

Calls

no outgoing calls

Tested by 7

test_to_pythonFunction · 0.68
test_inherited_protocolFunction · 0.68
test_readonly_bufferFunction · 0.68
test_buffer_exceptionFunction · 0.68
TEST_SUBMODULEFunction · 0.68
test_vector_bufferFunction · 0.68