Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
48.81k stars 1.71k forks source link

[bug] rich + crlf = notepad python #1824

Open tx46 opened 2 years ago

tx46 commented 2 years ago

How to h4x?

After approx. 12 hours of debugging why my app is silently crashing, I have finally figured out the cause. Here's a minimal (non-)working example file for you to work on, lol. It only crashes on Windoze, and it does so in a really nasty way which seems to entirely screw up the Python runtime. Line 1088 in one of the rich source files may be related.

import os
import time

from rich.layout import Layout
from rich.live import Live
from rich.text import Text

text = Text()
text.append(Text("foo" + os.linesep, style="yellow"))
layout = Layout(renderable=text)

with Live(layout):
    while True:
        time.sleep(0.1)

Screenshot

image

Platform

Windows 10 Command Prompt in a VM, but also tested on live Windows 10 system. Does not crash on Linux.

┌─────────────────────── <class 'rich.console.Console'> ───────────────────────┐
│ A high level console interface.                                              │
│                                                                              │
│ ┌──────────────────────────────────────────────────────────────────────────┐ │
│ │ <console width=189 ColorSystem.WINDOWS>                                  │ │
│ └──────────────────────────────────────────────────────────────────────────┘ │
│                                                                              │
│     color_system = 'windows'                                                 │
│         encoding = 'utf-8'                                                   │
│             file = <colorama.ansitowin32.StreamWrapper object at 0x01809C10> │
│           height = 42                                                        │
│    is_alt_screen = False                                                     │
│ is_dumb_terminal = False                                                     │
│   is_interactive = True                                                      │
│       is_jupyter = False                                                     │
│      is_terminal = True                                                      │
│   legacy_windows = True                                                      │
│         no_color = False                                                     │
│          options = ConsoleOptions(                                           │
│                        size=ConsoleDimensions(width=189, height=42),         │
│                        legacy_windows=True,                                  │
│                        min_width=1,                                          │
│                        max_width=189,                                        │
│                        is_terminal=True,                                     │
│                        encoding='utf-8',                                     │
│                        max_height=42,                                        │
│                        justify=None,                                         │
│                        overflow=None,                                        │
│                        no_wrap=False,                                        │
│                        highlight=None,                                       │
│                        markup=None,                                          │
│                        height=None                                           │
│                    )                                                         │
│            quiet = False                                                     │
│           record = False                                                     │
│         safe_box = True                                                      │
│             size = ConsoleDimensions(width=189, height=42)                   │
│        soft_wrap = False                                                     │
│           stderr = False                                                     │
│            style = None                                                      │
│         tab_size = 8                                                         │
│            width = 189                                                       │
└──────────────────────────────────────────────────────────────────────────────┘
platform="Windows"
WindowsConsoleFeatures(vt=False, truecolor=False)
rich==11.0.0
darrenburns commented 2 years ago

Hi @philiparvidsson, thanks for the report. I'd just like to confirm a few things:

Thanks!

tx46 commented 2 years ago

After you import the module, does anything happen at all?

No.

Did the "live display" not appear?

It did not appear, correct.

Did you press any additional keys after the import to get the terminal into that state?

No.

Does the same behaviour happen if you run the module directly rather than importing it into the Python REPL?

Yes.

If you have access to Windows Terminal and could check if you can reproduce it on that instead of command prompt, that would be appreciated.

I don't have access to it. It seems the cause is crlf (\r) causing incorrect behavior in splitlines (or whatever it's called, line 1088 in text.py or something). So I don't think terminal matters. This doesn't crash on n*x because os.linesep has no \r in it.

aroberge commented 2 years ago

I have tried it with Windows Terminal (both cmd and Powershell) and, for all practical purpose, it disable the Python interpreter when importing the file. Anything typed in is ignored except that exit() is recognized.

Running python script.py simply returns to the console prompt.