I get IndexError on runtests.py

Hi, I downloaded django source code and ran tests:

$ python runtests.py mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses -v 2

I get IndexError:

Testing against Django installed in ‘C:\code\django\django\django’ with up to 8 processes
Importing application mail
Found 1 test(s).
Skipping setup of unused database(s): default, other.
System check identified no issues (0 silenced).
test_avoids_sending_to_invalid_addresses (mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses)
Verify invalid addresses can’t sneak into SMTP commands through …
test_avoids_sending_to_invalid_addresses (mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses) (email_address=‘to@’)
Verify invalid addresses can’t sneak into SMTP commands through … ERROR

======================================================================

ERROR: test_avoids_sending_to_invalid_addresses (mail.tests.SMTPBackendTests.test_avoids_sending_to_invalid_addresses) (email_address=‘to@’)
Verify invalid addresses can’t sneak into SMTP commands through

Traceback (most recent call last):
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1965, in get_address
token, value = get_group(value)
^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1923, in get_group
raise errors.HeaderParseError("expected ‘:’ at end of group "
email.errors.HeaderParseError: expected ‘:’ at end of group display name but found ‘@’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1791, in get_mailbox
token, value = get_name_addr(value)
^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1777, in get_name_addr
token, value = get_angle_addr(value)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1702, in get_angle_addr
raise errors.HeaderParseError(
email.errors.HeaderParseError: expected angle-addr but found ‘@’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\code\django\django\tests\mail\tests.py”, line 3070, in test_avoids_sending_to_invalid_addresses
backend.send_messages([email])
File “C:\code\django\django\django\core\mail\backends\smtp.py”, line 138, in send_messages
sent = self._send(message)
^^^^^^^^^^^^^^^^^^^
File “C:\code\django\django\django\core\mail\backends\smtp.py”, line 151, in _send
recipients = [self.prep_address(addr) for addr in email_message.recipients()]
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\code\django\django\django\core\mail\backends\smtp.py”, line 172, in prep_address
parsed = AddressHeader.value_parser(address)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email\headerregistry.py”, line 333, in value_parser
address_list, value = parser.get_address_list(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1988, in get_address_list
token, value = get_address(value)
^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1968, in get_address
token, value = get_mailbox(value)
^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1794, in get_mailbox
token, value = get_addr_spec(value)
^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1647, in get_addr_spec
token, value = get_domain(value[1:])
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Info\AppData\Local\Programs\Python\Python312\Lib\email_header_value_parser.py”, line 1604, in get_domain
if value[0] in CFWS_LEADER:
~~~~~^^^
IndexError: string index out of range


Ran 1 test in 0.035s

FAILED (errors=1)

the python version 3.12.0 and django version ‘6.1.dev20251003211257’ however in test method below when I remove “to@” I don’t get the exception again.

def test_avoids_sending_to_invalid_addresses(self):
    """
    Verify invalid addresses can't sneak into SMTP commands through
    EmailMessage.all_recipients() (which is distinct from message header
    fields).
    """
    backend = smtp.EmailBackend()
    backend.connection = mock.Mock()
    for email_address in (
        # Invalid address with two @ signs.
        "to@other.com@example.com",
        # Invalid address without the quotes.
        "to@other.com <to@example.com>",
        # Multiple mailboxes in a single address.
        "to@example.com, other@example.com",
        # Other invalid addresses.
        "@",
        "to@",  #<-------------------Here
        "@example.com",
        # CR/NL in addr-spec. (SMTP strips display-name.)
        '"evil@example.com\r\nto"@example.com',
        "to\nevil@example.com",
    ):

is this an compatibility issue should I download different version of python or just testcases is buggy?

Please update to the latest Python 3.12.11 and try again. (Or at minimum 3.12.10.)

There were several fixes to Python’s email library since 3.12.0. I’m not sure whether that’s responsible for the error you’re seeing, but there are some other django.core.mail tests that will definitely fail with 3.12.0 (and are meant to fail with older versions).

1 Like

Yes. The .0 releases of anything (Python, Django, …) typically have a bunch of bugs that were missed during development. And Django’s release notes state:

We highly recommend, and only officially support, the latest release of each series.

1 Like

Greate, switching to 3.12.11 or higher fixed my problem.