Skip to content

test_SeqIO_SeqXML.py UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 #1319

Closed

Description

@peterjc

Spin out from #855 which was specifically for test_NCBIXML.py but has the same root cause.

One of the test XML files contains non-ASCII accented characters (etc):

SeqXML/rna_example.xml:        <description>åÅüöÖ߸䢣$€香肠</description>

Note that this XML file does define an encoding:

<?xml version="1.0" encoding="UTF-8"?>

Testing with Biopython 1.70 with my default locale, everything is fine as a UTF8 encoding is the default. However, under some systems (including the multibuild systems for compiling wheels), you can get a default encoding of ascii.

The failure can be recreated under Python 3 as follows, here on Mac OS X using Python 3.6:

$ LANG=C python3 test_SeqIO_SeqXML.py
test_duplicated_dbxref (__main__.TestDetailedRead)
Read multiple cross references to a single source ... ERROR
test_duplicated_property (__main__.TestDetailedRead)
Read property with multiple values ... ERROR
test_empty_description (__main__.TestDetailedRead)
Check empty description. ... ERROR
test_full_characters_set_read (__main__.TestDetailedRead)
Read full characters set for each type ... ERROR
test_global_species (__main__.TestDetailedRead)
Check global species. ... ERROR
test_local_source_definition (__main__.TestDetailedRead)
Check local source. ... ERROR
test_local_species (__main__.TestDetailedRead)
Check local species. ... ERROR
test_read_minimal_required (__main__.TestDetailedRead)
Check minimal record. ... ERROR
test_special_characters_desc (__main__.TestDetailedRead)
Read special XML characters in description. ... ERROR
test_unicode_characters_desc (__main__.TestDetailedRead)
Test special unicode characters in the description. ... ERROR
test_read_write_dna (__main__.TestReadAndWrite)
Read and write DNA. ... ok
test_read_write_globalSpecies (__main__.TestReadAndWrite)
Read and write global species. ... ok
test_read_write_protein (__main__.TestReadAndWrite)
Read and write protein. ... ok
test_read_write_rna (__main__.TestReadAndWrite)
Read and write RNA. ... ERROR
test_write_species (__main__.TestReadAndWrite)
Test writing species from annotation tags. ... ok
test_for_errors (__main__.TestReadCorruptFiles)
Handling of corrupt files. ... ok
test_check_SeqIO (__main__.TestSimpleRead)
Files readable using parser via SeqIO. ... ERROR

======================================================================
ERROR: test_duplicated_dbxref (__main__.TestDetailedRead)
Read multiple cross references to a single source
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_duplicated_property (__main__.TestDetailedRead)
Read property with multiple values
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_empty_description (__main__.TestDetailedRead)
Check empty description.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_full_characters_set_read (__main__.TestDetailedRead)
Read full characters set for each type
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_global_species (__main__.TestDetailedRead)
Check global species.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_local_source_definition (__main__.TestDetailedRead)
Check local source.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_local_species (__main__.TestDetailedRead)
Check local species.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_read_minimal_required (__main__.TestDetailedRead)
Check minimal record.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_special_characters_desc (__main__.TestDetailedRead)
Read special XML characters in description.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_unicode_characters_desc (__main__.TestDetailedRead)
Test special unicode characters in the description.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 50, in setUp
    self.records[key] = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_read_write_rna (__main__.TestReadAndWrite)
Read and write RNA.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 115, in test_read_write_rna
    read1_records = list(SeqIO.parse(test_files["rna"][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

======================================================================
ERROR: test_check_SeqIO (__main__.TestSimpleRead)
Files readable using parser via SeqIO.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_SeqIO_SeqXML.py", line 40, in test_check_SeqIO
    records = list(SeqIO.parse(test_files[key][0], "seqxml"))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/__init__.py", line 611, in parse
    for r in i:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Bio/SeqIO/SeqXmlIO.py", line 58, in __iter__
    for event, node in self._events:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 226, in __next__
    rc = self.getEvent()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/dom/pulldom.py", line 255, in getEvent
    buf = self.stream.read(self.bufsize)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 797: ordinal not in range(128)

----------------------------------------------------------------------
Ran 17 tests in 0.028s

FAILED (errors=12)

Activity

  1. added a commit that references this issue on Jul 13, 2017
    976db87
  2. peterjc commented on Jul 13, 2017

    @peterjc
    MemberAuthor

    Right now when given a filename Bio.SeqIO for text files, it will open the file using the default encoding (which depends on the locale).

    One simple fix seems to be to class seqxml as a binary file format, which means opening it in bytes mode, and let the XML parser take care of the encoding. That seems to work perfectly.

    Alternatively we might make Bio.SeqIO default to opening text files using encoding='utf-8' but that might have side effects...

  3. added a commit that references this issue on Oct 31, 2017
    b20c220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions