Back to index

python3.2  3.2.2
Public Member Functions
test.test_mmap.MmapTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def test_basic
def test_access_parameter
def test_bad_file_desc
def test_tougher_find
def test_find_end
def test_rfind
def test_double_close
def test_entire_file
def test_length_0_offset
def test_length_0_large_offset
def test_move
def test_anonymous
def test_extended_getslice
def test_extended_set_del_slice
def make_mmap_file
def test_offset
def test_subclass
def test_prot_readonly
def test_error
def test_io_methods
def test_non_ascii_byte
def test_tagname
def test_crasher_on_windows
def test_invalid_descriptor
def test_context_manager
def test_context_manager_exception

Detailed Description

Definition at line 15 of file test_mmap.py.


Member Function Documentation

def test.test_mmap.MmapTests.make_mmap_file (   self,
  f,
  halfsize 
)

Definition at line 454 of file test_mmap.py.

00454 
00455     def make_mmap_file (self, f, halfsize):
00456         # Write 2 pages worth of data to the file
00457         f.write (b'\0' * halfsize)
00458         f.write (b'foo')
00459         f.write (b'\0' * (halfsize - 3))
00460         f.flush ()
00461         return mmap.mmap (f.fileno(), 0)

Here is the caller graph for this function:

Definition at line 17 of file test_mmap.py.

00017 
00018     def setUp(self):
00019         if os.path.exists(TESTFN):
00020             os.unlink(TESTFN)

Here is the caller graph for this function:

Definition at line 21 of file test_mmap.py.

00021 
00022     def tearDown(self):
00023         try:
00024             os.unlink(TESTFN)
00025         except OSError:
00026             pass

Here is the caller graph for this function:

Definition at line 121 of file test_mmap.py.

00121 
00122     def test_access_parameter(self):
00123         # Test for "access" keyword parameter
00124         mapsize = 10
00125         with open(TESTFN, "wb") as fp:
00126             fp.write(b"a"*mapsize)
00127         with open(TESTFN, "rb") as f:
00128             m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
00129             self.assertEqual(m[:], b'a'*mapsize, "Readonly memory map data incorrect.")
00130 
00131             # Ensuring that readonly mmap can't be slice assigned
00132             try:
00133                 m[:] = b'b'*mapsize
00134             except TypeError:
00135                 pass
00136             else:
00137                 self.fail("Able to write to readonly memory map")
00138 
00139             # Ensuring that readonly mmap can't be item assigned
00140             try:
00141                 m[0] = b'b'
00142             except TypeError:
00143                 pass
00144             else:
00145                 self.fail("Able to write to readonly memory map")
00146 
00147             # Ensuring that readonly mmap can't be write() to
00148             try:
00149                 m.seek(0,0)
00150                 m.write(b'abc')
00151             except TypeError:
00152                 pass
00153             else:
00154                 self.fail("Able to write to readonly memory map")
00155 
00156             # Ensuring that readonly mmap can't be write_byte() to
00157             try:
00158                 m.seek(0,0)
00159                 m.write_byte(b'd')
00160             except TypeError:
00161                 pass
00162             else:
00163                 self.fail("Able to write to readonly memory map")
00164 
00165             # Ensuring that readonly mmap can't be resized
00166             try:
00167                 m.resize(2*mapsize)
00168             except SystemError:   # resize is not universally supported
00169                 pass
00170             except TypeError:
00171                 pass
00172             else:
00173                 self.fail("Able to resize readonly memory map")
00174             with open(TESTFN, "rb") as fp:
00175                 self.assertEqual(fp.read(), b'a'*mapsize,
00176                                  "Readonly memory map data file was modified")
00177 
00178         # Opening mmap with size too big
00179         with open(TESTFN, "r+b") as f:
00180             try:
00181                 m = mmap.mmap(f.fileno(), mapsize+1)
00182             except ValueError:
00183                 # we do not expect a ValueError on Windows
00184                 # CAUTION:  This also changes the size of the file on disk, and
00185                 # later tests assume that the length hasn't changed.  We need to
00186                 # repair that.
00187                 if sys.platform.startswith('win'):
00188                     self.fail("Opening mmap with size+1 should work on Windows.")
00189             else:
00190                 # we expect a ValueError on Unix, but not on Windows
00191                 if not sys.platform.startswith('win'):
00192                     self.fail("Opening mmap with size+1 should raise ValueError.")
00193                 m.close()
00194             if sys.platform.startswith('win'):
00195                 # Repair damage from the resizing test.
00196                 with open(TESTFN, 'r+b') as f:
00197                     f.truncate(mapsize)
00198 
00199         # Opening mmap with access=ACCESS_WRITE
00200         with open(TESTFN, "r+b") as f:
00201             m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
00202             # Modifying write-through memory map
00203             m[:] = b'c'*mapsize
00204             self.assertEqual(m[:], b'c'*mapsize,
00205                    "Write-through memory map memory not updated properly.")
00206             m.flush()
00207             m.close()
00208         with open(TESTFN, 'rb') as f:
00209             stuff = f.read()
00210         self.assertEqual(stuff, b'c'*mapsize,
00211                "Write-through memory map data file not updated properly.")
00212 
00213         # Opening mmap with access=ACCESS_COPY
00214         with open(TESTFN, "r+b") as f:
00215             m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
00216             # Modifying copy-on-write memory map
00217             m[:] = b'd'*mapsize
00218             self.assertEqual(m[:], b'd' * mapsize,
00219                              "Copy-on-write memory map data not written correctly.")
00220             m.flush()
00221             with open(TESTFN, "rb") as fp:
00222                 self.assertEqual(fp.read(), b'c'*mapsize,
00223                                  "Copy-on-write test data file should not be modified.")
00224             # Ensuring copy-on-write maps cannot be resized
00225             self.assertRaises(TypeError, m.resize, 2*mapsize)
00226             m.close()
00227 
00228         # Ensuring invalid access parameter raises exception
00229         with open(TESTFN, "r+b") as f:
00230             self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
00231 
00232         if os.name == "posix":
00233             # Try incompatible flags, prot and access parameters.
00234             with open(TESTFN, "r+b") as f:
00235                 self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
00236                                   flags=mmap.MAP_PRIVATE,
00237                                   prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
00238 
00239             # Try writing with PROT_EXEC and without PROT_WRITE
00240             prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
00241             with open(TESTFN, "r+b") as f:
00242                 m = mmap.mmap(f.fileno(), mapsize, prot=prot)
00243                 self.assertRaises(TypeError, m.write, b"abcdef")
00244                 self.assertRaises(TypeError, m.write_byte, 0)
00245                 m.close()

Here is the call graph for this function:

Definition at line 408 of file test_mmap.py.

00408 
00409     def test_anonymous(self):
00410         # anonymous mmap.mmap(-1, PAGE)
00411         m = mmap.mmap(-1, PAGESIZE)
00412         for x in range(PAGESIZE):
00413             self.assertEqual(m[x], 0,
00414                              "anonymously mmap'ed contents should be zero")
00415 
00416         for x in range(PAGESIZE):
00417             b = x & 0xff
00418             m[x] = b
00419             self.assertEqual(m[x], b)

Here is the call graph for this function:

Definition at line 246 of file test_mmap.py.

00246 
00247     def test_bad_file_desc(self):
00248         # Try opening a bad file descriptor...
00249         self.assertRaises(mmap.error, mmap.mmap, -2, 4096)

Here is the call graph for this function:

Definition at line 27 of file test_mmap.py.

00027 
00028     def test_basic(self):
00029         # Test mmap module on Unix systems and Windows
00030 
00031         # Create a file to be mmap'ed.
00032         f = open(TESTFN, 'bw+')
00033         try:
00034             # Write 2 pages worth of data to the file
00035             f.write(b'\0'* PAGESIZE)
00036             f.write(b'foo')
00037             f.write(b'\0'* (PAGESIZE-3) )
00038             f.flush()
00039             m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
00040         finally:
00041             f.close()
00042 
00043         # Simple sanity checks
00044 
00045         tp = str(type(m))  # SF bug 128713:  segfaulted on Linux
00046         self.assertEqual(m.find(b'foo'), PAGESIZE)
00047 
00048         self.assertEqual(len(m), 2*PAGESIZE)
00049 
00050         self.assertEqual(m[0], 0)
00051         self.assertEqual(m[0:3], b'\0\0\0')
00052 
00053         # Shouldn't crash on boundary (Issue #5292)
00054         self.assertRaises(IndexError, m.__getitem__, len(m))
00055         self.assertRaises(IndexError, m.__setitem__, len(m), b'\0')
00056 
00057         # Modify the file's content
00058         m[0] = b'3'[0]
00059         m[PAGESIZE +3: PAGESIZE +3+3] = b'bar'
00060 
00061         # Check that the modification worked
00062         self.assertEqual(m[0], b'3'[0])
00063         self.assertEqual(m[0:3], b'3\0\0')
00064         self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], b'\0foobar\0')
00065 
00066         m.flush()
00067 
00068         # Test doing a regular expression match in an mmap'ed file
00069         match = re.search(b'[A-Za-z]+', m)
00070         if match is None:
00071             self.fail('regex match on mmap failed!')
00072         else:
00073             start, end = match.span(0)
00074             length = end - start
00075 
00076             self.assertEqual(start, PAGESIZE)
00077             self.assertEqual(end, PAGESIZE + 6)
00078 
00079         # test seeking around (try to overflow the seek implementation)
00080         m.seek(0,0)
00081         self.assertEqual(m.tell(), 0)
00082         m.seek(42,1)
00083         self.assertEqual(m.tell(), 42)
00084         m.seek(0,2)
00085         self.assertEqual(m.tell(), len(m))
00086 
00087         # Try to seek to negative position...
00088         self.assertRaises(ValueError, m.seek, -1)
00089 
00090         # Try to seek beyond end of mmap...
00091         self.assertRaises(ValueError, m.seek, 1, 2)
00092 
00093         # Try to seek to negative position...
00094         self.assertRaises(ValueError, m.seek, -len(m)-1, 2)
00095 
00096         # Try resizing map
00097         try:
00098             m.resize(512)
00099         except SystemError:
00100             # resize() not supported
00101             # No messages are printed, since the output of this test suite
00102             # would then be different across platforms.
00103             pass
00104         else:
00105             # resize() is supported
00106             self.assertEqual(len(m), 512)
00107             # Check that we can no longer seek beyond the new size.
00108             self.assertRaises(ValueError, m.seek, 513, 0)
00109 
00110             # Check that the underlying file is truncated too
00111             # (bug #728515)
00112             f = open(TESTFN, 'rb')
00113             try:
00114                 f.seek(0, 2)
00115                 self.assertEqual(f.tell(), 512)
00116             finally:
00117                 f.close()
00118             self.assertEqual(m.size(), 512)
00119 
00120         m.close()

Here is the call graph for this function:

Definition at line 644 of file test_mmap.py.

00644 
00645     def test_context_manager(self):
00646         with mmap.mmap(-1, 10) as m:
00647             self.assertFalse(m.closed)
00648         self.assertTrue(m.closed)

Here is the call graph for this function:

Definition at line 649 of file test_mmap.py.

00649 
00650     def test_context_manager_exception(self):
00651         # Test that the IOError gets passed through
00652         with self.assertRaises(Exception) as exc:
00653             with mmap.mmap(-1, 10) as m:
00654                 raise IOError
00655         self.assertIsInstance(exc.exception, IOError,
00656                               "wrong exception raised in context manager")
00657         self.assertTrue(m.closed, "context manager failed")

Here is the call graph for this function:

Definition at line 608 of file test_mmap.py.

00608 
00609         def test_crasher_on_windows(self):
00610             # Should not crash (Issue 1733986)
00611             m = mmap.mmap(-1, 1000, tagname="foo")
00612             try:
00613                 mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
00614             except:
00615                 pass
00616             m.close()
00617 
00618             # Should not crash (Issue 5385)
00619             with open(TESTFN, "wb") as fp:
00620                 fp.write(b"x"*10)
00621             f = open(TESTFN, "r+b")
00622             m = mmap.mmap(f.fileno(), 0)
00623             f.close()
00624             try:
00625                 m.resize(0) # will raise WindowsError
00626             except:
00627                 pass
00628             try:
00629                 m[:]
00630             except:
00631                 pass
00632             m.close()

Definition at line 304 of file test_mmap.py.

00304 
00305     def test_double_close(self):
00306         # make sure a double close doesn't crash on Solaris (Bug# 665913)
00307         f = open(TESTFN, 'wb+')
00308 
00309         f.write(2**16 * b'a') # Arbitrary character
00310         f.close()
00311 
00312         f = open(TESTFN, 'rb')
00313         mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
00314         mf.close()
00315         mf.close()
00316         f.close()

Definition at line 317 of file test_mmap.py.

00317 
00318     def test_entire_file(self):
00319         # test mapping of entire file by passing 0 for map length
00320         if hasattr(os, "stat"):
00321             f = open(TESTFN, "wb+")
00322 
00323             f.write(2**16 * b'm') # Arbitrary character
00324             f.close()
00325 
00326             f = open(TESTFN, "rb+")
00327             mf = mmap.mmap(f.fileno(), 0)
00328             self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
00329             self.assertEqual(mf.read(2**16), 2**16 * b"m")
00330             mf.close()
00331             f.close()

Here is the call graph for this function:

Definition at line 536 of file test_mmap.py.

00536 
00537     def test_error(self):
00538         self.assertTrue(issubclass(mmap.error, EnvironmentError))
00539         self.assertIn("mmap.error", str(mmap.error))

Here is the call graph for this function:

Definition at line 420 of file test_mmap.py.

00420 
00421     def test_extended_getslice(self):
00422         # Test extended slicing by comparing with list slicing.
00423         s = bytes(reversed(range(256)))
00424         m = mmap.mmap(-1, len(s))
00425         m[:] = s
00426         self.assertEqual(m[:], s)
00427         indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
00428         for start in indices:
00429             for stop in indices:
00430                 # Skip step 0 (invalid)
00431                 for step in indices[1:]:
00432                     self.assertEqual(m[start:stop:step],
00433                                      s[start:stop:step])

Here is the call graph for this function:

Definition at line 434 of file test_mmap.py.

00434 
00435     def test_extended_set_del_slice(self):
00436         # Test extended slicing by comparing with list slicing.
00437         s = bytes(reversed(range(256)))
00438         m = mmap.mmap(-1, len(s))
00439         indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
00440         for start in indices:
00441             for stop in indices:
00442                 # Skip invalid step 0
00443                 for step in indices[1:]:
00444                     m[:] = s
00445                     self.assertEqual(m[:], s)
00446                     L = list(s)
00447                     # Make sure we have a slice of exactly the right length,
00448                     # but with different data.
00449                     data = L[start:stop:step]
00450                     data = bytes(reversed(data))
00451                     L[start:stop:step] = data
00452                     m[start:stop:step] = data
00453                     self.assertEqual(m[:], bytes(L))

Here is the call graph for this function:

Definition at line 268 of file test_mmap.py.

00268 
00269     def test_find_end(self):
00270         # test the new 'end' parameter works as expected
00271         f = open(TESTFN, 'wb+')
00272         data = b'one two ones'
00273         n = len(data)
00274         f.write(data)
00275         f.flush()
00276         m = mmap.mmap(f.fileno(), n)
00277         f.close()
00278 
00279         self.assertEqual(m.find(b'one'), 0)
00280         self.assertEqual(m.find(b'ones'), 8)
00281         self.assertEqual(m.find(b'one', 0, -1), 0)
00282         self.assertEqual(m.find(b'one', 1), 8)
00283         self.assertEqual(m.find(b'one', 1, -1), 8)
00284         self.assertEqual(m.find(b'one', 1, -2), -1)
00285 

Here is the call graph for this function:

Definition at line 633 of file test_mmap.py.

00633 
00634         def test_invalid_descriptor(self):
00635             # socket file descriptors are valid, but out of range
00636             # for _get_osfhandle, causing a crash when validating the
00637             # parameters to _get_osfhandle.
00638             s = socket.socket()
00639             try:
00640                 with self.assertRaises(mmap.error):
00641                     m = mmap.mmap(s.fileno(), 10)
00642             finally:
00643                 s.close()

Here is the call graph for this function:

Definition at line 540 of file test_mmap.py.

00540 
00541     def test_io_methods(self):
00542         data = b"0123456789"
00543         with open(TESTFN, "wb") as fp:
00544             fp.write(b"x"*len(data))
00545         f = open(TESTFN, "r+b")
00546         m = mmap.mmap(f.fileno(), len(data))
00547         f.close()
00548         # Test write_byte()
00549         for i in range(len(data)):
00550             self.assertEqual(m.tell(), i)
00551             m.write_byte(data[i])
00552             self.assertEqual(m.tell(), i+1)
00553         self.assertRaises(ValueError, m.write_byte, b"x"[0])
00554         self.assertEqual(m[:], data)
00555         # Test read_byte()
00556         m.seek(0)
00557         for i in range(len(data)):
00558             self.assertEqual(m.tell(), i)
00559             self.assertEqual(m.read_byte(), data[i])
00560             self.assertEqual(m.tell(), i+1)
00561         self.assertRaises(ValueError, m.read_byte)
00562         # Test read()
00563         m.seek(3)
00564         self.assertEqual(m.read(3), b"345")
00565         self.assertEqual(m.tell(), 6)
00566         # Test write()
00567         m.seek(3)
00568         m.write(b"bar")
00569         self.assertEqual(m.tell(), 6)
00570         self.assertEqual(m[:], b"012bar6789")
00571         m.seek(8)
00572         self.assertRaises(ValueError, m.write, b"bar")

Here is the call graph for this function:

Definition at line 346 of file test_mmap.py.

00346 
00347     def test_length_0_large_offset(self):
00348         # Issue #10959: test mapping of a file by passing 0 for
00349         # map length with a large offset doesn't cause a segfault.
00350         if not hasattr(os, "stat"):
00351             self.skipTest("needs os.stat")
00352 
00353         with open(TESTFN, "wb") as f:
00354             f.write(115699 * b'm') # Arbitrary character
00355 
00356         with open(TESTFN, "w+b") as f:
00357             self.assertRaises(ValueError, mmap.mmap, f.fileno(), 0,
00358                               offset=2147418112)

Here is the call graph for this function:

Definition at line 332 of file test_mmap.py.

00332 
00333     def test_length_0_offset(self):
00334         # Issue #10916: test mapping of remainder of file by passing 0 for
00335         # map length with an offset doesn't cause a segfault.
00336         if not hasattr(os, "stat"):
00337             self.skipTest("needs os.stat")
00338         # NOTE: allocation granularity is currently 65536 under Win64,
00339         # and therefore the minimum offset alignment.
00340         with open(TESTFN, "wb") as f:
00341             f.write((65536 * 2) * b'm') # Arbitrary character
00342 
00343         with open(TESTFN, "rb") as f:
00344             with mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ) as mf:
00345                 self.assertRaises(IndexError, mf.__getitem__, 80000)

Here is the call graph for this function:

Definition at line 359 of file test_mmap.py.

00359 
00360     def test_move(self):
00361         # make move works everywhere (64-bit format problem earlier)
00362         f = open(TESTFN, 'wb+')
00363 
00364         f.write(b"ABCDEabcde") # Arbitrary character
00365         f.flush()
00366 
00367         mf = mmap.mmap(f.fileno(), 10)
00368         mf.move(5, 0, 5)
00369         self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5")
00370         mf.close()
00371         f.close()
00372 
00373         # more excessive test
00374         data = b"0123456789"
00375         for dest in range(len(data)):
00376             for src in range(len(data)):
00377                 for count in range(len(data) - max(dest, src)):
00378                     expected = data[:dest] + data[src:src+count] + data[dest+count:]
00379                     m = mmap.mmap(-1, len(data))
00380                     m[:] = data
00381                     m.move(dest, src, count)
00382                     self.assertEqual(m[:], expected)
00383                     m.close()
00384 
00385         # segfault test (Issue 5387)
00386         m = mmap.mmap(-1, 100)
00387         offsets = [-100, -1, 0, 1, 100]
00388         for source, dest, size in itertools.product(offsets, offsets, offsets):
00389             try:
00390                 m.move(source, dest, size)
00391             except ValueError:
00392                 pass
00393 
00394         offsets = [(-1, -1, -1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1),
00395                    (-1, 0, 0), (0, -1, 0), (0, 0, -1)]
00396         for source, dest, size in offsets:
00397             self.assertRaises(ValueError, m.move, source, dest, size)
00398 
00399         m.close()
00400 
00401         m = mmap.mmap(-1, 1) # single byte
00402         self.assertRaises(ValueError, m.move, 0, 0, 2)
00403         self.assertRaises(ValueError, m.move, 1, 0, 1)
00404         self.assertRaises(ValueError, m.move, 0, 1, 1)
00405         m.move(0, 0, 1)
00406         m.move(0, 0, 0)
00407 

Here is the call graph for this function:

Definition at line 573 of file test_mmap.py.

00573 
00574     def test_non_ascii_byte(self):
00575         for b in (129, 200, 255): # > 128
00576             m = mmap.mmap(-1, 1)
00577             m.write_byte(b)
00578             self.assertEqual(m[0], b)
00579             m.seek(0)
00580             self.assertEqual(m.read_byte(), b)
00581             m.close()

Here is the call graph for this function:

Definition at line 462 of file test_mmap.py.

00462 
00463     def test_offset (self):
00464         f = open (TESTFN, 'w+b')
00465 
00466         try: # unlink TESTFN no matter what
00467             halfsize = mmap.ALLOCATIONGRANULARITY
00468             m = self.make_mmap_file (f, halfsize)
00469             m.close ()
00470             f.close ()
00471 
00472             mapsize = halfsize * 2
00473             # Try invalid offset
00474             f = open(TESTFN, "r+b")
00475             for offset in [-2, -1, None]:
00476                 try:
00477                     m = mmap.mmap(f.fileno(), mapsize, offset=offset)
00478                     self.assertEqual(0, 1)
00479                 except (ValueError, TypeError, OverflowError):
00480                     pass
00481                 else:
00482                     self.assertEqual(0, 0)
00483             f.close()
00484 
00485             # Try valid offset, hopefully 8192 works on all OSes
00486             f = open(TESTFN, "r+b")
00487             m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
00488             self.assertEqual(m[0:3], b'foo')
00489             f.close()
00490 
00491             # Try resizing map
00492             try:
00493                 m.resize(512)
00494             except SystemError:
00495                 pass
00496             else:
00497                 # resize() is supported
00498                 self.assertEqual(len(m), 512)
00499                 # Check that we can no longer seek beyond the new size.
00500                 self.assertRaises(ValueError, m.seek, 513, 0)
00501                 # Check that the content is not changed
00502                 self.assertEqual(m[0:3], b'foo')
00503 
00504                 # Check that the underlying file is truncated too
00505                 f = open(TESTFN, 'rb')
00506                 f.seek(0, 2)
00507                 self.assertEqual(f.tell(), halfsize + 512)
00508                 f.close()
00509                 self.assertEqual(m.size(), halfsize + 512)
00510 
00511             m.close()
00512 
00513         finally:
00514             f.close()
00515             try:
00516                 os.unlink(TESTFN)
00517             except OSError:
00518                 pass

Here is the call graph for this function:

Definition at line 525 of file test_mmap.py.

00525 
00526     def test_prot_readonly(self):
00527         if not hasattr(mmap, 'PROT_READ'):
00528             return
00529         mapsize = 10
00530         with open(TESTFN, "wb") as fp:
00531             fp.write(b"a"*mapsize)
00532         f = open(TESTFN, "rb")
00533         m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
00534         self.assertRaises(TypeError, m.write, "foo")
00535         f.close()

Here is the call graph for this function:

Definition at line 286 of file test_mmap.py.

00286 
00287     def test_rfind(self):
00288         # test the new 'end' parameter works as expected
00289         f = open(TESTFN, 'wb+')
00290         data = b'one two ones'
00291         n = len(data)
00292         f.write(data)
00293         f.flush()
00294         m = mmap.mmap(f.fileno(), n)
00295         f.close()
00296 
00297         self.assertEqual(m.rfind(b'one'), 8)
00298         self.assertEqual(m.rfind(b'one '), 0)
00299         self.assertEqual(m.rfind(b'one', 0, -1), 8)
00300         self.assertEqual(m.rfind(b'one', 0, -2), 0)
00301         self.assertEqual(m.rfind(b'one', 1, -1), 8)
00302         self.assertEqual(m.rfind(b'one', 1, -2), -1)
00303 

Here is the call graph for this function:

Definition at line 519 of file test_mmap.py.

00519 
00520     def test_subclass(self):
00521         class anon_mmap(mmap.mmap):
00522             def __new__(klass, *args, **kwargs):
00523                 return mmap.mmap.__new__(klass, -1, *args, **kwargs)
00524         anon_mmap(PAGESIZE)

Definition at line 583 of file test_mmap.py.

00583 
00584         def test_tagname(self):
00585             data1 = b"0123456789"
00586             data2 = b"abcdefghij"
00587             assert len(data1) == len(data2)
00588 
00589             # Test same tag
00590             m1 = mmap.mmap(-1, len(data1), tagname="foo")
00591             m1[:] = data1
00592             m2 = mmap.mmap(-1, len(data2), tagname="foo")
00593             m2[:] = data2
00594             self.assertEqual(m1[:], data2)
00595             self.assertEqual(m2[:], data2)
00596             m2.close()
00597             m1.close()
00598 
00599             # Test different tag
00600             m1 = mmap.mmap(-1, len(data1), tagname="foo")
00601             m1[:] = data1
00602             m2 = mmap.mmap(-1, len(data2), tagname="boo")
00603             m2[:] = data2
00604             self.assertEqual(m1[:], data1)
00605             self.assertEqual(m2[:], data2)
00606             m2.close()
00607             m1.close()

Here is the call graph for this function:

Definition at line 250 of file test_mmap.py.

00250 
00251     def test_tougher_find(self):
00252         # Do a tougher .find() test.  SF bug 515943 pointed out that, in 2.2,
00253         # searching for data with embedded \0 bytes didn't work.
00254         with open(TESTFN, 'wb+') as f:
00255 
00256             data = b'aabaac\x00deef\x00\x00aa\x00'
00257             n = len(data)
00258             f.write(data)
00259             f.flush()
00260             m = mmap.mmap(f.fileno(), n)
00261 
00262         for start in range(n+1):
00263             for finish in range(start, n+1):
00264                 slice = data[start : finish]
00265                 self.assertEqual(m.find(slice), data.find(slice))
00266                 self.assertEqual(m.find(slice + b'x'), -1)
00267         m.close()

Here is the call graph for this function:


The documentation for this class was generated from the following file: