本文整理匯總了Python中_pyio.BytesIO方法的典型用法代碼示例。如果您正苦於以下問題:Python _pyio.BytesIO方法的具體用法?Python _pyio.BytesIO怎麽用?Python _pyio.BytesIO使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類_pyio
的用法示例。
在下文中一共展示了_pyio.BytesIO方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_getbuffer
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_getbuffer(self):
memio = self.ioclass(b"1234567890")
buf = memio.getbuffer()
self.assertEqual(bytes(buf), b"1234567890")
memio.seek(5)
buf = memio.getbuffer()
self.assertEqual(bytes(buf), b"1234567890")
# Trying to change the size of the BytesIO while a buffer is exported
# raises a BufferError.
self.assertRaises(BufferError, memio.write, b'x' * 100)
self.assertRaises(BufferError, memio.truncate)
self.assertRaises(BufferError, memio.close)
self.assertFalse(memio.closed)
# Mutating the buffer updates the BytesIO
buf[3:6] = b"abc"
self.assertEqual(bytes(buf), b"123abc7890")
self.assertEqual(memio.getvalue(), b"123abc7890")
# After the buffer gets released, we can resize and close the BytesIO
# again
del buf
support.gc_collect()
memio.truncate()
memio.close()
self.assertRaises(ValueError, memio.getbuffer)
示例2: test_cancel_chunked_upload
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_cancel_chunked_upload():
chunk_cleanup_queue = FakeQueue()
args = dict(base_args)
args["context"] = StorageContext("nyc", chunk_cleanup_queue, None, None)
swift = FakeSwiftStorage(**args)
uuid, metadata = swift.initiate_chunked_upload()
chunks = [b"this", b"is", b"some", b"chunked", b"data", b""]
offset = 0
for chunk in chunks:
bytes_written, metadata, error = swift.stream_upload_chunk(
uuid, offset, len(chunk), io.BytesIO(chunk), metadata
)
assert error is None
assert len(chunk) == bytes_written
offset += len(chunk)
swift.cancel_chunked_upload(uuid, metadata)
found = chunk_cleanup_queue.get()
assert found is not None
示例3: test_sizeof
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_sizeof(self):
basesize = support.calcobjsize(b'P2PP2P')
check = self.check_sizeof
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
check(io.BytesIO(), basesize )
check(io.BytesIO(b'a'), basesize + 1 + 1 )
check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
示例4: test_sizeof
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_sizeof(self):
basesize = support.calcobjsize('P2n2Pn')
check = self.check_sizeof
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
check(io.BytesIO(), basesize )
check(io.BytesIO(b'a' * 1000), basesize + sys.getsizeof(b'a' * 1000))
# Various tests of copy-on-write behaviour for BytesIO.
示例5: _test_cow_mutation
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def _test_cow_mutation(self, mutation):
# Common code for all BytesIO copy-on-write mutation tests.
imm = b' ' * 1024
old_rc = sys.getrefcount(imm)
memio = self.ioclass(imm)
self.assertEqual(sys.getrefcount(imm), old_rc + 1)
mutation(memio)
self.assertEqual(sys.getrefcount(imm), old_rc)
示例6: test_cow_mutable
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_cow_mutable(self):
# BytesIO should accept only Bytes for copy-on-write sharing, since
# arbitrary buffer-exporting objects like bytearray() aren't guaranteed
# to be immutable.
ba = bytearray(1024)
old_rc = sys.getrefcount(ba)
memio = self.ioclass(ba)
self.assertEqual(sys.getrefcount(ba), old_rc)
示例7: test_sizeof
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_sizeof(self):
basesize = support.calcobjsize('P2nN2Pn')
check = self.check_sizeof
self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
check(io.BytesIO(), basesize )
check(io.BytesIO(b'a'), basesize + 1 + 1 )
check(io.BytesIO(b'a' * 1000), basesize + 1000 + 1 )
示例8: test_stream_read_write
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_stream_read_write():
swift = FakeSwiftStorage(**base_args)
assert not swift.exists("somepath")
swift.stream_write("somepath", io.BytesIO(b"some content here"))
assert swift.exists("somepath")
assert swift.get_content("somepath") == b"some content here"
assert b"".join([c for c in swift.stream_read("somepath")]) == b"some content here"
示例9: test_stream_read_write_invalid_checksum
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_stream_read_write_invalid_checksum():
swift = FakeSwiftStorage(fail_checksum=True, **base_args)
assert not swift.exists("somepath")
with pytest.raises(IOError):
swift.stream_write("somepath", io.BytesIO(b"some content here"))
示例10: test_chunked_upload
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_chunked_upload(chunks, max_chunk_size, read_until_end):
swift = FakeSwiftStorage(**base_args)
uuid, metadata = swift.initiate_chunked_upload()
offset = 0
with patch("storage.swift._MAXIMUM_SEGMENT_SIZE", max_chunk_size):
for chunk in chunks:
chunk_length = len(chunk) if not read_until_end else -1
bytes_written, metadata, error = swift.stream_upload_chunk(
uuid, offset, chunk_length, io.BytesIO(chunk), metadata
)
assert error is None
assert len(chunk) == bytes_written
offset += len(chunk)
swift.complete_chunked_upload(uuid, "somepath", metadata)
assert swift.get_content("somepath") == b"".join(chunks)
# Ensure each of the segments exist.
for segment in metadata["segments"]:
assert swift.exists(segment.path)
# Delete the file and ensure all of its segments were removed.
swift.remove("somepath")
assert not swift.exists("somepath")
for segment in metadata["segments"]:
assert not swift.exists(segment.path)
示例11: test_empty_chunks_queued_for_deletion
# 需要導入模塊: import _pyio [as 別名]
# 或者: from _pyio import BytesIO [as 別名]
def test_empty_chunks_queued_for_deletion():
chunk_cleanup_queue = FakeQueue()
args = dict(base_args)
args["context"] = StorageContext("nyc", chunk_cleanup_queue, None, None)
swift = FakeSwiftStorage(**args)
uuid, metadata = swift.initiate_chunked_upload()
chunks = [b"this", b"", b"is", b"some", b"", b"chunked", b"data", b""]
offset = 0
for chunk in chunks:
length = len(chunk)
if length == 0:
length = 1
bytes_written, metadata, error = swift.stream_upload_chunk(
uuid, offset, length, io.BytesIO(chunk), metadata
)
assert error is None
assert len(chunk) == bytes_written
offset += len(chunk)
swift.complete_chunked_upload(uuid, "somepath", metadata)
assert b"".join(chunks) == swift.get_content("somepath")
# Check the chunk deletion queue and ensure we have the last chunk queued.
found = chunk_cleanup_queue.get()
assert found is not None
found2 = chunk_cleanup_queue.get()
assert found2 is None