Skip to content

Fix detecting piped input#27

Open
Burrito-Bazooka wants to merge 2 commits into
adityarathod:masterfrom
Burrito-Bazooka:patch-1
Open

Fix detecting piped input#27
Burrito-Bazooka wants to merge 2 commits into
adityarathod:masterfrom
Burrito-Bazooka:patch-1

Conversation

@Burrito-Bazooka

@Burrito-Bazooka Burrito-Bazooka commented Jun 11, 2016

Copy link
Copy Markdown

Fixes case when used (through subprocess) inside Python script which is not run from a tty, I bet there are a few other cases it fixes as well.

edit: I actually encountered the behaviour while making a HexChat script which uses shell commands (among them, xerox). xerox behaved differently depending on whether I ran HexChat from a terminal, or from a desktop launcher. /e

edit2 (17 Jul): To be clear, the rest of this message is just to demonstrate why the current behaviour is buggy. They aren't files I want committed./e2

Files

isatty_test1.py (executable):

#!/usr/bin/env python3

import sys, stat, os

mode = os.fstat(0).st_mode
print(mode)

print("isatty", sys.stdin.isatty())

print("isfifo", stat.S_ISFIFO(mode))
print("isreg", stat.S_ISREG(mode))

isatty_test2.py

import subprocess
p = subprocess.Popen(['./isatty_test1.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(p.communicate())

isatty_test3.py (executable - same as test2 but writes to a file so that we can run it without a tty):

#!/usr/bin/env python3

import subprocess
p = subprocess.Popen(['./isatty_test1.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
with open("./test3.out", "w") as f:
    print(p.communicate(), file=f)

Tests

(on Linux, Debian testing (stretch/sid))

In a terminal ./isatty_test1.py
Output:

8592
isatty True
isfifo False
isreg False

In a terminal python3 ./isatty_test2.py
Output:

(b'8592\nisatty True\nisfifo False\nisreg False\n', b'')

Running isatty_test3.py as an executable from my file manager to capture buggy behaviour
(note that no input was piped in, but the first output doesn't match xerox's current expectations)
Output (test3.out):

(b'8630\nisatty False\nisfifo False\nisreg False\n', b'')

In terminal ./isatty_test1.py < test3.out
Output:

33188
isatty False
isfifo False
isreg True

In a terminal cat ./test3.out | ./isatty_test1.py
Output:

4480
isatty False
isfifo True
isreg False

Fixes case when used inside Python script which is not run from a tty, I bet there are a few other cases it fixes as well.
@kennethreitz kennethreitz self-assigned this Jul 25, 2016
@Burrito-Bazooka

Copy link
Copy Markdown
Author

Hey, are there any problems with this PR? Not that I need it quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants