Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`mmap` --- Memory-mapped file support |
| 2 | ========================================== |
| 3 | |
| 4 | .. module:: mmap |
| 5 | :synopsis: Interface to memory-mapped files for Unix and Windows. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 9 | Memory-mapped file objects behave like both :class:`bytearray` and like |
| 10 | :term:`file objects <file object>`. You can use mmap objects in most places |
| 11 | where :class:`bytearray` are expected; for example, you can use the :mod:`re` |
| 12 | module to search through a memory-mapped file. You can also change a single |
| 13 | byte by doing ``obj[index] = 97``, or change a subsequence by assigning to a |
| 14 | slice: ``obj[i1:i2] = b'...'``. You can also read and write data starting at |
| 15 | the current file position, and :meth:`seek` through the file to different positions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 16 | |
Serhiy Storchaka | ee1b01a | 2016-12-02 23:13:53 +0200 | [diff] [blame] | 17 | A memory-mapped file is created by the :class:`~mmap.mmap` constructor, which is |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 18 | different on Unix and on Windows. In either case you must provide a file |
| 19 | descriptor for a file opened for update. If you wish to map an existing Python |
| 20 | file object, use its :meth:`fileno` method to obtain the correct value for the |
| 21 | *fileno* parameter. Otherwise, you can open the file using the |
| 22 | :func:`os.open` function, which returns a file descriptor directly (the file |
| 23 | still needs to be closed when done). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Ross Lagerwall | 59c01ed | 2011-07-25 07:12:43 +0200 | [diff] [blame] | 25 | .. note:: |
| 26 | If you want to create a memory-mapping for a writable, buffered file, you |
| 27 | should :func:`~io.IOBase.flush` the file first. This is necessary to ensure |
| 28 | that local modifications to the buffers are actually available to the |
| 29 | mapping. |
| 30 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 31 | For both the Unix and Windows versions of the constructor, *access* may be |
Justus Schwabedal | 5a8a84b | 2017-11-07 15:51:43 -0500 | [diff] [blame] | 32 | specified as an optional keyword parameter. *access* accepts one of four |
| 33 | values: :const:`ACCESS_READ`, :const:`ACCESS_WRITE`, or :const:`ACCESS_COPY` to |
| 34 | specify read-only, write-through or copy-on-write memory respectively, or |
| 35 | :const:`ACCESS_DEFAULT` to defer to *prot*. *access* can be used on both Unix |
| 36 | and Windows. If *access* is not specified, Windows mmap returns a |
| 37 | write-through mapping. The initial memory values for all three access types |
| 38 | are taken from the specified file. Assignment to an :const:`ACCESS_READ` |
| 39 | memory map raises a :exc:`TypeError` exception. Assignment to an |
| 40 | :const:`ACCESS_WRITE` memory map affects both memory and the underlying file. |
| 41 | Assignment to an :const:`ACCESS_COPY` memory map affects memory but does not |
| 42 | update the underlying file. |
| 43 | |
| 44 | .. versionchanged:: 3.7 |
| 45 | Added :const:`ACCESS_DEFAULT` constant. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 47 | To map anonymous memory, -1 should be passed as the fileno along with the length. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 49 | .. class:: mmap(fileno, length, tagname=None, access=ACCESS_DEFAULT[, offset]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 51 | **(Windows version)** Maps *length* bytes from the file specified by the |
| 52 | file handle *fileno*, and creates a mmap object. If *length* is larger |
| 53 | than the current size of the file, the file is extended to contain *length* |
| 54 | bytes. If *length* is ``0``, the maximum length of the map is the current |
| 55 | size of the file, except that if the file is empty Windows raises an |
| 56 | exception (you cannot create an empty mapping on Windows). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 58 | *tagname*, if specified and not ``None``, is a string giving a tag name for |
| 59 | the mapping. Windows allows you to have many different mappings against |
| 60 | the same file. If you specify the name of an existing tag, that tag is |
| 61 | opened, otherwise a new tag of this name is created. If this parameter is |
| 62 | omitted or ``None``, the mapping is created without a name. Avoiding the |
| 63 | use of the tag parameter will assist in keeping your code portable between |
| 64 | Unix and Windows. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 66 | *offset* may be specified as a non-negative integer offset. mmap references |
| 67 | will be relative to the offset from the beginning of the file. *offset* |
Pablo Galindo | 027664a | 2018-10-20 01:37:55 +0100 | [diff] [blame] | 68 | defaults to 0. *offset* must be a multiple of the :const:`ALLOCATIONGRANULARITY`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 70 | .. audit-event:: mmap.__new__ fileno,length,access,offset mmap.mmap |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 71 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 72 | .. class:: mmap(fileno, length, flags=MAP_SHARED, prot=PROT_WRITE|PROT_READ, access=ACCESS_DEFAULT[, offset]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | :noindex: |
| 74 | |
| 75 | **(Unix version)** Maps *length* bytes from the file specified by the file |
| 76 | descriptor *fileno*, and returns a mmap object. If *length* is ``0``, the |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 77 | maximum length of the map will be the current size of the file when |
Serhiy Storchaka | ee1b01a | 2016-12-02 23:13:53 +0200 | [diff] [blame] | 78 | :class:`~mmap.mmap` is called. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | *flags* specifies the nature of the mapping. :const:`MAP_PRIVATE` creates a |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 81 | private copy-on-write mapping, so changes to the contents of the mmap |
| 82 | object will be private to this process, and :const:`MAP_SHARED` creates a |
| 83 | mapping that's shared with all other processes mapping the same areas of |
Ethan Steinberg | 21fda91 | 2020-05-26 14:42:18 -0700 | [diff] [blame] | 84 | the file. The default value is :const:`MAP_SHARED`. Some systems have |
| 85 | additional possible flags with the full list specified in |
| 86 | :ref:`MAP_* constants <map-constants>`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 88 | *prot*, if specified, gives the desired memory protection; the two most |
| 89 | useful values are :const:`PROT_READ` and :const:`PROT_WRITE`, to specify |
| 90 | that the pages may be read or written. *prot* defaults to |
| 91 | :const:`PROT_READ \| PROT_WRITE`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 93 | *access* may be specified in lieu of *flags* and *prot* as an optional |
| 94 | keyword parameter. It is an error to specify both *flags*, *prot* and |
| 95 | *access*. See the description of *access* above for information on how to |
| 96 | use this parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 98 | *offset* may be specified as a non-negative integer offset. mmap references |
| 99 | will be relative to the offset from the beginning of the file. *offset* |
Pablo Galindo | 027664a | 2018-10-20 01:37:55 +0100 | [diff] [blame] | 100 | defaults to 0. *offset* must be a multiple of :const:`ALLOCATIONGRANULARITY` |
| 101 | which is equal to :const:`PAGESIZE` on Unix systems. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 102 | |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 103 | To ensure validity of the created memory mapping the file specified |
| 104 | by the descriptor *fileno* is internally automatically synchronized |
| 105 | with physical backing store on Mac OS X and OpenVMS. |
| 106 | |
Serhiy Storchaka | ee1b01a | 2016-12-02 23:13:53 +0200 | [diff] [blame] | 107 | This example shows a simple way of using :class:`~mmap.mmap`:: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 108 | |
| 109 | import mmap |
| 110 | |
| 111 | # write a simple example file |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 112 | with open("hello.txt", "wb") as f: |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 113 | f.write(b"Hello Python!\n") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 115 | with open("hello.txt", "r+b") as f: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 116 | # memory-map the file, size 0 means whole file |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 117 | mm = mmap.mmap(f.fileno(), 0) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 118 | # read content via standard file methods |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 119 | print(mm.readline()) # prints b"Hello Python!\n" |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 120 | # read content via slice notation |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 121 | print(mm[:5]) # prints b"Hello" |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 122 | # update content using slice notation; |
| 123 | # note that new content must have same size |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 124 | mm[6:] = b" world!\n" |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 125 | # ... and read again using standard file methods |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 126 | mm.seek(0) |
| 127 | print(mm.readline()) # prints b"Hello world!\n" |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 128 | # close the map |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 129 | mm.close() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 130 | |
| 131 | |
Serhiy Storchaka | ee1b01a | 2016-12-02 23:13:53 +0200 | [diff] [blame] | 132 | :class:`~mmap.mmap` can also be used as a context manager in a :keyword:`with` |
Julien Palard | 7855313 | 2018-03-28 23:14:15 +0200 | [diff] [blame] | 133 | statement:: |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 134 | |
| 135 | import mmap |
| 136 | |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 137 | with mmap.mmap(-1, 13) as mm: |
Zachary Ware | 42f740d | 2016-04-28 14:47:12 -0500 | [diff] [blame] | 138 | mm.write(b"Hello world!") |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 139 | |
| 140 | .. versionadded:: 3.2 |
| 141 | Context manager support. |
| 142 | |
| 143 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 144 | The next example demonstrates how to create an anonymous map and exchange |
| 145 | data between the parent and child processes:: |
| 146 | |
| 147 | import mmap |
| 148 | import os |
| 149 | |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 150 | mm = mmap.mmap(-1, 13) |
| 151 | mm.write(b"Hello world!") |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 152 | |
| 153 | pid = os.fork() |
| 154 | |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 155 | if pid == 0: # In a child process |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 156 | mm.seek(0) |
| 157 | print(mm.readline()) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 158 | |
Ezio Melotti | 6771462 | 2013-03-13 02:27:00 +0200 | [diff] [blame] | 159 | mm.close() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 160 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 161 | .. audit-event:: mmap.__new__ fileno,length,access,offset mmap.mmap |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 163 | Memory-mapped file objects support the following methods: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 165 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |
Senthil Kumaran | b918395 | 2013-09-09 22:39:28 -0700 | [diff] [blame] | 167 | Closes the mmap. Subsequent calls to other methods of the object will |
| 168 | result in a ValueError exception being raised. This will not close |
| 169 | the open file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 | |
| 171 | |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 172 | .. attribute:: closed |
| 173 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 174 | ``True`` if the file is closed. |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 175 | |
| 176 | .. versionadded:: 3.2 |
| 177 | |
| 178 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 179 | .. method:: find(sub[, start[, end]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 181 | Returns the lowest index in the object where the subsequence *sub* is |
| 182 | found, such that *sub* is contained in the range [*start*, *end*]. |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 183 | Optional arguments *start* and *end* are interpreted as in slice notation. |
| 184 | Returns ``-1`` on failure. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 186 | .. versionchanged:: 3.5 |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 187 | Writable :term:`bytes-like object` is now accepted. |
| 188 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
Georg Brandl | cd7f32b | 2009-06-08 09:13:45 +0000 | [diff] [blame] | 190 | .. method:: flush([offset[, size]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 192 | Flushes changes made to the in-memory copy of a file back to disk. Without |
| 193 | use of this call there is no guarantee that changes are written back before |
| 194 | the object is destroyed. If *offset* and *size* are specified, only |
| 195 | changes to the given range of bytes will be flushed to disk; otherwise, the |
Pablo Galindo | 027664a | 2018-10-20 01:37:55 +0100 | [diff] [blame] | 196 | whole extent of the mapping is flushed. *offset* must be a multiple of the |
| 197 | :const:`PAGESIZE` or :const:`ALLOCATIONGRANULARITY`. |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 198 | |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 199 | ``None`` is returned to indicate success. An exception is raised when the |
| 200 | call failed. |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 201 | |
Berker Peksag | e7d4b2f | 2018-08-22 21:21:05 +0300 | [diff] [blame] | 202 | .. versionchanged:: 3.8 |
| 203 | Previously, a nonzero value was returned on success; zero was returned |
| 204 | on error under Windows. A zero value was returned on success; an |
| 205 | exception was raised on error under Unix. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
| 207 | |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 208 | .. method:: madvise(option[, start[, length]]) |
| 209 | |
| 210 | Send advice *option* to the kernel about the memory region beginning at |
| 211 | *start* and extending *length* bytes. *option* must be one of the |
| 212 | :ref:`MADV_* constants <madvise-constants>` available on the system. If |
| 213 | *start* and *length* are omitted, the entire mapping is spanned. On |
| 214 | some systems (including Linux), *start* must be a multiple of the |
| 215 | :const:`PAGESIZE`. |
| 216 | |
| 217 | Availability: Systems with the ``madvise()`` system call. |
| 218 | |
| 219 | .. versionadded:: 3.8 |
| 220 | |
| 221 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 222 | .. method:: move(dest, src, count) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 224 | Copy the *count* bytes starting at offset *src* to the destination index |
| 225 | *dest*. If the mmap was created with :const:`ACCESS_READ`, then calls to |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 226 | move will raise a :exc:`TypeError` exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | |
| 228 | |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 229 | .. method:: read([n]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 231 | Return a :class:`bytes` containing up to *n* bytes starting from the |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 232 | current file position. If the argument is omitted, ``None`` or negative, |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 233 | return all bytes from the current file position to the end of the |
| 234 | mapping. The file position is updated to point after the bytes that were |
| 235 | returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 237 | .. versionchanged:: 3.3 |
Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 238 | Argument can be omitted or ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 240 | .. method:: read_byte() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 242 | Returns a byte at the current file position as an integer, and advances |
| 243 | the file position by 1. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
| 245 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 246 | .. method:: readline() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 248 | Returns a single line, starting at the current file position and up to the |
Wellington Pardim | 6c9974e | 2020-02-10 17:13:41 -0300 | [diff] [blame] | 249 | next newline. The file position is updated to point after the bytes that were |
| 250 | returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
| 252 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 253 | .. method:: resize(newsize) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 255 | Resizes the map and the underlying file, if any. If the mmap was created |
| 256 | with :const:`ACCESS_READ` or :const:`ACCESS_COPY`, resizing the map will |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 257 | raise a :exc:`TypeError` exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | |
| 259 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 260 | .. method:: rfind(sub[, start[, end]]) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 261 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 262 | Returns the highest index in the object where the subsequence *sub* is |
| 263 | found, such that *sub* is contained in the range [*start*, *end*]. |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 264 | Optional arguments *start* and *end* are interpreted as in slice notation. |
| 265 | Returns ``-1`` on failure. |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 266 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 267 | .. versionchanged:: 3.5 |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 268 | Writable :term:`bytes-like object` is now accepted. |
| 269 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 270 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 271 | .. method:: seek(pos[, whence]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 273 | Set the file's current position. *whence* argument is optional and |
| 274 | defaults to ``os.SEEK_SET`` or ``0`` (absolute file positioning); other |
| 275 | values are ``os.SEEK_CUR`` or ``1`` (seek relative to the current |
| 276 | position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's end). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
| 278 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 279 | .. method:: size() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 281 | Return the length of the file, which can be larger than the size of the |
| 282 | memory-mapped area. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 283 | |
| 284 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 285 | .. method:: tell() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 287 | Returns the current position of the file pointer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | |
| 289 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 290 | .. method:: write(bytes) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | e099b37 | 2009-04-04 17:09:35 +0000 | [diff] [blame] | 292 | Write the bytes in *bytes* into memory at the current position of the |
Berker Peksag | 6282e65 | 2016-03-02 19:30:18 +0200 | [diff] [blame] | 293 | file pointer and return the number of bytes written (never less than |
| 294 | ``len(bytes)``, since if the write fails, a :exc:`ValueError` will be |
| 295 | raised). The file position is updated to point after the bytes that |
| 296 | were written. If the mmap was created with :const:`ACCESS_READ`, then |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 297 | writing to it will raise a :exc:`TypeError` exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 298 | |
Georg Brandl | 8c16cb9 | 2016-02-25 20:17:45 +0100 | [diff] [blame] | 299 | .. versionchanged:: 3.5 |
Serhiy Storchaka | 8490f5a | 2015-03-20 09:00:36 +0200 | [diff] [blame] | 300 | Writable :term:`bytes-like object` is now accepted. |
| 301 | |
Berker Peksag | 6282e65 | 2016-03-02 19:30:18 +0200 | [diff] [blame] | 302 | .. versionchanged:: 3.6 |
| 303 | The number of bytes written is now returned. |
| 304 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 305 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 306 | .. method:: write_byte(byte) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
Ezio Melotti | e130a52 | 2011-10-19 10:58:56 +0300 | [diff] [blame] | 308 | Write the integer *byte* into memory at the current |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 309 | position of the file pointer; the file position is advanced by ``1``. If |
| 310 | the mmap was created with :const:`ACCESS_READ`, then writing to it will |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 311 | raise a :exc:`TypeError` exception. |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 312 | |
| 313 | .. _madvise-constants: |
| 314 | |
| 315 | MADV_* Constants |
| 316 | ++++++++++++++++ |
| 317 | |
| 318 | .. data:: MADV_NORMAL |
| 319 | MADV_RANDOM |
| 320 | MADV_SEQUENTIAL |
| 321 | MADV_WILLNEED |
| 322 | MADV_DONTNEED |
| 323 | MADV_REMOVE |
| 324 | MADV_DONTFORK |
| 325 | MADV_DOFORK |
| 326 | MADV_HWPOISON |
| 327 | MADV_MERGEABLE |
| 328 | MADV_UNMERGEABLE |
| 329 | MADV_SOFT_OFFLINE |
| 330 | MADV_HUGEPAGE |
| 331 | MADV_NOHUGEPAGE |
| 332 | MADV_DONTDUMP |
| 333 | MADV_DODUMP |
| 334 | MADV_FREE |
| 335 | MADV_NOSYNC |
| 336 | MADV_AUTOSYNC |
| 337 | MADV_NOCORE |
| 338 | MADV_CORE |
| 339 | MADV_PROTECT |
David CARLIER | 0e62efc | 2020-11-21 11:39:56 +0000 | [diff] [blame] | 340 | MADV_FREE_REUSABLE |
| 341 | MADV_FREE_REUSE |
Zackery Spytz | 02db696 | 2019-05-27 10:48:17 -0600 | [diff] [blame] | 342 | |
| 343 | These options can be passed to :meth:`mmap.madvise`. Not every option will |
| 344 | be present on every system. |
| 345 | |
| 346 | Availability: Systems with the madvise() system call. |
| 347 | |
| 348 | .. versionadded:: 3.8 |
Ethan Steinberg | 21fda91 | 2020-05-26 14:42:18 -0700 | [diff] [blame] | 349 | |
| 350 | .. _map-constants: |
| 351 | |
| 352 | MAP_* Constants |
| 353 | +++++++++++++++ |
| 354 | |
| 355 | .. data:: MAP_SHARED |
| 356 | MAP_PRIVATE |
| 357 | MAP_DENYWRITE |
| 358 | MAP_EXECUTABLE |
| 359 | MAP_ANON |
| 360 | MAP_ANONYMOUS |
| 361 | MAP_POPULATE |
| 362 | |
| 363 | These are the various flags that can be passed to :meth:`mmap.mmap`. Note that some options might not be present on some systems. |
| 364 | |
| 365 | .. versionchanged:: 3.10 |
| 366 | Added MAP_POPULATE constant. |