blob: 8d04ededb32209edabccf18d14841c2126861a83 [file] [log] [blame]
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +00001\section{\module{mmap} ---
2 Memory-mapped file support}
3
4\declaremodule{builtin}{mmap}
5\modulesynopsis{Interface to memory-mapped files for Unix and Windows.}
6
7Memory-mapped file objects behave like both mutable strings and like
8file objects. You can use mmap objects in most places where strings
9are expected; for example, you can use the \module{re} module to
10search through a memory-mapped file. Since they're mutable, you can
Fred Drake8ff4cd72000-09-05 13:50:21 +000011change a single character by doing \code{obj[\var{index}] = 'a'}, or
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000012change a substring by assigning to a slice:
Fred Drake8ff4cd72000-09-05 13:50:21 +000013\code{obj[\var{i1}:\var{i2}] = '...'}. You can also read and write
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000014data starting at the current file position, and \method{seek()}
15through the file to different positions.
16
17A memory-mapped file is created by the following function, which is
18different on Unix and on Windows.
19
Fred Drake8ff4cd72000-09-05 13:50:21 +000020\begin{funcdesc}{mmap}{fileno, length\optional{, tagname}}
21\strong{(Windows version)} Maps \var{length} bytes from the file
22specified by the file handle \var{fileno}, and returns a mmap object.
Fred Drakeca7e1ee2001-01-11 22:49:49 +000023If \var{length} is \code{0}, the maximum length of the map will be the
24current size of the file when \function{mmap()} is called.
Fred Drake8ff4cd72000-09-05 13:50:21 +000025If you wish to map an existing Python file object, use its
26\method{fileno()} method to obtain the correct value for the
Fred Drakeca7e1ee2001-01-11 22:49:49 +000027\var{fileno} parameter. The file must be opened for update.
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000028
Fred Drakeca7e1ee2001-01-11 22:49:49 +000029\var{tagname}, if specified and not \code{None}, is a string giving a
30tag name for the mapping. Windows allows you to have many different
31mappings against the same file. If you specify the name of an
32existing tag, that tag is opened, otherwise a new tag of this name is
33created. If this parameter is omitted or \code{None}, the mapping is
34created without a name. Avoiding the use of the tag parameter will
35assist in keeping your code portable between \UNIX{} and Windows.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000036\end{funcdesc}
37
Fred Drake8ff4cd72000-09-05 13:50:21 +000038\begin{funcdesc}{mmap}{fileno, size\optional{, flags, prot}}
Fred Drakeca7e1ee2001-01-11 22:49:49 +000039\strong{(\UNIX{} version)} Maps \var{length} bytes from the file
Fred Drake8ff4cd72000-09-05 13:50:21 +000040specified by the file handle \var{fileno}, and returns a mmap object.
41If you wish to map an existing Python file object, use its
42\method{fileno()} method to obtain the correct value for the
Fred Drakeca7e1ee2001-01-11 22:49:49 +000043\var{fileno} parameter. The file must be opened for update.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000044
45\var{flags} specifies the nature of the mapping.
Fred Drake8ff4cd72000-09-05 13:50:21 +000046\constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000047changes to the contents of the mmap object will be private to this
Fred Drake8ff4cd72000-09-05 13:50:21 +000048process, and \constant{MAP_SHARED} creates a mapping that's shared
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000049with all other processes mapping the same areas of the file.
Fred Drake8ff4cd72000-09-05 13:50:21 +000050The default value is \constant{MAP_SHARED}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000051
52\var{prot}, if specified, gives the desired memory protection; the two
Fred Drake8ff4cd72000-09-05 13:50:21 +000053most useful values are \constant{PROT_READ} and \constant{PROT_WRITE},
54to specify that the pages may be read or written.
55\var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000056\end{funcdesc}
57
58Memory-mapped file objects support the following methods:
59
60
61\begin{methoddesc}{close}{}
62Close the file. Subsequent calls to other methods of the object
63will result in an exception being raised.
64\end{methoddesc}
65
Fred Drake8ff4cd72000-09-05 13:50:21 +000066\begin{methoddesc}{find}{string\optional{, start}}
67Returns the lowest index in the object where the substring
68\var{string} is found. Returns \code{-1} on failure. \var{start} is
69the index at which the search begins, and defaults to zero.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000070\end{methoddesc}
71
Fred Drake8ff4cd72000-09-05 13:50:21 +000072\begin{methoddesc}{flush}{\optional{offset, size}}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000073Flushes changes made to the in-memory copy of a file back to disk.
74Without use of this call there is no guarantee that changes are
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000075written back before the object is destroyed. If \var{offset} and
76\var{size} are specified, only changes to the given range of bytes
77will be flushed to disk; otherwise, the whole extent of the mapping is
78flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000079\end{methoddesc}
80
81\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
82Copy the \var{count} bytes starting at offset \var{src}
83to the destination index \var{dest}.
84\end{methoddesc}
85
86\begin{methoddesc}{read}{\var{num}}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000087Return a string containing up to \var{num} bytes starting from the
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000088current file position; the file position is updated to point after the
89bytes that were returned.
90\end{methoddesc}
91
92\begin{methoddesc}{read_byte}{}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000093Returns a string of length 1 containing the character at the current
94file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000095\end{methoddesc}
96
97\begin{methoddesc}{readline}{}
98Returns a single line, starting at the current file position and up to
99the next newline.
100\end{methoddesc}
101
102\begin{methoddesc}{resize}{\var{newsize}}
103\end{methoddesc}
104
Fred Drake8ff4cd72000-09-05 13:50:21 +0000105\begin{methoddesc}{seek}{pos\optional{, whence}}
106Set the file's current position.
107\var{whence} argument is optional and defaults to \code{0} (absolute
108file positioning); other values are \code{1} (seek relative to the
109current position) and \code{2} (seek relative to the file's end).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000110\end{methoddesc}
111
112\begin{methoddesc}{size}{}
113Return the length of the file, which can be larger than the size
114of the memory-mapped area.
115\end{methoddesc}
116
117\begin{methoddesc}{tell}{}
118Returns the current position of the file pointer.
119\end{methoddesc}
120
121\begin{methoddesc}{write}{\var{string}}
122Write the bytes in \var{string} into memory at the current position of
123the file pointer; the file position is updated to point after the
124bytes that were written.
125\end{methoddesc}
126
127\begin{methoddesc}{write_byte}{\var{byte}}
Fred Drake8ff4cd72000-09-05 13:50:21 +0000128Write the single-character string \var{byte} into memory at the
129current position of the file pointer; the file position is advanced by
130\code{1}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000131\end{methoddesc}