MCPcopy Index your code
hub / github.com/python/cpython / test_offset

Method test_offset

Lib/test/test_mmap.py:580–631  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

578 access=mmap.ACCESS_READ)
579
580 def test_offset (self):
581 f = open (TESTFN, 'w+b')
582
583 try: # unlink TESTFN no matter what
584 halfsize = mmap.ALLOCATIONGRANULARITY
585 m = self.make_mmap_file (f, halfsize)
586 m.close ()
587 f.close ()
588
589 mapsize = halfsize * 2
590 # Try invalid offset
591 f = open(TESTFN, "r+b")
592 for offset in [-2, -1, None]:
593 try:
594 m = mmap.mmap(f.fileno(), mapsize, offset=offset)
595 self.assertEqual(0, 1)
596 except (ValueError, TypeError, OverflowError):
597 pass
598 else:
599 self.assertEqual(0, 0)
600 f.close()
601
602 # Try valid offset, hopefully 8192 works on all OSes
603 f = open(TESTFN, "r+b")
604 m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
605 self.assertEqual(m[0:3], b'foo')
606 f.close()
607
608 if hasattr(m, 'resize'):
609 # Try resizing map
610 m.resize(512)
611 self.assertEqual(len(m), 512)
612 # Check that we can no longer seek beyond the new size.
613 self.assertRaises(ValueError, m.seek, 513, 0)
614 # Check that the content is not changed
615 self.assertEqual(m[0:3], b'foo')
616
617 # Check that the underlying file is truncated too
618 f = open(TESTFN, 'rb')
619 f.seek(0, 2)
620 self.assertEqual(f.tell(), halfsize + 512)
621 f.close()
622 self.assertEqual(m.size(), halfsize + 512)
623
624 m.close()
625
626 finally:
627 f.close()
628 try:
629 os.unlink(TESTFN)
630 except OSError:
631 pass
632
633 def test_subclass(self):
634 class anon_mmap(mmap.mmap):

Callers

nothing calls this directly

Calls 11

make_mmap_fileMethod · 0.95
openFunction · 0.50
closeMethod · 0.45
filenoMethod · 0.45
assertEqualMethod · 0.45
resizeMethod · 0.45
assertRaisesMethod · 0.45
seekMethod · 0.45
tellMethod · 0.45
sizeMethod · 0.45
unlinkMethod · 0.45

Tested by

no test coverage detected