Skip to content

GLib.MappedFile

record (struct)

The MappedFile represents a file mapping created with MappedFile.new. It has only private members and should not be accessed directly.

Constructors

new

@classmethod
def new(cls, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes], writable: bool) -> MappedFile

Maps a file into memory. On UNIX, this is using the mmap() function.

If writable is True, the mapped buffer may be modified, otherwise it is an error to modify the mapped buffer. Modifications to the buffer are not visible to other processes mapping the same file, and are not written back to the file.

Note that modifications of the underlying file might affect the contents of the MappedFile. Therefore, mapping should only be used if the file will not be modified, or if all modifications of the file are done atomically (e.g. using file_set_contents).

If filename is the name of an empty, regular file, the function will successfully return an empty MappedFile. In other cases of size 0 (e.g. device files such as /dev/null), error will be set to the FileError value FileError.INVAL.

Parameters:

  • filename — The path of the file to load, in the GLib filename encoding
  • writable — whether the mapping should be writable

new_from_fd

@classmethod
def new_from_fd(cls, fd: int, writable: bool) -> MappedFile

Maps a file into memory. On UNIX, this is using the mmap() function.

If writable is True, the mapped buffer may be modified, otherwise it is an error to modify the mapped buffer. Modifications to the buffer are not visible to other processes mapping the same file, and are not written back to the file.

Note that modifications of the underlying file might affect the contents of the MappedFile. Therefore, mapping should only be used if the file will not be modified, or if all modifications of the file are done atomically (e.g. using file_set_contents).

Parameters:

  • fd — The file descriptor of the file to load
  • writable — whether the mapping should be writable

Methods

free

def free(self) -> None

:::warning Deprecated since 2.22 This API is deprecated. :::

This call existed before MappedFile had refcounting and is currently exactly the same as MappedFile.unref.

get_bytes

def get_bytes(self) -> Bytes

Creates a new Bytes which references the data mapped from file. The mapped contents of the file must not be modified after creating this bytes object, because a Bytes should be immutable.

get_contents

def get_contents(self) -> str | None

Returns the contents of a MappedFile.

Note that the contents may not be zero-terminated, even if the MappedFile is backed by a text file.

If the file is empty then None is returned.

get_length

def get_length(self) -> int

Returns the length of the contents of a MappedFile.

ref

def ref(self) -> MappedFile

Increments the reference count of file by one. It is safe to call this function from any thread.

unref

def unref(self) -> None

Decrements the reference count of file by one. If the reference count drops to 0, unmaps the buffer of file and frees it.

It is safe to call this function from any thread.

Since 2.22