MappedFile#
- class MappedFile(**kwargs)#
The MappedFile represents a file mapping created with
new(). It has only private members and should
not be accessed directly.
Constructors#
- class MappedFile
- classmethod new(filename: str, writable: bool) MappedFile#
Maps a file into memory. On UNIX, this is using the mmap() function.
If
writableisTrue, 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. usingfile_set_contents()).If
filenameis the name of an empty, regular file, the function will successfully return an emptyMappedFile. In other cases of size 0 (e.g. device files such as /dev/null),errorwill be set to theFileErrorvalueINVAL.Added in version 2.8.
- Parameters:
filename – The path of the file to load, in the GLib filename encoding
writable – whether the mapping should be writable
- classmethod new_from_fd(fd: int, writable: bool) MappedFile#
Maps a file into memory. On UNIX, this is using the mmap() function.
If
writableisTrue, 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. usingfile_set_contents()).Added in version 2.32.
- Parameters:
fd – The file descriptor of the file to load
writable – whether the mapping should be writable
Methods#
- class MappedFile
- free() None#
This call existed before
MappedFilehad refcounting and is currently exactly the same asunref().Added in version 2.8.
Deprecated since version 2.22: Use
unref()instead.
- get_bytes() Bytes#
Creates a new
Byteswhich references the data mapped fromfile. The mapped contents of the file must not be modified after creating this bytes object, because aBytesshould be immutable.Added in version 2.34.
- get_contents() str | None#
Returns the contents of a
MappedFile.Note that the contents may not be zero-terminated, even if the
MappedFileis backed by a text file.If the file is empty then
Noneis returned.Added in version 2.8.
- get_length() int#
Returns the length of the contents of a
MappedFile.Added in version 2.8.