GLib.MappedFile

Fields

None

Methods

class

new (filename, writable)

class

new_from_fd (fd, writable)

free ()

get_bytes ()

get_contents ()

get_length ()

ref ()

unref ()

Details

class GLib.MappedFile

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

classmethod new(filename, writable)[source]
Parameters:
  • filename (str) – The path of the file to load, in the GLib filename encoding

  • writable (bool) – whether the mapping should be writable

Raises:

GLib.Error

Returns:

a newly allocated GLib.MappedFile which must be unref’d with GLib.MappedFile.unref(), or None if the mapping failed.

Return type:

GLib.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 GLib.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 GLib.file_set_contents()).

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

New in version 2.8.

classmethod new_from_fd(fd, writable)[source]
Parameters:
  • fd (int) – The file descriptor of the file to load

  • writable (bool) – whether the mapping should be writable

Raises:

GLib.Error

Returns:

a newly allocated GLib.MappedFile which must be unref’d with GLib.MappedFile.unref(), or None if the mapping failed.

Return type:

GLib.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 GLib.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 GLib.file_set_contents()).

New in version 2.32.

free()[source]

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

New in version 2.8.

Deprecated since version 2.22: Use GLib.MappedFile.unref() instead.

get_bytes()[source]
Returns:

A newly allocated GLib.Bytes referencing data from self

Return type:

GLib.Bytes

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

New in version 2.34.

get_contents()[source]
Returns:

the contents of self, or None.

Return type:

str

Returns the contents of a GLib.MappedFile.

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

If the file is empty then None is returned.

New in version 2.8.

get_length()[source]
Returns:

the length of the contents of self.

Return type:

int

Returns the length of the contents of a GLib.MappedFile.

New in version 2.8.

ref()[source]
Returns:

the passed in GLib.MappedFile.

Return type:

GLib.MappedFile

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

New in version 2.22.

unref()[source]

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

It is safe to call this function from any thread.

New in version 2.22.