Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#34732 closed Uncategorized (invalid)

UnicodeEncodeError on two tests in Python 3.12

Reported by: Michel Alexandre Salim Owned by: nobody
Component: Uncategorized Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When running the tests inside a virtual environment with Fedora 39's python3-3.12.0~b4-1.fc39.x86_64, there are currently two test failures left with the test_sqlite backend

======================================================================
ERROR: test_safe_mime_multipart (mail.tests.MailTests.test_safe_mime_multipart)
Make sure headers can be set with a different encoding than utf-8 in
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 64, in forbid_multi_line_headers
    val.encode("ascii")                                           
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 2: ordinal not in range(128)
                                                                                                                                    
During handling of the above exception, another exception occurred:
                                                                                                                                    
Traceback (most recent call last):                                                                                                  
  File "/home/michel/src/github/django/django/tests/mail/tests.py", line 504, in test_safe_mime_multipart
    msg.message()["To"],
    ^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 266, in message
    self._set_list_header_if_not_empty(msg, "To", self.to)
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 431, in _set_list_header_if_not_empty
    msg[header] = value
    ~~~^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 188, in __setitem__
    name, val = forbid_multi_line_headers(name, val, self.encoding)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 67, in forbid_multi_line_headers
    val = ", ".join(
          ^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 68, in <genexpr>
    sanitize_address(addr, encoding) for addr in getaddresses((val,))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 101, in sanitize_address
    raise ValueError(f'Invalid address "{address}"')
ValueError: Invalid address ""

======================================================================
ERROR: test_unicode_address_header (mail.tests.MailTests.test_unicode_address_header)
Regression for #11144 - When a to/from/cc header contains Unicode, 
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 64, in forbid_multi_line_headers
    val.encode("ascii")
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 2: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/michel/src/github/django/django/tests/mail/tests.py", line 461, in test_unicode_address_header
    email.message()["To"],
    ^^^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 266, in message
    self._set_list_header_if_not_empty(msg, "To", self.to)
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 431, in _set_list_header_if_not_empty
    msg[header] = value
    ~~~^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 165, in __setitem__
    name, val = forbid_multi_line_headers(name, val, self.encoding)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 67, in forbid_multi_line_headers
    val = ", ".join(
          ^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 68, in <genexpr>
    sanitize_address(addr, encoding) for addr in getaddresses((val,))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/michel/src/github/django/django/django/core/mail/message.py", line 101, in sanitize_address
    raise ValueError(f'Invalid address "{address}"')
ValueError: Invalid address ""

Change History (2)

comment:1 by Mariusz Felisiak, 16 months ago

Resolution: invalid
Status: newclosed

This not an issue in Django, but a regression in Python 3.12.0b4, check out https://github.com/python/cpython/issues/106669.

comment:2 by Michel Alexandre Salim, 16 months ago

Thanks! Yeah, I was just debugging with breakpoint() and realized it's getaddresses that now returns [('', '')] as you noted

Note: See TracTickets for help on using tickets.
Back to Top