blob: b88c34820a39946006d68fa540cec8a11e75c882 [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
11change a single character by doing \code{obj[ \var{index} ] = 'a'}, or
12change a substring by assigning to a slice:
13\code{obj[ \var{i1}:\var{i2} ] = '...'}. You can also read and write
14data 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
20\begin{funcdesc}{mmap}{fileno, length \optional{, tagname} }
21(Windows version) Maps \var{length} bytes from the file specified by
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000022the file handle \var{fileno}, and returns a mmap object. If you wish
23to map an existing Python file object, use its \method{fileno()}
24method to obtain the correct value for the \var{fileno} parameter.
25
26\var{tagname}, if specified, is a string giving a tag name for the mapping.
27Windows allows you to have many different mappings against the same
28file. If you specify the name of an existing tag, that tag is opened,
29otherwise a new tag of this name is created. If this parameter is
30None, the mapping is created without a name. Avoiding the use of the
31tag parameter will assist in keeping your code portable between Unix
32and Windows.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000033\end{funcdesc}
34
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000035\begin{funcdesc}{mmap}{fileno, size \optional{, flags, prot}}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000036(Unix version) Maps \var{length} bytes from the file specified by the
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000037file handle \var{fileno}, and returns a mmap object. If you wish to
38map an existing Python file object, use its \method{fileno()} method
39to obtain the correct value for the \var{fileno} parameter.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000040
41\var{flags} specifies the nature of the mapping.
42\code{MAP_PRIVATE} creates a private copy-on-write mapping, so
43changes to the contents of the mmap object will be private to this
44process, and \code{MAP_SHARED} creates a mapping that's shared
45with all other processes mapping the same areas of the file.
46The default value is \code{MAP_SHARED}.
47
48\var{prot}, if specified, gives the desired memory protection; the two
49most useful values are \code{PROT_READ} and \code{PROT_WRITE}, to
50specify that the pages may be read or written.
51\var{prot} defaults to \code{PROT_READ | PROT_WRITE}.
52\end{funcdesc}
53
54Memory-mapped file objects support the following methods:
55
56
57\begin{methoddesc}{close}{}
58Close the file. Subsequent calls to other methods of the object
59will result in an exception being raised.
60\end{methoddesc}
61
62\begin{methoddesc}{find}{\var{string} \optional{, \var{start}}}
63 Returns the lowest index in the object where the substring \var{string} is
64 found. Returns \code{-1} on failure.
65 \var{start} is the index at which the search begins, and defaults to zero.
66\end{methoddesc}
67
68\begin{methoddesc}{flush}{\optional{\var{offset}, \var{size}}}
69Flushes changes made to the in-memory copy of a file back to disk.
70Without use of this call there is no guarantee that changes are
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000071written back before the object is destroyed. If \var{offset} and
72\var{size} are specified, only changes to the given range of bytes
73will be flushed to disk; otherwise, the whole extent of the mapping is
74flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000075\end{methoddesc}
76
77\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
78Copy the \var{count} bytes starting at offset \var{src}
79to the destination index \var{dest}.
80\end{methoddesc}
81
82\begin{methoddesc}{read}{\var{num}}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000083Return a string containing up to \var{num} bytes starting from the
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000084current file position; the file position is updated to point after the
85bytes that were returned.
86\end{methoddesc}
87
88\begin{methoddesc}{read_byte}{}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000089Returns a string of length 1 containing the character at the current
90file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000091\end{methoddesc}
92
93\begin{methoddesc}{readline}{}
94Returns a single line, starting at the current file position and up to
95the next newline.
96\end{methoddesc}
97
98\begin{methoddesc}{resize}{\var{newsize}}
99\end{methoddesc}
100
101\begin{methoddesc}{seek}{\var{pos} \optional{, \var{whence}}}
102 Set the file's current position.
103 \var{whence} argument is optional and defaults to \code{0}
104 (absolute file positioning); other values are \code{1} (seek
105 relative to the current position) and \code{2} (seek relative to the
106 file's end).
107\end{methoddesc}
108
109\begin{methoddesc}{size}{}
110Return the length of the file, which can be larger than the size
111of the memory-mapped area.
112\end{methoddesc}
113
114\begin{methoddesc}{tell}{}
115Returns the current position of the file pointer.
116\end{methoddesc}
117
118\begin{methoddesc}{write}{\var{string}}
119Write the bytes in \var{string} into memory at the current position of
120the file pointer; the file position is updated to point after the
121bytes that were written.
122\end{methoddesc}
123
124\begin{methoddesc}{write_byte}{\var{byte}}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +0000125Write the single-character string \var{byte} into memory at the current position of
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000126the file pointer; the file position is advanced by 1.
127\end{methoddesc}
128
129