AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'?
pjz opened this issue · 2 comments
pjz commented
I'm working on a CLI for editing ebook metadata (called ebmeta, available on pypi if you care), and I found this:
Traceback (most recent call last):
File "/home/pj/.local/pipx/venvs/ebmeta/lib/python3.10/site-packages/ebooklib/utils.py", line 35, in parse_string
tree = etree.parse(io.BytesIO(s.encode('utf-8')))
AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'?
It was in a long logfile and among a bunch of other errors, but it looks fairly easy to fix.
pjz commented
This was me misunderstanding the way the code works. Malformed XML threw this as part of the error due to the re-tried call in the except: block. Maybe parse_string ()
starting at line 35 could be rewritten like:
if isinstance(s, str):
s = s.encode('utf-8')
return etree.parse(io.BytesIO(s) , parser=parser)
to avoid confusing nested exceptions?
c1924959470 commented
您好!您的来信我已接受,我会尽快回复您。