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

Method test_seek_tell

Lib/test/test_zipfile/test_core.py:2454–2492  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2452 self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz'])
2453
2454 def test_seek_tell(self):
2455 # Test seek functionality
2456 txt = b"Where's Bruce?"
2457 bloc = txt.find(b"Bruce")
2458 # Check seek on a file
2459 with zipfile.ZipFile(TESTFN, "w") as zipf:
2460 zipf.writestr("foo.txt", txt)
2461 with zipfile.ZipFile(TESTFN, "r") as zipf:
2462 with zipf.open("foo.txt", "r") as fp:
2463 fp.seek(bloc, os.SEEK_SET)
2464 self.assertEqual(fp.tell(), bloc)
2465 fp.seek(-bloc, os.SEEK_CUR)
2466 self.assertEqual(fp.tell(), 0)
2467 fp.seek(bloc, os.SEEK_CUR)
2468 self.assertEqual(fp.tell(), bloc)
2469 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2470 self.assertEqual(fp.tell(), bloc + 5)
2471 fp.seek(0, os.SEEK_END)
2472 self.assertEqual(fp.tell(), len(txt))
2473 fp.seek(0, os.SEEK_SET)
2474 self.assertEqual(fp.tell(), 0)
2475 # Check seek on memory file
2476 data = io.BytesIO()
2477 with zipfile.ZipFile(data, mode="w") as zipf:
2478 zipf.writestr("foo.txt", txt)
2479 with zipfile.ZipFile(data, mode="r") as zipf:
2480 with zipf.open("foo.txt", "r") as fp:
2481 fp.seek(bloc, os.SEEK_SET)
2482 self.assertEqual(fp.tell(), bloc)
2483 fp.seek(-bloc, os.SEEK_CUR)
2484 self.assertEqual(fp.tell(), 0)
2485 fp.seek(bloc, os.SEEK_CUR)
2486 self.assertEqual(fp.tell(), bloc)
2487 self.assertEqual(fp.read(5), txt[bloc:bloc+5])
2488 self.assertEqual(fp.tell(), bloc + 5)
2489 fp.seek(0, os.SEEK_END)
2490 self.assertEqual(fp.tell(), len(txt))
2491 fp.seek(0, os.SEEK_SET)
2492 self.assertEqual(fp.tell(), 0)
2493
2494 def test_read_after_seek(self):
2495 # Issue 102956: Make sure seek(x, os.SEEK_CUR) doesn't break read()

Callers

nothing calls this directly

Calls 7

writestrMethod · 0.80
findMethod · 0.45
openMethod · 0.45
seekMethod · 0.45
assertEqualMethod · 0.45
tellMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected