message_from_string return msg with wrong encode
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Bug report

The text contain non ascii chars. The content of text/plain should show as above. While use email.message_from_string to parse the mime, message.get_payload(decode=True) decode "text/plain" part return wrong encode message.
Debug the code, found here https://github.com/python/cpython/blob/3.10/Lib/email/message.py#L278, get_payload return payload.encode('raw-unicode-escape'), but when I use message.get_charsets() it return utf-8, it doesn't match the encode charset. So the result is wrong. The final result is below, the charset is wrong, then I can't get the correct message.

Your environment
Python 3.9.10, macOS Catalina, version 10.15.5
Test code is below.
Runing command: python3 t.py TextBased.eml
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import sys
import email
def get_all_block(message, block_type = "text/plain"):
content_type = message.get_content_type()
main_type = message.get_content_maintype()
if main_type == "multipart":
if message.is_multipart():
block = None
for part in message.get_payload():
result = get_all_block(part, block_type)
if result:
if block is None:
block = result
else:
block += result
return block
else:
return None
elif content_type == block_type:
result = message.get_payload(decode=True)
if result is not None:
charsets = message.get_charsets()
print('charsets', charsets, result)
return result
else:
return None
if __name__ == '__main__':
fname = sys.argv[1]
fp = open(fname, 'rb')
mime = fp.read().decode('utf-8', errors='ignore')
message = email.message_from_string(mime)
text = get_all_block(message, "text/plain")
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece por Lib/email/message.py, na região da implementação de get_payload vinculada no relatório, e depois reproduza o comportamento usando o script fornecido e o anexo TextBased.txt. Compare get_payload(decode=True) com o charset declarado pela parte em get_charsets(). A tarefa estará concluída quando o payload text/plain for retornado com uma codificação consistente com seu charset declarado e for adicionada cobertura de regressão nos testes relevantes de email.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- backend
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 45/100