Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

email.message_from_bytes not working on BytesIO() object #89033

Closed
cd311 mannequin opened this issue Aug 9, 2021 · 2 comments
Closed

email.message_from_bytes not working on BytesIO() object #89033

cd311 mannequin opened this issue Aug 9, 2021 · 2 comments
Labels
3.8 only security fixes topic-email type-bug An unexpected behavior, bug, or error

Comments

@cd311
Copy link
Mannequin

cd311 mannequin commented Aug 9, 2021

BPO 44870
Nosy @warsaw, @ericvsmith, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-08-09.13:11:33.727>
created_at = <Date 2021-08-09.11:49:52.449>
labels = ['invalid', 'type-bug', '3.8', 'expert-email']
title = 'email.message_from_bytes not working on BytesIO() object'
updated_at = <Date 2021-08-09.13:11:33.725>
user = 'https://bugs.python.org/cd311'

bugs.python.org fields:

activity = <Date 2021-08-09.13:11:33.725>
actor = 'eric.smith'
assignee = 'none'
closed = True
closed_date = <Date 2021-08-09.13:11:33.727>
closer = 'eric.smith'
components = ['email']
creation = <Date 2021-08-09.11:49:52.449>
creator = 'cd311'
dependencies = []
files = []
hgrepos = []
issue_num = 44870
keywords = []
message_count = 2.0
messages = ['399259', '399262']
nosy_count = 4.0
nosy_names = ['barry', 'eric.smith', 'r.david.murray', 'cd311']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44870'
versions = ['Python 3.8']

@cd311
Copy link
Mannequin Author

cd311 mannequin commented Aug 9, 2021

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

@cd311 cd311 mannequin added 3.8 only security fixes topic-email type-bug An unexpected behavior, bug, or error labels Aug 9, 2021
@ericvsmith
Copy link
Member

Use filecontent.getvalue(): https://docs.python.org/3/library/io.html#io.BytesIO.getvalue

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant