def test_fileno(tmp_path: Path) -> None: file_path = tmp_path / "file" file_path.write_bytes(b"abcd") with file_path.open("rb") as fin: obj = IOProxy(fin, 1, 3) assert obj.fileno() == fin.fileno()
def test_seek() -> None: original = Mock() proxy = IOProxy(original, 4, 14) assert proxy.tell() == 0 assert proxy.seek(7) == 7 assert proxy.tell() == 7 assert not original.method_calls # did not touch original
def parse( cls, fileobj: BinaryIO, block_read_strategy: Optional[_BlockReadStrategyType] = None, ) -> "XZStream": """Parse one XZ stream from a fileobj. fileobj position should be right at the end of the stream when calling and will be moved right at the start of the stream """ # footer footer_end_pos = fileobj.seek(-12, SEEK_CUR) + 12 footer = fileobj.read(12) check, backward_size = parse_xz_footer(footer) # index block_start = fileobj.seek(-12 - backward_size, SEEK_CUR) index = fileobj.read(backward_size) records = parse_xz_index(index) blocks_len = sum( round_up(unpadded_size) for unpadded_size, _ in records) block_start -= blocks_len blocks = [] for unpadded_size, uncompressed_size in records: block_end = block_start + round_up(unpadded_size) blocks.append( XZBlock( IOProxy(fileobj, block_start, block_end), check, unpadded_size, uncompressed_size, block_read_strategy=block_read_strategy, )) block_start = block_end # header fileobj.seek(-12 - blocks_len - backward_size, SEEK_CUR) header = fileobj.read(12) header_check = parse_xz_header(header) if header_check != check: raise XZError("stream: inconsistent check value") header_start_pos = fileobj.seek(-12, SEEK_CUR) stream_fileobj = IOProxy(fileobj, header_start_pos, footer_end_pos) stream = cls(stream_fileobj, check, block_read_strategy=block_read_strategy) for block in blocks: stream._append(block) return stream
def test_read() -> None: originals: List[IOAbstract] = [ IOProxy(BytesIO(b"abc"), 0, 3), generate_mock(0), # size 0, will be never used IOProxy(BytesIO(b"defghij"), 0, 7), ] combiner = IOCombiner(*originals) # read all originals[0].seek(2) originals[2].seek(4) combiner.seek(0) assert combiner.read() == b"abcdefghij" assert originals[0].tell() == 3 assert originals[2].tell() == 7 combiner.seek(4) assert combiner.read() == b"efghij" assert originals[0].tell() == 3 assert originals[2].tell() == 7 # read partial originals[0].seek(2) originals[2].seek(4) combiner.seek(1) assert combiner.read(6) == b"bcdefg" assert originals[0].tell() == 3 assert originals[2].tell() == 4 assert combiner.read(6) == b"hij" assert originals[0].tell() == 3 assert originals[2].tell() == 7 assert combiner.read(6) == b"" assert originals[0].tell() == 3 assert originals[2].tell() == 7 assert combiner.read(6) == b"" assert originals[0].tell() == 3 assert originals[2].tell() == 7 # with original seek combiner.seek(1) originals[0].seek(2) originals[2].seek(4) assert combiner.read(5) == b"bcdef" assert originals[0].tell() == 3 assert originals[2].tell() == 3 # never used at all assert not cast(Mock, originals[1]).method_calls
def test_truncate() -> None: original = Mock() with IOProxy(original, 4, 14) as proxy: assert proxy.truncate(5) == 5 assert original.method_calls == [call.truncate(9)] original.reset_mock() assert proxy.truncate(20) == 20 assert original.method_calls == [call.truncate(24)]
def test_append() -> None: combiner = IOCombiner[IOAbstract](generate_mock(13), generate_mock(37)) assert len(combiner) == 50 combiner._append( # pylint: disable=protected-access IOProxy(BytesIO(b"abcdefghij"), 0, 10) ) assert len(combiner) == 60 combiner.seek(54) assert combiner.read(4) == b"efgh"
def test_write() -> None: original = BytesIO(b"xxxxabcdefghijyyyyy") with IOProxy(original, 4, 14) as proxy: proxy.seek(10) assert proxy.write(b"uvw") == 3 assert original.getvalue() == b"xxxxabcdefghijuvwyy" assert proxy.write(b"UVWXYZ") == 6 assert original.getvalue() == b"xxxxabcdefghijuvwUVWXYZ"
def _create_fileobj(self) -> XZBlock: self.fileobj.truncate(self._fileobj_blocks_end_pos) return XZBlock( IOProxy( self.fileobj, self._fileobj_blocks_end_pos, self._fileobj_blocks_end_pos, ), self.check, 0, 0, self.preset, self.filters, self.block_read_strategy, )
def test_read() -> None: original = BytesIO(b"xxxxabcdefghijyyyyy") proxy = IOProxy(original, 4, 14) # read all original.seek(2) proxy.seek(0) assert proxy.read() == b"abcdefghij" assert original.tell() == 14 proxy.seek(4) assert proxy.read() == b"efghij" assert original.tell() == 14 # read partial original.seek(2) proxy.seek(6) assert proxy.read(3) == b"ghi" assert original.tell() == 13 assert proxy.read(3) == b"j" assert original.tell() == 14 assert proxy.read(3) == b"" assert original.tell() == 14 assert proxy.read(3) == b"" assert original.tell() == 14 # with original seek original.seek(2) proxy.seek(4) original.seek(1) assert proxy.read() == b"efghij" assert original.tell() == 14