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