Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`mmap` --- Memory-mapped file support |
| 3 | ========================================== |
| 4 | |
| 5 | .. module:: mmap |
| 6 | :synopsis: Interface to memory-mapped files for Unix and Windows. |
| 7 | |
| 8 | |
| 9 | Memory-mapped file objects behave like both strings and like file objects. |
| 10 | Unlike normal string objects, however, these are mutable. You can use mmap |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 11 | objects in most places where strings are expected; for example, you can use |
| 12 | the :mod:`re` module to search through a memory-mapped file. Since they're |
| 13 | mutable, you can change a single character by doing ``obj[index] = 'a'``, or |
| 14 | change a substring by assigning to a slice: ``obj[i1:i2] = '...'``. You can |
| 15 | also read and write data starting at the current file position, and |
| 16 | :meth:`seek` through the file to different positions. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 17 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 18 | A memory-mapped file is created by the :class:`mmap` constructor, which is |
| 19 | different on Unix and on Windows. In either case you must provide a file |
| 20 | descriptor for a file opened for update. If you wish to map an existing Python |
| 21 | file object, use its :meth:`fileno` method to obtain the correct value for the |
| 22 | *fileno* parameter. Otherwise, you can open the file using the |
| 23 | :func:`os.open` function, which returns a file descriptor directly (the file |
| 24 | still needs to be closed when done). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 26 | For both the Unix and Windows versions of the constructor, *access* may be |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 27 | specified as an optional keyword parameter. *access* accepts one of three |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 28 | values: :const:`ACCESS_READ`, :const:`ACCESS_WRITE`, or :const:`ACCESS_COPY` |
Jeroen Ruigrok van der Werven | ea7fa72 | 2008-04-17 12:39:45 +0000 | [diff] [blame] | 29 | to specify read-only, write-through or copy-on-write memory respectively. |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 30 | *access* can be used on both Unix and Windows. If *access* is not specified, |
| 31 | Windows mmap returns a write-through mapping. The initial memory values for |
| 32 | all three access types are taken from the specified file. Assignment to an |
| 33 | :const:`ACCESS_READ` memory map raises a :exc:`TypeError` exception. |
| 34 | Assignment to an :const:`ACCESS_WRITE` memory map affects both memory and the |
| 35 | underlying file. Assignment to an :const:`ACCESS_COPY` memory map affects |
| 36 | memory but does not update the underlying file. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 37 | |
| 38 | .. versionchanged:: 2.5 |
| 39 | To map anonymous memory, -1 should be passed as the fileno along with the |
| 40 | length. |
| 41 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 42 | .. versionchanged:: 2.6 |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 43 | mmap.mmap has formerly been a factory function creating mmap objects. Now |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 44 | mmap.mmap is the class itself. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 45 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 46 | .. class:: mmap(fileno, length[, tagname[, access[, offset]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 47 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 48 | **(Windows version)** Maps *length* bytes from the file specified by the |
| 49 | file handle *fileno*, and creates a mmap object. If *length* is larger |
| 50 | than the current size of the file, the file is extended to contain *length* |
| 51 | bytes. If *length* is ``0``, the maximum length of the map is the current |
| 52 | size of the file, except that if the file is empty Windows raises an |
| 53 | exception (you cannot create an empty mapping on Windows). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 54 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 55 | *tagname*, if specified and not ``None``, is a string giving a tag name for |
| 56 | the mapping. Windows allows you to have many different mappings against |
| 57 | the same file. If you specify the name of an existing tag, that tag is |
| 58 | opened, otherwise a new tag of this name is created. If this parameter is |
| 59 | omitted or ``None``, the mapping is created without a name. Avoiding the |
| 60 | use of the tag parameter will assist in keeping your code portable between |
| 61 | Unix and Windows. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 63 | *offset* may be specified as a non-negative integer offset. mmap references |
| 64 | will be relative to the offset from the beginning of the file. *offset* |
| 65 | defaults to 0. *offset* must be a multiple of the ALLOCATIONGRANULARITY. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 66 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 68 | .. class:: mmap(fileno, length[, flags[, prot[, access[, offset]]]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | :noindex: |
| 70 | |
| 71 | **(Unix version)** Maps *length* bytes from the file specified by the file |
| 72 | descriptor *fileno*, and returns a mmap object. If *length* is ``0``, the |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 73 | maximum length of the map will be the current size of the file when |
| 74 | :class:`mmap` is called. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 75 | |
| 76 | *flags* specifies the nature of the mapping. :const:`MAP_PRIVATE` creates a |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 77 | private copy-on-write mapping, so changes to the contents of the mmap |
| 78 | object will be private to this process, and :const:`MAP_SHARED` creates a |
| 79 | mapping that's shared with all other processes mapping the same areas of |
| 80 | the file. The default value is :const:`MAP_SHARED`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 81 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 82 | *prot*, if specified, gives the desired memory protection; the two most |
| 83 | useful values are :const:`PROT_READ` and :const:`PROT_WRITE`, to specify |
| 84 | that the pages may be read or written. *prot* defaults to |
| 85 | :const:`PROT_READ \| PROT_WRITE`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 86 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 87 | *access* may be specified in lieu of *flags* and *prot* as an optional |
| 88 | keyword parameter. It is an error to specify both *flags*, *prot* and |
| 89 | *access*. See the description of *access* above for information on how to |
| 90 | use this parameter. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 91 | |
Jeroen Ruigrok van der Werven | 069dfad | 2008-04-16 12:47:01 +0000 | [diff] [blame] | 92 | *offset* may be specified as a non-negative integer offset. mmap references |
| 93 | will be relative to the offset from the beginning of the file. *offset* |
| 94 | defaults to 0. *offset* must be a multiple of the PAGESIZE or |
| 95 | ALLOCATIONGRANULARITY. |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 96 | |
Victor Stinner | 112d48a | 2011-05-03 14:36:36 +0200 | [diff] [blame] | 97 | To ensure validity of the created memory mapping the file specified |
| 98 | by the descriptor *fileno* is internally automatically synchronized |
| 99 | with physical backing store on Mac OS X and OpenVMS. |
| 100 | |
Georg Brandl | 845c403 | 2008-01-21 14:16:46 +0000 | [diff] [blame] | 101 | This example shows a simple way of using :class:`mmap`:: |
Georg Brandl | fefcd4e | 2007-12-02 14:34:34 +0000 | [diff] [blame] | 102 | |
| 103 | import mmap |
| 104 | |
| 105 | # write a simple example file |
Hirokazu Yamamoto | 02172dd | 2009-02-28 15:24:00 +0000 | [diff] [blame] | 106 | with open("hello.txt", "wb") as f: |
Georg Brandl | fefcd4e | 2007-12-02 14:34:34 +0000 | [diff] [blame] | 107 | f.write("Hello Python!\n") |
| 108 | |
Hirokazu Yamamoto | 02172dd | 2009-02-28 15:24:00 +0000 | [diff] [blame] | 109 | with open("hello.txt", "r+b") as f: |
Georg Brandl | fefcd4e | 2007-12-02 14:34:34 +0000 | [diff] [blame] | 110 | # memory-map the file, size 0 means whole file |
| 111 | map = mmap.mmap(f.fileno(), 0) |
| 112 | # read content via standard file methods |
| 113 | print map.readline() # prints "Hello Python!" |
| 114 | # read content via slice notation |
| 115 | print map[:5] # prints "Hello" |
| 116 | # update content using slice notation; |
| 117 | # note that new content must have same size |
| 118 | map[6:] = " world!\n" |
| 119 | # ... and read again using standard file methods |
| 120 | map.seek(0) |
| 121 | print map.readline() # prints "Hello world!" |
| 122 | # close the map |
| 123 | map.close() |
| 124 | |
| 125 | |
| 126 | The next example demonstrates how to create an anonymous map and exchange |
| 127 | data between the parent and child processes:: |
| 128 | |
| 129 | import mmap |
| 130 | import os |
| 131 | |
| 132 | map = mmap.mmap(-1, 13) |
| 133 | map.write("Hello world!") |
| 134 | |
| 135 | pid = os.fork() |
| 136 | |
| 137 | if pid == 0: # In a child process |
| 138 | map.seek(0) |
| 139 | print map.readline() |
| 140 | |
| 141 | map.close() |
| 142 | |
Travis E. Oliphant | 8feafab | 2007-10-23 02:40:56 +0000 | [diff] [blame] | 143 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 144 | Memory-mapped file objects support the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 | |
| 146 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 147 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 149 | Close the file. Subsequent calls to other methods of the object will |
| 150 | result in an exception being raised. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 151 | |
| 152 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 153 | .. method:: find(string[, start[, end]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 154 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 155 | Returns the lowest index in the object where the substring *string* is |
| 156 | found, such that *string* is contained in the range [*start*, *end*]. |
| 157 | Optional arguments *start* and *end* are interpreted as in slice notation. |
| 158 | Returns ``-1`` on failure. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 159 | |
| 160 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 161 | .. method:: flush([offset, size]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 163 | Flushes changes made to the in-memory copy of a file back to disk. Without |
| 164 | use of this call there is no guarantee that changes are written back before |
| 165 | the object is destroyed. If *offset* and *size* are specified, only |
| 166 | changes to the given range of bytes will be flushed to disk; otherwise, the |
| 167 | whole extent of the mapping is flushed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 169 | **(Windows version)** A nonzero value returned indicates success; zero |
| 170 | indicates failure. |
Jeroen Ruigrok van der Werven | 967a83c | 2008-04-16 12:57:43 +0000 | [diff] [blame] | 171 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 172 | **(Unix version)** A zero value is returned to indicate success. An |
| 173 | exception is raised when the call failed. |
Jeroen Ruigrok van der Werven | 967a83c | 2008-04-16 12:57:43 +0000 | [diff] [blame] | 174 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 175 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 176 | .. method:: move(dest, src, count) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 178 | Copy the *count* bytes starting at offset *src* to the destination index |
| 179 | *dest*. If the mmap was created with :const:`ACCESS_READ`, then calls to |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 180 | move will raise a :exc:`TypeError` exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 181 | |
| 182 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 183 | .. method:: read(num) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 184 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 185 | Return a string containing up to *num* bytes starting from the current |
| 186 | file position; the file position is updated to point after the bytes that |
| 187 | were returned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 188 | |
| 189 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 190 | .. method:: read_byte() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 191 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 192 | Returns a string of length 1 containing the character at the current file |
| 193 | position, and advances the file position by 1. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 194 | |
| 195 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 196 | .. method:: readline() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 197 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 198 | Returns a single line, starting at the current file position and up to the |
| 199 | next newline. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 200 | |
| 201 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 202 | .. method:: resize(newsize) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 203 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 204 | Resizes the map and the underlying file, if any. If the mmap was created |
| 205 | with :const:`ACCESS_READ` or :const:`ACCESS_COPY`, resizing the map will |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 206 | raise a :exc:`TypeError` exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 207 | |
| 208 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 209 | .. method:: rfind(string[, start[, end]]) |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 210 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 211 | Returns the highest index in the object where the substring *string* is |
| 212 | found, such that *string* is contained in the range [*start*, *end*]. |
| 213 | Optional arguments *start* and *end* are interpreted as in slice notation. |
| 214 | Returns ``-1`` on failure. |
Andrew M. Kuchling | 5c60bfc | 2008-01-19 18:18:41 +0000 | [diff] [blame] | 215 | |
| 216 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 217 | .. method:: seek(pos[, whence]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 218 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 219 | Set the file's current position. *whence* argument is optional and |
| 220 | defaults to ``os.SEEK_SET`` or ``0`` (absolute file positioning); other |
| 221 | values are ``os.SEEK_CUR`` or ``1`` (seek relative to the current |
| 222 | position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's end). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 223 | |
| 224 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 225 | .. method:: size() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 226 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 227 | Return the length of the file, which can be larger than the size of the |
| 228 | memory-mapped area. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 229 | |
| 230 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 231 | .. method:: tell() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 232 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 233 | Returns the current position of the file pointer. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 234 | |
| 235 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 236 | .. method:: write(string) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 237 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 238 | Write the bytes in *string* into memory at the current position of the |
| 239 | file pointer; the file position is updated to point after the bytes that |
| 240 | were written. If the mmap was created with :const:`ACCESS_READ`, then |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 241 | writing to it will raise a :exc:`TypeError` exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 242 | |
| 243 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 244 | .. method:: write_byte(byte) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 245 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 246 | Write the single-character string *byte* into memory at the current |
| 247 | position of the file pointer; the file position is advanced by ``1``. If |
| 248 | the mmap was created with :const:`ACCESS_READ`, then writing to it will |
Georg Brandl | 21946af | 2010-10-06 09:28:45 +0000 | [diff] [blame] | 249 | raise a :exc:`TypeError` exception. |