blob: 80c50b008304e73352dc82663f57acbc6004aff6 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Guido van Rossum393661d2001-08-31 17:40:15 +00005#include "compile.h" /* For CO_FUTURE_DIVISION */
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00007#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#endif
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000012#include <fcntl.h>
13#endif
14
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000015#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000016#define PYTHONHOMEHELP "<prefix>\\lib"
17#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#if defined(PYOS_OS2) && defined(PYCC_GCC)
19#define PYTHONHOMEHELP "<prefix>/Lib"
20#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000021#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000022#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000023#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000024
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000025#include "pygetopt.h"
26
Guido van Rossuma22865e2000-09-05 04:41:18 +000027#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000028 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
29 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000030
Guido van Rossumac56b031996-07-21 02:33:38 +000031/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000032static char **orig_argv;
33static int orig_argc;
34
Guido van Rossumbceccf52001-04-10 22:07:43 +000035/* command line options */
Guido van Rossum61c345f2001-09-04 03:26:15 +000036#define BASE_OPTS "c:dEhiOQ:StuUvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000037
38#ifndef RISCOS
39#define PROGRAM_OPTS BASE_OPTS
40#else /*RISCOS*/
41/* extra option saying that we are running under a special task window
42 frontend; especially my_readline will behave different */
43#define PROGRAM_OPTS BASE_OPTS "w"
44/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000045extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000046#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000047
Guido van Rossum667d7041995-08-04 04:20:48 +000048/* Short usage message (with %s for argv0) */
49static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000050"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000051
52/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000053static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000054Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000055-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000056-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000057-E : ignore environment variables (such as PYTHONPATH)\n\
58-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000059-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000060 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000061";
62static char *usage_2 = "\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000063-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000064-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000065-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000066-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000067-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000068-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Neal Norwitzce233b42002-07-28 13:53:05 +000069 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000070";
71static char *usage_3 = "\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000073-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000074-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076file : program read from script file\n\
77- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000078";
Guido van Rossum393661d2001-08-31 17:40:15 +000079static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000080arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000081Other environment variables:\n\
82PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000083PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000084 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000085PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
86 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000087PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088";
89
90
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000091static void
92usage(int exitcode, char* program)
93{
Guido van Rossum393661d2001-08-31 17:40:15 +000094 FILE *f = exitcode ? stderr : stdout;
95
96 fprintf(f, usage_line, program);
97 if (exitcode)
98 fprintf(f, "Try `python -h' for more information.\n");
99 else {
100 fprintf(f, usage_1);
101 fprintf(f, usage_2);
102 fprintf(f, usage_3);
103 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
104 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000105#if defined(__VMS)
106 if (exitcode == 0) {
107 /* suppress 'error' message */
108 exit(1);
109 }
110 else {
111 /* STS$M_INHIB_MSG + SS$_ABORT */
112 exit(0x1000002c);
113 }
114#else
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000115 exit(exitcode);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000116#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000117 /*NOTREACHED*/
118}
119
120
Guido van Rossum667d7041995-08-04 04:20:48 +0000121/* Main program */
122
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000123int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000124Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000125{
126 int c;
127 int sts;
128 char *command = NULL;
129 char *filename = NULL;
130 FILE *fp = stdin;
131 char *p;
132 int inspect = 0;
133 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000134 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000135 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000136 int help = 0;
137 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000138 int saw_inspect_flag = 0;
139 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000140 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000141
Guido van Rossum393661d2001-08-31 17:40:15 +0000142 cf.cf_flags = 0;
143
Guido van Rossumac56b031996-07-21 02:33:38 +0000144 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000145 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000146
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000147#ifdef RISCOS
148 Py_RISCOSWimpFlag = 0;
149#endif
150
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000151 PySys_ResetWarnOptions();
152
Guido van Rossumbceccf52001-04-10 22:07:43 +0000153 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000154 if (c == 'c') {
155 /* -c is the last option; following arguments
156 that look like options are left for the
157 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000158 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000159 if (command == NULL)
160 Py_FatalError(
161 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000162 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000163 strcat(command, "\n");
164 break;
165 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000166
Guido van Rossum667d7041995-08-04 04:20:48 +0000167 switch (c) {
168
169 case 'd':
170 Py_DebugFlag++;
171 break;
172
Guido van Rossum61c345f2001-09-04 03:26:15 +0000173 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000174 if (strcmp(_PyOS_optarg, "old") == 0) {
175 Py_DivisionWarningFlag = 0;
176 break;
177 }
178 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000179 Py_DivisionWarningFlag = 1;
180 break;
181 }
182 if (strcmp(_PyOS_optarg, "warnall") == 0) {
183 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000184 break;
185 }
186 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000187 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000188 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000189 /* And this tells the eval loop to treat
190 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
191 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000192 break;
193 }
194 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000195 "-Q option should be `-Qold', "
196 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Guido van Rossum393661d2001-08-31 17:40:15 +0000197 usage(2, argv[0]);
198 /* NOTREACHED */
199
Guido van Rossum667d7041995-08-04 04:20:48 +0000200 case 'i':
201 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000202 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000203 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000204 break;
205
Guido van Rossum7614da61997-03-03 19:14:45 +0000206 case 'O':
207 Py_OptimizeFlag++;
208 break;
209
Guido van Rossum7922bd71997-08-29 22:34:47 +0000210 case 'S':
211 Py_NoSiteFlag++;
212 break;
213
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000214 case 'E':
215 Py_IgnoreEnvironmentFlag++;
216 break;
217
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000218 case 't':
219 Py_TabcheckFlag++;
220 break;
221
Guido van Rossum667d7041995-08-04 04:20:48 +0000222 case 'u':
223 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000224 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000225 break;
226
227 case 'v':
228 Py_VerboseFlag++;
229 break;
230
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000231#ifdef RISCOS
232 case 'w':
233 Py_RISCOSWimpFlag = 1;
234 break;
235#endif
236
Guido van Rossuma075ce11997-12-05 21:56:45 +0000237 case 'x':
238 skipfirstline = 1;
239 break;
240
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000241 case 'U':
242 Py_UnicodeFlag++;
243 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000244 case 'h':
245 help++;
246 break;
247 case 'V':
248 version++;
249 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000250
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000251 case 'W':
252 PySys_AddWarnOption(_PyOS_optarg);
253 break;
254
Guido van Rossum667d7041995-08-04 04:20:48 +0000255 /* This space reserved for other options */
256
257 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000258 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 /*NOTREACHED*/
260
261 }
262 }
263
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000264 if (help)
265 usage(0, argv[0]);
266
267 if (version) {
268 fprintf(stderr, "Python %s\n", PY_VERSION);
269 exit(0);
270 }
271
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000272 if (!saw_inspect_flag &&
273 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
274 inspect = 1;
275 if (!saw_unbuffered_flag &&
276 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
277 unbuffered = 1;
278
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000279 if (command == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000280 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000281 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000282#ifdef __VMS
283 filename = decc$translate_vms(argv[_PyOS_optind]);
284 if (filename == (char *)0 || filename == (char *)-1)
285 filename = argv[_PyOS_optind];
286
287#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000288 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000289#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000290 if (filename != NULL) {
291 if ((fp = fopen(filename, "r")) == NULL) {
292 fprintf(stderr, "%s: can't open file '%s'\n",
293 argv[0], filename);
294 exit(2);
295 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000296 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000297 int ch;
298 /* Push back first newline so line numbers
299 remain the same */
300 while ((ch = getc(fp)) != EOF) {
301 if (ch == '\n') {
302 (void)ungetc(ch, fp);
303 break;
304 }
305 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000306 }
Guido van Rossum775af911997-02-14 19:50:32 +0000307 }
308 }
309
310 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
311
Guido van Rossum667d7041995-08-04 04:20:48 +0000312 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000313#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000314 _setmode(fileno(stdin), O_BINARY);
315 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000316#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000317#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000318#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000319 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
320 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
321 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000322#else /* !HAVE_SETVBUF */
323 setbuf(stdin, (char *)NULL);
324 setbuf(stdout, (char *)NULL);
325 setbuf(stderr, (char *)NULL);
326#endif /* !HAVE_SETVBUF */
327#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000328 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000329 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000330 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
331 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000332#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000333 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000334 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000335#ifdef MS_WINDOWS
336 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000337 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000338 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000339#else /* !MS_WINDOWS */
340#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000341 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
342 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000343#endif /* HAVE_SETVBUF */
344#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000345 /* Leave stderr alone - it should be unbuffered anyway. */
346 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000347#ifdef __VMS
348 else {
349 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
350 }
351#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000352
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000353#ifdef __APPLE__
354 /* On MacOS X, when the Python interpreter is embedded in an
355 application bundle, it gets executed by a bootstrapping script
356 that does os.execve() with an argv[0] that's different from the
357 actual Python executable. This is needed to keep the Finder happy,
358 or rather, to work around Apple's overly strict requirements of
359 the process name. However, we still need a usable sys.executable,
360 so the actual executable path is passed in an environment variable.
361 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
362 script. */
363 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
364 Py_SetProgramName(p);
365 else
366 Py_SetProgramName(argv[0]);
367#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000368 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000369#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000370 Py_Initialize();
371
Guido van Rossum667d7041995-08-04 04:20:48 +0000372 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000373 (command == NULL && filename == NULL && stdin_is_interactive))
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000374 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000375 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Guido van Rossum393661d2001-08-31 17:40:15 +0000376
Guido van Rossum667d7041995-08-04 04:20:48 +0000377 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000378 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
379 _PyOS_optind--;
380 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000381 }
382
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000383 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000384
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000385 if ((inspect || (command == NULL && filename == NULL)) &&
386 isatty(fileno(stdin))) {
387 PyObject *v;
388 v = PyImport_ImportModule("readline");
389 if (v == NULL)
390 PyErr_Clear();
391 else
392 Py_DECREF(v);
393 }
394
Guido van Rossum667d7041995-08-04 04:20:48 +0000395 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000396 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000397 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 }
399 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000400 if (filename == NULL && stdin_is_interactive) {
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000401 char *startup = Py_GETENV("PYTHONSTARTUP");
Guido van Rossum667d7041995-08-04 04:20:48 +0000402 if (startup != NULL && startup[0] != '\0') {
403 FILE *fp = fopen(startup, "r");
404 if (fp != NULL) {
405 (void) PyRun_SimpleFile(fp, startup);
406 PyErr_Clear();
407 fclose(fp);
408 }
409 }
410 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000411 /* XXX */
412 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000413 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000414 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000415 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000416 }
417
Guido van Rossum775af911997-02-14 19:50:32 +0000418 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000419 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000420 /* XXX */
421 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000422
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000423 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000424#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000425 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000426 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
427#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000428
429#ifdef __INSURE__
430 /* Insure++ is a memory analysis tool that aids in discovering
431 * memory leaks and other memory problems. On Python exit, the
432 * interned string dictionary is flagged as being in use at exit
433 * (which it is). Under normal circumstances, this is fine because
434 * the memory will be automatically reclaimed by the system. Under
435 * memory debugging, it's a huge source of useless noise, so we
436 * trade off slower shutdown for less distraction in the memory
437 * reports. -baw
438 */
439 _Py_ReleaseInternedStrings();
440#endif /* __INSURE__ */
441
Guido van Rossum05f7c501997-08-02 03:00:42 +0000442 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000443}
444
445
Guido van Rossum667d7041995-08-04 04:20:48 +0000446/* Make the *original* argc/argv available to other modules.
447 This is rare, but it is needed by the secureware extension. */
448
449void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000450Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000451{
452 *argc = orig_argc;
453 *argv = orig_argv;
454}