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

Method from_memory

include/pybind11/pytypes.h:2491–2498  ·  view source on GitHub ↗

\rst Creates ``memoryview`` from static memory. This method is meant for providing a ``memoryview`` for C/C++ buffer not managed by Python. The caller is responsible for managing the lifetime of ``mem``, which MUST outlive the memoryview constructed here. See also: Python C API documentation for `PyMemoryView_FromBuffer`_. .. _PyMemoryView_FromMem

Source from the content-addressed store, hash-verified

2489 https://docs.python.org/c-api/memoryview.html#c.PyMemoryView_FromMemory
2490 \endrst */
2491 static memoryview from_memory(void *mem, ssize_t size, bool readonly = false) {
2492 PyObject *ptr = PyMemoryView_FromMemory(
2493 reinterpret_cast<char *>(mem), size, (readonly) ? PyBUF_READ : PyBUF_WRITE);
2494 if (!ptr) {
2495 pybind11_fail("Could not allocate memoryview object!");
2496 }
2497 return memoryview(object(ptr, stolen_t{}));
2498 }
2499
2500 static memoryview from_memory(const void *mem, ssize_t size) {
2501 return memoryview::from_memory(const_cast<void *>(mem), size, true);

Callers

nothing calls this directly

Calls 4

memoryviewClass · 0.85
objectFunction · 0.70
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected