Skip to content
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

tox 4: support isatty on stdout/stderr #1773

Closed
gaborbernat opened this issue Jan 8, 2021 · 2 comments
Closed

tox 4: support isatty on stdout/stderr #1773

gaborbernat opened this issue Jan 8, 2021 · 2 comments
Labels
feature:new something does not exist yet, but should tox4

Comments

@gaborbernat
Copy link
Member

gaborbernat commented Jan 8, 2021

Why? mypy and pytest both use this flag to enable color output support.

Instead of using subprocess.PIPE we could pass in the openpty child handler (which would also allow directly setting the termios information on the stream), but once done that the stdin echo seems to no longer buffer realtime, and is instead only line-buffered. This is bad for interacting with the GDB as you can't see what you're typing. Need to understand why, and fix it (if possible).

Code for LocalSubProcessExecuteInstance.get_stream_file_no:

# on UNIX let's forward it via a pseudo terminal to allow host streams traits to be inherited
try:
    import pty

    main, child = pty.openpty()
except OSError:  # abort if could not open a tty  # pragma: no cover
    pass  # pragma: no cover
else:
    try:
        import termios

        mode = termios.tcgetattr(stream)
        termios.tcsetattr(child, termios.TCSANOW, mode)
    except OSError:  # abort if could inherit traits  # pragma: no cover
        pass  # pragma: no cover
    else:
        if columns != -1 and lines != -1:
            import fcntl
            import struct

            size = struct.pack("HHHH", columns, lines, 0, 0)
            fcntl.ioctl(child, termios.TIOCSWINSZ, size)
        yield child

        # the stream will be written by the subprocess - not by us, so on our end we can close it
        os.close(child)
        yield main  # drain content from main

Code to test with:

import shutil
import sys
import termios
import threading
import time


def _run():
    for i in range(10000):
        time.sleep(1)


thread = threading.Thread(target=_run)
thread.daemon = True
thread.start()

print("----------- START -----------------")
print(f'stdout {sys.stdout.isatty()}')
print(f'stderr {sys.stderr.isatty()}')
print(f'stdin {sys.stdin.isatty()}')

print(shutil.get_terminal_size())
print(termios.tcgetattr(sys.stdin))

print("----------- BREAKPOINT -----------------")
breakpoint()

Of interest links:

@gaborbernat gaborbernat added the feature:new something does not exist yet, but should label Jan 8, 2021
gaborbernat added a commit that referenced this issue Jan 8, 2021
For now sacrificed isatty support on subprocess-es, see
#1773 for more details.

Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
gaborbernat added a commit that referenced this issue Jan 8, 2021
This allows users to force isatty until we fix
#1773.

Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
gaborbernat added a commit that referenced this issue Jan 8, 2021
This allows users to force isatty until we fix
#1773.

Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
@gaborbernat gaborbernat changed the title tox 4 - support isatty on stdout/stderr tox 4: support isatty on stdout/stderr Jan 9, 2021
@gaborbernat gaborbernat added this to the 4.1 milestone Feb 27, 2021
@gaborbernat
Copy link
Member Author

@Julian suggested to take a look at https://github.com/prompt-toolkit/python-prompt-toolkit and https://github.com/urwid/urwid too for inspiration

blairconrad added a commit to blairconrad/dicognito that referenced this issue Dec 8, 2022
masenf added a commit to masenf/tox that referenced this issue Dec 14, 2022
The purpose of allocating a pty is to allow better interactive use and
enable commands to detect that they are running interactively.

From starter code on issue tox-dev#1773
masenf added a commit to masenf/tox that referenced this issue Dec 16, 2022
The purpose of allocating a pty is to allow better interactive use and
enable commands to detect that they are running interactively.

Based on starter code on issue tox-dev#1773. Fixes issue tox-dev#1773.
masenf added a commit to masenf/tox that referenced this issue Dec 16, 2022
The purpose of allocating a pty is to allow better interactive use and
enable commands to detect that they are running interactively.

Based on starter code on issue tox-dev#1773. Fixes issue tox-dev#1773.
@gaborbernat
Copy link
Member Author

This is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:new something does not exist yet, but should tox4
Projects
None yet
Development

No branches or pull requests

1 participant