Skip to content

Failed assertion in _PyUnicode_Equal from calculate_suggestions with non-string candidate #129573

Open
@devdanzin

Description

@devdanzin

Crash report

What happened?

The interpreter will abort if runpy._run_module_code is called with invalid values:

import runpy
runpy._run_module_code("A", {0: ""}, "")

Another way to trigger:

class Parent:
    def __dir__(self):
        return [0]

class WithNonStringAttrs(Parent):
    blech = None

WithNonStringAttrs().bluch

Abort message:

python: Objects/unicodeobject.c:10799: _PyUnicode_Equal: Assertion `PyUnicode_Check(str2)' failed.
Aborted (core dumped)

The suggestions machinery is the source of the issue, with calculate_suggestions calling _PyUnicode_Equal(name, item) for an item that might not be unicode.

Only aborts in 3.12. In 3.13 and main it results in an error in the traceback machinery:

Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/home/danzin/projects/upstream_cpython/Lib/traceback.py", line 139, in _print_exception_bltin
    return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
  File "/home/danzin/projects/upstream_cpython/Lib/traceback.py", line 129, in print_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/home/danzin/projects/upstream_cpython/Lib/traceback.py", line 1090, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/home/danzin/projects/upstream_cpython/Lib/traceback.py", line 1531, in _compute_suggestion_error
    return _suggestions._generate_suggestions(d, wrong_name)
TypeError: all elements in 'candidates' must be strings
[...]

Will submit a PR shortly.

Found using fusil by @vstinner.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.12.8+ (heads/3.12:580d7810946, Feb 2 2025, 01:56:47) [GCC 13.3.0]

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions