The email parser class returns the wrong type for the parsed message

Aperta
#101,104 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Eseguire il reproducer fornito e ispezionare l’entry point email.message_from_bytes, insieme alla documentazione collegata di parser e EmailMessage. Determinare se la correzione prevista consiste nel modificare il tipo restituito o nel correggere la documentazione, quindi aggiungere una copertura per il comportamento dimostrato e verificare che l’API documentata e il risultato a runtime concordino.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

topic-email type-bug

Summary

According to the documentation,

The email package provides a standard parser that understands most email document structures, including MIME documents. You can pass the parser a bytes, string or file object, and the parser will return to you the root EmailMessage instance of the object structure.

In fact, the parser returns an object of the legacy Message class. Code which takes the documentation at face value will blow up if it attempts to invoke methods which the newer class has but the older class does not.

Environment

Python 3.11.1 (v3.11.1:a7a450f84a, Dec 6 2022, 15:24:06) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

Repro

Here is a small program which demonstrates the problem.

Code
#!/usr/bin/env python3

import email

message_bytes = (
    b'Content-type: multipart/form-data; boundary=------abracadabra\r\n'
    b'\r\n'
    b'------abracadabra\r\n'
    b'Content-Disposition: form-data; name="f"; filename="f1"\r\n'
    b'Content-Type: application/octet-stream\r\n'
    b'\r\n'
    b'\x01\r\n'
    b'------abracadabra--\r\n'
)

message = email.message_from_bytes(message_bytes)
print(f'type of message is {type(message)}')
print('have get_content()?', hasattr(message, 'get_content'))
Output
% ./email-message-repro.py
type of message is <class 'email.message.Message'>
have get_content()? False
Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.