mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
#17402: avoid shadowing built-in map in mmap examples. Initial patch by Aman Shah.
This commit is contained in:
parent
a7d64a6f4c
commit
6771462961
1 changed files with 14 additions and 14 deletions
|
@ -106,19 +106,19 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
|
||||||
|
|
||||||
with open("hello.txt", "r+b") as f:
|
with open("hello.txt", "r+b") as f:
|
||||||
# memory-map the file, size 0 means whole file
|
# memory-map the file, size 0 means whole file
|
||||||
map = mmap.mmap(f.fileno(), 0)
|
mm = mmap.mmap(f.fileno(), 0)
|
||||||
# read content via standard file methods
|
# read content via standard file methods
|
||||||
print(map.readline()) # prints b"Hello Python!\n"
|
print(mm.readline()) # prints b"Hello Python!\n"
|
||||||
# read content via slice notation
|
# read content via slice notation
|
||||||
print(map[:5]) # prints b"Hello"
|
print(mm[:5]) # prints b"Hello"
|
||||||
# update content using slice notation;
|
# update content using slice notation;
|
||||||
# note that new content must have same size
|
# note that new content must have same size
|
||||||
map[6:] = b" world!\n"
|
mm[6:] = b" world!\n"
|
||||||
# ... and read again using standard file methods
|
# ... and read again using standard file methods
|
||||||
map.seek(0)
|
mm.seek(0)
|
||||||
print(map.readline()) # prints b"Hello world!\n"
|
print(mm.readline()) # prints b"Hello world!\n"
|
||||||
# close the map
|
# close the map
|
||||||
map.close()
|
mm.close()
|
||||||
|
|
||||||
|
|
||||||
:class:`mmap` can also be used as a context manager in a :keyword:`with`
|
:class:`mmap` can also be used as a context manager in a :keyword:`with`
|
||||||
|
@ -126,8 +126,8 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
|
||||||
|
|
||||||
import mmap
|
import mmap
|
||||||
|
|
||||||
with mmap.mmap(-1, 13) as map:
|
with mmap.mmap(-1, 13) as mm:
|
||||||
map.write("Hello world!")
|
mm.write("Hello world!")
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
Context manager support.
|
Context manager support.
|
||||||
|
@ -139,16 +139,16 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
|
||||||
import mmap
|
import mmap
|
||||||
import os
|
import os
|
||||||
|
|
||||||
map = mmap.mmap(-1, 13)
|
mm = mmap.mmap(-1, 13)
|
||||||
map.write(b"Hello world!")
|
mm.write(b"Hello world!")
|
||||||
|
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
|
|
||||||
if pid == 0: # In a child process
|
if pid == 0: # In a child process
|
||||||
map.seek(0)
|
mm.seek(0)
|
||||||
print(map.readline())
|
print(mm.readline())
|
||||||
|
|
||||||
map.close()
|
mm.close()
|
||||||
|
|
||||||
|
|
||||||
Memory-mapped file objects support the following methods:
|
Memory-mapped file objects support the following methods:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue