Skip to content

NameError has different actions when be raised in IDLE #135511

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

Open
Locked-chess-official opened this issue Jun 14, 2025 · 7 comments
Open

NameError has different actions when be raised in IDLE #135511

Locked-chess-official opened this issue Jun 14, 2025 · 7 comments
Labels
stdlib Python modules in the Lib dir topic-IDLE type-bug An unexpected behavior, bug, or error

Comments

@Locked-chess-official
Copy link

Locked-chess-official commented Jun 14, 2025

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

@Locked-chess-official Locked-chess-official added the type-bug An unexpected behavior, bug, or error label Jun 14, 2025
@Locked-chess-official
Copy link
Author

Image

@picnixz picnixz added stdlib Python modules in the Lib dir topic-IDLE labels Jun 14, 2025
@shuimu5418
Copy link

shuimu5418 commented Jun 14, 2025

3.12.10:

Image

latest build:

Image

@picnixz
Copy link
Member

picnixz commented Jun 14, 2025

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

@terryjreedy
Copy link
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.

@terryjreedy
Copy link
Member

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

@terryjreedy
Copy link
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.

@Locked-chess-official
Copy link
Author

Image
After the change in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-IDLE type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

4 participants