Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
3163179f
Commit
3163179f
authored
Oct 29, 2006
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert test_mmap to unittest.
parent
b9f4ad3a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
276 deletions
+156
-276
Lib/test/output/test_mmap
Lib/test/output/test_mmap
+0
-38
Lib/test/test_mmap.py
Lib/test/test_mmap.py
+156
-238
No files found.
Lib/test/output/test_mmap
deleted
100644 → 0
View file @
b9f4ad3a
test_mmap
<type 'mmap.mmap'>
Position of foo: 1.0 pages
Length of file: 2.0 pages
Contents of byte 0: '\x00'
Contents of first 3 bytes: '\x00\x00\x00'
Modifying file's content...
Contents of byte 0: '3'
Contents of first 3 bytes: '3\x00\x00'
Contents of second page: '\x00foobar\x00'
Regex match on mmap (page start, length of match): 1.0 6
Seek to zeroth byte
Seek to 42nd byte
Seek to last byte
Try to seek to negative position...
Try to seek beyond end of mmap...
Try to seek to negative position...
Attempting resize()
Creating 10 byte test data file.
Opening mmap with access=ACCESS_READ
Ensuring that readonly mmap can't be slice assigned.
Ensuring that readonly mmap can't be item assigned.
Ensuring that readonly mmap can't be write() to.
Ensuring that readonly mmap can't be write_byte() to.
Ensuring that readonly mmap can't be resized.
Opening mmap with size too big
Opening mmap with access=ACCESS_WRITE
Modifying write-through memory map.
Opening mmap with access=ACCESS_COPY
Modifying copy-on-write memory map.
Ensuring copy-on-write maps cannot be resized.
Ensuring invalid access parameter raises exception.
Try opening a bad file descriptor...
Ensuring that passing 0 as map length sets map size to current file size.
Ensuring that passing 0 as map length sets map size to current file size.
anonymous mmap.mmap(-1, PAGESIZE)...
Test passed
Lib/test/test_mmap.py
View file @
3163179f
from
test.test_support
import
verify
,
vereq
,
TESTFN
from
test.test_support
import
TESTFN
,
run_unittest
import
mmap
import
mmap
import
unittest
import
os
,
re
import
os
,
re
PAGESIZE
=
mmap
.
PAGESIZE
PAGESIZE
=
mmap
.
PAGESIZE
def
test_both
():
class
MmapTests
(
unittest
.
TestCase
):
"Test mmap module on Unix systems and Windows"
# Create a file to be mmap'ed.
def
setUp
(
self
):
if
os
.
path
.
exists
(
TESTFN
):
if
os
.
path
.
exists
(
TESTFN
):
os
.
unlink
(
TESTFN
)
os
.
unlink
(
TESTFN
)
f
=
open
(
TESTFN
,
'w+'
)
try
:
# unlink TESTFN no matter what
def
tearDown
(
self
):
try
:
os
.
unlink
(
TESTFN
)
except
OSError
:
pass
def
test_basic
(
self
):
# Test mmap module on Unix systems and Windows
# Create a file to be mmap'ed.
f
=
open
(
TESTFN
,
'w+'
)
try
:
# Write 2 pages worth of data to the file
# Write 2 pages worth of data to the file
f
.
write
(
'
\
0
'
*
PAGESIZE
)
f
.
write
(
'
\
0
'
*
PAGESIZE
)
f
.
write
(
'foo'
)
f
.
write
(
'foo'
)
...
@@ -23,84 +33,54 @@ def test_both():
...
@@ -23,84 +33,54 @@ def test_both():
# Simple sanity checks
# Simple sanity checks
print
type
(
m
)
# SF bug 128713: segfaulted on Linux
tp
=
str
(
type
(
m
))
# SF bug 128713: segfaulted on Linux
print
' Position of foo:'
,
m
.
find
(
'foo'
)
/
float
(
PAGESIZE
),
'pages'
self
.
assertEqual
(
m
.
find
(
'foo'
),
PAGESIZE
)
vereq
(
m
.
find
(
'foo'
),
PAGESIZE
)
print
' Length of file:'
,
len
(
m
)
/
float
(
PAGESIZE
),
'pages'
self
.
assertEqual
(
len
(
m
),
2
*
PAGESIZE
)
vereq
(
len
(
m
),
2
*
PAGESIZE
)
print
' Contents of byte 0:'
,
repr
(
m
[
0
])
self
.
assertEqual
(
m
[
0
],
'
\
0
'
)
vereq
(
m
[
0
],
'
\
0
'
)
self
.
assertEqual
(
m
[
0
:
3
],
'
\
0
\
0
\
0
'
)
print
' Contents of first 3 bytes:'
,
repr
(
m
[
0
:
3
])
vereq
(
m
[
0
:
3
],
'
\
0
\
0
\
0
'
)
# Modify the file's content
# Modify the file's content
print
"
\
n
Modifying file's content..."
m
[
0
]
=
'3'
m
[
0
]
=
'3'
m
[
PAGESIZE
+
3
:
PAGESIZE
+
3
+
3
]
=
'bar'
m
[
PAGESIZE
+
3
:
PAGESIZE
+
3
+
3
]
=
'bar'
# Check that the modification worked
# Check that the modification worked
print
' Contents of byte 0:'
,
repr
(
m
[
0
])
self
.
assertEqual
(
m
[
0
],
'3'
)
vereq
(
m
[
0
],
'3'
)
self
.
assertEqual
(
m
[
0
:
3
],
'3
\
0
\
0
'
)
print
' Contents of first 3 bytes:'
,
repr
(
m
[
0
:
3
])
self
.
assertEqual
(
m
[
PAGESIZE
-
1
:
PAGESIZE
+
7
],
'
\
0
foobar
\
0
'
)
vereq
(
m
[
0
:
3
],
'3
\
0
\
0
'
)
print
' Contents of second page:'
,
repr
(
m
[
PAGESIZE
-
1
:
PAGESIZE
+
7
])
vereq
(
m
[
PAGESIZE
-
1
:
PAGESIZE
+
7
],
'
\
0
foobar
\
0
'
)
m
.
flush
()
m
.
flush
()
# Test doing a regular expression match in an mmap'ed file
# Test doing a regular expression match in an mmap'ed file
match
=
re
.
search
(
'[A-Za-z]+'
,
m
)
match
=
re
.
search
(
'[A-Za-z]+'
,
m
)
if
match
is
None
:
if
match
is
None
:
print
' ERROR: regex match on mmap failed!'
self
.
fail
(
'regex match on mmap failed!'
)
else
:
else
:
start
,
end
=
match
.
span
(
0
)
start
,
end
=
match
.
span
(
0
)
length
=
end
-
start
length
=
end
-
start
print
' Regex match on mmap (page start, length of match):'
,
self
.
assertEqual
(
start
,
PAGESIZE
)
print
start
/
float
(
PAGESIZE
),
length
self
.
assertEqual
(
end
,
PAGESIZE
+
6
)
vereq
(
start
,
PAGESIZE
)
vereq
(
end
,
PAGESIZE
+
6
)
# test seeking around (try to overflow the seek implementation)
# test seeking around (try to overflow the seek implementation)
m
.
seek
(
0
,
0
)
m
.
seek
(
0
,
0
)
print
' Seek to zeroth byte'
self
.
assertEqual
(
m
.
tell
(),
0
)
vereq
(
m
.
tell
(),
0
)
m
.
seek
(
42
,
1
)
m
.
seek
(
42
,
1
)
print
' Seek to 42nd byte'
self
.
assertEqual
(
m
.
tell
(),
42
)
vereq
(
m
.
tell
(),
42
)
m
.
seek
(
0
,
2
)
m
.
seek
(
0
,
2
)
print
' Seek to last byte'
self
.
assertEqual
(
m
.
tell
(),
len
(
m
))
vereq
(
m
.
tell
(),
len
(
m
))
print
' Try to seek to negative position...'
# Try to seek to negative position...
try
:
self
.
assertRaises
(
ValueError
,
m
.
seek
,
-
1
)
m
.
seek
(
-
1
)
except
ValueError
:
pass
else
:
verify
(
0
,
'expected a ValueError but did not get it'
)
print
' Try to seek beyond end of mmap...'
# Try to seek beyond end of mmap...
try
:
self
.
assertRaises
(
ValueError
,
m
.
seek
,
1
,
2
)
m
.
seek
(
1
,
2
)
except
ValueError
:
pass
else
:
verify
(
0
,
'expected a ValueError but did not get it'
)
print
' Try to seek to negative position...'
# Try to seek to negative position...
try
:
self
.
assertRaises
(
ValueError
,
m
.
seek
,
-
len
(
m
)
-
1
,
2
)
m
.
seek
(
-
len
(
m
)
-
1
,
2
)
except
ValueError
:
pass
else
:
verify
(
0
,
'expected a ValueError but did not get it'
)
# Try resizing map
# Try resizing map
print
' Attempting resize()'
try
:
try
:
m
.
resize
(
512
)
m
.
resize
(
512
)
except
SystemError
:
except
SystemError
:
...
@@ -110,23 +90,17 @@ def test_both():
...
@@ -110,23 +90,17 @@ def test_both():
pass
pass
else
:
else
:
# resize() is supported
# resize() is supported
verify
(
len
(
m
)
==
512
,
self
.
assertEqual
(
len
(
m
),
512
)
"len(m) is %d, but expecting 512"
%
(
len
(
m
),)
)
# Check that we can no longer seek beyond the new size.
# Check that we can no longer seek beyond the new size.
try
:
self
.
assertRaises
(
ValueError
,
m
.
seek
,
513
,
0
)
m
.
seek
(
513
,
0
)
except
ValueError
:
pass
else
:
verify
(
0
,
'Could seek beyond the new size'
)
# Check that the underlying file is truncated too
# Check that the underlying file is truncated too
# (bug #728515)
# (bug #728515)
f
=
open
(
TESTFN
)
f
=
open
(
TESTFN
)
f
.
seek
(
0
,
2
)
f
.
seek
(
0
,
2
)
verify
(
f
.
tell
()
==
512
,
'Underlying file not truncated'
)
self
.
assertEqual
(
f
.
tell
(),
512
)
f
.
close
()
f
.
close
()
verify
(
m
.
size
()
==
512
,
'New size not reflected in file'
)
self
.
assertEqual
(
m
.
size
(),
512
)
m
.
close
()
m
.
close
()
...
@@ -135,56 +109,50 @@ def test_both():
...
@@ -135,56 +109,50 @@ def test_both():
f
.
close
()
f
.
close
()
except
OSError
:
except
OSError
:
pass
pass
try
:
os
.
unlink
(
TESTFN
)
except
OSError
:
pass
def
test_access_parameter
(
self
):
# Test for "access" keyword parameter
# Test for "access" keyword parameter
try
:
mapsize
=
10
mapsize
=
10
print
" Creating"
,
mapsize
,
"byte test data file."
open
(
TESTFN
,
"wb"
).
write
(
"a"
*
mapsize
)
open
(
TESTFN
,
"wb"
).
write
(
"a"
*
mapsize
)
print
" Opening mmap with access=ACCESS_READ"
f
=
open
(
TESTFN
,
"rb"
)
f
=
open
(
TESTFN
,
"rb"
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_READ
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_READ
)
verify
(
m
[:]
==
'a'
*
mapsize
,
"Readonly memory map data incorrect."
)
self
.
assertEqual
(
m
[:],
'a'
*
mapsize
,
"Readonly memory map data incorrect."
)
print
" Ensuring that readonly mmap can't be slice assigned."
# Ensuring that readonly mmap can't be slice assigned
try
:
try
:
m
[:]
=
'b'
*
mapsize
m
[:]
=
'b'
*
mapsize
except
TypeError
:
except
TypeError
:
pass
pass
else
:
else
:
verify
(
0
,
"Able to write to readonly memory map"
)
self
.
fail
(
"Able to write to readonly memory map"
)
print
" Ensuring that readonly mmap can't be item assigned."
# Ensuring that readonly mmap can't be item assigned
try
:
try
:
m
[
0
]
=
'b'
m
[
0
]
=
'b'
except
TypeError
:
except
TypeError
:
pass
pass
else
:
else
:
verify
(
0
,
"Able to write to readonly memory map"
)
self
.
fail
(
"Able to write to readonly memory map"
)
print
" Ensuring that readonly mmap can't be write() to."
# Ensuring that readonly mmap can't be write() to
try
:
try
:
m
.
seek
(
0
,
0
)
m
.
seek
(
0
,
0
)
m
.
write
(
'abc'
)
m
.
write
(
'abc'
)
except
TypeError
:
except
TypeError
:
pass
pass
else
:
else
:
verify
(
0
,
"Able to write to readonly memory map"
)
self
.
fail
(
"Able to write to readonly memory map"
)
print
" Ensuring that readonly mmap can't be write_byte() to."
# Ensuring that readonly mmap can't be write_byte() to
try
:
try
:
m
.
seek
(
0
,
0
)
m
.
seek
(
0
,
0
)
m
.
write_byte
(
'd'
)
m
.
write_byte
(
'd'
)
except
TypeError
:
except
TypeError
:
pass
pass
else
:
else
:
verify
(
0
,
"Able to write to readonly memory map"
)
self
.
fail
(
"Able to write to readonly memory map"
)
print
" Ensuring that readonly mmap can't be resized."
# Ensuring that readonly mmap can't be resized
try
:
try
:
m
.
resize
(
2
*
mapsize
)
m
.
resize
(
2
*
mapsize
)
except
SystemError
:
# resize is not universally supported
except
SystemError
:
# resize is not universally supported
...
@@ -192,12 +160,12 @@ def test_both():
...
@@ -192,12 +160,12 @@ def test_both():
except
TypeError
:
except
TypeError
:
pass
pass
else
:
else
:
verify
(
0
,
"Able to resize readonly memory map"
)
self
.
fail
(
"Able to resize readonly memory map"
)
del
m
,
f
del
m
,
f
verify
(
open
(
TESTFN
,
"rb"
).
read
()
==
'a'
*
mapsize
,
self
.
assertEqual
(
open
(
TESTFN
,
"rb"
).
read
(),
'a'
*
mapsize
,
"Readonly memory map data file was modified"
)
"Readonly memory map data file was modified"
)
print
" Opening mmap with size too big"
# Opening mmap with size too big
import
sys
import
sys
f
=
open
(
TESTFN
,
"r+b"
)
f
=
open
(
TESTFN
,
"r+b"
)
try
:
try
:
...
@@ -208,11 +176,11 @@ def test_both():
...
@@ -208,11 +176,11 @@ def test_both():
# later tests assume that the length hasn't changed. We need to
# later tests assume that the length hasn't changed. We need to
# repair that.
# repair that.
if
sys
.
platform
.
startswith
(
'win'
):
if
sys
.
platform
.
startswith
(
'win'
):
verify
(
0
,
"Opening mmap with size+1 should work on Windows."
)
self
.
fail
(
"Opening mmap with size+1 should work on Windows."
)
else
:
else
:
# we expect a ValueError on Unix, but not on Windows
# we expect a ValueError on Unix, but not on Windows
if
not
sys
.
platform
.
startswith
(
'win'
):
if
not
sys
.
platform
.
startswith
(
'win'
):
verify
(
0
,
"Opening mmap with size+1 should raise ValueError."
)
self
.
fail
(
"Opening mmap with size+1 should raise ValueError."
)
m
.
close
()
m
.
close
()
f
.
close
()
f
.
close
()
if
sys
.
platform
.
startswith
(
'win'
):
if
sys
.
platform
.
startswith
(
'win'
):
...
@@ -221,12 +189,12 @@ def test_both():
...
@@ -221,12 +189,12 @@ def test_both():
f
.
truncate
(
mapsize
)
f
.
truncate
(
mapsize
)
f
.
close
()
f
.
close
()
print
" Opening mmap with access=ACCESS_WRITE"
# Opening mmap with access=ACCESS_WRITE
f
=
open
(
TESTFN
,
"r+b"
)
f
=
open
(
TESTFN
,
"r+b"
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_WRITE
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_WRITE
)
print
" Modifying write-through memory map."
# Modifying write-through memory map
m
[:]
=
'c'
*
mapsize
m
[:]
=
'c'
*
mapsize
verify
(
m
[:]
==
'c'
*
mapsize
,
self
.
assertEqual
(
m
[:],
'c'
*
mapsize
,
"Write-through memory map memory not updated properly."
)
"Write-through memory map memory not updated properly."
)
m
.
flush
()
m
.
flush
()
m
.
close
()
m
.
close
()
...
@@ -234,66 +202,45 @@ def test_both():
...
@@ -234,66 +202,45 @@ def test_both():
f
=
open
(
TESTFN
,
'rb'
)
f
=
open
(
TESTFN
,
'rb'
)
stuff
=
f
.
read
()
stuff
=
f
.
read
()
f
.
close
()
f
.
close
()
verify
(
stuff
==
'c'
*
mapsize
,
self
.
assertEqual
(
stuff
,
'c'
*
mapsize
,
"Write-through memory map data file not updated properly."
)
"Write-through memory map data file not updated properly."
)
print
" Opening mmap with access=ACCESS_COPY"
# Opening mmap with access=ACCESS_COPY
f
=
open
(
TESTFN
,
"r+b"
)
f
=
open
(
TESTFN
,
"r+b"
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_COPY
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
mmap
.
ACCESS_COPY
)
print
" Modifying copy-on-write memory map."
# Modifying copy-on-write memory map
m
[:]
=
'd'
*
mapsize
m
[:]
=
'd'
*
mapsize
verify
(
m
[:]
==
'd'
*
mapsize
,
self
.
assertEqual
(
m
[:],
'd'
*
mapsize
,
"Copy-on-write memory map data not written correctly."
)
"Copy-on-write memory map data not written correctly."
)
m
.
flush
()
m
.
flush
()
verify
(
open
(
TESTFN
,
"rb"
).
read
()
==
'c'
*
mapsize
,
self
.
assertEqual
(
open
(
TESTFN
,
"rb"
).
read
(),
'c'
*
mapsize
,
"Copy-on-write test data file should not be modified."
)
"Copy-on-write test data file should not be modified."
)
try
:
# Ensuring copy-on-write maps cannot be resized
print
" Ensuring copy-on-write maps cannot be resized."
self
.
assertRaises
(
TypeError
,
m
.
resize
,
2
*
mapsize
)
m
.
resize
(
2
*
mapsize
)
except
TypeError
:
pass
else
:
verify
(
0
,
"Copy-on-write mmap resize did not raise exception."
)
del
m
,
f
del
m
,
f
try
:
print
" Ensuring invalid access parameter raises exception."
# Ensuring invalid access parameter raises exception
f
=
open
(
TESTFN
,
"r+b"
)
f
=
open
(
TESTFN
,
"r+b"
)
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
access
=
4
)
self
.
assertRaises
(
ValueError
,
mmap
.
mmap
,
f
.
fileno
(),
mapsize
,
access
=
4
)
except
ValueError
:
f
.
close
()
pass
else
:
verify
(
0
,
"Invalid access code should have raised exception."
)
if
os
.
name
==
"posix"
:
if
os
.
name
==
"posix"
:
# Try incompatible flags, prot and access parameters.
# Try incompatible flags, prot and access parameters.
f
=
open
(
TESTFN
,
"r+b"
)
f
=
open
(
TESTFN
,
"r+b"
)
try
:
self
.
assertRaises
(
ValueError
,
mmap
.
mmap
,
f
.
fileno
(),
mapsize
,
m
=
mmap
.
mmap
(
f
.
fileno
(),
mapsize
,
flags
=
mmap
.
MAP_PRIVATE
,
flags
=
mmap
.
MAP_PRIVATE
,
prot
=
mmap
.
PROT_READ
,
access
=
mmap
.
ACCESS_WRITE
)
prot
=
mmap
.
PROT_READ
,
access
=
mmap
.
ACCESS_WRITE
)
except
ValueError
:
pass
else
:
verify
(
0
,
"Incompatible parameters should raise ValueError."
)
f
.
close
()
f
.
close
()
finally
:
try
:
os
.
unlink
(
TESTFN
)
except
OSError
:
pass
print
' Try opening a bad file descriptor...'
def
test_bad_file_desc
(
self
):
try
:
# Try opening a bad file descriptor...
mmap
.
mmap
(
-
2
,
4096
)
self
.
assertRaises
(
mmap
.
error
,
mmap
.
mmap
,
-
2
,
4096
)
except
mmap
.
error
:
pass
else
:
verify
(
0
,
'expected a mmap.error but did not get it'
)
def
test_tougher_find
(
self
):
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
# searching for data with embedded \0 bytes didn't work.
# searching for data with embedded \0 bytes didn't work.
f
=
open
(
TESTFN
,
'w+'
)
f
=
open
(
TESTFN
,
'w+'
)
try
:
# unlink TESTFN no matter what
data
=
'aabaac
\
x00
deef
\
x00
\
x00
aa
\
x00
'
data
=
'aabaac
\
x00
deef
\
x00
\
x00
aa
\
x00
'
n
=
len
(
data
)
n
=
len
(
data
)
f
.
write
(
data
)
f
.
write
(
data
)
...
@@ -304,17 +251,14 @@ def test_both():
...
@@ -304,17 +251,14 @@ def test_both():
for
start
in
range
(
n
+
1
):
for
start
in
range
(
n
+
1
):
for
finish
in
range
(
start
,
n
+
1
):
for
finish
in
range
(
start
,
n
+
1
):
slice
=
data
[
start
:
finish
]
slice
=
data
[
start
:
finish
]
vereq
(
m
.
find
(
slice
),
data
.
find
(
slice
))
self
.
assertEqual
(
m
.
find
(
slice
),
data
.
find
(
slice
))
vereq
(
m
.
find
(
slice
+
'x'
),
-
1
)
self
.
assertEqual
(
m
.
find
(
slice
+
'x'
),
-
1
)
m
.
close
()
m
.
close
()
finally
:
def
test_double_close
(
self
):
os
.
unlink
(
TESTFN
)
# make sure a double close doesn't crash on Solaris (Bug# 665913)
# make sure a double close doesn't crash on Solaris (Bug# 665913)
f
=
open
(
TESTFN
,
'w+'
)
f
=
open
(
TESTFN
,
'w+'
)
try
:
# unlink TESTFN no matter what
f
.
write
(
2
**
16
*
'a'
)
# Arbitrary character
f
.
write
(
2
**
16
*
'a'
)
# Arbitrary character
f
.
close
()
f
.
close
()
...
@@ -324,72 +268,46 @@ def test_both():
...
@@ -324,72 +268,46 @@ def test_both():
mf
.
close
()
mf
.
close
()
f
.
close
()
f
.
close
()
finally
:
def
test_entire_file
(
self
):
os
.
unlink
(
TESTFN
)
# test mapping of entire file by passing 0 for map length
# test mapping of entire file by passing 0 for map length
if
hasattr
(
os
,
"stat"
):
if
hasattr
(
os
,
"stat"
):
print
" Ensuring that passing 0 as map length sets map size to current file size."
f
=
open
(
TESTFN
,
"w+"
)
f
=
open
(
TESTFN
,
"w+"
)
try
:
f
.
write
(
2
**
16
*
'm'
)
# Arbitrary character
f
.
close
()
f
=
open
(
TESTFN
,
"rb+"
)
mf
=
mmap
.
mmap
(
f
.
fileno
(),
0
)
verify
(
len
(
mf
)
==
2
**
16
,
"Map size should equal file size."
)
vereq
(
mf
.
read
(
2
**
16
),
2
**
16
*
"m"
)
mf
.
close
()
f
.
close
()
finally
:
os
.
unlink
(
TESTFN
)
# test mapping of entire file by passing 0 for map length
if
hasattr
(
os
,
"stat"
):
print
" Ensuring that passing 0 as map length sets map size to current file size."
f
=
open
(
TESTFN
,
"w+"
)
try
:
f
.
write
(
2
**
16
*
'm'
)
# Arbitrary character
f
.
write
(
2
**
16
*
'm'
)
# Arbitrary character
f
.
close
()
f
.
close
()
f
=
open
(
TESTFN
,
"rb+"
)
f
=
open
(
TESTFN
,
"rb+"
)
mf
=
mmap
.
mmap
(
f
.
fileno
(),
0
)
mf
=
mmap
.
mmap
(
f
.
fileno
(),
0
)
verify
(
len
(
mf
)
==
2
**
16
,
"Map size should equal file size."
)
self
.
assertEqual
(
len
(
mf
),
2
**
16
,
"Map size should equal file size."
)
vereq
(
mf
.
read
(
2
**
16
),
2
**
16
*
"m"
)
self
.
assertEqual
(
mf
.
read
(
2
**
16
),
2
**
16
*
"m"
)
mf
.
close
()
mf
.
close
()
f
.
close
()
f
.
close
()
finally
:
def
test_move
(
self
):
os
.
unlink
(
TESTFN
)
# make move works everywhere (64-bit format problem earlier)
# make move works everywhere (64-bit format problem earlier)
f
=
open
(
TESTFN
,
'w+'
)
f
=
open
(
TESTFN
,
'w+'
)
try
:
# unlink TESTFN no matter what
f
.
write
(
"ABCDEabcde"
)
# Arbitrary character
f
.
write
(
"ABCDEabcde"
)
# Arbitrary character
f
.
flush
()
f
.
flush
()
mf
=
mmap
.
mmap
(
f
.
fileno
(),
10
)
mf
=
mmap
.
mmap
(
f
.
fileno
(),
10
)
mf
.
move
(
5
,
0
,
5
)
mf
.
move
(
5
,
0
,
5
)
verify
(
mf
[:]
==
"ABCDEABCDE"
,
"Map move should have duplicated front 5"
)
self
.
assertEqual
(
mf
[:],
"ABCDEABCDE"
,
"Map move should have duplicated front 5"
)
mf
.
close
()
mf
.
close
()
f
.
close
()
f
.
close
()
finally
:
def
test_anonymous
(
self
):
os
.
unlink
(
TESTFN
)
# anonymous mmap.mmap(-1, PAGE)
def
test_anon
():
print
" anonymous mmap.mmap(-1, PAGESIZE)..."
m
=
mmap
.
mmap
(
-
1
,
PAGESIZE
)
m
=
mmap
.
mmap
(
-
1
,
PAGESIZE
)
for
x
in
xrange
(
PAGESIZE
):
for
x
in
xrange
(
PAGESIZE
):
verify
(
m
[
x
]
==
'
\
0
'
,
"anonymously mmap'ed contents should be zero"
)
self
.
assertEqual
(
m
[
x
],
'
\
0
'
,
"anonymously mmap'ed contents should be zero"
)
for
x
in
xrange
(
PAGESIZE
):
for
x
in
xrange
(
PAGESIZE
):
m
[
x
]
=
ch
=
chr
(
x
&
255
)
m
[
x
]
=
ch
=
chr
(
x
&
255
)
vereq
(
m
[
x
],
ch
)
self
.
assertEqual
(
m
[
x
],
ch
)
def
test_main
():
run_unittest
(
MmapTests
)
test_both
()
if
__name__
==
'__main__'
:
test_anon
()
test_main
()
print
' Test passed'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment