| 2391 | }; |
| 2392 | |
| 2393 | class memoryview : public object { |
| 2394 | public: |
| 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, |
no outgoing calls