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