blob: 68a82c7be372f499870ae32a995633700ec35f2b [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
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000091static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000092usage(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 */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000108 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000109 }
110 else {
111 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000112 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000113 }
114#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000115 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000116#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000117 /*NOTREACHED*/
118}
119
Martin v. Löwis6caea372003-11-18 19:46:25 +0000120static void RunStartupFile(PyCompilerFlags *cf)
121{
122 char *startup = Py_GETENV("PYTHONSTARTUP");
123 if (startup != NULL && startup[0] != '\0') {
124 FILE *fp = fopen(startup, "r");
125 if (fp != NULL) {
126 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
127 PyErr_Clear();
128 fclose(fp);
129 }
130 }
131}
132
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000133
Guido van Rossum667d7041995-08-04 04:20:48 +0000134/* Main program */
135
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000136int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000137Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000138{
139 int c;
140 int sts;
141 char *command = NULL;
142 char *filename = NULL;
143 FILE *fp = stdin;
144 char *p;
145 int inspect = 0;
146 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000147 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000148 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000149 int help = 0;
150 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000151 int saw_inspect_flag = 0;
152 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000153 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000154
Guido van Rossum393661d2001-08-31 17:40:15 +0000155 cf.cf_flags = 0;
156
Guido van Rossumac56b031996-07-21 02:33:38 +0000157 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000158 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000159
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000160#ifdef RISCOS
161 Py_RISCOSWimpFlag = 0;
162#endif
163
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000164 PySys_ResetWarnOptions();
165
Guido van Rossumbceccf52001-04-10 22:07:43 +0000166 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000167 if (c == 'c') {
168 /* -c is the last option; following arguments
169 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000170 command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000171 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000172 if (command == NULL)
173 Py_FatalError(
174 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000175 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000176 strcat(command, "\n");
177 break;
178 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000179
Guido van Rossum667d7041995-08-04 04:20:48 +0000180 switch (c) {
181
182 case 'd':
183 Py_DebugFlag++;
184 break;
185
Guido van Rossum61c345f2001-09-04 03:26:15 +0000186 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000187 if (strcmp(_PyOS_optarg, "old") == 0) {
188 Py_DivisionWarningFlag = 0;
189 break;
190 }
191 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000192 Py_DivisionWarningFlag = 1;
193 break;
194 }
195 if (strcmp(_PyOS_optarg, "warnall") == 0) {
196 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000197 break;
198 }
199 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000200 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000201 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000202 /* And this tells the eval loop to treat
203 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
204 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000205 break;
206 }
207 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000208 "-Q option should be `-Qold', "
209 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000210 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000211 /* NOTREACHED */
212
Guido van Rossum667d7041995-08-04 04:20:48 +0000213 case 'i':
214 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000215 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000216 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 break;
218
Guido van Rossum7614da61997-03-03 19:14:45 +0000219 case 'O':
220 Py_OptimizeFlag++;
221 break;
222
Guido van Rossum7922bd71997-08-29 22:34:47 +0000223 case 'S':
224 Py_NoSiteFlag++;
225 break;
226
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000227 case 'E':
228 Py_IgnoreEnvironmentFlag++;
229 break;
230
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000231 case 't':
232 Py_TabcheckFlag++;
233 break;
234
Guido van Rossum667d7041995-08-04 04:20:48 +0000235 case 'u':
236 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000237 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000238 break;
239
240 case 'v':
241 Py_VerboseFlag++;
242 break;
243
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000244#ifdef RISCOS
245 case 'w':
246 Py_RISCOSWimpFlag = 1;
247 break;
248#endif
249
Guido van Rossuma075ce11997-12-05 21:56:45 +0000250 case 'x':
251 skipfirstline = 1;
252 break;
253
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000254 case 'U':
255 Py_UnicodeFlag++;
256 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000257 case 'h':
258 help++;
259 break;
260 case 'V':
261 version++;
262 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000263
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000264 case 'W':
265 PySys_AddWarnOption(_PyOS_optarg);
266 break;
267
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 /* This space reserved for other options */
269
270 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000271 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 /*NOTREACHED*/
273
274 }
275 }
276
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000277 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000278 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000279
280 if (version) {
281 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000282 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000283 }
284
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000285 if (!saw_inspect_flag &&
286 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
287 inspect = 1;
288 if (!saw_unbuffered_flag &&
289 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
290 unbuffered = 1;
291
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000292 if (command == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000293 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000294 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000295#ifdef __VMS
296 filename = decc$translate_vms(argv[_PyOS_optind]);
297 if (filename == (char *)0 || filename == (char *)-1)
298 filename = argv[_PyOS_optind];
299
300#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000301 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000302#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000303 if (filename != NULL) {
304 if ((fp = fopen(filename, "r")) == NULL) {
305 fprintf(stderr, "%s: can't open file '%s'\n",
306 argv[0], filename);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000307 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000308 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000309 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000310 int ch;
311 /* Push back first newline so line numbers
312 remain the same */
313 while ((ch = getc(fp)) != EOF) {
314 if (ch == '\n') {
315 (void)ungetc(ch, fp);
316 break;
317 }
318 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000319 }
Guido van Rossum775af911997-02-14 19:50:32 +0000320 }
321 }
322
323 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
324
Guido van Rossum667d7041995-08-04 04:20:48 +0000325 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000326#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000327 _setmode(fileno(stdin), O_BINARY);
328 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000329#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000330#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000331 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
332 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
333 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000334#else /* !HAVE_SETVBUF */
335 setbuf(stdin, (char *)NULL);
336 setbuf(stdout, (char *)NULL);
337 setbuf(stderr, (char *)NULL);
338#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000339 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000340 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000341#ifdef MS_WINDOWS
342 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000343 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000344 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000345#else /* !MS_WINDOWS */
346#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000347 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
348 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000349#endif /* HAVE_SETVBUF */
350#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000351 /* Leave stderr alone - it should be unbuffered anyway. */
352 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000353#ifdef __VMS
354 else {
355 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
356 }
357#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000358
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000359#ifdef __APPLE__
360 /* On MacOS X, when the Python interpreter is embedded in an
361 application bundle, it gets executed by a bootstrapping script
362 that does os.execve() with an argv[0] that's different from the
363 actual Python executable. This is needed to keep the Finder happy,
364 or rather, to work around Apple's overly strict requirements of
365 the process name. However, we still need a usable sys.executable,
366 so the actual executable path is passed in an environment variable.
367 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
368 script. */
369 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
370 Py_SetProgramName(p);
371 else
372 Py_SetProgramName(argv[0]);
373#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000374 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000375#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000376 Py_Initialize();
377
Guido van Rossum667d7041995-08-04 04:20:48 +0000378 if (Py_VerboseFlag ||
Martin v. Löwise98922f2003-03-30 17:00:39 +0000379 (command == NULL && filename == NULL && stdin_is_interactive)) {
380 fprintf(stderr, "Python %s on %s\n",
381 Py_GetVersion(), Py_GetPlatform());
382 if (!Py_NoSiteFlag)
383 fprintf(stderr, "%s\n", COPYRIGHT);
384 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000385
Guido van Rossum667d7041995-08-04 04:20:48 +0000386 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000387 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
388 _PyOS_optind--;
389 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000390 }
391
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000392 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000393
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000394 if ((inspect || (command == NULL && filename == NULL)) &&
395 isatty(fileno(stdin))) {
396 PyObject *v;
397 v = PyImport_ImportModule("readline");
398 if (v == NULL)
399 PyErr_Clear();
400 else
401 Py_DECREF(v);
402 }
403
Guido van Rossum667d7041995-08-04 04:20:48 +0000404 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000405 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000406 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000407 }
408 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000409 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000410 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000411 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000412 /* XXX */
413 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000414 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000415 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000416 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000417 }
418
Barry Warsawd86dcd32003-06-29 17:07:06 +0000419 /* Check this environment variable at the end, to give programs the
420 * opportunity to set it from Python.
421 */
422 if (!saw_inspect_flag &&
423 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
424 {
425 inspect = 1;
426 }
427
Guido van Rossum775af911997-02-14 19:50:32 +0000428 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000429 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000430 /* XXX */
431 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000432
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000433 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000434#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000435 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000436 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
437#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000438
439#ifdef __INSURE__
440 /* Insure++ is a memory analysis tool that aids in discovering
441 * memory leaks and other memory problems. On Python exit, the
442 * interned string dictionary is flagged as being in use at exit
443 * (which it is). Under normal circumstances, this is fine because
444 * the memory will be automatically reclaimed by the system. Under
445 * memory debugging, it's a huge source of useless noise, so we
446 * trade off slower shutdown for less distraction in the memory
447 * reports. -baw
448 */
449 _Py_ReleaseInternedStrings();
450#endif /* __INSURE__ */
451
Guido van Rossum05f7c501997-08-02 03:00:42 +0000452 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000453}
454
455
Guido van Rossum667d7041995-08-04 04:20:48 +0000456/* Make the *original* argc/argv available to other modules.
457 This is rare, but it is needed by the secureware extension. */
458
459void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000460Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000461{
462 *argc = orig_argc;
463 *argv = orig_argv;
464}