donkirkby / live-py-plugin

Live coding in Python with PyCharm, Emacs, Sublime Text, or even a browser
https://donkirkby.github.io/live-py-plugin
MIT License
292 stars 56 forks source link

UTF 8 error #515

Closed donkirkby closed 1 year ago

donkirkby commented 1 year ago

What I did

Align genetic sequence data with BioPython, and tried to trace it. Here's the Python code:

# scratch.py
from Bio import Align

seq1 = 'GATTACA'
seq2 = 'GTCGACG'
aligner = Align.PairwiseAligner()
aligner.match_score = 1
aligner.mismatch_score = -1
aligner.gap_score = -2
alignments = aligner.align(seq1, seq2)
print(alignments[0])
print(alignments[0].score)
print(repr(aligner))

I tried to trace it:

$ space_tracer --traced Bio.Align.PairwiseAligner.align scratch2.py
    def align(self, seqA, seqB, strand="+"):                                 | seqA = 'GATTACA' | seqB = 'GTCGACG' | strand = '+'
        """Return the alignments of two sequences using PairwiseAligner."""  |
        if isinstance(seqA, (Seq, MutableSeq)):                              |
            sA = bytes(seqA)                                                 |
        else:                                                                |
            sA = seqA                                                        | sA = 'GATTACA'
        if strand == "+":                                                    |
            sB = seqB                                                        | sB = 'GTCGACG'
        else:  # strand == "-":                                              |
            sB = reverse_complement(seqB)                                    |
        if isinstance(sB, (Seq, MutableSeq)):                                |
            sB = bytes(sB)                                                   |
        score, paths = _aligners.PairwiseAligner.align(self, sA, sB, strand) | (score, paths) = (-1.0, <Path generator object at 0x7f0005cf1f30>)
        alignments = PairwiseAlignments(seqA, seqB, score, paths)            |
        return alignments                                                    | return <Bio.Align.PairwiseAlignments object at 0x7f00094b5070>
$ space_tracer --traced Bio.Align._aligners scratch2.py
-------------------------------------------------------------------------------------------------------------------------------- |
Traceback (most recent call last):                                                                                               |
  File "scratch2.py", line 2, in <module>                                                                                        |
    from Bio import Align                                                                                                        |
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load                                                              |
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked                                                     |
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked                                                              |
  File "/home/don/git/MiCall/venv_micall/lib/python3.8/site-packages/space_tracer/module_importers.py", line 181, in exec_module |
    return original_loader.exec_module(module)                                                                                   |
  File "/home/don/git/MiCall/venv_micall/lib/python3.8/site-packages/Bio/Align/__init__.py", line 18, in <module>                |
    from Bio.Align import _aligners                                                                                              |
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load                                                              |
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked                                                     |
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked                                                              |
  File "/home/don/git/MiCall/venv_micall/lib/python3.8/site-packages/space_tracer/module_importers.py", line 155, in exec_module |
    source_code = source_file.read()                                                                                             |
  File "/usr/lib/python3.8/codecs.py", line 322, in decode                                                                       |
    (result, consumed) = self._buffer_decode(data, self.errors, final)                                                           |
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 24: invalid continuation byte                               |
-------------------------------------------------------------------------------------------------------------------------------- |
$ 

What happened

The first tracing call worked fine, but when I tried to trace the Bio.Align._aligners module, it failed.

What I wanted to happen

See what's happening in the _aligners module, or give a more understandable error.

Analysis

When I look in my virtual environment, it looks like that module is some kind of compiled C code, not Python. I guess we could use an error like, "Unable to trace Bio.Align._aligners module."

$ ls lib/python3.8/site-packages/Bio/Align
_aligners.c  _aligners.cpython-38-x86_64-linux-gnu.so  AlignInfo.py  Applications  __init__.py  __pycache__  substitution_matrices
$

My environment

Describe the versions of everything you were using: