python / cpython

The Python programming language
https://www.python.org
Other
62.16k stars 29.87k forks source link

macOS Big Sur input() removes colors #113533

Open enty8080 opened 8 months ago

enty8080 commented 8 months ago

Bug report

Bug description:

Providing screenshot because of colors:

Screenshot 2023-12-28 at 03 53 02

More details:

The same code works on Linux Ubuntu LTS and both input() and print() are colored.

NOTE: \001 and \002 are necessary for preventing libreadline from breaking prompt on tab completion. However removing them fixes the problem with colors. Unfortunately I can't go without them because otherwise libreadline breaks prompt:

Thanks in advance, Ivan Nikolskiy (@enty8080)

CPython versions tested on:

3.11

Operating systems tested on:

macOS

ronaldoussoren commented 8 months ago

The one of the answers to the stackoverflow question indicates there's little we can do here: readline on macOS uses the system libedit and apparently that's broken for this feature.

ronaldoussoren commented 8 months ago

That said, the stackoverflow question also documents a way to fix the libedit sources and the fix they mention can probably be used to implement a hot fix by directly calling some native libedit APIs (not through its readline implementation).

enty8080 commented 8 months ago

I also tried to write prompt through sys.stdout.write so all the colors will be available and then called blank input(). However in this case, when you use backspace, it breaks the prompt too (erases it).

ronaldoussoren commented 8 months ago

That said, the stackoverflow question also documents a way to fix the libedit sources and the fix they mention can probably be used to implement a hot fix by directly calling some native libedit APIs (not through its readline implementation).

I was too positive here. The EditLine instance used in the readline emulation is private to that library and cannot be accessed which means we cannot work around this.

This is a bug in the system version of libedit, the version at https://www.thrysoee.dk/editline/ does not have this bug.

@ned-deily

enty8080 commented 8 months ago

Looks like readline on my system does not use libedit (from otool)

$ otool -L /usr/local/readline/8.1/lib/libreadline.8.1.dylib
/usr/local/readline/8.1/lib/libreadline.8.1.dylib:
    /usr/local/readline/8.1/lib/libreadline.8.dylib (compatibility version 8.1.0, current version 8.1.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)

felix@Felixs-Air ~ % find /opt -name 'libreadline*' -type f 2>/dev/null     
/opt/homebrew/Cellar/readline/8.2.7/lib/libreadline.8.2.dylib
/opt/homebrew/Cellar/readline/8.2.7/lib/libreadline.a

$ otool -L /opt/homebrew/Cellar/readline/8.2.7/lib/libreadline.8.2.dylib
/opt/homebrew/Cellar/readline/8.2.7/lib/libreadline.8.2.dylib:
    /opt/homebrew/opt/readline/lib/libreadline.8.dylib (compatibility version 8.2.0, current version 8.2.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

$ otool -L /opt/homebrew/opt/readline/lib/libreadline.8.dylib
/opt/homebrew/opt/readline/lib/libreadline.8.dylib:
    /opt/homebrew/opt/readline/lib/libreadline.8.dylib (compatibility version 8.2.0, current version 8.2.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)
dtrodrigues commented 8 months ago

What's the output of python3 -c "import readline; print(readline._READLINE_LIBRARY_VERSION)" and otool -L $(python3 -c "import readline; print(readline.__file__)")? Where did you get your Python from?

enty8080 commented 8 months ago

output 1:

EditLine wrapper

output 2:

/opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload/readline.cpython-311-darwin.so:
    /usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.100.5)

I see it now. I got python3 from homebrew.