diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-04-04 17:09:35 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-04-04 17:09:35 (GMT) |
commit | e099b37428ab72cf70c745e941b9767fcad8eaa3 (patch) | |
tree | 11ba876743d835964f1b6a9cb52286d39326e54d /Doc/library/mmap.rst | |
parent | f3abcc9d3c5c2a0bb739df2f3649ffa8b958a084 (diff) | |
download | cpython-e099b37428ab72cf70c745e941b9767fcad8eaa3.zip cpython-e099b37428ab72cf70c745e941b9767fcad8eaa3.tar.gz cpython-e099b37428ab72cf70c745e941b9767fcad8eaa3.tar.bz2 |
#5391 make mmap work exclusively with bytes
Diffstat (limited to 'Doc/library/mmap.rst')
-rw-r--r-- | Doc/library/mmap.rst | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst index ceca29a..c3dba43 100644 --- a/Doc/library/mmap.rst +++ b/Doc/library/mmap.rst @@ -6,13 +6,13 @@ :synopsis: Interface to memory-mapped files for Unix and Windows. -Memory-mapped file objects behave like both strings and like file objects. -Unlike normal string objects, however, these are mutable. You can use mmap -objects in most places where strings are expected; for example, you can use -the :mod:`re` module to search through a memory-mapped file. Since they're -mutable, you can change a single character by doing ``obj[index] = 'a'``, or -change a substring by assigning to a slice: ``obj[i1:i2] = '...'``. You can -also read and write data starting at the current file position, and +Memory-mapped file objects behave like both :class:`bytes` and like file +objects. Unlike normal :class:`bytes` objects, however, these are mutable. +You can use mmap objects in most places where :class:`bytes` are expected; for +example, you can use the :mod:`re` module to search through a memory-mapped file. +Since they're mutable, you can change a single byte by doing ``obj[index] = 97``, +or change a subsequence by assigning to a slice: ``obj[i1:i2] = b'...'``. +You can also read and write data starting at the current file position, and :meth:`seek` through the file to different positions. A memory-mapped file is created by the :class:`mmap` constructor, which is @@ -94,21 +94,21 @@ To map anonymous memory, -1 should be passed as the fileno along with the length # write a simple example file with open("hello.txt", "wb") as f: - f.write("Hello Python!\n") + f.write(b"Hello Python!\n") with open("hello.txt", "r+b") as f: # memory-map the file, size 0 means whole file map = mmap.mmap(f.fileno(), 0) # read content via standard file methods - print(map.readline()) # prints "Hello Python!" + print(map.readline()) # prints b"Hello Python!\n" # read content via slice notation - print(map[:5]) # prints "Hello" + print(map[:5]) # prints b"Hello" # update content using slice notation; # note that new content must have same size - map[6:] = " world!\n" + map[6:] = b" world!\n" # ... and read again using standard file methods map.seek(0) - print(map.readline()) # prints "Hello world!" + print(map.readline()) # prints b"Hello world!\n" # close the map map.close() @@ -120,7 +120,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length import os map = mmap.mmap(-1, 13) - map.write("Hello world!") + map.write(b"Hello world!") pid = os.fork() @@ -140,10 +140,10 @@ To map anonymous memory, -1 should be passed as the fileno along with the length result in an exception being raised. - .. method:: find(string[, start[, end]]) + .. method:: find(sub[, start[, end]]) - Returns the lowest index in the object where the substring *string* is - found, such that *string* is contained in the range [*start*, *end*]. + Returns the lowest index in the object where the subsequence *sub* is + found, such that *sub* is contained in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. Returns ``-1`` on failure. @@ -172,15 +172,15 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. method:: read(num) - Return a string containing up to *num* bytes starting from the current - file position; the file position is updated to point after the bytes that - were returned. + Return a :class:`bytes` containing up to *num* bytes starting from the + current file position; the file position is updated to point after the + bytes that were returned. .. method:: read_byte() - Returns a string of length 1 containing the character at the current file - position, and advances the file position by 1. + Returns a byte at the current file position as an integer, and advances + the file position by 1. .. method:: readline() @@ -196,10 +196,10 @@ To map anonymous memory, -1 should be passed as the fileno along with the length throw a :exc:`TypeError` exception. - .. method:: rfind(string[, start[, end]]) + .. method:: rfind(sub[, start[, end]]) - Returns the highest index in the object where the substring *string* is - found, such that *string* is contained in the range [*start*, *end*]. + Returns the highest index in the object where the subsequence *sub* is + found, such that *sub* is contained in the range [*start*, *end*]. Optional arguments *start* and *end* are interpreted as in slice notation. Returns ``-1`` on failure. @@ -223,9 +223,9 @@ To map anonymous memory, -1 should be passed as the fileno along with the length Returns the current position of the file pointer. - .. method:: write(string) + .. method:: write(bytes) - Write the bytes in *string* into memory at the current position of the + Write the bytes in *bytes* into memory at the current position of the file pointer; the file position is updated to point after the bytes that were written. If the mmap was created with :const:`ACCESS_READ`, then writing to it will throw a :exc:`TypeError` exception. @@ -233,7 +233,7 @@ To map anonymous memory, -1 should be passed as the fileno along with the length .. method:: write_byte(byte) - Write the single-character string *byte* into memory at the current + Write the the integer *byte* into memory at the current position of the file pointer; the file position is advanced by ``1``. If the mmap was created with :const:`ACCESS_READ`, then writing to it will throw a :exc:`TypeError` exception. |