blob: 0334147575c54f70267a3866149439db66587dfe [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
Fred Drake1722e4a2001-12-03 18:27:22 +00007Memory-mapped file objects behave like both strings and like
8file objects. Unlike normal string objects, however, these are
9mutable. You can use mmap objects in most places where strings
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000010are expected; for example, you can use the \module{re} module to
11search through a memory-mapped file. Since they're mutable, you can
Fred Drake8ff4cd72000-09-05 13:50:21 +000012change a single character by doing \code{obj[\var{index}] = 'a'}, or
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000013change a substring by assigning to a slice:
Fred Drake8ff4cd72000-09-05 13:50:21 +000014\code{obj[\var{i1}:\var{i2}] = '...'}. You can also read and write
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000015data starting at the current file position, and \method{seek()}
16through the file to different positions.
17
Fred Drake50555452001-09-25 19:00:08 +000018A memory-mapped file is created by the \function{mmap()} function,
19which is different on \UNIX{} and on Windows. In either case you must
20provide a file descriptor for a file opened for update.
21If you wish to map an existing Python file object, use its
22\method{fileno()} method to obtain the correct value for the
23\var{fileno} parameter. Otherwise, you can open the file using the
24\function{os.open()} function, which returns a file descriptor
25directly (the file still needs to be closed when done).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000026
Fred Drake1722e4a2001-12-03 18:27:22 +000027For both the \UNIX{} and Windows versions of the function,
28\var{access} may be specified as an optional keyword parameter.
29\var{access} accepts one of three values: \constant{ACCESS_READ},
30\constant{ACCESS_WRITE}, or \constant{ACCESS_COPY} to specify
31readonly, write-through or copy-on-write memory respectively.
32\var{access} can be used on both \UNIX{} and Windows. If
33\var{access} is not specified, Windows mmap returns a write-through
34mapping. The initial memory values for all three access types are
35taken from the specified file. Assignment to an
36\constant{ACCESS_READ} memory map raises a \exception{TypeError}
37exception. Assignment to an \constant{ACCESS_WRITE} memory map
38affects both memory and the underlying file. Assigment to an
39\constant{ACCESS_COPY} memory map affects memory but does not update
40the underlying file.
41
Tim Peters5ebfd362001-11-13 23:11:19 +000042\begin{funcdesc}{mmap}{fileno, length\optional{, tagname\optional{, access}}}
43 \strong{(Windows version)} Maps \var{length} bytes from the file
44 specified by the file handle \var{fileno}, and returns a mmap
Tim Peters0b4d1ee2004-06-06 16:51:46 +000045 object. If \var{length} is larger than the current size of the file,
46 the file is extended to contain \var{length} bytes. If \var{length}
47 is \code{0}, the maximum length of the map is the current size
48 of the file, except that if the file is empty Windows raises an
49 exception (you cannot create an empty mapping on Windows).
50
Tim Peters5ebfd362001-11-13 23:11:19 +000051 \var{tagname}, if specified and not \code{None}, is a string giving
52 a tag name for the mapping. Windows allows you to have many
53 different mappings against the same file. If you specify the name
54 of an existing tag, that tag is opened, otherwise a new tag of this
55 name is created. If this parameter is omitted or \code{None}, the
56 mapping is created without a name. Avoiding the use of the tag
57 parameter will assist in keeping your code portable between \UNIX{}
58 and Windows.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000059\end{funcdesc}
60
Fred Drake1722e4a2001-12-03 18:27:22 +000061\begin{funcdescni}{mmap}{fileno, length\optional{, flags\optional{,
62 prot\optional{, access}}}}
Tim Peters5ebfd362001-11-13 23:11:19 +000063 \strong{(\UNIX{} version)} Maps \var{length} bytes from the file
64 specified by the file descriptor \var{fileno}, and returns a mmap
65 object.
Tim Peters0b4d1ee2004-06-06 16:51:46 +000066
Tim Peters5ebfd362001-11-13 23:11:19 +000067 \var{flags} specifies the nature of the mapping.
68 \constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
69 changes to the contents of the mmap object will be private to this
70 process, and \constant{MAP_SHARED} creates a mapping that's shared
71 with all other processes mapping the same areas of the file. The
72 default value is \constant{MAP_SHARED}.
Tim Peters0b4d1ee2004-06-06 16:51:46 +000073
Tim Peters5ebfd362001-11-13 23:11:19 +000074 \var{prot}, if specified, gives the desired memory protection; the
75 two most useful values are \constant{PROT_READ} and
76 \constant{PROT_WRITE}, to specify that the pages may be read or
77 written. \var{prot} defaults to \constant{PROT_READ | PROT_WRITE}.
Tim Peters0b4d1ee2004-06-06 16:51:46 +000078
Tim Peters5ebfd362001-11-13 23:11:19 +000079 \var{access} may be specified in lieu of \var{flags} and \var{prot}
Fred Drake1722e4a2001-12-03 18:27:22 +000080 as an optional keyword parameter. It is an error to specify both
81 \var{flags}, \var{prot} and \var{access}. See the description of
82 \var{access} above for information on how to use this parameter.
83\end{funcdescni}
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000084
Fred Drake50555452001-09-25 19:00:08 +000085
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000086Memory-mapped file objects support the following methods:
87
88
89\begin{methoddesc}{close}{}
Tim Peters5ebfd362001-11-13 23:11:19 +000090 Close the file. Subsequent calls to other methods of the object
91 will result in an exception being raised.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000092\end{methoddesc}
93
Fred Drake8ff4cd72000-09-05 13:50:21 +000094\begin{methoddesc}{find}{string\optional{, start}}
Tim Peters5ebfd362001-11-13 23:11:19 +000095 Returns the lowest index in the object where the substring
96 \var{string} is found. Returns \code{-1} on failure. \var{start}
97 is the index at which the search begins, and defaults to zero.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +000098\end{methoddesc}
99
Fred Drake8ff4cd72000-09-05 13:50:21 +0000100\begin{methoddesc}{flush}{\optional{offset, size}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000101 Flushes changes made to the in-memory copy of a file back to disk.
102 Without use of this call there is no guarantee that changes are
103 written back before the object is destroyed. If \var{offset} and
104 \var{size} are specified, only changes to the given range of bytes
105 will be flushed to disk; otherwise, the whole extent of the mapping
106 is flushed.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000107\end{methoddesc}
108
109\begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000110 Copy the \var{count} bytes starting at offset \var{src} to the
111 destination index \var{dest}. If the mmap was created with
112 \constant{ACCESS_READ}, then calls to move will throw a
113 \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000114\end{methoddesc}
115
116\begin{methoddesc}{read}{\var{num}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000117 Return a string containing up to \var{num} bytes starting from the
118 current file position; the file position is updated to point after the
119 bytes that were returned.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000120\end{methoddesc}
121
122\begin{methoddesc}{read_byte}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000123 Returns a string of length 1 containing the character at the current
124 file position, and advances the file position by 1.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000125\end{methoddesc}
126
127\begin{methoddesc}{readline}{}
Tim Peters0b4d1ee2004-06-06 16:51:46 +0000128 Returns a single line, starting at the current file position and up to
Tim Peters5ebfd362001-11-13 23:11:19 +0000129 the next newline.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000130\end{methoddesc}
131
132\begin{methoddesc}{resize}{\var{newsize}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000133 If the mmap was created with \constant{ACCESS_READ} or
134 \constant{ACCESS_COPY}, resizing the map will throw a \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000135\end{methoddesc}
136
Fred Drake8ff4cd72000-09-05 13:50:21 +0000137\begin{methoddesc}{seek}{pos\optional{, whence}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000138 Set the file's current position. \var{whence} argument is optional
139 and defaults to \code{0} (absolute file positioning); other values
140 are \code{1} (seek relative to the current position) and \code{2}
141 (seek relative to the file's end).
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000142\end{methoddesc}
143
144\begin{methoddesc}{size}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000145 Return the length of the file, which can be larger than the size of
146 the memory-mapped area.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000147\end{methoddesc}
148
149\begin{methoddesc}{tell}{}
Tim Peters5ebfd362001-11-13 23:11:19 +0000150 Returns the current position of the file pointer.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000151\end{methoddesc}
152
153\begin{methoddesc}{write}{\var{string}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000154 Write the bytes in \var{string} into memory at the current position
155 of the file pointer; the file position is updated to point after the
156 bytes that were written. If the mmap was created with
157 \constant{ACCESS_READ}, then writing to it will throw a
158 \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000159\end{methoddesc}
160
161\begin{methoddesc}{write_byte}{\var{byte}}
Tim Peters5ebfd362001-11-13 23:11:19 +0000162 Write the single-character string \var{byte} into memory at the
163 current position of the file pointer; the file position is advanced
164 by \code{1}.If the mmap was created with \constant{ACCESS_READ},
165 then writing to it will throw a \exception{TypeError} exception.
Andrew M. Kuchlingb8050692000-06-17 22:39:05 +0000166\end{methoddesc}