blob: 0bd03321f25588dbb0037d58d623dc6e59e531a7 [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
Fred Drake50555452001-09-25 19:00:08 +000017A memory-mapped file is created by the \function{mmap()} function,
18which is different on \UNIX{} and on Windows. In either case you must
19provide a file descriptor for a file opened for update.
20If you wish to map an existing Python file object, use its
21\method{fileno()} method to obtain the correct value for the
22\var{fileno} parameter. Otherwise, you can open the file using the
23\function{os.open()} function, which returns a file descriptor
24directly (the file still needs to be closed when done).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000025
Fred Drake8ff4cd72000-09-05 13:50:21 +000026\begin{funcdesc}{mmap}{fileno, length\optional{, tagname}}
27\strong{(Windows version)} Maps \var{length} bytes from the file
28specified by the file handle \var{fileno}, and returns a mmap object.
Fred Drakeca7e1ee2001-01-11 22:49:49 +000029If \var{length} is \code{0}, the maximum length of the map will be the
30current size of the file when \function{mmap()} is called.
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000031
Fred Drakeca7e1ee2001-01-11 22:49:49 +000032\var{tagname}, if specified and not \code{None}, is a string giving a
33tag name for the mapping. Windows allows you to have many different
34mappings against the same file. If you specify the name of an
35existing tag, that tag is opened, otherwise a new tag of this name is
36created. If this parameter is omitted or \code{None}, the mapping is
37created without a name. Avoiding the use of the tag parameter will
38assist in keeping your code portable between \UNIX{} and Windows.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000039\end{funcdesc}
40
Fred Drake50555452001-09-25 19:00:08 +000041\begin{funcdesc}{mmap}{fileno, length\optional{, flags\optional{, prot}}}
Fred Drakeca7e1ee2001-01-11 22:49:49 +000042\strong{(\UNIX{} version)} Maps \var{length} bytes from the file
Fred Drake50555452001-09-25 19:00:08 +000043specified by the file descriptor \var{fileno}, and returns a mmap object.
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
Fred Drake50555452001-09-25 19:00:08 +000058
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000059Memory-mapped file objects support the following methods:
60
61
62\begin{methoddesc}{close}{}
63Close the file. Subsequent calls to other methods of the object
64will result in an exception being raised.
65\end{methoddesc}
66
Fred Drake8ff4cd72000-09-05 13:50:21 +000067\begin{methoddesc}{find}{string\optional{, start}}
68Returns the lowest index in the object where the substring
69\var{string} is found. Returns \code{-1} on failure. \var{start} is
70the index at which the search begins, and defaults to zero.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000071\end{methoddesc}
72
Fred Drake8ff4cd72000-09-05 13:50:21 +000073\begin{methoddesc}{flush}{\optional{offset, size}}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000074Flushes changes made to the in-memory copy of a file back to disk.
75Without use of this call there is no guarantee that changes are
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000076written back before the object is destroyed. If \var{offset} and
77\var{size} are specified, only changes to the given range of bytes
78will be flushed to disk; otherwise, the whole extent of the mapping is
79flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000080\end{methoddesc}
81
82\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
83Copy the \var{count} bytes starting at offset \var{src}
84to the destination index \var{dest}.
85\end{methoddesc}
86
87\begin{methoddesc}{read}{\var{num}}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000088Return a string containing up to \var{num} bytes starting from the
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000089current file position; the file position is updated to point after the
90bytes that were returned.
91\end{methoddesc}
92
93\begin{methoddesc}{read_byte}{}
Andrew M. Kuchling0adfb452000-06-18 04:17:38 +000094Returns a string of length 1 containing the character at the current
95file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000096\end{methoddesc}
97
98\begin{methoddesc}{readline}{}
99Returns a single line, starting at the current file position and up to
100the next newline.
101\end{methoddesc}
102
103\begin{methoddesc}{resize}{\var{newsize}}
104\end{methoddesc}
105
Fred Drake8ff4cd72000-09-05 13:50:21 +0000106\begin{methoddesc}{seek}{pos\optional{, whence}}
107Set the file's current position.
108\var{whence} argument is optional and defaults to \code{0} (absolute
109file positioning); other values are \code{1} (seek relative to the
110current position) and \code{2} (seek relative to the file's end).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000111\end{methoddesc}
112
113\begin{methoddesc}{size}{}
114Return the length of the file, which can be larger than the size
115of the memory-mapped area.
116\end{methoddesc}
117
118\begin{methoddesc}{tell}{}
119Returns the current position of the file pointer.
120\end{methoddesc}
121
122\begin{methoddesc}{write}{\var{string}}
123Write the bytes in \var{string} into memory at the current position of
124the file pointer; the file position is updated to point after the
125bytes that were written.
126\end{methoddesc}
127
128\begin{methoddesc}{write_byte}{\var{byte}}
Fred Drake8ff4cd72000-09-05 13:50:21 +0000129Write the single-character string \var{byte} into memory at the
130current position of the file pointer; the file position is advanced by
131\code{1}.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000132\end{methoddesc}