This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author cd311
Recipients barry, cd311, r.david.murray
Date 2021-08-09.11:49:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628509792.47.0.257700542933.issue44870@roundup.psfhosted.org>
In-reply-to
Content
Hi, 

the following minimal working example of the problem  

from io import BytesIO
from os import read
import email

fp = BytesIO()

with open('mail.eml', 'rb') as f:
    filecontent = f.read()
    print("type(filecontent)= ", type(filecontent))
    fp.write(filecontent)

mailobj = email.message_from_bytes(fp)

produces the following exception

$ python testparser.py 
type(filecontent)=  <class 'bytes'>
Traceback (most recent call last):
  File "testparser.py", line 16, in <module>
    mailobj = email.message_from_bytes(fp)
  File "/usr/lib/python3.8/email/__init__.py", line 46, in message_from_bytes
    return BytesParser(*args, **kws).parsebytes(s)
  File "/usr/lib/python3.8/email/parser.py", line 122, in parsebytes
    text = text.decode('ASCII', errors='surrogateescape')
AttributeError: '_io.BytesIO' object has no attribute 'decode'


This is a python 3.8.10 on an Ubuntu 20.04 LTS installed from the packages. 

The documentation for message_from_bytes https://docs.python.org/3.10/library/email.parser.html#email.message_from_bytes mentions bytes-like objects which links to https://docs.python.org/3.10/glossary.html#term-bytes-like-object and references object of type byte. 

Shouldn't this work with BytesIO()? A'm I missing something?

with regards
Christian
History
Date User Action Args
2021-08-09 11:49:52cd311setrecipients: + cd311, barry, r.david.murray
2021-08-09 11:49:52cd311setmessageid: <1628509792.47.0.257700542933.issue44870@roundup.psfhosted.org>
2021-08-09 11:49:52cd311linkissue44870 messages
2021-08-09 11:49:52cd311create