Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 1 | \section{\module{mmap} --- |
| 2 | Memory-mapped file support} |
| 3 | |
| 4 | \declaremodule{builtin}{mmap} |
| 5 | \modulesynopsis{Interface to memory-mapped files for Unix and Windows.} |
| 6 | |
| 7 | Memory-mapped file objects behave like both mutable strings and like |
| 8 | file objects. You can use mmap objects in most places where strings |
| 9 | are expected; for example, you can use the \module{re} module to |
| 10 | search through a memory-mapped file. Since they're mutable, you can |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 11 | change a single character by doing \code{obj[\var{index}] = 'a'}, or |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 12 | change a substring by assigning to a slice: |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 13 | \code{obj[\var{i1}:\var{i2}] = '...'}. You can also read and write |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 14 | data starting at the current file position, and \method{seek()} |
| 15 | through the file to different positions. |
| 16 | |
| 17 | A memory-mapped file is created by the following function, which is |
| 18 | different on Unix and on Windows. |
| 19 | |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 20 | \begin{funcdesc}{mmap}{fileno, length\optional{, tagname}} |
| 21 | \strong{(Windows version)} Maps \var{length} bytes from the file |
| 22 | specified by the file handle \var{fileno}, and returns a mmap object. |
| 23 | If you wish to map an existing Python file object, use its |
| 24 | \method{fileno()} method to obtain the correct value for the |
| 25 | \var{fileno} parameter. |
Andrew M. Kuchling | 0adfb45 | 2000-06-18 04:17:38 +0000 | [diff] [blame] | 26 | |
| 27 | \var{tagname}, if specified, is a string giving a tag name for the mapping. |
| 28 | Windows allows you to have many different mappings against the same |
| 29 | file. If you specify the name of an existing tag, that tag is opened, |
| 30 | otherwise a new tag of this name is created. If this parameter is |
| 31 | None, the mapping is created without a name. Avoiding the use of the |
| 32 | tag parameter will assist in keeping your code portable between Unix |
| 33 | and Windows. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 34 | \end{funcdesc} |
| 35 | |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{mmap}{fileno, size\optional{, flags, prot}} |
| 37 | \strong{(Unix version)} Maps \var{length} bytes from the file |
| 38 | specified by the file handle \var{fileno}, and returns a mmap object. |
| 39 | If you wish to map an existing Python file object, use its |
| 40 | \method{fileno()} method to obtain the correct value for the |
| 41 | \var{fileno} parameter. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 42 | |
| 43 | \var{flags} specifies the nature of the mapping. |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 44 | \constant{MAP_PRIVATE} creates a private copy-on-write mapping, so |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 45 | changes to the contents of the mmap object will be private to this |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 46 | process, and \constant{MAP_SHARED} creates a mapping that's shared |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 47 | with all other processes mapping the same areas of the file. |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 48 | The default value is \constant{MAP_SHARED}. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 49 | |
| 50 | \var{prot}, if specified, gives the desired memory protection; the two |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 51 | most useful values are \constant{PROT_READ} and \constant{PROT_WRITE}, |
| 52 | to specify that the pages may be read or written. |
| 53 | \var{prot} defaults to \constant{PROT_READ | PROT_WRITE}. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 54 | \end{funcdesc} |
| 55 | |
| 56 | Memory-mapped file objects support the following methods: |
| 57 | |
| 58 | |
| 59 | \begin{methoddesc}{close}{} |
| 60 | Close the file. Subsequent calls to other methods of the object |
| 61 | will result in an exception being raised. |
| 62 | \end{methoddesc} |
| 63 | |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 64 | \begin{methoddesc}{find}{string\optional{, start}} |
| 65 | Returns the lowest index in the object where the substring |
| 66 | \var{string} is found. Returns \code{-1} on failure. \var{start} is |
| 67 | the index at which the search begins, and defaults to zero. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 68 | \end{methoddesc} |
| 69 | |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 70 | \begin{methoddesc}{flush}{\optional{offset, size}} |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 71 | Flushes changes made to the in-memory copy of a file back to disk. |
| 72 | Without use of this call there is no guarantee that changes are |
Andrew M. Kuchling | 0adfb45 | 2000-06-18 04:17:38 +0000 | [diff] [blame] | 73 | written back before the object is destroyed. If \var{offset} and |
| 74 | \var{size} are specified, only changes to the given range of bytes |
| 75 | will be flushed to disk; otherwise, the whole extent of the mapping is |
| 76 | flushed. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 77 | \end{methoddesc} |
| 78 | |
| 79 | \begin{methoddesc}{move}{\var{dest}, \var{src}, \var{count}} |
| 80 | Copy the \var{count} bytes starting at offset \var{src} |
| 81 | to the destination index \var{dest}. |
| 82 | \end{methoddesc} |
| 83 | |
| 84 | \begin{methoddesc}{read}{\var{num}} |
Andrew M. Kuchling | 0adfb45 | 2000-06-18 04:17:38 +0000 | [diff] [blame] | 85 | Return a string containing up to \var{num} bytes starting from the |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 86 | current file position; the file position is updated to point after the |
| 87 | bytes that were returned. |
| 88 | \end{methoddesc} |
| 89 | |
| 90 | \begin{methoddesc}{read_byte}{} |
Andrew M. Kuchling | 0adfb45 | 2000-06-18 04:17:38 +0000 | [diff] [blame] | 91 | Returns a string of length 1 containing the character at the current |
| 92 | file position, and advances the file position by 1. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 93 | \end{methoddesc} |
| 94 | |
| 95 | \begin{methoddesc}{readline}{} |
| 96 | Returns a single line, starting at the current file position and up to |
| 97 | the next newline. |
| 98 | \end{methoddesc} |
| 99 | |
| 100 | \begin{methoddesc}{resize}{\var{newsize}} |
| 101 | \end{methoddesc} |
| 102 | |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 103 | \begin{methoddesc}{seek}{pos\optional{, whence}} |
| 104 | Set the file's current position. |
| 105 | \var{whence} argument is optional and defaults to \code{0} (absolute |
| 106 | file positioning); other values are \code{1} (seek relative to the |
| 107 | current position) and \code{2} (seek relative to the file's end). |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 108 | \end{methoddesc} |
| 109 | |
| 110 | \begin{methoddesc}{size}{} |
| 111 | Return the length of the file, which can be larger than the size |
| 112 | of the memory-mapped area. |
| 113 | \end{methoddesc} |
| 114 | |
| 115 | \begin{methoddesc}{tell}{} |
| 116 | Returns the current position of the file pointer. |
| 117 | \end{methoddesc} |
| 118 | |
| 119 | \begin{methoddesc}{write}{\var{string}} |
| 120 | Write the bytes in \var{string} into memory at the current position of |
| 121 | the file pointer; the file position is updated to point after the |
| 122 | bytes that were written. |
| 123 | \end{methoddesc} |
| 124 | |
| 125 | \begin{methoddesc}{write_byte}{\var{byte}} |
Fred Drake | 8ff4cd7 | 2000-09-05 13:50:21 +0000 | [diff] [blame] | 126 | Write the single-character string \var{byte} into memory at the |
| 127 | current position of the file pointer; the file position is advanced by |
| 128 | \code{1}. |
Andrew M. Kuchling | b805069 | 2000-06-17 22:39:05 +0000 | [diff] [blame] | 129 | \end{methoddesc} |