[New-bugs-announce] [issue44870] email.message_from_bytes not working on BytesIO() object

Christian Degenkolb report at bugs.python.org
Mon Aug 9 07:49:52 EDT 2021


New submission from Christian Degenkolb <christian+python at degenkolb.net>:

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

----------
components: email
messages: 399259
nosy: barry, cd311, r.david.murray
priority: normal
severity: normal
status: open
title: email.message_from_bytes not working on BytesIO() object
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44870>
_______________________________________


More information about the New-bugs-announce mailing list