blob: 466dfcc84ee150027290a2355ee56a16c5a7ae9a [file] [log] [blame]
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +00001\section{\module{mmap} ---
Tim Peters5ebfd362001-11-13 23:11:19 +00002Memory-mapped file support}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +00003
4\declaremodule{builtin}{mmap}
Fred Drakec37b65e2001-11-28 07:26:15 +00005\modulesynopsis{Interface to memory-mapped files for \UNIX\ and Windows.}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +00006
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
Tim Peters5ebfd362001-11-13 23:11:19 +000026\begin{funcdesc}{mmap}{fileno, length\optional{, tagname\optional{, access}}}
27 \strong{(Windows version)} Maps \var{length} bytes from the file
28 specified by the file handle \var{fileno}, and returns a mmap
29 object. If \var{length} is \code{0}, the maximum length of the map
30 will be the current size of the file when \function{mmap()} is
31 called.
32
33 \var{tagname}, if specified and not \code{None}, is a string giving
34 a tag name for the mapping. Windows allows you to have many
35 different mappings against the same file. If you specify the name
36 of an existing tag, that tag is opened, otherwise a new tag of this
37 name is created. If this parameter is omitted or \code{None}, the
38 mapping is created without a name. Avoiding the use of the tag
39 parameter will assist in keeping your code portable between \UNIX{}
40 and Windows.
41
42 \var{access} may be specified as an optional keyword parameter.
43 \var{access} accepts one of three values: \constant{ACCESS_READ},
44 \constant{ACCESS_WRITE}, or \constant{ACCESS_COPY} to specify
45 readonly, write-through or copy-on-write memory respectively.
46 \var{access} can be used on both \UNIX{} and Windows. If
47 \var{access} is not specified, Windows mmap returns a write-through
48 mapping. The initial memory values for all three access types are
49 taken from the specified file. Assignment to an
50 \constant{ACCESS_READ} memory map raises a \exception{TypeError}
51 exception. Assignment to an \constant{ACCESS_WRITE} memory map
52 affects both memory and the underlying file. Assigment to an
53 \constant{ACCESS_COPY} memory map affects memory but does not update
54 the underlying file.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000055\end{funcdesc}
56
Tim Peters5ebfd362001-11-13 23:11:19 +000057\begin{funcdesc}{mmap}{fileno, length\optional{, flags\optional{, prot\optional{, access}}}}
58 \strong{(\UNIX{} version)} Maps \var{length} bytes from the file
59 specified by the file descriptor \var{fileno}, and returns a mmap
60 object.
61
62 \var{flags} specifies the nature of the mapping.
63 \constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
64 changes to the contents of the mmap object will be private to this
65 process, and \constant{MAP_SHARED} creates a mapping that's shared
66 with all other processes mapping the same areas of the file. The
67 default value is \constant{MAP_SHARED}.
68
69 \var{prot}, if specified, gives the desired memory protection; the
70 two most useful values are \constant{PROT_READ} and
71 \constant{PROT_WRITE}, to specify that the pages may be read or
72 written. \var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
73
74 \var{access} may be specified in lieu of \var{flags} and \var{prot}
75 as an optional keyword parameter. \var{access} accepts one of three
76 values: \constant{ACCESS_READ}, \constant{ACCESS_WRITE}, or
77 \constant{ACCESS_COPY} to specify readonly, write-through, or
78 copy-on-write memory respectively. \var{access} can be used on both
79 \UNIX{} and Windows. It is an error to specify both \var{flags},
80 \var{prot} and \var{access}. The initial memory values for all
81 three access types are taken from the specified file. Assignment to
82 an \constant{ACCESS_READ} memory map raises a \exception{TypeError}
83 exception. Assignment to an \constant{ACCESS_WRITE} memory map
84 affects both memory and the underlying file. Assigment to an
85 \constant{ACCESS_COPY} memory map affects memory but does not update
86 the underlying file.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000087\end{funcdesc}
88
Fred Drake50555452001-09-25 19:00:08 +000089
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000090Memory-mapped file objects support the following methods:
91
92
93\begin{methoddesc}{close}{}
Tim Peters5ebfd362001-11-13 23:11:19 +000094 Close the file. Subsequent calls to other methods of the object
95 will result in an exception being raised.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000096\end{methoddesc}
97
Fred Drake8ff4cd72000-09-05 13:50:21 +000098\begin{methoddesc}{find}{string\optional{, start}}
Tim Peters5ebfd362001-11-13 23:11:19 +000099 Returns the lowest index in the object where the substring
100 \var{string} is found. Returns \code{-1} on failure. \var{start}
101 is the index at which the search begins, and defaults to zero.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000102\end{methoddesc}
103
Fred Drake8ff4cd72000-09-05 13:50:21 +0000104\begin{methoddesc}{flush}{\optional{offset, size}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000105 Flushes changes made to the in-memory copy of a file back to disk.
106 Without use of this call there is no guarantee that changes are
107 written back before the object is destroyed. If \var{offset} and
108 \var{size} are specified, only changes to the given range of bytes
109 will be flushed to disk; otherwise, the whole extent of the mapping
110 is flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000111\end{methoddesc}
112
113\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000114 Copy the \var{count} bytes starting at offset \var{src} to the
115 destination index \var{dest}. If the mmap was created with
116 \constant{ACCESS_READ}, then calls to move will throw a
117 \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000118\end{methoddesc}
119
120\begin{methoddesc}{read}{\var{num}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000121 Return a string containing up to \var{num} bytes starting from the
122 current file position; the file position is updated to point after the
123 bytes that were returned.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000124\end{methoddesc}
125
126\begin{methoddesc}{read_byte}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000127 Returns a string of length 1 containing the character at the current
128 file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000129\end{methoddesc}
130
131\begin{methoddesc}{readline}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000132 Returns a single line, starting at the current file position and up to
133 the next newline.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000134\end{methoddesc}
135
136\begin{methoddesc}{resize}{\var{newsize}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000137 If the mmap was created with \constant{ACCESS_READ} or
138 \constant{ACCESS_COPY}, resizing the map will throw a \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000139\end{methoddesc}
140
Fred Drake8ff4cd72000-09-05 13:50:21 +0000141\begin{methoddesc}{seek}{pos\optional{, whence}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000142 Set the file's current position. \var{whence} argument is optional
143 and defaults to \code{0} (absolute file positioning); other values
144 are \code{1} (seek relative to the current position) and \code{2}
145 (seek relative to the file's end).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000146\end{methoddesc}
147
148\begin{methoddesc}{size}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000149 Return the length of the file, which can be larger than the size of
150 the memory-mapped area.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000151\end{methoddesc}
152
153\begin{methoddesc}{tell}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000154 Returns the current position of the file pointer.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000155\end{methoddesc}
156
157\begin{methoddesc}{write}{\var{string}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000158 Write the bytes in \var{string} into memory at the current position
159 of the file pointer; the file position is updated to point after the
160 bytes that were written. If the mmap was created with
161 \constant{ACCESS_READ}, then writing to it will throw a
162 \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000163\end{methoddesc}
164
165\begin{methoddesc}{write_byte}{\var{byte}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000166 Write the single-character string \var{byte} into memory at the
167 current position of the file pointer; the file position is advanced
168 by \code{1}.If the mmap was created with \constant{ACCESS_READ},
169 then writing to it will throw a \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000170\end{methoddesc}