email: UnboundLocalError parsing a Message-ID whose domain literal starts with whitespace

Abierto
#161,966 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python
Área
backend

Línea de trabajo

Start in email._header_value_parser, at _get_ptext_to_endchars, and reproduce the issue with parse_message_id('<a@[ x]>'). Done means parsing returns a header with a defect instead of raising UnboundLocalError; the issue mentions linked PR gh-161967, so check its status before starting.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Bug report

email._header_value_parser.parse_message_id and parse_message_ids raise UnboundLocalError when the part after @ is a domain literal that starts with a space or tab, such as <a@[ x]>. Because of this, reading Message-ID, References or In-Reply-To from such a message with policy.default raises instead of returning a header with a defect. gh-134152 fixed the same error for an empty literal (<T@[), but a literal that begins with whitespace still reaches the same code.

import email
from email import policy
from email._header_value_parser import parse_message_id

parse_message_id('<a@[ x]>')

msg = email.message_from_string('Message-ID: <a@[ x]>\n\nbody\n', policy=policy.default)
msg['Message-ID']

Actual:

UnboundLocalError: cannot access local variable 'pos' where it is not associated with a value

Expected: no exception. The header should parse with a defect, the same way <a@[x ]> already does (it gives a MessageID with an ObsoleteHeaderDefect, "obsolete id-right in msg-id").

The cause is in _get_ptext_to_endchars: when value starts with whitespace, the first fragment from _wsp_splitter is empty, the for pos in ... loop never runs, and the else clause reads pos before it is assigned.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: textlibs-email-msgid-no-fold-literal-space

Linked PRs
  • gh-161967
Lenguaje dominante
Python
Estrellas
78.2k
Forks
36.6k
Merge medio
19 h 57 min
PR fusionados (30 d)
630

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/cpython

Todos los issues de python/cpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.