Repository navigation
Look at the charset of the content type of the handle to determine the encoding for io.TextIoWrapper - #1429
Conversation
… some missing docstrings in other tests and close handles everywhere
…as encoding. Otherwise fall back to the default behaviour.
Codecov Report
@@ Coverage Diff @@
## master #1429 +/- ##
==========================================
- Coverage 84.79% 84.78% -0.01%
==========================================
Files 320 320
Lines 49051 49056 +5
==========================================
+ Hits 41591 41594 +3
- Misses 7460 7462 +2
Continue to review full report at Codecov.
|
…ed during testing?)
|
Note: The AppVeyor tests pass, but the file where I added an extra test, And apologies for pushing from the master branch. Next time I'll make a proper branch. |
| """Treat a binary (bytes) handle like a text (unicode) handle.""" | ||
| wrapped = io.TextIOWrapper(io.BufferedReader(handle)) | ||
| try: | ||
| encoding = handle.headers.get_content_charset() |
There was a problem hiding this comment.
I would add a comment here that you are checking the HTML headers which would be present if this was a network handle. Without context I find this is hard to understand.
|
This looks good - the test coverage has dropped ever so slightly because you've added code which does not get tested on TravisCI/AppVeyor where we deliberately do not run the online tests. |
|
Looks good in local testing on the mac, here with the old code your new test fails in a non-UTF8 locale like Then updating to your fix, the new test passes :) I wonder if for this test method we should explicitly temporarily change the locale to |
|
I did wondering if a better fix would be to add an optional encoding argument to |
|
I have also thought about temporarily changing the locale inside the test to make more explicit what we are testing, but I wasn't sure of a proper way to do this. Changing the name of |
|
Merged, thank you. If you can find a good what to cleanly change the locale for this one test, that would be worth adding as a new pull request. |
* If the handle has a headers attribute, then use it's content charset as encoding. Otherwise fall back to the default behaviour. * Add test for correct handling of encodings in Entrez.efetch. * Also add some missing docstrings in other tests, and close handles everywhere. This is a squashed commit from from pull request biopython#1429 to address biopython#1402 where on non-UTF8 locale machines under Python 3 (typically Windows machines), we can run into problems with characters in data returned from the NCBI (or elsewhere) using an incompatible encoding.
This pull request addresses issue #1402.
I think the root cause is as follows:
Entrez.efetchreturns under Python 3 is aio.TextIOWrapperobject.io.TextIOWrapperhas an optional argumentencoding. It defaults tolocale.getpreferredencoding(False).utf-8(we presume).locale.getpreferredencoding(False)of the user is not equal toutf-8, we getUnicodeDecodeErrors.In the case that the record returned by NCBI is in XML format, then we can determine the encoding from the first line of the XML. But how do we determine the encoding of a record returned in text format?
Some digging in the code revealed that, in the function
_binary_to_string_handle(handle), thehandleis ahttp.client.HTTPResponseobject. This has aheadersattribute which is aHTTPMessageobject. In our case, it has the following Content-Type:('Content-Type', 'text/plain; charset=UTF-8')So, this pull request looks at the charset of the content type of the handle to determine the encoding. If no
headersattribute is found, it falls back to the default behaviour and useslocale.getpreferredencoding(False).I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.
I am happy to be thanked by name in the
NEWS.rstandCONTRIB.rstfiles.I have read the
CONTRIBUTING.rstfile and understand that AppVeyor andTravisCI will be used to confirm the Biopython unit tests and
flake8stylechecks pass with these changes.