This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess output seems to depend on size of terminal screen
Type: behavior Stage: resolved
Components: Demos and Tools Versions: Python 3.5
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: epsolos, eryksun, matrixise
Priority: normal Keywords:

Created on 2018-10-24 15:36 by epsolos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg328371 - (view) Author: Edward Pratt (epsolos) Date: 2018-10-24 15:36
I am looking for a string inside of a process, and it seems that the output of the check_output command depends on the screen size of the terminal I run the code in. 

Here I ran the code with a normal screen resolution:
>>> result = subprocess.check_output(['ps', 'aux']).decode('ascii', errors='ignore')
>>> 'app-id' in result
False
 
Then I zoom out to the point where I can barely read the text on the screen, and this is the output I get:
>>> result = subprocess.check_output(['ps', 'aux']).decode('ascii', errors='ignore')
>>> 'app-id' in result
True
msg328373 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-24 15:57
Very strange, I tried with 3.8a and I did not get this issue.

Normally we use a pipe for stdin and stdout when we use check_output, so in this case, there is no relation with the size of the terminal. this is just a stream.

second point, you explain that you use python 3.5, could you try with 3.6 or 3.7 because 3.5 is in security mode and we won't work on this issue.

Thank you
msg328384 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2018-10-24 17:40
This is due to the ps command itself. You'd have the same problem when piping to grep or redirecting output to a file. I don't know how it determines terminal size. I tried overriding stdin, stdout and stderr to pipes and calling setsid() in the forked child process to detach from the controlling terminal, but it still detected the terminal size. Anyway, the "ww" option of ps overrides this behavior.
msg328385 - (view) Author: Edward Pratt (epsolos) Date: 2018-10-24 18:46
I don’t think that is true. I tried grepping for what I need in a very small terminal and still got the correct result.

> On Oct 24, 2018, at 1:40 PM, Eryk Sun <report@bugs.python.org> wrote:
> 
> 
> Eryk Sun <eryksun@gmail.com> added the comment:
> 
> This is due to the ps command itself. You'd have the same problem when piping to grep or redirecting output to a file. I don't know how it determines terminal size. I tried overriding stdin, stdout and stderr to pipes and calling setsid() in the forked child process to detach from the controlling terminal, but it still detected the terminal size. Anyway, the "ww" option of ps overrides this behavior.
> 
> ----------
> nosy: +eryksun
> resolution:  -> third party
> stage:  -> resolved
> status: open -> closed
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue35060>
> _______________________________________
msg328390 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-24 20:29
just one question, did you use this command in the REPL?
msg328391 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-24 20:34
My script:

#!/usr/bin/env python
import pathlib
import subprocess

output = subprocess.check_output(['ps', 'aux'])
pathlib.Path('/tmp/ps_aux.txt').write_bytes(output)


When I execute the following script in the REPL, I get your issue but for me, it's normal because the REPL is running in a terminal with a limited size. 

And when I execute the same script like as a simple python script, I don't have your issue.
msg328392 - (view) Author: Edward Pratt (epsolos) Date: 2018-10-24 20:38
You are correct. It works as expected outside of the REPL.

> On Oct 24, 2018, at 4:34 PM, Stéphane Wirtel <report@bugs.python.org> wrote:
> 
> 
> Stéphane Wirtel <stephane@wirtel.be> added the comment:
> 
> My script:
> 
> #!/usr/bin/env python
> import pathlib
> import subprocess
> 
> output = subprocess.check_output(['ps', 'aux'])
> pathlib.Path('/tmp/ps_aux.txt').write_bytes(output)
> 
> 
> When I execute the following script in the REPL, I get your issue but for me, it's normal because the REPL is running in a terminal with a limited size. 
> 
> And when I execute the same script like as a simple python script, I don't have your issue.
> 
> ----------
> status: closed -> open
> 
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue35060>
> _______________________________________
msg328393 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-24 21:09
Here is the doc from ps with man.

> man ps

If ps cannot determine display width, as when output is redirected (piped) into a file or another command, the output width is undefined (it may be 80, unlimited, determined by the TERM variable, and so on).  The COLUMNS environment variable or --cols option may be used to exactly determine the width in this case.  The w or -w option may be also be used to adjust width.

-w     Wide output.  Use this option twice for unlimited width.


So for my part, in my terminal, the COLUMNS envvar is different in function of the size of my window, from 86 to 131 etc...

so, I think we can definitively close this issue.

Thank you,
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79241
2018-10-24 21:09:19matrixisesetstatus: open -> closed
2018-10-24 21:09:04matrixisesetmessages: + msg328393
2018-10-24 20:38:09epsolossetmessages: + msg328392
2018-10-24 20:34:27matrixisesetstatus: closed -> open

messages: + msg328391
2018-10-24 20:29:03matrixisesetmessages: + msg328390
2018-10-24 18:46:00epsolossetmessages: + msg328385
2018-10-24 17:40:45eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg328384

resolution: third party
stage: resolved
2018-10-24 15:57:48matrixisesetnosy: + matrixise
messages: + msg328373
2018-10-24 15:36:53epsoloscreate