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