Skip to content

New REPL exits when there are non-string candidates for suggestions #130999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
devdanzin opened this issue Mar 9, 2025 · 7 comments
Closed

New REPL exits when there are non-string candidates for suggestions #130999

devdanzin opened this issue Mar 9, 2025 · 7 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@devdanzin
Copy link
Contributor

devdanzin commented Mar 9, 2025

Crash report

What happened?

The new REPL in main will exit if a suggestion would be offered, but there are non-string candidates like below:

>>> import runpy
... runpy._run_module_code("blech", {0: "", "bluch": ""}, "")
...
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/home/danzin/projects/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/cpython/Lib/traceback.py", line 129, in print_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1138, in __init__
    context = TracebackException(
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1094, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1535, in _compute_suggestion_error
    return _suggestions._generate_suggestions(d, wrong_name)
TypeError: all elements in 'candidates' must be strings
Traceback (most recent call last):
  File "<python-input-0>", line 2, in <module>
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 98, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "<string>", line 1, in <module>
NameError: name 'blech' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/danzin/projects/cpython/Lib/_pyrepl/console.py", line 173, in _excepthook
    lines = traceback.format_exception(
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 154, in format_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1094, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1535, in _compute_suggestion_error
    return _suggestions._generate_suggestions(d, wrong_name)
TypeError: all elements in 'candidates' must be strings

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/danzin/projects/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
  File "/home/danzin/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/simple_interact.py", line 152, in run_multiline_interactive_console
    more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single")  # type: ignore[call-arg]
  File "/home/danzin/projects/cpython/Lib/code.py", line 324, in push
    more = self.runsource(source, filename, symbol=_symbol)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/console.py", line 231, in runsource
    result = self.runcode(code)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/console.py", line 191, in runcode
    self.showtraceback()
  File "/home/danzin/projects/cpython/Lib/code.py", line 128, in showtraceback
    self._showtraceback(typ, value, tb.tb_next, "")
  File "/home/danzin/projects/cpython/Lib/code.py", line 144, in _showtraceback
    self._excepthook(typ, value, tb)
  File "/home/danzin/projects/cpython/Lib/_pyrepl/console.py", line 179, in _excepthook
    lines = traceback.format_exception(
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 154, in format_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1138, in __init__
    context = TracebackException(
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1094, in __init__
    suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
  File "/home/danzin/projects/cpython/Lib/traceback.py", line 1535, in _compute_suggestion_error
    return _suggestions._generate_suggestions(d, wrong_name)
TypeError: all elements in 'candidates' must be strings

This is an offshoot of #129573, where code like above would abort in 3.12.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

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

Python 3.14.0a5+ (heads/main:a3990df6121, Mar 9 2025, 00:02:58) [GCC 13.3.0]

Linked PRs

@devdanzin devdanzin added the type-crash A hard crash of the interpreter, possibly with a core dump label Mar 9, 2025
@picnixz picnixz added stdlib Python modules in the Lib dir 3.13 bugs and security fixes 3.14 bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error type-crash A hard crash of the interpreter, possibly with a core dump 3.12 only security fixes and removed type-crash A hard crash of the interpreter, possibly with a core dump type-bug An unexpected behavior, bug, or error 3.12 only security fixes labels Mar 9, 2025
@picnixz
Copy link
Member

picnixz commented Mar 9, 2025

Ok, I'll say it's a bug for 3.13/3.14 and a crash for 3.12 but the crash issue is tracked elsewhere

@terryjreedy
Copy link
Member

In dup #133748, the code was globals().update({123: 'test'}); not_defined. However, globals()[123] = 'a'; xxx also works. Both of these (on Windows) have just one "During handling ..." section. If REPL is started from an icon, it 'crashes' in that the window closes when the REPL quits due to its internal error (instead of printing the traceback and then a new '>>>' prompt).

On the other issue, @StanFromIreland proposed, as I understand it, that globals be made a string-only dict. Since, AFAIK, it is not documented as such, I suspect that this would break something. As I remember, @gvanrossum once rejected the idea of making class .__dict__ string only, so that setting non-string attributes (with setattr, for instance), would raise. I think the same may be true for globals dict.

With a non-string in globals, dir() exits with "TypeError: '<' not supported between instances of 'int' and 'str'". This says a) non-strings in globals are not ordinarily a good idea, but b) the possibility has be anticipated and gracefully handled. The simplest solution here is for the hinter to ignore non-strings as possible name corrections, as they definitely are not, instead of crashing the REPL.

@sergey-miryanov
Copy link
Contributor

sergey-miryanov commented May 11, 2025

Does it exit after this PR #131910 was merged?

@StanFromIreland
Copy link
Contributor

Does it exit after this PR #131910 was merged?

It still does. A reproducer was linked in the issue.

@devdanzin
Copy link
Contributor Author

The fix is very simple, but I'd like some feedback on the best way to do it: #131001 (comment).

ambv pushed a commit that referenced this issue Jun 2, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 2, 2025
… candidates for suggestions (pythongh-131001)

(cherry picked from commit baccfdb)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 2, 2025
… candidates for suggestions (pythongh-131001)

(cherry picked from commit baccfdb)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
@encukou
Copy link
Member

encukou commented Jun 2, 2025

I think the fix here is what's causing the buildbot failure seen at: https://buildbot.python.org/#/builders/1368/builds/3297:

test test_traceback crashed -- Traceback (most recent call last):
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/single.py", line 210, in _runtest_env_changed_exc
    _load_run_test(result, runtests)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/single.py", line 165, in _load_run_test
    regrtest_runner(result, test_func, runtests)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/single.py", line 114, in regrtest_runner
    refleak, test_result = runtest_refleak(result.test_name, test_func,
                           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                           runtests.hunt_refleak,
                                           ^^^^^^^^^^^^^^^^^^^^^^
                                           runtests.quiet)
                                           ^^^^^^^^^^^^^^^
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/refleak.py", line 138, in runtest_refleak
    result = test_func()
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/single.py", line 162, in test_func
    return run_unittest(test_mod, runtests)
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/test/libregrtest/single.py", line 33, in run_unittest
    tests = loader.loadTestsFromModule(test_mod)
  File "/Users/ec2-user/buildbot/buildarea/3.x.itamaro-macos-arm64-aws.macos-with-brew.refleak.nogil/build/Lib/unittest/loader.py", line 100, in loadTestsFromModule
    for name in dir(module):
                ~~~^^^^^^^^
TypeError: '<' not supported between instances of 'int' and 'str'
1 test failed again:
    test_traceback

@ambv
Copy link
Contributor

ambv commented Jun 2, 2025

I'm on it.

ambv added a commit to miss-islington/cpython that referenced this issue Jun 2, 2025
ambv added a commit that referenced this issue Jun 2, 2025
…g candidates for suggestions (gh-131001) (gh-135019)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this issue Jun 2, 2025
…g candidates for suggestions (gh-131001) (gh-135020)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@ambv ambv closed this as completed Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

7 participants