Python: test on different writable streams #98
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So far, the generic
test_read_write_roundtrip
test and some specific write tests for EOF errors were only testing on the memory-backedBytesIO
stream. This is fine in most cases, but it turns out that sometimes it's necessary to test on specific file streams instead, because they have slightly different behavior in certain cases, as evidenced by these issues:Neither of these issues can be reproduced with
BytesIO
, but they are easy to reproduce with the changes in this PR.This PR replaces one
test_read_write_roundtrip
test with several tests, each using a different stream. In Python 3, the built-inopen()
and theio.open()
functions are equal (at least in CPython, they even both refer to the same function object, i.e.(open is io.open) == True
, which I also use in the test code), whereas in Python 2,open()
andio.open()
are different and they also return streams with slightly different API and behavior of the provided methods.So these are the new roundtrip test methods in Python 3:
test_read_write_roundtrip__InMemoryStream
test_read_write_roundtrip__TemporaryFile_io_open_rdwr
test_read_write_roundtrip__TemporaryFile_io_open_rdwr_nobuf
test_read_write_roundtrip__TemporaryFile_io_open_wronly
test_read_write_roundtrip__TemporaryFile_io_open_wronly_nobuf
and Python 2 has the same methods plus these:
test_read_write_roundtrip__TemporaryFile_builtin_open_rdwr
test_read_write_roundtrip__TemporaryFile_builtin_open_rdwr_nobuf
test_read_write_roundtrip__TemporaryFile_builtin_open_wronly
test_read_write_roundtrip__TemporaryFile_builtin_open_wronly_nobuf
InMemoryStream
andTemporaryFile
refer to the names of helper classes inspec/python/extra/helpers.py
.InMemoryStream
is a wrapper forio.BytesIO
,TemporaryFile
creates (usingtempfile.mkstemp()
) and manages a temporary file.There is also a helper class
Pipe
for wrapping a pipe provided by the OS, but it's currently not used. Current implementation of serialization relies on seeking quite a bit - even substreams require seeking support, EOF checks rely on seeking (even though we could skip them in case seeking doesn't work), etc. Therefore, the Python runtime raisesValueError("writing to non-seekable streams is not supported")
when there's an attempt to write to a non-seekable stream. However, testing on pipes would make sense for parsing, where the only features requiring seeking should beinstances
andconsume: false
. This is left as a future improvement.