GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pure-Python suggestion fallback differs from the C implementation · Issue #161495 · python/cpython

pure-Python suggestion fallback differs from the C implementation #161495

New issue
Copy link
New issue
Copy link
Open
Open
pure-Python suggestion fallback differs from the C implementation#161495
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@moontc
moontc
opened on Nov 6, 2026
Issue body actions

Bug report

The pure-Python loop in traceback._compute_suggestion_error() gives different results from _Py_CalculateSuggestions() in Python/suggestions.c:

  1. Initial best distance: len(wrong_name) in Python, PY_SSIZE_T_MAX in C.
  2. Candidate limit: len(d) > 750 in Python, >= 750 in C.
  3. Python returns None for names longer than 40 characters; C only checks the length after trimming the common prefix and suffix.
import sys, traceback, _suggestions

class A:
    def __init__(self, names): self.names = names
    def __dir__(self): return self.names

def pure_python(names, wrong):
    sys.modules['_suggestions'] = None
    try:
        getattr(A(names), wrong)
    except AttributeError as e:
        return traceback._compute_suggestion_error(e, e.__traceback__, wrong)
    finally:
        sys.modules['_suggestions'] = _suggestions

for names, wrong in [
    (['tuple', 'True'], 'ture'),
    (['x' * 41 + 'y'], 'x' * 41),
    ([f'name{i:03}' for i in range(749)] + ['vvvvv'], 'vvvvx'),
]:
    print(_suggestions._generate_suggestions(sorted(names), wrong),
          pure_python(names, wrong))
tuple None
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy None
None vvvvv

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

  • gh-161495: Make traceback suggestion fallback match the C implementation #161497

Activity

  1. moontc
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 6, 2026
  2. moontc
    added a commit that references this issue on Nov 6, 2026

    pythongh-161495: Optimize common-affix trimming in traceback

    144707f
  3. NekoAsakura
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 6, 2026
  4. picnixz commented on Nov 7, 2026

    @picnixz
    picnixz
    on Nov 7, 2026
    Member
    More actions

    Are these functions interchangeable? they are also private so you are not meant to use them outside CPython. When do they produce different meaningful results?

    Reacted by moontc
  5. moontc commented on Nov 8, 2026

    @moontc
    moontc
    on Nov 8, 2026
    ContributorAuthor
    More actions

    Before gh-97008, the default exception display was mainly done in C, and the suggestions only existed in Python/suggestions.c. Then gh-97008 pointed out:

    This is implemented in Python/suggestions.c and unavailable in the pure Python implementation of traceback formatting in Lib/traceback.py. This makes the output of both different, which is problematic for tooling using traceback.py, as well as for alternative implementations of Python like PyPy and GraalPy.

    Then a pure Python implementation of suggestions was added to traceback.py.

    Let's implement the equivalent of suggestions.c in Lib/traceback.py to bring both in line.

    so its design goal was to "implement the equivalent of suggestions.c in Lib/traceback.py".

    after #110702, default exception display prioritizes the use of python's traceback.
    after #113712, _compute_suggestion_error() can call the C implementation through _suggestions and also contains a pure-Python implementation

    "Are these functions interchangeable?"
    Yes, more precisely, the pure-Python suggestion calculation inside _compute_suggestion_error() should give the same results as _Py_CalculateSuggestions()

    “When do they produce different meaningful results?”
    They produce different suggestions depending on whether the _suggestions module is available.
    For example, the same code produces different suggestions on CPython and PyPy, because PyPy doesn't have the _suggestions module and uses the pure Python implementation in traceback.py.

    Reacted by Bénédikt Tran
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.