mmap — 0.1.0

org.pycforge.mmap · Author: PyCForge

License: GPL-3.0-only · Created: 2026-11-17

Runtime conversion · Direct declared calls

Using this import · Functions · Mapped view methods · Conversion contract · Worked examples

Function index

mmap

Overview

Map a regular file or anonymous storage as an independently owned native view.

Using this import

Use *import mmap as regions. For an existing file, open it in rb or r+b and pass file.fileno() and length 0. For a private copy select regions.ACCESS_COPY.

Author's limitations

Use unnamed data mappings on a supported 64-bit host. Keep the backing file large enough for every live view. Exported memoryviews, named Windows mappings and trackfd fall outside this profile.

Release notes

Portable native memory mappings.

Conversion contract

Use *import mmap in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.

Create a mapping

mmap.mmap(fileno, length, access=..., offset=...)

On POSIX, the positional order is fileno, length, flags, prot, access, offset. On Windows it is fileno, length, tagname, access, offset. The Windows tag must be None.

Pass a live Runtime file descriptor or -1 for anonymous storage. Flush buffered file writes before mapping. Length zero maps the remainder of a nonempty file. Align a file offset to ALLOCATIONGRANULARITY.

The view owns its reference independently of the file. Use with or close() for prompt cleanup. The total live mapped-byte budget is max_file_read_bytes multiplied by max_file_operations. A map and a file each count toward max_open_files.

Mapped view methods

Call these methods on the returned view. Supply method arguments by position. An index reads one integer byte. Iteration yields one-byte bytes values. Slices return independent bytes and slice writes require equal-length data.

Mapped view operationResult and effect
read([n])Returns up to n bytes and advances. Omit n, pass None or use a negative n to read the remainder. EOF returns empty bytes.
readline()Returns bytes through the next LF or the end of the view. Advances the cursor and returns empty bytes at EOF.
read_byte()Returns the next byte as an integer from 0 through 255 and advances once. Raises ValueError at EOF.
write(data)Overwrites from the cursor and returns the byte count. Accepts bytes, bytearray or an open mapping. The write must fit in the view.
write_byte(value)Writes an integer from 0 through 255 and advances once. Returns None.
seek(offset[, whence])Positions the cursor between zero and len(view), inclusive. Use 0 for the start, 1 for current position or 2 for the end. Returns None in this Python 3.12 profile.
tell()Returns the current byte offset within the view.
find(needle[, start[, end]])Returns the first matching offset or -1. The default start is the cursor and the default end is the view length. Leaves the cursor in place.
rfind(needle[, start[, end]])Returns the last matching offset or -1, with the same bounds and cursor behavior as find.
move(dest, src, count)Copies count bytes within a writable view. The ranges may overlap. Leaves the cursor in place and returns None.
size()For a file-backed view, returns the current backing-file length. Use len(view) for the window size and for portable anonymous mappings.
resize(newsize)Resizes a shared writable view and adjusts its backing file when present. Use a positive size. The cursor is preserved, even after a shrink. Seek if it is now past the end. Returns None.
flush([offset[, size]])With no arguments, requests writeback for the whole shared view. For a range, supply both offset and size in bytes, keep it within the view and align the offset to the target allocation granularity. Returns None. Private edits stay private.
close() and closedclose() releases the view and can be called again. The closed attribute is a Boolean shared by aliases. Most operations on a closed view raise ValueError.
madvise(option[, start[, length]])On supported POSIX hosts, supplies an access hint and returns None. Use one of the published MADV constants and a page-aligned start within the view.

A binary file can write from an open mapping or readinto a writable mapping. Close other views before resizing a file on Windows. Keep the backing file long enough for every live view.

Module constants and error

Use these names through your module alias. The numeric values of platform constants come from the target that runs the program.

ACCESS_READ, ACCESS_WRITE, ACCESS_COPY — Open a read-only, shared writable or private copy-on-write view.

ACCESS_DEFAULT — Use the constructor’s default access on the target. Choose an explicit access mode when the same source will run on both platforms.

PAGESIZE, ALLOCATIONGRANULARITY — Target page size and mapping offset alignment, in bytes. Use ALLOCATIONGRANULARITY to align a file window.

MAP_SHARED, MAP_PRIVATE — POSIX flags for shared updates or a private copy.

MAP_ANON, MAP_ANONYMOUS — POSIX names for anonymous storage.

PROT_READ, PROT_WRITE — POSIX flags for read and write permissions.

MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, MADV_DONTNEED — Access hints for madvise on systems that provide it.

error — Alias for OSError. Use the qualified name in an exception handler.

mmap.mmap(fileno, length, *, access=ACCESS_DEFAULT, offset=0, flags=<POSIX default>, prot=<POSIX default>) -> mapped-bytes

Map a regular file or anonymous storage as an independently owned native view.

Normalized semantic slots: fileno, length, access, offset, flags, prot. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: An open descriptor or -1.

Argument 2: Byte length or zero for a whole file.

Argument 3: Read-only, shared-write or private-copy access.

Argument 4: Aligned byte offset within the file.

Argument 5: POSIX shared or private data mapping.

Argument 6: POSIX read or read/write protection.

Returns: An independently owned mapped view.

View example: mapped-bytes

Worked examples

Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.

Example: mapped-bytes

*import mmap as regions
def main():
    with regions.mmap(-1, 4) as view:
        view[:] = b"data"
        print(view.read(2), view.tell(), view[:])
    print(view.closed)
    return 0

Expected standard output:

b'da' 2 b'data'
True

Expected exit code: 0