Skip to content

NameError has different actions when be raised in IDLE #135511

Description

@Locked-chess-official

Bug report

Bug description:

In IDLE, I raised three different exceptions. The output is:

>>>raise Exception("123\n456")
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    raise Exception("123\n456")
Exception: 123
456
>>>raise NameError("123\n456")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    raise NameError("123\n456")
456
>>>raise OSError("123\n456")
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    raise OSError("123\n456")
OSError: 123
456

Only NameError cannot show the Exception Type if the text has two or more lines.
In shell the action is same with other exception.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Activity

  1. Locked-chess-official commented on Jun 14, 2025

    @Locked-chess-official
    ContributorAuthor

    Image

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 14, 2025
  3. shuimu5418 commented on Jun 14, 2025

    @shuimu5418

    3.12.10:

    Image

    latest build:

    Image

  4. picnixz commented on Jun 14, 2025

    @picnixz
    Member

    3.12 won't get updates as it now only accepts security-only patches but we'll try to fix this in 3.13 and later

  5. terryjreedy commented on Jun 14, 2025

    @terryjreedy
    Member

    I confirmed that this is IDLE and exception specific. Also, it does not happen if IDLE is started with the -n (no subprocess) option. So it is specific to the exception display in normal mode, which I am sure uses a method in run.py instead of the one in pyshell.py.

  6. terryjreedy commented on Jun 14, 2025

    @terryjreedy
    Member

    The same error occurs when the raise statement in run from an editor file instead of entered interactively.

  7. terryjreedy commented on Jun 14, 2025

    @terryjreedy
    Member

    The run.py function print_exception calls nested function print_exc, which prints the lines returned by get_message_lines(typ, exc, tb). The latter special cases AttributeError and NameError to indirectly get the type hint, if available, that is not directly available to Python code. It does this by calling sys.excepthook with stderr temporarily redirected to an io.StringIO. For example: input (NameError, "name 'aab' is not defined.") might result in output "NameError name 'aab' is not defined. Did you mean: 'aaa'?". The returned message is always a single logical line for NameErrors raised by Python itself. The function extracts and return it.

    This bug would only affect a situation where user code catches a NameError, adds second line to the message, and re-raises a NameError with the 2 (or more) line message. A fix should check the message for embedded \ns and if present, extract more lines from the output.

  8. Locked-chess-official commented on Jun 15, 2025

    @Locked-chess-official
    ContributorAuthor

    Image
    After the change in the PR.

  9. removed
    stdlibStandard Library Python modules in the Lib/ directory
    on Jan 15, 2026
  10. terryjreedy commented on Sep 26, 2026

    @terryjreedy
    Member

    @Locked-chess-official @shuimu5418 The little images you two posted above took several minutes to appear. If you right-click in the Shell sidebar, you can copy a selection with prompts. Then paste between triple backtick (```) lines.

  11. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 26, 2026
  12. added 3 commits that reference this issue on Sep 27, 2026
  13. added a commit that references this issue on Oct 9, 2026
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/ directorytopic-IDLEtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions