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