-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix byte mode detection on BgzfWriter
#4724
Conversation
3a87ede
to
9369479
Compare
In commit c4a47ff, there was an attempt to detect handles which are not in binary mode. However, there's a core problem here in that most handles open in write mode *cannot* be read from. For example: ```python >>> handle = open('/tmp/demo.txt', 'wb') >>> handle.read(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> io.UnsupportedOperation: read ``` This means that one cannot pass a plain writeable file object to `BgzfWriter`: ```python >>> Bio.__version__ '1.83' >>> Bio.bgzf Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'Bio' has no attribute 'bgzf' >>> from Bio import bgzf >>> with open('/tmp/demo.txt', 'wb') as handle: ... bgzf.BgzfWriter(fileobj=handle) ... if fileobj.read(0) != b"": ^^^^^^^^^^^^^^^ io.UnsupportedOperation: read ``` If eager detection of the handle's mode is desired, one can check `handle.mode` as was done before -- I opt here to just hardcode handling of `io.StringIO`. The simplest option in my opinion is to just rely on ducktyping -- the `_write_block()` method will try to write the data -- either the handle accepts `bytes` and all is well, or it fails.
9369479
to
a543ca5
Compare
Oh my, we missed that on #4270 - and therefore we're missing something on the test suite too. |
Do you agree to dual licence this and any previous contributions under both the Biopython License Agreement AND the BSD 3-Clause License. Also do you want to be named in the NEWS file etc? |
Yes, I agree to dual-license this work, and sure -- feel free to mention me in NEWS and such (I should already be in CONTRIB.rst!). Thanks, @peterjc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge it in the next few days (leaving you a chance to update the NEWS file to add your name if you want). Thanks!
Thanks, @peterjc -- just added! |
In commit c4a47ff, there was an
attempt to detect handles which are not in binary mode. However, there's
a core problem here in that most handles open in write mode cannot be
read from. For example:
This means that one cannot pass a plain writeable file object to
BgzfWriter
:If eager detection of the handle's mode is desired, one can check
handle.mode
as was done before -- I opt here to just hardcode handlingof
io.StringIO
. The simplest option in my opinion is to just rely onducktyping -- the
_write_block()
method will try to write the data --either the handle accepts
bytes
and all is well, or it fails.