[Python-ideas] A better interactive prompt

Cody Piersall cody.piersall at gmail.com
Wed Oct 26 18:16:44 EDT 2016


On Wed, Oct 26, 2016 at 4:48 PM, Paul Moore <p.f.moore at gmail.com> wrote:
> Good point. We could, of course, detect when stdin is non-interactive,
> but at that point the code is starting to get unreasonably complex, as
> well as having way too many special cases. So I agree, that probably
> kills the proposal.

Isn't that check really just an isatty() check?  Or is that not
reliable enough for some reason?  Here's some code that performs that
check, and works on Linux and Windows:

#include <stdio.h>

#ifdef _WIN32
#  include <io.h>
#  define isatty _isatty
#  define fileno _fileno
#else
#  include <unistd.h>
#endif

int main( void ) {
    /* If stdin is a tty, you would launch the user's configured REPL*/
    if(isatty(fileno(stdin)))
        printf("stdin has not been redirected to a file\n");
    /* Otherwise, launch the default REPL (or maybe don't launch a REPL at all,
     * and just treat stdin as a file */
    else
        printf("stdin has been redirected to a file\n");
}


More information about the Python-ideas mailing list