Skip to main content

A helper / hack to allow us to cast a mmap.mmap or other buffer to a Cython pointer of the correct type.

Cython is capable of casting a lot of things to a C pointer of the correct type, especially with the aid of memoryview. However, in Python 2, memoryview lacks the memoryview.cast method (so Cython won’t let us change the dimensions of the array). Further, both Python 2 and 3 require the memory map to be writable (making the pointer type const does not seem to help here either).

This class takes a (possibly read-only) memmap object, and produces a Python object with a __getbuffer__ method that returns The Right Thing. It pretends that the underlying buffer is writable to make Cython happy. If you give it a read-only buffer, and try to write to the result, then you will have a bad time.

When a Python object is cast by Cython to a pointer, it holds a reference to the underlying Python object in order to prevent the memory to which it refers being garbage collected. The MagicMemoryView in turn keeps a reference to the underlying data, so everything should behave as expected.

Usage:

from magicmemoryview import MagicMemoryView

cdef double data[:, :, :]

data = MagicMemoryView(source_buffer, (24, 12, 25), "d")

Metadata

Release files for magicmemoryview 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magicmemoryview 0.1.5
File Size Uploaded
magicmemoryview-0.1.5.tar.gz 23.5 kB Details

Release files / magicmemoryview-0.1.5.tar.gz

Download URL magicmemoryview-0.1.5.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d1001a6fa084175ef65801f2133fac577a2a472bf8456e174dd328293dcf386b
BLAKE2b-256 checksum
How to use checksums
5789a174c3abaf1da91cedfca8dda9dddcac25d429cefcbc290ebcd7b88edb84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.5 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page