Running python from pty without prompt
steve+python at pearwood.info
Tue Dec 13 19:20:35 EST 2016
On Wed, 14 Dec 2016 04:48 am, Random832 wrote:
> On Tue, Dec 13, 2016, at 11:01, Michael Torrie wrote:
>> On 12/13/2016 05:39 AM, Samuel Williams wrote:
>> > Michael, yes.
>> > FYI, I found out why this works. Pressing Ctrl-D flushes the input
>> > buffer. If you do this on an empty line, it causes read(...) to return
>> > 0 which Ruby considers end of input for the script, but the pipe is
>> > not closed.
>> Currently Python does not appear to support this behavior. Possibly it
>> could be patched to support something similar, though.
> The problem is there's currently no way to differentiate "interactive
> mode" from "script run on a tty".
sys.flags.interactive will tell you whether or not your script was launched
with the -i flag.
hasattr(sys, 'ps1') or hasattr(sys, 'ps2') will tell you if you are running
in the REPL (interactive interpreter). The ps1 and ps2 variables aren't
defined in non-interactive mode.
Does that help?
> You can get similar behavior with python -c "import
[steve at ando ~]$ python -c "import sys; print hasattr(sys, 'ps1')"
[steve at ando ~]$ python -c "import sys; exec(sys.stdin.read())"
print hasattr(sys, 'ps1')
It's not obvious, but after I entered the line "print hasattr(...)" I typed
Ctrl-D, ending the stream.
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list