[Python] Injecting unicode characters via termios.TIOCSTI

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have a piece of python code that injects entries from the bash history into the command prompt.

    Everything worked perfectly until I switched to Python 3. Now German Umlaute appear wrong.

    eg.

    python3 console_test.py mööp


    results in:

    $ m�


    Here's the relevant code:

    import fcntl
    import sys
    import termios

    command = sys.argv[1]

    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~termios.ECHO # disable echo
    termios.tcsetattr(fd, termios.TCSANOW, new)
    for c in command:
    fcntl.ioctl(fd, termios.TIOCSTI, c)
    termios.tcsetattr(fd, termios.TCSANOW, old)


    I tried encoding the input to utf-8 but that gave me:

    OSError: [Errno 14] Bad address

    Continue reading...

Compartilhe esta Página