2000-06-17 19:39:05 -03:00
|
|
|
\section{\module{mmap} ---
|
2001-11-13 19:11:19 -04:00
|
|
|
Memory-mapped file support}
|
2000-06-17 19:39:05 -03:00
|
|
|
|
|
|
|
\declaremodule{builtin}{mmap}
|
2001-11-28 03:26:15 -04:00
|
|
|
\modulesynopsis{Interface to memory-mapped files for \UNIX\ and Windows.}
|
2000-06-17 19:39:05 -03:00
|
|
|
|
2001-12-03 14:27:22 -04:00
|
|
|
Memory-mapped file objects behave like both strings and like
|
|
|
|
file objects. Unlike normal string objects, however, these are
|
|
|
|
mutable. You can use mmap objects in most places where strings
|
2000-06-17 19:39:05 -03:00
|
|
|
are expected; for example, you can use the \module{re} module to
|
|
|
|
search through a memory-mapped file. Since they're mutable, you can
|
2000-09-05 10:50:21 -03:00
|
|
|
change a single character by doing \code{obj[\var{index}] = 'a'}, or
|
2000-06-17 19:39:05 -03:00
|
|
|
change a substring by assigning to a slice:
|
2000-09-05 10:50:21 -03:00
|
|
|
\code{obj[\var{i1}:\var{i2}] = '...'}. You can also read and write
|
2000-06-17 19:39:05 -03:00
|
|
|
data starting at the current file position, and \method{seek()}
|
|
|
|
through the file to different positions.
|
|
|
|
|
2001-09-25 16:00:08 -03:00
|
|
|
A memory-mapped file is created by the \function{mmap()} function,
|
|
|
|
which is different on \UNIX{} and on Windows. In either case you must
|
|
|
|
provide a file descriptor for a file opened for update.
|
|
|
|
If you wish to map an existing Python file object, use its
|
|
|
|
\method{fileno()} method to obtain the correct value for the
|
|
|
|
\var{fileno} parameter. Otherwise, you can open the file using the
|
|
|
|
\function{os.open()} function, which returns a file descriptor
|
|
|
|
directly (the file still needs to be closed when done).
|
2000-06-17 19:39:05 -03:00
|
|
|
|
2001-12-03 14:27:22 -04:00
|
|
|
For both the \UNIX{} and Windows versions of the function,
|
|
|
|
\var{access} may be specified as an optional keyword parameter.
|
|
|
|
\var{access} accepts one of three values: \constant{ACCESS_READ},
|
|
|
|
\constant{ACCESS_WRITE}, or \constant{ACCESS_COPY} to specify
|
|
|
|
readonly, write-through or copy-on-write memory respectively.
|
|
|
|
\var{access} can be used on both \UNIX{} and Windows. If
|
|
|
|
\var{access} is not specified, Windows mmap returns a write-through
|
|
|
|
mapping. The initial memory values for all three access types are
|
|
|
|
taken from the specified file. Assignment to an
|
|
|
|
\constant{ACCESS_READ} memory map raises a \exception{TypeError}
|
|
|
|
exception. Assignment to an \constant{ACCESS_WRITE} memory map
|
2005-01-18 23:39:17 -04:00
|
|
|
affects both memory and the underlying file. Assignment to an
|
2001-12-03 14:27:22 -04:00
|
|
|
\constant{ACCESS_COPY} memory map affects memory but does not update
|
2006-02-05 01:45:43 -04:00
|
|
|
the underlying file. \versionchanged[To map anonymous memory,
|
|
|
|
-1 should be passed as the fileno along with the length]{2.5}
|
2001-12-03 14:27:22 -04:00
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
\begin{funcdesc}{mmap}{fileno, length\optional{, tagname\optional{, access}}}
|
|
|
|
\strong{(Windows version)} Maps \var{length} bytes from the file
|
|
|
|
specified by the file handle \var{fileno}, and returns a mmap
|
2004-06-06 13:51:46 -03:00
|
|
|
object. If \var{length} is larger than the current size of the file,
|
|
|
|
the file is extended to contain \var{length} bytes. If \var{length}
|
|
|
|
is \code{0}, the maximum length of the map is the current size
|
|
|
|
of the file, except that if the file is empty Windows raises an
|
|
|
|
exception (you cannot create an empty mapping on Windows).
|
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
\var{tagname}, if specified and not \code{None}, is a string giving
|
|
|
|
a tag name for the mapping. Windows allows you to have many
|
|
|
|
different mappings against the same file. If you specify the name
|
|
|
|
of an existing tag, that tag is opened, otherwise a new tag of this
|
|
|
|
name is created. If this parameter is omitted or \code{None}, the
|
|
|
|
mapping is created without a name. Avoiding the use of the tag
|
|
|
|
parameter will assist in keeping your code portable between \UNIX{}
|
|
|
|
and Windows.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{funcdesc}
|
|
|
|
|
2001-12-03 14:27:22 -04:00
|
|
|
\begin{funcdescni}{mmap}{fileno, length\optional{, flags\optional{,
|
|
|
|
prot\optional{, access}}}}
|
2001-11-13 19:11:19 -04:00
|
|
|
\strong{(\UNIX{} version)} Maps \var{length} bytes from the file
|
|
|
|
specified by the file descriptor \var{fileno}, and returns a mmap
|
2005-03-03 07:22:44 -04:00
|
|
|
object. If \var{length} is \code{0}, the maximum length of the map
|
2006-01-23 17:33:03 -04:00
|
|
|
will be the current size of the file when \function{mmap()} is
|
2005-03-03 07:22:44 -04:00
|
|
|
called.
|
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
\var{flags} specifies the nature of the mapping.
|
|
|
|
\constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
|
|
|
|
changes to the contents of the mmap object will be private to this
|
|
|
|
process, and \constant{MAP_SHARED} creates a mapping that's shared
|
|
|
|
with all other processes mapping the same areas of the file. The
|
|
|
|
default value is \constant{MAP_SHARED}.
|
2004-06-06 13:51:46 -03:00
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
\var{prot}, if specified, gives the desired memory protection; the
|
|
|
|
two most useful values are \constant{PROT_READ} and
|
|
|
|
\constant{PROT_WRITE}, to specify that the pages may be read or
|
|
|
|
written. \var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
|
2004-06-06 13:51:46 -03:00
|
|
|
|
2001-11-13 19:11:19 -04:00
|
|
|
\var{access} may be specified in lieu of \var{flags} and \var{prot}
|
2001-12-03 14:27:22 -04:00
|
|
|
as an optional keyword parameter. It is an error to specify both
|
|
|
|
\var{flags}, \var{prot} and \var{access}. See the description of
|
|
|
|
\var{access} above for information on how to use this parameter.
|
|
|
|
\end{funcdescni}
|
2000-06-17 19:39:05 -03:00
|
|
|
|
2001-09-25 16:00:08 -03:00
|
|
|
|
2000-06-17 19:39:05 -03:00
|
|
|
Memory-mapped file objects support the following methods:
|
|
|
|
|
|
|
|
|
|
|
|
\begin{methoddesc}{close}{}
|
2001-11-13 19:11:19 -04:00
|
|
|
Close the file. Subsequent calls to other methods of the object
|
|
|
|
will result in an exception being raised.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-09-05 10:50:21 -03:00
|
|
|
\begin{methoddesc}{find}{string\optional{, start}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Returns the lowest index in the object where the substring
|
|
|
|
\var{string} is found. Returns \code{-1} on failure. \var{start}
|
|
|
|
is the index at which the search begins, and defaults to zero.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-09-05 10:50:21 -03:00
|
|
|
\begin{methoddesc}{flush}{\optional{offset, size}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Flushes changes made to the in-memory copy of a file back to disk.
|
|
|
|
Without use of this call there is no guarantee that changes are
|
|
|
|
written back before the object is destroyed. If \var{offset} and
|
|
|
|
\var{size} are specified, only changes to the given range of bytes
|
|
|
|
will be flushed to disk; otherwise, the whole extent of the mapping
|
|
|
|
is flushed.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Copy the \var{count} bytes starting at offset \var{src} to the
|
|
|
|
destination index \var{dest}. If the mmap was created with
|
|
|
|
\constant{ACCESS_READ}, then calls to move will throw a
|
|
|
|
\exception{TypeError} exception.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{read}{\var{num}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Return a string containing up to \var{num} bytes starting from the
|
|
|
|
current file position; the file position is updated to point after the
|
|
|
|
bytes that were returned.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{read_byte}{}
|
2001-11-13 19:11:19 -04:00
|
|
|
Returns a string of length 1 containing the character at the current
|
|
|
|
file position, and advances the file position by 1.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{readline}{}
|
2004-06-06 13:51:46 -03:00
|
|
|
Returns a single line, starting at the current file position and up to
|
2001-11-13 19:11:19 -04:00
|
|
|
the next newline.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{resize}{\var{newsize}}
|
2005-08-24 04:17:40 -03:00
|
|
|
Resizes the map and the underlying file, if any.
|
2001-11-13 19:11:19 -04:00
|
|
|
If the mmap was created with \constant{ACCESS_READ} or
|
|
|
|
\constant{ACCESS_COPY}, resizing the map will throw a \exception{TypeError} exception.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
2000-09-05 10:50:21 -03:00
|
|
|
\begin{methoddesc}{seek}{pos\optional{, whence}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Set the file's current position. \var{whence} argument is optional
|
2006-12-21 09:41:46 -04:00
|
|
|
and defaults to \code{os.SEEK_SET} or \code{0} (absolute file
|
|
|
|
positioning); other values are \code{os.SEEK_CUR} or \code{1} (seek
|
|
|
|
relative to the current position) and \code{os.SEEK_END} or \code{2}
|
2001-11-13 19:11:19 -04:00
|
|
|
(seek relative to the file's end).
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{size}{}
|
2001-11-13 19:11:19 -04:00
|
|
|
Return the length of the file, which can be larger than the size of
|
|
|
|
the memory-mapped area.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{tell}{}
|
2001-11-13 19:11:19 -04:00
|
|
|
Returns the current position of the file pointer.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{write}{\var{string}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Write the bytes in \var{string} into memory at the current position
|
|
|
|
of the file pointer; the file position is updated to point after the
|
|
|
|
bytes that were written. If the mmap was created with
|
|
|
|
\constant{ACCESS_READ}, then writing to it will throw a
|
|
|
|
\exception{TypeError} exception.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
\begin{methoddesc}{write_byte}{\var{byte}}
|
2001-11-13 19:11:19 -04:00
|
|
|
Write the single-character string \var{byte} into memory at the
|
|
|
|
current position of the file pointer; the file position is advanced
|
2006-01-15 06:17:59 -04:00
|
|
|
by \code{1}. If the mmap was created with \constant{ACCESS_READ},
|
2001-11-13 19:11:19 -04:00
|
|
|
then writing to it will throw a \exception{TypeError} exception.
|
2000-06-17 19:39:05 -03:00
|
|
|
\end{methoddesc}
|