Is shutil.get_terminal_size useless?

eryk sun eryksun at gmail.com
Sat Jan 28 15:04:45 EST 2017


On Sat, Jan 28, 2017 at 5:58 PM, Chris Angelico <rosuav at gmail.com> wrote:
> Processes in the middle of pipelines *do not have* terminals.

No, in the following case stderr is a terminal:

    $ echo spam |
    > python3 -c 'import os
    > print(os.get_terminal_size(2))' |
    > cat
    os.terminal_size(columns=132, lines=43)

Let's also redirect stderr to the pipe:

    $ echo spam |
    > 2>&1 python3 -c 'import os
    > print(os.get_terminal_size(2))' |
    > cat

    Traceback (most recent call last):
      File "<string>", line 2, in <module>
    OSError: [Errno 25] Inappropriate ioctl for device

Now let's open and use the controlling terminal instead:

    $ echo spam |
    > 2>&1 python3 -c 'import os
    > fd = os.open("/dev/tty", os.O_RDONLY)
    > print(os.get_terminal_size(fd))' |
    > cat
    os.terminal_size(columns=132, lines=43)

Now let's get rid of the terminal via setsid:

   $ echo spam |
    > 2>&1 setsid python3 -c 'import os
    > fd = os.open("/dev/tty", os.O_RDONLY)
    > print(os.get_terminal_size(fd))' |
    > cat
    Traceback (most recent call last):
      File "<string>", line 2, in <module>
    OSError: [Errno 6] No such device or address: '/dev/tty'



More information about the Python-list mailing list