Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
| 3 | #include "Python.h" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 4 | #include "osdefs.h" |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 5 | #include "compile.h" /* For CO_FUTURE_DIVISION */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 7 | #ifdef MS_WINDOWS |
| 8 | #include <fcntl.h> |
| 9 | #endif |
| 10 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 11 | #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS) |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 12 | #define PYTHONHOMEHELP "<prefix>\\lib" |
| 13 | #else |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 14 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 15 | #define PYTHONHOMEHELP "<prefix>/Lib" |
| 16 | #else |
Marc-André Lemburg | da4dbc3 | 2001-06-12 16:13:51 +0000 | [diff] [blame] | 17 | #define PYTHONHOMEHELP "<prefix>/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 18 | #endif |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 21 | #include "pygetopt.h" |
| 22 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 23 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 24 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 25 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 27 | /* For Py_GetArgcArgv(); set by main() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 28 | static char **orig_argv; |
| 29 | static int orig_argc; |
| 30 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 31 | /* command line options */ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 32 | #define BASE_OPTS "c:dEhiOQ:StuUvVW:xX" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 33 | |
| 34 | #ifndef RISCOS |
| 35 | #define PROGRAM_OPTS BASE_OPTS |
| 36 | #else /*RISCOS*/ |
| 37 | /* extra option saying that we are running under a special task window |
| 38 | frontend; especially my_readline will behave different */ |
| 39 | #define PROGRAM_OPTS BASE_OPTS "w" |
| 40 | /* corresponding flag */ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 41 | extern int Py_RISCOSWimpFlag; |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 42 | #endif /*RISCOS*/ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 44 | /* Short usage message (with %s for argv0) */ |
| 45 | static char *usage_line = |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 46 | "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 47 | |
| 48 | /* Long usage message, split into parts < 512 bytes */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 49 | static char *usage_1 = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 50 | Options and arguments (and corresponding environment variables):\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 51 | -c cmd : program passed in as string (terminates option list)\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 52 | -d : debug output from parser (also PYTHONDEBUG=x)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 53 | -E : ignore environment variables (such as PYTHONPATH)\n\ |
| 54 | -h : print this help message and exit\n\ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 55 | -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 56 | and force prompts, even if stdin does not appear to be a terminal\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 57 | "; |
| 58 | static char *usage_2 = "\ |
Guido van Rossum | e7adf3e | 1998-10-07 14:50:06 +0000 | [diff] [blame] | 59 | -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\ |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 60 | -OO : remove doc-strings in addition to the -O optimizations\n\ |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 61 | -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 62 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 63 | -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 64 | -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 65 | "; |
| 66 | static char *usage_3 = "\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 67 | -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 68 | -V : print the Python version number and exit\n\ |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 69 | -W arg : warning control (arg is action:message:category:module:lineno)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 70 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 71 | file : program read from script file\n\ |
| 72 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 73 | "; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 74 | static char *usage_4 = "\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 75 | arg ...: arguments passed to program in sys.argv[1:]\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 76 | Other environment variables:\n\ |
| 77 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 78 | PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 79 | default module search path. The result is sys.path.\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 80 | PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\ |
| 81 | The default module search path uses %s.\n\ |
Tim Peters | 793de09 | 2001-02-22 00:39:47 +0000 | [diff] [blame] | 82 | PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 83 | "; |
| 84 | |
| 85 | |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 86 | static void |
| 87 | usage(int exitcode, char* program) |
| 88 | { |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 89 | FILE *f = exitcode ? stderr : stdout; |
| 90 | |
| 91 | fprintf(f, usage_line, program); |
| 92 | if (exitcode) |
| 93 | fprintf(f, "Try `python -h' for more information.\n"); |
| 94 | else { |
| 95 | fprintf(f, usage_1); |
| 96 | fprintf(f, usage_2); |
| 97 | fprintf(f, usage_3); |
| 98 | fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP); |
| 99 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 100 | exit(exitcode); |
| 101 | /*NOTREACHED*/ |
| 102 | } |
| 103 | |
| 104 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 105 | /* Main program */ |
| 106 | |
Guido van Rossum | 9c1201f | 1998-12-07 14:28:47 +0000 | [diff] [blame] | 107 | DL_EXPORT(int) |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 108 | Py_Main(int argc, char **argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 109 | { |
| 110 | int c; |
| 111 | int sts; |
| 112 | char *command = NULL; |
| 113 | char *filename = NULL; |
| 114 | FILE *fp = stdin; |
| 115 | char *p; |
| 116 | int inspect = 0; |
| 117 | int unbuffered = 0; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 118 | int skipfirstline = 0; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 119 | int stdin_is_interactive = 0; |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 120 | int help = 0; |
| 121 | int version = 0; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 122 | int saw_inspect_flag = 0; |
| 123 | int saw_unbuffered_flag = 0; |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 124 | PyCompilerFlags cf; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 126 | cf.cf_flags = 0; |
| 127 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 128 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 129 | orig_argv = argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 131 | #ifdef RISCOS |
| 132 | Py_RISCOSWimpFlag = 0; |
| 133 | #endif |
| 134 | |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 135 | PySys_ResetWarnOptions(); |
| 136 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 137 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 138 | if (c == 'c') { |
| 139 | /* -c is the last option; following arguments |
| 140 | that look like options are left for the |
| 141 | the command to interpret. */ |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 142 | command = malloc(strlen(_PyOS_optarg) + 2); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 143 | if (command == NULL) |
| 144 | Py_FatalError( |
| 145 | "not enough memory to copy -c argument"); |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 146 | strcpy(command, _PyOS_optarg); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 147 | strcat(command, "\n"); |
| 148 | break; |
| 149 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 151 | switch (c) { |
| 152 | |
| 153 | case 'd': |
| 154 | Py_DebugFlag++; |
| 155 | break; |
| 156 | |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 157 | case 'Q': |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 158 | if (strcmp(_PyOS_optarg, "old") == 0) { |
| 159 | Py_DivisionWarningFlag = 0; |
| 160 | break; |
| 161 | } |
| 162 | if (strcmp(_PyOS_optarg, "warn") == 0) { |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 163 | Py_DivisionWarningFlag = 1; |
| 164 | break; |
| 165 | } |
| 166 | if (strcmp(_PyOS_optarg, "warnall") == 0) { |
| 167 | Py_DivisionWarningFlag = 2; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | if (strcmp(_PyOS_optarg, "new") == 0) { |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 171 | /* This only affects __main__ */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 172 | cf.cf_flags |= CO_FUTURE_DIVISION; |
Tim Peters | 3caca23 | 2001-12-06 06:23:26 +0000 | [diff] [blame] | 173 | /* And this tells the eval loop to treat |
| 174 | BINARY_DIVIDE as BINARY_TRUE_DIVIDE */ |
| 175 | _Py_QnewFlag = 1; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | fprintf(stderr, |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 179 | "-Q option should be `-Qold', " |
| 180 | "`-Qwarn', `-Qwarnall', or `-Qnew' only\n"); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 181 | usage(2, argv[0]); |
| 182 | /* NOTREACHED */ |
| 183 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 184 | case 'i': |
| 185 | inspect++; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 186 | saw_inspect_flag = 1; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 187 | Py_InteractiveFlag++; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 188 | break; |
| 189 | |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 190 | case 'O': |
| 191 | Py_OptimizeFlag++; |
| 192 | break; |
| 193 | |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 194 | case 'S': |
| 195 | Py_NoSiteFlag++; |
| 196 | break; |
| 197 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 198 | case 'E': |
| 199 | Py_IgnoreEnvironmentFlag++; |
| 200 | break; |
| 201 | |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 202 | case 't': |
| 203 | Py_TabcheckFlag++; |
| 204 | break; |
| 205 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 206 | case 'u': |
| 207 | unbuffered++; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 208 | saw_unbuffered_flag = 1; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | case 'v': |
| 212 | Py_VerboseFlag++; |
| 213 | break; |
| 214 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 215 | #ifdef RISCOS |
| 216 | case 'w': |
| 217 | Py_RISCOSWimpFlag = 1; |
| 218 | break; |
| 219 | #endif |
| 220 | |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 221 | case 'x': |
| 222 | skipfirstline = 1; |
| 223 | break; |
| 224 | |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 225 | case 'U': |
| 226 | Py_UnicodeFlag++; |
| 227 | break; |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 228 | case 'h': |
| 229 | help++; |
| 230 | break; |
| 231 | case 'V': |
| 232 | version++; |
| 233 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 235 | case 'W': |
| 236 | PySys_AddWarnOption(_PyOS_optarg); |
| 237 | break; |
| 238 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 239 | /* This space reserved for other options */ |
| 240 | |
| 241 | default: |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 242 | usage(2, argv[0]); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 243 | /*NOTREACHED*/ |
| 244 | |
| 245 | } |
| 246 | } |
| 247 | |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 248 | if (help) |
| 249 | usage(0, argv[0]); |
| 250 | |
| 251 | if (version) { |
| 252 | fprintf(stderr, "Python %s\n", PY_VERSION); |
| 253 | exit(0); |
| 254 | } |
| 255 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 256 | if (!saw_inspect_flag && |
| 257 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 258 | inspect = 1; |
| 259 | if (!saw_unbuffered_flag && |
| 260 | (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 261 | unbuffered = 1; |
| 262 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 263 | if (command == NULL && _PyOS_optind < argc && |
| 264 | strcmp(argv[_PyOS_optind], "-") != 0) |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 265 | { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 266 | filename = argv[_PyOS_optind]; |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 267 | if (filename != NULL) { |
| 268 | if ((fp = fopen(filename, "r")) == NULL) { |
| 269 | fprintf(stderr, "%s: can't open file '%s'\n", |
| 270 | argv[0], filename); |
| 271 | exit(2); |
| 272 | } |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 273 | else if (skipfirstline) { |
Guido van Rossum | dc8b569 | 1999-04-19 17:54:19 +0000 | [diff] [blame] | 274 | int ch; |
| 275 | /* Push back first newline so line numbers |
| 276 | remain the same */ |
| 277 | while ((ch = getc(fp)) != EOF) { |
| 278 | if (ch == '\n') { |
| 279 | (void)ungetc(ch, fp); |
| 280 | break; |
| 281 | } |
| 282 | } |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 283 | } |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
| 288 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 289 | if (unbuffered) { |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 290 | #ifdef MS_WINDOWS |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 291 | _setmode(fileno(stdin), O_BINARY); |
| 292 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 293 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 294 | #ifndef MPW |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 295 | #ifdef HAVE_SETVBUF |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 296 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 297 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 298 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 299 | #else /* !HAVE_SETVBUF */ |
| 300 | setbuf(stdin, (char *)NULL); |
| 301 | setbuf(stdout, (char *)NULL); |
| 302 | setbuf(stderr, (char *)NULL); |
| 303 | #endif /* !HAVE_SETVBUF */ |
| 304 | #else /* MPW */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 305 | /* On MPW (3.2) unbuffered seems to hang */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 306 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 307 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 308 | setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 309 | #endif /* MPW */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 310 | } |
Guido van Rossum | 2a21219 | 1997-04-11 21:57:53 +0000 | [diff] [blame] | 311 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 312 | #ifdef MS_WINDOWS |
| 313 | /* Doesn't have to have line-buffered -- use unbuffered */ |
Guido van Rossum | 01b7ced | 1999-02-09 18:36:51 +0000 | [diff] [blame] | 314 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 315 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 316 | #else /* !MS_WINDOWS */ |
| 317 | #ifdef HAVE_SETVBUF |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 318 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 319 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 320 | #endif /* HAVE_SETVBUF */ |
| 321 | #endif /* !MS_WINDOWS */ |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 322 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 323 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 325 | Py_SetProgramName(argv[0]); |
| 326 | Py_Initialize(); |
| 327 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 328 | if (Py_VerboseFlag || |
Guido van Rossum | 129e91a | 1997-02-14 21:00:50 +0000 | [diff] [blame] | 329 | (command == NULL && filename == NULL && stdin_is_interactive)) |
Guido van Rossum | fe4dfc7 | 1997-05-19 18:33:01 +0000 | [diff] [blame] | 330 | fprintf(stderr, "Python %s on %s\n%s\n", |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 331 | Py_GetVersion(), Py_GetPlatform(), COPYRIGHT); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 333 | if (command != NULL) { |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 334 | /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ |
| 335 | _PyOS_optind--; |
| 336 | argv[_PyOS_optind] = "-c"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 339 | PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 3d26cc9 | 1997-09-16 16:11:28 +0000 | [diff] [blame] | 341 | if ((inspect || (command == NULL && filename == NULL)) && |
| 342 | isatty(fileno(stdin))) { |
| 343 | PyObject *v; |
| 344 | v = PyImport_ImportModule("readline"); |
| 345 | if (v == NULL) |
| 346 | PyErr_Clear(); |
| 347 | else |
| 348 | Py_DECREF(v); |
| 349 | } |
| 350 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 351 | if (command) { |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 352 | sts = PyRun_SimpleStringFlags(command, &cf) != 0; |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 353 | free(command); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 354 | } |
| 355 | else { |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 356 | if (filename == NULL && stdin_is_interactive) { |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 357 | char *startup = Py_GETENV("PYTHONSTARTUP"); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 358 | if (startup != NULL && startup[0] != '\0') { |
| 359 | FILE *fp = fopen(startup, "r"); |
| 360 | if (fp != NULL) { |
| 361 | (void) PyRun_SimpleFile(fp, startup); |
| 362 | PyErr_Clear(); |
| 363 | fclose(fp); |
| 364 | } |
| 365 | } |
| 366 | } |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 367 | /* XXX */ |
| 368 | sts = PyRun_AnyFileExFlags( |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 369 | fp, |
Guido van Rossum | 0df002c | 2000-08-27 19:21:52 +0000 | [diff] [blame] | 370 | filename == NULL ? "<stdin>" : filename, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 371 | filename != NULL, &cf) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 374 | if (inspect && stdin_is_interactive && |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 375 | (filename != NULL || command != NULL)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 376 | /* XXX */ |
| 377 | sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | 5d1770e | 1997-08-05 02:23:48 +0000 | [diff] [blame] | 379 | Py_Finalize(); |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 380 | #ifdef RISCOS |
| 381 | if(Py_RISCOSWimpFlag) |
| 382 | fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */ |
| 383 | #endif |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 384 | |
| 385 | #ifdef __INSURE__ |
| 386 | /* Insure++ is a memory analysis tool that aids in discovering |
| 387 | * memory leaks and other memory problems. On Python exit, the |
| 388 | * interned string dictionary is flagged as being in use at exit |
| 389 | * (which it is). Under normal circumstances, this is fine because |
| 390 | * the memory will be automatically reclaimed by the system. Under |
| 391 | * memory debugging, it's a huge source of useless noise, so we |
| 392 | * trade off slower shutdown for less distraction in the memory |
| 393 | * reports. -baw |
| 394 | */ |
| 395 | _Py_ReleaseInternedStrings(); |
| 396 | #endif /* __INSURE__ */ |
| 397 | |
Guido van Rossum | 05f7c50 | 1997-08-02 03:00:42 +0000 | [diff] [blame] | 398 | return sts; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 402 | /* Make the *original* argc/argv available to other modules. |
| 403 | This is rare, but it is needed by the secureware extension. */ |
| 404 | |
| 405 | void |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 406 | Py_GetArgcArgv(int *argc, char ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 407 | { |
| 408 | *argc = orig_argc; |
| 409 | *argv = orig_argv; |
| 410 | } |