Kaydet (Commit) 775af919 authored tarafından Guido van Rossum's avatar Guido van Rossum

My version of Lee Busby's patches to make '-i' pretend stdin is a tty

even if it isn't.  Changes:

- set the global flag Py_InteractiveFlag when -i is given
- call Py_FdIsInteractive() instead of isatty()
- make stdin unbuffered, too, when using -u
- make stdin and stdout line buffered, when stdin is interactive and not -u

Note that setting the environment variable PYTHONINSPECT does not have
these extra effects of -i.  (Should it?)

Unlike Lee's changes, I don't set change the prompt to go to stderr
when -i is given.
üst 7433b12a
...@@ -59,22 +59,22 @@ static char *argv0; ...@@ -59,22 +59,22 @@ static char *argv0;
static char **orig_argv; static char **orig_argv;
static int orig_argc; static int orig_argc;
/* Short usage message (with %s for argv0) */ /* Short usage message (with %s for argv0) */
static char *usage_line = static char *usage_line =
"usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n"; "usage: %s [-d] [-i] [-s] [-u] [-v] [-c cmd | file | -] [arg] ...\n";
/* Long usage message, split into parts < 512 bytes */ /* Long usage message, split into parts < 512 bytes */
static char *usage_top = "\n\ static char *usage_top = "\n\
Options and arguments (and corresponding environment variables):\n\ Options and arguments (and corresponding environment variables):\n\
-d : debug output from parser (also PYTHONDEBUG=x)\n\ -d : debug output from parser (also PYTHONDEBUG=x)\n\
-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\ -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
and force prompts, even if stdin does not appear to be a terminal.\n\
-s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\ -s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
-u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\ -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
-c cmd : program passed in as string (terminates option list)\n\
"; ";
static char *usage_bot = "\ static char *usage_bot = "\
-c cmd : program passed in as string (terminates option list)\n\
file : program read from script file\n\ file : program read from script file\n\
- : program read from stdin (default; interactive mode if a tty)\n\ - : program read from stdin (default; interactive mode if a tty)\n\
arg ...: arguments passed to program in sys.argv[1:]\n\ arg ...: arguments passed to program in sys.argv[1:]\n\
...@@ -101,6 +101,7 @@ main(argc, argv) ...@@ -101,6 +101,7 @@ main(argc, argv)
char *p; char *p;
int inspect = 0; int inspect = 0;
int unbuffered = 0; int unbuffered = 0;
int stdin_is_interactive = 0;
orig_argc = argc; /* For Py_GetArgcArgv() */ orig_argc = argc; /* For Py_GetArgcArgv() */
orig_argv = argv; orig_argv = argv;
...@@ -139,6 +140,7 @@ main(argc, argv) ...@@ -139,6 +140,7 @@ main(argc, argv)
case 'i': case 'i':
inspect++; inspect++;
Py_InteractiveFlag++;
break; break;
case 's': case 's':
...@@ -165,38 +167,48 @@ main(argc, argv) ...@@ -165,38 +167,48 @@ main(argc, argv)
} }
} }
if (command == NULL && optind < argc &&
strcmp(argv[optind], "-") != 0)
{
filename = argv[optind];
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "%s: can't open file '%s'\n",
argv[0], filename);
exit(2);
}
}
}
stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
if (unbuffered) { if (unbuffered) {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
_setmode(fileno(stdin), O_BINARY); _setmode(fileno(stdin), O_BINARY);
_setmode(fileno(stdout), O_BINARY); _setmode(fileno(stdout), O_BINARY);
#endif #endif
#ifndef MPW #ifndef MPW
setbuf(stdout, (char *)NULL); setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
setbuf(stderr, (char *)NULL); setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
#else #else
/* On MPW (3.2) unbuffered seems to hang */ /* On MPW (3.2) unbuffered seems to hang */
setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
#endif #endif
} }
else if (stdin_is_interactive) {
if (command == NULL && optind < argc && setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
strcmp(argv[optind], "-") != 0) setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
filename = argv[optind]; /* Leave stderr alone - it should be unbuffered anyway. */
}
if (Py_VerboseFlag || if (Py_VerboseFlag ||
(command == NULL && filename == NULL && isatty((int)fileno(fp)))) command == NULL && filename == NULL && stdin_is_interactive)
fprintf(stderr, "Python %s\n%s\n", fprintf(stderr, "Python %s\n%s\n",
Py_GetVersion(), Py_GetCopyright()); Py_GetVersion(), Py_GetCopyright());
if (filename != NULL) {
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "%s: can't open file '%s'\n",
argv[0], filename);
exit(2);
}
}
Py_Initialize(); Py_Initialize();
if (command != NULL) { if (command != NULL) {
...@@ -211,7 +223,7 @@ main(argc, argv) ...@@ -211,7 +223,7 @@ main(argc, argv)
sts = PyRun_SimpleString(command) != 0; sts = PyRun_SimpleString(command) != 0;
} }
else { else {
if (filename == NULL && isatty((int)fileno(fp))) { if (filename == NULL && stdin_is_interactive) {
char *startup = getenv("PYTHONSTARTUP"); char *startup = getenv("PYTHONSTARTUP");
if (startup != NULL && startup[0] != '\0') { if (startup != NULL && startup[0] != '\0') {
FILE *fp = fopen(startup, "r"); FILE *fp = fopen(startup, "r");
...@@ -223,12 +235,13 @@ main(argc, argv) ...@@ -223,12 +235,13 @@ main(argc, argv)
} }
} }
sts = PyRun_AnyFile( sts = PyRun_AnyFile(
fp, filename == NULL ? "<stdin>" : filename) != 0; fp,
filename == NULL ? "<stdin>" : filename) != 0;
if (filename != NULL) if (filename != NULL)
fclose(fp); fclose(fp);
} }
if (inspect && isatty((int)fileno(stdin)) && if (inspect && stdin_is_interactive &&
(filename != NULL || command != NULL)) (filename != NULL || command != NULL))
sts = PyRun_AnyFile(stdin, "<stdin>") != 0; sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment