blob: bc543a4cac83734663dd1ce31936853baff99792 [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) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000305#ifdef HAVE_STRERROR
306 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
307 argv[0], filename, errno, strerror(errno));
308#else
309 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
310 argv[0], filename, errno);
311#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000312 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000313 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000314 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000315 int ch;
316 /* Push back first newline so line numbers
317 remain the same */
318 while ((ch = getc(fp)) != EOF) {
319 if (ch == '\n') {
320 (void)ungetc(ch, fp);
321 break;
322 }
323 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000324 }
Guido van Rossum775af911997-02-14 19:50:32 +0000325 }
326 }
327
328 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
329
Guido van Rossum667d7041995-08-04 04:20:48 +0000330 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000331#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000332 _setmode(fileno(stdin), O_BINARY);
333 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000334#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000335#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000336 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
337 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
338 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000339#else /* !HAVE_SETVBUF */
340 setbuf(stdin, (char *)NULL);
341 setbuf(stdout, (char *)NULL);
342 setbuf(stderr, (char *)NULL);
343#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000344 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000345 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000346#ifdef MS_WINDOWS
347 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000348 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000349 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000350#else /* !MS_WINDOWS */
351#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000352 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
353 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000354#endif /* HAVE_SETVBUF */
355#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000356 /* Leave stderr alone - it should be unbuffered anyway. */
357 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000358#ifdef __VMS
359 else {
360 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
361 }
362#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000363
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000364#ifdef __APPLE__
365 /* On MacOS X, when the Python interpreter is embedded in an
366 application bundle, it gets executed by a bootstrapping script
367 that does os.execve() with an argv[0] that's different from the
368 actual Python executable. This is needed to keep the Finder happy,
369 or rather, to work around Apple's overly strict requirements of
370 the process name. However, we still need a usable sys.executable,
371 so the actual executable path is passed in an environment variable.
372 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
373 script. */
374 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
375 Py_SetProgramName(p);
376 else
377 Py_SetProgramName(argv[0]);
378#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000379 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000380#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000381 Py_Initialize();
382
Guido van Rossum667d7041995-08-04 04:20:48 +0000383 if (Py_VerboseFlag ||
Martin v. Löwise98922f2003-03-30 17:00:39 +0000384 (command == NULL && filename == NULL && stdin_is_interactive)) {
385 fprintf(stderr, "Python %s on %s\n",
386 Py_GetVersion(), Py_GetPlatform());
387 if (!Py_NoSiteFlag)
388 fprintf(stderr, "%s\n", COPYRIGHT);
389 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000390
Guido van Rossum667d7041995-08-04 04:20:48 +0000391 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000392 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
393 _PyOS_optind--;
394 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000395 }
396
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000397 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000398
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000399 if ((inspect || (command == NULL && filename == NULL)) &&
400 isatty(fileno(stdin))) {
401 PyObject *v;
402 v = PyImport_ImportModule("readline");
403 if (v == NULL)
404 PyErr_Clear();
405 else
406 Py_DECREF(v);
407 }
408
Guido van Rossum667d7041995-08-04 04:20:48 +0000409 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000410 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000411 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000412 }
413 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000414 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000415 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000416 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000417 /* XXX */
418 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000419 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000420 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000421 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000422 }
423
Barry Warsawd86dcd32003-06-29 17:07:06 +0000424 /* Check this environment variable at the end, to give programs the
425 * opportunity to set it from Python.
426 */
427 if (!saw_inspect_flag &&
428 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
429 {
430 inspect = 1;
431 }
432
Guido van Rossum775af911997-02-14 19:50:32 +0000433 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000434 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000435 /* XXX */
436 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000437
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000438 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000439#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000440 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000441 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
442#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000443
444#ifdef __INSURE__
445 /* Insure++ is a memory analysis tool that aids in discovering
446 * memory leaks and other memory problems. On Python exit, the
447 * interned string dictionary is flagged as being in use at exit
448 * (which it is). Under normal circumstances, this is fine because
449 * the memory will be automatically reclaimed by the system. Under
450 * memory debugging, it's a huge source of useless noise, so we
451 * trade off slower shutdown for less distraction in the memory
452 * reports. -baw
453 */
454 _Py_ReleaseInternedStrings();
455#endif /* __INSURE__ */
456
Guido van Rossum05f7c501997-08-02 03:00:42 +0000457 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000458}
459
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000460/* this is gonna seem *real weird*, but if you put some other code between
461 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
462 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000463
Guido van Rossum667d7041995-08-04 04:20:48 +0000464/* Make the *original* argc/argv available to other modules.
465 This is rare, but it is needed by the secureware extension. */
466
467void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000468Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000469{
470 *argc = orig_argc;
471 *argv = orig_argv;
472}