diff --git a/Modules/main.c b/Modules/main.c index 80c0c04..83b16a7 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -9,7 +9,7 @@ #include #endif -#if defined(MS_WINDOWS) || defined(__CYGWIN__) +#if defined(MS_WINDOWS) || defined(__CYGWIN__) || defined(__MINGW32__) #ifdef HAVE_FCNTL_H #include #endif @@ -368,6 +368,20 @@ Py_Main(int argc, char **argv) return 0; } +#if defined(__MINGW32__) + /* something weird with interactive flag - unless interactive + * flag is set when python is _supposed_ to be running in + * interactive mode, Py_FdIsInteractive(stdin) returns FALSE. + * so, we detect no command, no module, no filename, no further + * arguments, or the last argument is "-" as "interactive" + */ + if (command == NULL && module == NULL && _PyOS_optind == argc || + strcmp(argv[_PyOS_optind], "-") == 0) + { + Py_InteractiveFlag++; + } +#endif + if (!saw_inspect_flag && (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') inspect = 1; @@ -375,55 +389,8 @@ Py_Main(int argc, char **argv) (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') unbuffered = 1; - if (command == NULL && module == NULL && _PyOS_optind < argc && - strcmp(argv[_PyOS_optind], "-") != 0) - { -#ifdef __VMS - filename = decc$translate_vms(argv[_PyOS_optind]); - if (filename == (char *)0 || filename == (char *)-1) - filename = argv[_PyOS_optind]; - -#else - filename = argv[_PyOS_optind]; -#endif - if (filename != NULL) { - if ((fp = fopen(filename, "r")) == NULL) { -#ifdef HAVE_STRERROR - fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", - argv[0], filename, errno, strerror(errno)); -#else - fprintf(stderr, "%s: can't open file '%s': Errno %d\n", - argv[0], filename, errno); -#endif - return 2; - } - else if (skipfirstline) { - int ch; - /* Push back first newline so line numbers - remain the same */ - while ((ch = getc(fp)) != EOF) { - if (ch == '\n') { - (void)ungetc(ch, fp); - break; - } - } - } - { - /* XXX: does this work on Win/Win64? (see posix_fstat) */ - struct stat sb; - if (fstat(fileno(fp), &sb) == 0 && - S_ISDIR(sb.st_mode)) { - fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); - return 1; - } - } - } - } - - stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); - if (unbuffered) { -#if defined(MS_WINDOWS) || defined(__CYGWIN__) +#if defined(MS_WINDOWS) || defined(__CYGWIN__) || defined(__MINGW32__) _setmode(fileno(stdin), O_BINARY); _setmode(fileno(stdout), O_BINARY); #endif @@ -438,7 +405,7 @@ Py_Main(int argc, char **argv) #endif /* !HAVE_SETVBUF */ } else if (Py_InteractiveFlag) { -#ifdef MS_WINDOWS +#ifdef MS_WINDOWS /* Doesn't have to have line-buffered -- use unbuffered */ /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); @@ -473,8 +440,56 @@ Py_Main(int argc, char **argv) #else Py_SetProgramName(argv[0]); #endif + Py_Initialize(); + stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); + + if (command == NULL && module == NULL && _PyOS_optind < argc && + strcmp(argv[_PyOS_optind], "-") != 0) + { +#ifdef __VMS + filename = decc$translate_vms(argv[_PyOS_optind]); + if (filename == (char *)0 || filename == (char *)-1) + filename = argv[_PyOS_optind]; + +#else + filename = argv[_PyOS_optind]; +#endif + if (filename != NULL) { + if ((fp = fopen(filename, "r")) == NULL) { +#ifdef HAVE_STRERROR + fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", + argv[0], filename, errno, strerror(errno)); +#else + fprintf(stderr, "%s: can't open file '%s': Errno %d\n", + argv[0], filename, errno); +#endif + return 2; + } + else if (skipfirstline) { + int ch; + /* Push back first newline so line numbers + remain the same */ + while ((ch = getc(fp)) != EOF) { + if (ch == '\n') { + (void)ungetc(ch, fp); + break; + } + } + } + { + /* XXX: does this work on Win/Win64? (see posix_fstat) */ + struct stat sb; + if (fstat(fileno(fp), &sb) == 0 && + S_ISDIR(sb.st_mode)) { + fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); + return 1; + } + } + } + } + if (Py_VerboseFlag || (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) { fprintf(stderr, "Python %s on %s\n",