blob: 41fcc34099b377c8456e028d9138f41e85a42d20 [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.
23If you wish to map an existing Python file object, use its
24\method{fileno()} method to obtain the correct value for the
25\var{fileno} parameter.
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000026
27\var{tagname}, if specified, is a string giving a tag name for the mapping.
28Windows allows you to have many different mappings against the same
29file. If you specify the name of an existing tag, that tag is opened,
30otherwise a new tag of this name is created. If this parameter is
31None, the mapping is created without a name. Avoiding the use of the
32tag parameter will assist in keeping your code portable between Unix
33and Windows.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000034\end{funcdesc}
35
Fred Drake8ff4cd72000-09-05 13:50:21 +000036\begin{funcdesc}{mmap}{fileno, size\optional{, flags, prot}}
37\strong{(Unix version)} Maps \var{length} bytes from the file
38specified by the file handle \var{fileno}, and returns a mmap object.
39If you wish to map an existing Python file object, use its
40\method{fileno()} method to obtain the correct value for the
41\var{fileno} parameter.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000042
43\var{flags} specifies the nature of the mapping.
Fred Drake8ff4cd72000-09-05 13:50:21 +000044\constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000045changes to the contents of the mmap object will be private to this
Fred Drake8ff4cd72000-09-05 13:50:21 +000046process, and \constant{MAP_SHARED} creates a mapping that's shared
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000047with all other processes mapping the same areas of the file.
Fred Drake8ff4cd72000-09-05 13:50:21 +000048The default value is \constant{MAP_SHARED}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000049
50\var{prot}, if specified, gives the desired memory protection; the two
Fred Drake8ff4cd72000-09-05 13:50:21 +000051most useful values are \constant{PROT_READ} and \constant{PROT_WRITE},
52to specify that the pages may be read or written.
53\var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000054\end{funcdesc}
55
56Memory-mapped file objects support the following methods:
57
58
59\begin{methoddesc}{close}{}
60Close the file. Subsequent calls to other methods of the object
61will result in an exception being raised.
62\end{methoddesc}
63
Fred Drake8ff4cd72000-09-05 13:50:21 +000064\begin{methoddesc}{find}{string\optional{, start}}
65Returns the lowest index in the object where the substring
66\var{string} is found. Returns \code{-1} on failure. \var{start} is
67the index at which the search begins, and defaults to zero.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000068\end{methoddesc}
69
Fred Drake8ff4cd72000-09-05 13:50:21 +000070\begin{methoddesc}{flush}{\optional{offset, size}}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000071Flushes changes made to the in-memory copy of a file back to disk.
72Without use of this call there is no guarantee that changes are
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000073written back before the object is destroyed. If \var{offset} and
74\var{size} are specified, only changes to the given range of bytes
75will be flushed to disk; otherwise, the whole extent of the mapping is
76flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000077\end{methoddesc}
78
79\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
80Copy the \var{count} bytes starting at offset \var{src}
81to the destination index \var{dest}.
82\end{methoddesc}
83
84\begin{methoddesc}{read}{\var{num}}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000085Return a string containing up to \var{num} bytes starting from the
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000086current file position; the file position is updated to point after the
87bytes that were returned.
88\end{methoddesc}
89
90\begin{methoddesc}{read_byte}{}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000091Returns a string of length 1 containing the character at the current
92file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000093\end{methoddesc}
94
95\begin{methoddesc}{readline}{}
96Returns a single line, starting at the current file position and up to
97the next newline.
98\end{methoddesc}
99
100\begin{methoddesc}{resize}{\var{newsize}}
101\end{methoddesc}
102
Fred Drake8ff4cd72000-09-05 13:50:21 +0000103\begin{methoddesc}{seek}{pos\optional{, whence}}
104Set the file's current position.
105\var{whence} argument is optional and defaults to \code{0} (absolute
106file positioning); other values are \code{1} (seek relative to the
107current position) and \code{2} (seek relative to the file's end).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000108\end{methoddesc}
109
110\begin{methoddesc}{size}{}
111Return the length of the file, which can be larger than the size
112of the memory-mapped area.
113\end{methoddesc}
114
115\begin{methoddesc}{tell}{}
116Returns the current position of the file pointer.
117\end{methoddesc}
118
119\begin{methoddesc}{write}{\var{string}}
120Write the bytes in \var{string} into memory at the current position of
121the file pointer; the file position is updated to point after the
122bytes that were written.
123\end{methoddesc}
124
125\begin{methoddesc}{write_byte}{\var{byte}}
Fred Drake8ff4cd72000-09-05 13:50:21 +0000126Write the single-character string \var{byte} into memory at the
127current position of the file pointer; the file position is advanced by
128\code{1}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000129\end{methoddesc}