osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

if STREAM.isatty():


On Sat, Aug 31, 2019 at 7:42 AM Eryk Sun <eryksun at gmail.com> wrote:
>
> On 8/30/19, Chris Angelico <rosuav at gmail.com> wrote:
> > On Sat, Aug 31, 2019 at 5:40 AM Eryk Sun <eryksun at gmail.com> wrote:
> >
> >> Or simply run python.exe from another console process that keeps the
> >> console alive (it's reference counted), which is typically cmd.exe or
> >> powershell.exe.
> >
> > Not sure what you mean by "reference counted"
>
> In Windows 8+, a process attaches to a console by opening
> "\Device\ConDrv\Connect" as a connection to an instance of the console
> host process, conhost.exe. This handle is stored internally as
> "ConsoleHandle" (in its PEB->ProcessParameters). The console API uses
> this handle for functions that don't operate on a stream, such as
> GetConsoleTitle.
> [chomp more details]

Huh. I kinda just assumed that, since cmd.exe was still running, the
console would continue to be in use. I guess technically it's a lot
more complicated than that, but still, to the average user, the
difference won't be visible.

> > Of course, most of us use a shell that's a tad more powerful than
> > cmd.exe, but the effect is the same regardless.
>
> Yes, CMD is not a great shell, and PowerShell is tedious (IMO). We can
> use bash.exe from MSYS2 or Git instead. But stick to the normal
> console interface (i.e. run bash.exe directly), or use winpty if you
> need to run Windows Python. The MSYS2 terminal interface (mintty) is
> based on named pipes. Its build of Python special cases these pipes,
> but they're just pipes to the Windows build, so isatty() is false and
> it defaults to full buffering. Also, the REPL of the Windows build
> depends on the system console for line editing and history, so even if
> we force interactive mode via `python -i`, the UI is horrible without
> a console.

And that's why the pseudo-tty system is vital to making things make
sense. On Unix-like platforms, a tool like ssh can connect a program
to something that it perceives as a TTY, and then take the contents
elsewhere. It's a consistent system that works with any program.

Oh, and, well, GNU Readline is just a smidge better than the default
Windows line editing, too...

ChrisA