-
-
Notifications
You must be signed in to change notification settings - Fork 32k
IDLE displays ANSI sequences in tracebacks when sys.excepthook is customized #127060
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
Comments
IDLE StdOutputFile.isatty() now returns False instead of True.
On Windows, 3.14.0a1 install or fresh build, I do not see this when running from editor. Traceback (most recent call last):
File "f:\dev\3x\Lib\idlelib\run.py", line 590, in runcode
exec(code, self.locals)
~~~~^^^^^^^^^^^^^^^^^^^
File "F:\dev\tem\tem.py", line 4, in <module>
os
NameError: name 'os' is not defined. Did you forget to import 'os'? After entering at prompt, the only differences are that the file is I do see the code on macOS, so I presume you are using mac or linux. IDLE normally calls print_exception, which call a function to rid tracebacks of IDLE artifacts. When The absence of interactive code lines, such as I have thought about converting IDLE always replaces the run process stderr with StdOutputfile. Normally, print_exception is called. After printing the traceback created by python while stderr is bound to an StdOutput file, it calls get_message_line to get The sys.stdxxx bindings are visible to user code. Changing an attribute might break the intended behavior of existing code. |
Right, I'm on Linux. I expected the behavior to be the same on Windows. On Fedora, the ABRT service installs a custom So this IDLE bug is always reproduced on Fedora without having to install a custom excepthook manually, see: https://bugzilla.redhat.com/show_bug.cgi?id=2325502 |
Oh, I forgot that on Windows Python doesn't use ANSI sequences, but control directly the console using the Windows API. So right, Windows is not affected by this issue. |
What's the purpose of pretending that |
@terryjreedy: So you don't like my simple #127061 approach? It does fix the issue for me. Do you have access to Linux or macOS to reproduce the issue? |
I will try to test on my Mac tomorrow. And maybe get setup to do so on Ubuntu. (I have it on a partition on Windows, which my daughter used, but have not yet done anything with it myself.) I need to be able to create and download/run PRs on both other systems. |
I am a bit worried that changing isatty could break something. I can grep idlelib to check locally, but 3rd party code might be accessing sys.stdxxx.isatty. As I said above, this seems like a public interface. Besides this, I would like to revise IDLE to recognize the code and add highlights. This would require switching back. Now that Hugh made it (supposedly, not yet tested) possible to request color codes on Windows, I will be more motivated to do so. A different temporary fix would by to filter out color codes. If I can get them on Windows, I can work on that immediately on my Windows machine. (This would be the first step in using them anyway.) Besides familiarily, my main Windows machine is by far the best ergonomically. |
To me, this means that one can use |
Since Hugo has started adding color codes to other output (unittest and something else so far), perhaps all stderr output should be filtered, not just Tracebacks. I would not want to filter stdout, as users might want to test sending ansi codes. It might be better to change the environment setting on startup, as long as this only affects IDLE and its subprocesses. |
Why not simply set the TERM environment variable to "idle" or whatever? Not all terminals support ANSI sequences, IDLE is the one that does not support. Later we can implement support of ANSI sequences in IDLE. |
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences.
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences.
Good idea: I wrote PR gh-128028 to fix this issue. |
TERM is set in pyshell line 427. Should move to init when do other cleanups. Can easily change if needed. |
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences.
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences. (cherry picked from commit 559b0e7) Co-authored-by: Victor Stinner <[email protected]>
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences. (cherry picked from commit 559b0e7) Co-authored-by: Victor Stinner <[email protected]>
Set TERM environment variable to "dumb" to disable traceback colors in IDLE, since IDLE doesn't understand ANSI escape sequences.
Uh oh!
There was an error while loading. Please reload this page.
Error on Python 3.13 and newer.
Reproducer
sys.excepthook
:Output
ANSI sequences to colorize the output are unexpected and makes the output hard to read.
Expected output
Explanation
In the normal case:
sys.stderr
withio.StringIO()
to call the "excepthook"._colorize.can_colorize()
returns False.If
sys.excepthook
is overridden:idlelib.run.StdOutputFile
object at startup._colorize.can_colorize()
returnsTrue
becauseStdOutputFile.isatty()
always returnTrue
.Linked PRs
The text was updated successfully, but these errors were encountered: