PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/modified-2.7/test/test_mmap.py

https://bitbucket.org/dac_io/pypy
Python | 715 lines | 586 code | 70 blank | 59 comment | 61 complexity | fc218234638738d183aca9bdd6f68c88 MD5 | raw file
  1. from test.test_support import (TESTFN, run_unittest, import_module, unlink,
  2. requires, _2G, _4G)
  3. import unittest
  4. import os, re, itertools, socket, sys
  5. mmap = import_module('mmap')
  6. PAGESIZE = mmap.PAGESIZE
  7. class MmapTests(unittest.TestCase):
  8. def setUp(self):
  9. if os.path.exists(TESTFN):
  10. os.unlink(TESTFN)
  11. def tearDown(self):
  12. try:
  13. os.unlink(TESTFN)
  14. except OSError:
  15. pass
  16. def test_basic(self):
  17. # Test mmap module on Unix systems and Windows
  18. # Create a file to be mmap'ed.
  19. f = open(TESTFN, 'w+')
  20. try:
  21. # Write 2 pages worth of data to the file
  22. f.write('\0'* PAGESIZE)
  23. f.write('foo')
  24. f.write('\0'* (PAGESIZE-3) )
  25. f.flush()
  26. m = mmap.mmap(f.fileno(), 2 * PAGESIZE)
  27. f.close()
  28. # Simple sanity checks
  29. tp = str(type(m)) # SF bug 128713: segfaulted on Linux
  30. self.assertEqual(m.find('foo'), PAGESIZE)
  31. self.assertEqual(len(m), 2*PAGESIZE)
  32. self.assertEqual(m[0], '\0')
  33. self.assertEqual(m[0:3], '\0\0\0')
  34. # Shouldn't crash on boundary (Issue #5292)
  35. self.assertRaises(IndexError, m.__getitem__, len(m))
  36. self.assertRaises(IndexError, m.__setitem__, len(m), '\0')
  37. # Modify the file's content
  38. m[0] = '3'
  39. m[PAGESIZE +3: PAGESIZE +3+3] = 'bar'
  40. # Check that the modification worked
  41. self.assertEqual(m[0], '3')
  42. self.assertEqual(m[0:3], '3\0\0')
  43. self.assertEqual(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
  44. m.flush()
  45. # Test doing a regular expression match in an mmap'ed file
  46. match = re.search('[A-Za-z]+', m)
  47. if match is None:
  48. self.fail('regex match on mmap failed!')
  49. else:
  50. start, end = match.span(0)
  51. length = end - start
  52. self.assertEqual(start, PAGESIZE)
  53. self.assertEqual(end, PAGESIZE + 6)
  54. # test seeking around (try to overflow the seek implementation)
  55. m.seek(0,0)
  56. self.assertEqual(m.tell(), 0)
  57. m.seek(42,1)
  58. self.assertEqual(m.tell(), 42)
  59. m.seek(0,2)
  60. self.assertEqual(m.tell(), len(m))
  61. # Try to seek to negative position...
  62. self.assertRaises(ValueError, m.seek, -1)
  63. # Try to seek beyond end of mmap...
  64. self.assertRaises(ValueError, m.seek, 1, 2)
  65. # Try to seek to negative position...
  66. self.assertRaises(ValueError, m.seek, -len(m)-1, 2)
  67. # Try resizing map
  68. try:
  69. m.resize(512)
  70. except SystemError:
  71. # resize() not supported
  72. # No messages are printed, since the output of this test suite
  73. # would then be different across platforms.
  74. pass
  75. else:
  76. # resize() is supported
  77. self.assertEqual(len(m), 512)
  78. # Check that we can no longer seek beyond the new size.
  79. self.assertRaises(ValueError, m.seek, 513, 0)
  80. # Check that the underlying file is truncated too
  81. # (bug #728515)
  82. f = open(TESTFN)
  83. f.seek(0, 2)
  84. self.assertEqual(f.tell(), 512)
  85. f.close()
  86. self.assertEqual(m.size(), 512)
  87. m.close()
  88. finally:
  89. try:
  90. f.close()
  91. except OSError:
  92. pass
  93. def test_access_parameter(self):
  94. # Test for "access" keyword parameter
  95. mapsize = 10
  96. with open(TESTFN, "wb") as f:
  97. f.write("a"*mapsize)
  98. f = open(TESTFN, "rb")
  99. m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
  100. self.assertEqual(m[:], 'a'*mapsize, "Readonly memory map data incorrect.")
  101. # Ensuring that readonly mmap can't be slice assigned
  102. try:
  103. m[:] = 'b'*mapsize
  104. except TypeError:
  105. pass
  106. else:
  107. self.fail("Able to write to readonly memory map")
  108. # Ensuring that readonly mmap can't be item assigned
  109. try:
  110. m[0] = 'b'
  111. except TypeError:
  112. pass
  113. else:
  114. self.fail("Able to write to readonly memory map")
  115. # Ensuring that readonly mmap can't be write() to
  116. try:
  117. m.seek(0,0)
  118. m.write('abc')
  119. except TypeError:
  120. pass
  121. else:
  122. self.fail("Able to write to readonly memory map")
  123. # Ensuring that readonly mmap can't be write_byte() to
  124. try:
  125. m.seek(0,0)
  126. m.write_byte('d')
  127. except TypeError:
  128. pass
  129. else:
  130. self.fail("Able to write to readonly memory map")
  131. # Ensuring that readonly mmap can't be resized
  132. try:
  133. m.resize(2*mapsize)
  134. except SystemError: # resize is not universally supported
  135. pass
  136. except TypeError:
  137. pass
  138. else:
  139. self.fail("Able to resize readonly memory map")
  140. f.close()
  141. m.close()
  142. del m, f
  143. with open(TESTFN, "rb") as f:
  144. self.assertEqual(f.read(), 'a'*mapsize,
  145. "Readonly memory map data file was modified")
  146. # Opening mmap with size too big
  147. import sys
  148. f = open(TESTFN, "r+b")
  149. try:
  150. m = mmap.mmap(f.fileno(), mapsize+1)
  151. except ValueError:
  152. # we do not expect a ValueError on Windows
  153. # CAUTION: This also changes the size of the file on disk, and
  154. # later tests assume that the length hasn't changed. We need to
  155. # repair that.
  156. if sys.platform.startswith('win'):
  157. self.fail("Opening mmap with size+1 should work on Windows.")
  158. else:
  159. # we expect a ValueError on Unix, but not on Windows
  160. if not sys.platform.startswith('win'):
  161. self.fail("Opening mmap with size+1 should raise ValueError.")
  162. m.close()
  163. f.close()
  164. if sys.platform.startswith('win'):
  165. # Repair damage from the resizing test.
  166. f = open(TESTFN, 'r+b')
  167. f.truncate(mapsize)
  168. f.close()
  169. # Opening mmap with access=ACCESS_WRITE
  170. f = open(TESTFN, "r+b")
  171. m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
  172. # Modifying write-through memory map
  173. m[:] = 'c'*mapsize
  174. self.assertEqual(m[:], 'c'*mapsize,
  175. "Write-through memory map memory not updated properly.")
  176. m.flush()
  177. m.close()
  178. f.close()
  179. f = open(TESTFN, 'rb')
  180. stuff = f.read()
  181. f.close()
  182. self.assertEqual(stuff, 'c'*mapsize,
  183. "Write-through memory map data file not updated properly.")
  184. # Opening mmap with access=ACCESS_COPY
  185. f = open(TESTFN, "r+b")
  186. m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
  187. # Modifying copy-on-write memory map
  188. m[:] = 'd'*mapsize
  189. self.assertEqual(m[:], 'd' * mapsize,
  190. "Copy-on-write memory map data not written correctly.")
  191. m.flush()
  192. f.close()
  193. with open(TESTFN, "rb") as f:
  194. self.assertEqual(f.read(), 'c'*mapsize,
  195. "Copy-on-write test data file should not be modified.")
  196. # Ensuring copy-on-write maps cannot be resized
  197. self.assertRaises(TypeError, m.resize, 2*mapsize)
  198. m.close()
  199. del m, f
  200. # Ensuring invalid access parameter raises exception
  201. f = open(TESTFN, "r+b")
  202. self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize, access=4)
  203. f.close()
  204. if os.name == "posix":
  205. # Try incompatible flags, prot and access parameters.
  206. f = open(TESTFN, "r+b")
  207. self.assertRaises(ValueError, mmap.mmap, f.fileno(), mapsize,
  208. flags=mmap.MAP_PRIVATE,
  209. prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
  210. f.close()
  211. # Try writing with PROT_EXEC and without PROT_WRITE
  212. prot = mmap.PROT_READ | getattr(mmap, 'PROT_EXEC', 0)
  213. with open(TESTFN, "r+b") as f:
  214. m = mmap.mmap(f.fileno(), mapsize, prot=prot)
  215. self.assertRaises(TypeError, m.write, b"abcdef")
  216. self.assertRaises(TypeError, m.write_byte, 0)
  217. m.close()
  218. def test_bad_file_desc(self):
  219. # Try opening a bad file descriptor...
  220. self.assertRaises(mmap.error, mmap.mmap, -2, 4096)
  221. def test_tougher_find(self):
  222. # Do a tougher .find() test. SF bug 515943 pointed out that, in 2.2,
  223. # searching for data with embedded \0 bytes didn't work.
  224. f = open(TESTFN, 'w+')
  225. data = 'aabaac\x00deef\x00\x00aa\x00'
  226. n = len(data)
  227. f.write(data)
  228. f.flush()
  229. m = mmap.mmap(f.fileno(), n)
  230. f.close()
  231. for start in range(n+1):
  232. for finish in range(start, n+1):
  233. slice = data[start : finish]
  234. self.assertEqual(m.find(slice), data.find(slice))
  235. self.assertEqual(m.find(slice + 'x'), -1)
  236. m.close()
  237. def test_find_end(self):
  238. # test the new 'end' parameter works as expected
  239. f = open(TESTFN, 'w+')
  240. data = 'one two ones'
  241. n = len(data)
  242. f.write(data)
  243. f.flush()
  244. m = mmap.mmap(f.fileno(), n)
  245. f.close()
  246. self.assertEqual(m.find('one'), 0)
  247. self.assertEqual(m.find('ones'), 8)
  248. self.assertEqual(m.find('one', 0, -1), 0)
  249. self.assertEqual(m.find('one', 1), 8)
  250. self.assertEqual(m.find('one', 1, -1), 8)
  251. self.assertEqual(m.find('one', 1, -2), -1)
  252. m.close()
  253. def test_rfind(self):
  254. # test the new 'end' parameter works as expected
  255. f = open(TESTFN, 'w+')
  256. data = 'one two ones'
  257. n = len(data)
  258. f.write(data)
  259. f.flush()
  260. m = mmap.mmap(f.fileno(), n)
  261. f.close()
  262. self.assertEqual(m.rfind('one'), 8)
  263. self.assertEqual(m.rfind('one '), 0)
  264. self.assertEqual(m.rfind('one', 0, -1), 8)
  265. self.assertEqual(m.rfind('one', 0, -2), 0)
  266. self.assertEqual(m.rfind('one', 1, -1), 8)
  267. self.assertEqual(m.rfind('one', 1, -2), -1)
  268. m.close()
  269. def test_double_close(self):
  270. # make sure a double close doesn't crash on Solaris (Bug# 665913)
  271. f = open(TESTFN, 'w+')
  272. f.write(2**16 * 'a') # Arbitrary character
  273. f.close()
  274. f = open(TESTFN)
  275. mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
  276. mf.close()
  277. mf.close()
  278. f.close()
  279. def test_entire_file(self):
  280. # test mapping of entire file by passing 0 for map length
  281. if hasattr(os, "stat"):
  282. f = open(TESTFN, "w+")
  283. f.write(2**16 * 'm') # Arbitrary character
  284. f.close()
  285. f = open(TESTFN, "rb+")
  286. mf = mmap.mmap(f.fileno(), 0)
  287. self.assertEqual(len(mf), 2**16, "Map size should equal file size.")
  288. self.assertEqual(mf.read(2**16), 2**16 * "m")
  289. mf.close()
  290. f.close()
  291. def test_length_0_offset(self):
  292. # Issue #10916: test mapping of remainder of file by passing 0 for
  293. # map length with an offset doesn't cause a segfault.
  294. if not hasattr(os, "stat"):
  295. self.skipTest("needs os.stat")
  296. # NOTE: allocation granularity is currently 65536 under Win64,
  297. # and therefore the minimum offset alignment.
  298. with open(TESTFN, "wb") as f:
  299. f.write((65536 * 2) * b'm') # Arbitrary character
  300. with open(TESTFN, "rb") as f:
  301. mf = mmap.mmap(f.fileno(), 0, offset=65536, access=mmap.ACCESS_READ)
  302. try:
  303. self.assertRaises(IndexError, mf.__getitem__, 80000)
  304. finally:
  305. mf.close()
  306. def test_length_0_large_offset(self):
  307. # Issue #10959: test mapping of a file by passing 0 for
  308. # map length with a large offset doesn't cause a segfault.
  309. if not hasattr(os, "stat"):
  310. self.skipTest("needs os.stat")
  311. with open(TESTFN, "wb") as f:
  312. f.write(115699 * b'm') # Arbitrary character
  313. with open(TESTFN, "w+b") as f:
  314. self.assertRaises(ValueError, mmap.mmap, f.fileno(), 0,
  315. offset=2147418112)
  316. def test_move(self):
  317. # make move works everywhere (64-bit format problem earlier)
  318. f = open(TESTFN, 'w+')
  319. f.write("ABCDEabcde") # Arbitrary character
  320. f.flush()
  321. mf = mmap.mmap(f.fileno(), 10)
  322. mf.move(5, 0, 5)
  323. self.assertEqual(mf[:], "ABCDEABCDE", "Map move should have duplicated front 5")
  324. mf.close()
  325. f.close()
  326. # more excessive test
  327. data = "0123456789"
  328. for dest in range(len(data)):
  329. for src in range(len(data)):
  330. for count in range(len(data) - max(dest, src)):
  331. expected = data[:dest] + data[src:src+count] + data[dest+count:]
  332. m = mmap.mmap(-1, len(data))
  333. m[:] = data
  334. m.move(dest, src, count)
  335. self.assertEqual(m[:], expected)
  336. m.close()
  337. # segfault test (Issue 5387)
  338. m = mmap.mmap(-1, 100)
  339. offsets = [-100, -1, 0, 1, 100]
  340. for source, dest, size in itertools.product(offsets, offsets, offsets):
  341. try:
  342. m.move(source, dest, size)
  343. except ValueError:
  344. pass
  345. offsets = [(-1, -1, -1), (-1, -1, 0), (-1, 0, -1), (0, -1, -1),
  346. (-1, 0, 0), (0, -1, 0), (0, 0, -1)]
  347. for source, dest, size in offsets:
  348. self.assertRaises(ValueError, m.move, source, dest, size)
  349. m.close()
  350. m = mmap.mmap(-1, 1) # single byte
  351. self.assertRaises(ValueError, m.move, 0, 0, 2)
  352. self.assertRaises(ValueError, m.move, 1, 0, 1)
  353. self.assertRaises(ValueError, m.move, 0, 1, 1)
  354. m.move(0, 0, 1)
  355. m.move(0, 0, 0)
  356. def test_anonymous(self):
  357. # anonymous mmap.mmap(-1, PAGE)
  358. m = mmap.mmap(-1, PAGESIZE)
  359. for x in xrange(PAGESIZE):
  360. self.assertEqual(m[x], '\0', "anonymously mmap'ed contents should be zero")
  361. for x in xrange(PAGESIZE):
  362. m[x] = ch = chr(x & 255)
  363. self.assertEqual(m[x], ch)
  364. def test_extended_getslice(self):
  365. # Test extended slicing by comparing with list slicing.
  366. s = "".join(chr(c) for c in reversed(range(256)))
  367. m = mmap.mmap(-1, len(s))
  368. m[:] = s
  369. self.assertEqual(m[:], s)
  370. indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
  371. for start in indices:
  372. for stop in indices:
  373. # Skip step 0 (invalid)
  374. for step in indices[1:]:
  375. self.assertEqual(m[start:stop:step],
  376. s[start:stop:step])
  377. def test_extended_set_del_slice(self):
  378. # Test extended slicing by comparing with list slicing.
  379. s = "".join(chr(c) for c in reversed(range(256)))
  380. m = mmap.mmap(-1, len(s))
  381. indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
  382. for start in indices:
  383. for stop in indices:
  384. # Skip invalid step 0
  385. for step in indices[1:]:
  386. m[:] = s
  387. self.assertEqual(m[:], s)
  388. L = list(s)
  389. # Make sure we have a slice of exactly the right length,
  390. # but with different data.
  391. data = L[start:stop:step]
  392. data = "".join(reversed(data))
  393. L[start:stop:step] = data
  394. m[start:stop:step] = data
  395. self.assertEqual(m[:], "".join(L))
  396. def make_mmap_file (self, f, halfsize):
  397. # Write 2 pages worth of data to the file
  398. f.write ('\0' * halfsize)
  399. f.write ('foo')
  400. f.write ('\0' * (halfsize - 3))
  401. f.flush ()
  402. return mmap.mmap (f.fileno(), 0)
  403. def test_offset (self):
  404. f = open (TESTFN, 'w+b')
  405. try: # unlink TESTFN no matter what
  406. halfsize = mmap.ALLOCATIONGRANULARITY
  407. m = self.make_mmap_file (f, halfsize)
  408. m.close ()
  409. f.close ()
  410. mapsize = halfsize * 2
  411. # Try invalid offset
  412. f = open(TESTFN, "r+b")
  413. for offset in [-2, -1, None]:
  414. try:
  415. m = mmap.mmap(f.fileno(), mapsize, offset=offset)
  416. self.assertEqual(0, 1)
  417. except (ValueError, TypeError, OverflowError):
  418. pass
  419. else:
  420. self.assertEqual(0, 0)
  421. f.close()
  422. # Try valid offset, hopefully 8192 works on all OSes
  423. f = open(TESTFN, "r+b")
  424. m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
  425. self.assertEqual(m[0:3], 'foo')
  426. f.close()
  427. # Try resizing map
  428. try:
  429. m.resize(512)
  430. except SystemError:
  431. pass
  432. else:
  433. # resize() is supported
  434. self.assertEqual(len(m), 512)
  435. # Check that we can no longer seek beyond the new size.
  436. self.assertRaises(ValueError, m.seek, 513, 0)
  437. # Check that the content is not changed
  438. self.assertEqual(m[0:3], 'foo')
  439. # Check that the underlying file is truncated too
  440. f = open(TESTFN)
  441. f.seek(0, 2)
  442. self.assertEqual(f.tell(), halfsize + 512)
  443. f.close()
  444. self.assertEqual(m.size(), halfsize + 512)
  445. m.close()
  446. finally:
  447. f.close()
  448. try:
  449. os.unlink(TESTFN)
  450. except OSError:
  451. pass
  452. def test_subclass(self):
  453. class anon_mmap(mmap.mmap):
  454. def __new__(klass, *args, **kwargs):
  455. return mmap.mmap.__new__(klass, -1, *args, **kwargs)
  456. anon_mmap(PAGESIZE)
  457. def test_prot_readonly(self):
  458. if not hasattr(mmap, 'PROT_READ'):
  459. return
  460. mapsize = 10
  461. with open(TESTFN, "wb") as f:
  462. f.write("a"*mapsize)
  463. f = open(TESTFN, "rb")
  464. m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ)
  465. self.assertRaises(TypeError, m.write, "foo")
  466. f.close()
  467. def test_error(self):
  468. self.assertTrue(issubclass(mmap.error, EnvironmentError))
  469. self.assertIn("mmap.error", str(mmap.error))
  470. def test_io_methods(self):
  471. data = "0123456789"
  472. with open(TESTFN, "wb") as f:
  473. f.write("x"*len(data))
  474. f = open(TESTFN, "r+b")
  475. m = mmap.mmap(f.fileno(), len(data))
  476. f.close()
  477. # Test write_byte()
  478. for i in xrange(len(data)):
  479. self.assertEqual(m.tell(), i)
  480. m.write_byte(data[i])
  481. self.assertEqual(m.tell(), i+1)
  482. self.assertRaises(ValueError, m.write_byte, "x")
  483. self.assertEqual(m[:], data)
  484. # Test read_byte()
  485. m.seek(0)
  486. for i in xrange(len(data)):
  487. self.assertEqual(m.tell(), i)
  488. self.assertEqual(m.read_byte(), data[i])
  489. self.assertEqual(m.tell(), i+1)
  490. self.assertRaises(ValueError, m.read_byte)
  491. # Test read()
  492. m.seek(3)
  493. self.assertEqual(m.read(3), "345")
  494. self.assertEqual(m.tell(), 6)
  495. # Test write()
  496. m.seek(3)
  497. m.write("bar")
  498. self.assertEqual(m.tell(), 6)
  499. self.assertEqual(m[:], "012bar6789")
  500. m.seek(8)
  501. self.assertRaises(ValueError, m.write, "bar")
  502. m.close()
  503. if os.name == 'nt':
  504. def test_tagname(self):
  505. data1 = "0123456789"
  506. data2 = "abcdefghij"
  507. assert len(data1) == len(data2)
  508. # Test same tag
  509. m1 = mmap.mmap(-1, len(data1), tagname="foo")
  510. m1[:] = data1
  511. m2 = mmap.mmap(-1, len(data2), tagname="foo")
  512. m2[:] = data2
  513. self.assertEqual(m1[:], data2)
  514. self.assertEqual(m2[:], data2)
  515. m2.close()
  516. m1.close()
  517. # Test different tag
  518. m1 = mmap.mmap(-1, len(data1), tagname="foo")
  519. m1[:] = data1
  520. m2 = mmap.mmap(-1, len(data2), tagname="boo")
  521. m2[:] = data2
  522. self.assertEqual(m1[:], data1)
  523. self.assertEqual(m2[:], data2)
  524. m2.close()
  525. m1.close()
  526. def test_crasher_on_windows(self):
  527. # Should not crash (Issue 1733986)
  528. m = mmap.mmap(-1, 1000, tagname="foo")
  529. try:
  530. mmap.mmap(-1, 5000, tagname="foo")[:] # same tagname, but larger size
  531. except:
  532. pass
  533. m.close()
  534. # Should not crash (Issue 5385)
  535. with open(TESTFN, "wb") as f:
  536. f.write("x"*10)
  537. f = open(TESTFN, "r+b")
  538. m = mmap.mmap(f.fileno(), 0)
  539. f.close()
  540. try:
  541. m.resize(0) # will raise WindowsError
  542. except:
  543. pass
  544. try:
  545. m[:]
  546. except:
  547. pass
  548. m.close()
  549. def test_invalid_descriptor(self):
  550. # socket file descriptors are valid, but out of range
  551. # for _get_osfhandle, causing a crash when validating the
  552. # parameters to _get_osfhandle.
  553. s = socket.socket()
  554. try:
  555. with self.assertRaises(mmap.error):
  556. m = mmap.mmap(s.fileno(), 10)
  557. finally:
  558. s.close()
  559. class LargeMmapTests(unittest.TestCase):
  560. def setUp(self):
  561. unlink(TESTFN)
  562. def tearDown(self):
  563. unlink(TESTFN)
  564. def _make_test_file(self, num_zeroes, tail):
  565. if sys.platform[:3] == 'win' or sys.platform == 'darwin':
  566. requires('largefile',
  567. 'test requires %s bytes and a long time to run' % str(0x180000000))
  568. f = open(TESTFN, 'w+b')
  569. try:
  570. f.seek(num_zeroes)
  571. f.write(tail)
  572. f.flush()
  573. except (IOError, OverflowError):
  574. f.close()
  575. raise unittest.SkipTest("filesystem does not have largefile support")
  576. return f
  577. def test_large_offset(self):
  578. with self._make_test_file(0x14FFFFFFF, b" ") as f:
  579. m = mmap.mmap(f.fileno(), 0, offset=0x140000000, access=mmap.ACCESS_READ)
  580. try:
  581. self.assertEqual(m[0xFFFFFFF], b" ")
  582. finally:
  583. m.close()
  584. def test_large_filesize(self):
  585. with self._make_test_file(0x17FFFFFFF, b" ") as f:
  586. m = mmap.mmap(f.fileno(), 0x10000, access=mmap.ACCESS_READ)
  587. try:
  588. self.assertEqual(m.size(), 0x180000000)
  589. finally:
  590. m.close()
  591. # Issue 11277: mmap() with large (~4GB) sparse files crashes on OS X.
  592. def _test_around_boundary(self, boundary):
  593. tail = b' DEARdear '
  594. start = boundary - len(tail) // 2
  595. end = start + len(tail)
  596. with self._make_test_file(start, tail) as f:
  597. m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
  598. try:
  599. self.assertEqual(m[start:end], tail)
  600. finally:
  601. m.close()
  602. @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
  603. def test_around_2GB(self):
  604. self._test_around_boundary(_2G)
  605. @unittest.skipUnless(sys.maxsize > _4G, "test cannot run on 32-bit systems")
  606. def test_around_4GB(self):
  607. self._test_around_boundary(_4G)
  608. def test_main():
  609. run_unittest(MmapTests, LargeMmapTests)
  610. if __name__ == '__main__':
  611. test_main()