1 \section{\module{mmap
} ---
2 Memory-mapped file support
}
4 \declaremodule{builtin
}{mmap
}
5 \modulesynopsis{Interface to memory-mapped files for
\UNIX\ and Windows.
}
7 Memory-mapped file objects behave like both strings and like
8 file objects. Unlike normal string objects, however, these are
9 mutable. You can use mmap objects in most places where strings
10 are expected; for example, you can use the
\module{re
} module to
11 search through a memory-mapped file. Since they're mutable, you can
12 change a single character by doing
\code{obj
[\var{index
}] = 'a'
}, or
13 change a substring by assigning to a slice:
14 \code{obj
[\var{i1
}:
\var{i2
}] = '...'
}. You can also read and write
15 data starting at the current file position, and
\method{seek()
}
16 through the file to different positions.
18 A memory-mapped file is created by the
\function{mmap()
} function,
19 which is different on
\UNIX{} and on Windows. In either case you must
20 provide a file descriptor for a file opened for update.
21 If you wish to map an existing Python file object, use its
22 \method{fileno()
} method to obtain the correct value for the
23 \var{fileno
} parameter. Otherwise, you can open the file using the
24 \function{os.open()
} function, which returns a file descriptor
25 directly (the file still needs to be closed when done).
27 For both the
\UNIX{} and Windows versions of the function,
28 \var{access
} may be specified as an optional keyword parameter.
29 \var{access
} accepts one of three values:
\constant{ACCESS_READ
},
30 \constant{ACCESS_WRITE
}, or
\constant{ACCESS_COPY
} to specify
31 readonly, write-through or copy-on-write memory respectively.
32 \var{access
} can be used on both
\UNIX{} and Windows. If
33 \var{access
} is not specified, Windows mmap returns a write-through
34 mapping. The initial memory values for all three access types are
35 taken from the specified file. Assignment to an
36 \constant{ACCESS_READ
} memory map raises a
\exception{TypeError
}
37 exception. Assignment to an
\constant{ACCESS_WRITE
} memory map
38 affects both memory and the underlying file. Assigment to an
39 \constant{ACCESS_COPY
} memory map affects memory but does not update
42 \begin{funcdesc
}{mmap
}{fileno, length
\optional{, tagname
\optional{, access
}}}
43 \strong{(Windows version)
} Maps
\var{length
} bytes from the file
44 specified by the file handle
\var{fileno
}, and returns a mmap
45 object. If
\var{length
} is
\code{0}, the maximum length of the map
46 will be the current size of the file when
\function{mmap()
} is
49 \var{tagname
}, if specified and not
\code{None
}, is a string giving
50 a tag name for the mapping. Windows allows you to have many
51 different mappings against the same file. If you specify the name
52 of an existing tag, that tag is opened, otherwise a new tag of this
53 name is created. If this parameter is omitted or
\code{None
}, the
54 mapping is created without a name. Avoiding the use of the tag
55 parameter will assist in keeping your code portable between
\UNIX{}
59 \begin{funcdescni
}{mmap
}{fileno, length
\optional{, flags
\optional{,
60 prot
\optional{, access
}}}}
61 \strong{(
\UNIX{} version)
} Maps
\var{length
} bytes from the file
62 specified by the file descriptor
\var{fileno
}, and returns a mmap
65 \var{flags
} specifies the nature of the mapping.
66 \constant{MAP_PRIVATE
} creates a private copy-on-write mapping, so
67 changes to the contents of the mmap object will be private to this
68 process, and
\constant{MAP_SHARED
} creates a mapping that's shared
69 with all other processes mapping the same areas of the file. The
70 default value is
\constant{MAP_SHARED
}.
72 \var{prot
}, if specified, gives the desired memory protection; the
73 two most useful values are
\constant{PROT_READ
} and
74 \constant{PROT_WRITE
}, to specify that the pages may be read or
75 written.
\var{prot
} defaults to
\constant{PROT_READ | PROT_WRITE
}.
77 \var{access
} may be specified in lieu of
\var{flags
} and
\var{prot
}
78 as an optional keyword parameter. It is an error to specify both
79 \var{flags
},
\var{prot
} and
\var{access
}. See the description of
80 \var{access
} above for information on how to use this parameter.
84 Memory-mapped file objects support the following methods:
87 \begin{methoddesc
}{close
}{}
88 Close the file. Subsequent calls to other methods of the object
89 will result in an exception being raised.
92 \begin{methoddesc
}{find
}{string
\optional{, start
}}
93 Returns the lowest index in the object where the substring
94 \var{string
} is found. Returns
\code{-
1} on failure.
\var{start
}
95 is the index at which the search begins, and defaults to zero.
98 \begin{methoddesc
}{flush
}{\optional{offset, size
}}
99 Flushes changes made to the in-memory copy of a file back to disk.
100 Without use of this call there is no guarantee that changes are
101 written back before the object is destroyed. If
\var{offset
} and
102 \var{size
} are specified, only changes to the given range of bytes
103 will be flushed to disk; otherwise, the whole extent of the mapping
107 \begin{methoddesc
}{move
}{\var{dest
},
\var{src
},
\var{count
}}
108 Copy the
\var{count
} bytes starting at offset
\var{src
} to the
109 destination index
\var{dest
}. If the mmap was created with
110 \constant{ACCESS_READ
}, then calls to move will throw a
111 \exception{TypeError
} exception.
114 \begin{methoddesc
}{read
}{\var{num
}}
115 Return a string containing up to
\var{num
} bytes starting from the
116 current file position; the file position is updated to point after the
117 bytes that were returned.
120 \begin{methoddesc
}{read_byte
}{}
121 Returns a string of length
1 containing the character at the current
122 file position, and advances the file position by
1.
125 \begin{methoddesc
}{readline
}{}
126 Returns a single line, starting at the current file position and up to
130 \begin{methoddesc
}{resize
}{\var{newsize
}}
131 If the mmap was created with
\constant{ACCESS_READ
} or
132 \constant{ACCESS_COPY
}, resizing the map will throw a
\exception{TypeError
} exception.
135 \begin{methoddesc
}{seek
}{pos
\optional{, whence
}}
136 Set the file's current position.
\var{whence
} argument is optional
137 and defaults to
\code{0} (absolute file positioning); other values
138 are
\code{1} (seek relative to the current position) and
\code{2}
139 (seek relative to the file's end).
142 \begin{methoddesc
}{size
}{}
143 Return the length of the file, which can be larger than the size of
144 the memory-mapped area.
147 \begin{methoddesc
}{tell
}{}
148 Returns the current position of the file pointer.
151 \begin{methoddesc
}{write
}{\var{string
}}
152 Write the bytes in
\var{string
} into memory at the current position
153 of the file pointer; the file position is updated to point after the
154 bytes that were written. If the mmap was created with
155 \constant{ACCESS_READ
}, then writing to it will throw a
156 \exception{TypeError
} exception.
159 \begin{methoddesc
}{write_byte
}{\var{byte
}}
160 Write the single-character string
\var{byte
} into memory at the
161 current position of the file pointer; the file position is advanced
162 by
\code{1}.If the mmap was created with
\constant{ACCESS_READ
},
163 then writing to it will throw a
\exception{TypeError
} exception.