blob: ca382c69d3224b95ca114067bc2651bc5dd893a1 [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
8extern int PyVMS_init(int* pvi_argc, char*** pvi_argv);
9extern PyObject* pyvms_gr_empty_string;
10#endif
11
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000012#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000013#include <fcntl.h>
14#endif
15
Jack Jansen0a116f32002-12-23 21:03:36 +000016#if defined(WITH_NEXT_FRAMEWORK)
17#include "pymactoolbox.h"
18#endif
19
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000020#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000021#define PYTHONHOMEHELP "<prefix>\\lib"
22#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000023#if defined(PYOS_OS2) && defined(PYCC_GCC)
24#define PYTHONHOMEHELP "<prefix>/Lib"
25#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000026#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000027#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000028#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000029
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000030#include "pygetopt.h"
31
Guido van Rossuma22865e2000-09-05 04:41:18 +000032#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000033 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
34 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000035
Guido van Rossumac56b031996-07-21 02:33:38 +000036/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000037static char **orig_argv;
38static int orig_argc;
39
Guido van Rossumbceccf52001-04-10 22:07:43 +000040/* command line options */
Guido van Rossum61c345f2001-09-04 03:26:15 +000041#define BASE_OPTS "c:dEhiOQ:StuUvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000042
43#ifndef RISCOS
44#define PROGRAM_OPTS BASE_OPTS
45#else /*RISCOS*/
46/* extra option saying that we are running under a special task window
47 frontend; especially my_readline will behave different */
48#define PROGRAM_OPTS BASE_OPTS "w"
49/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000050extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000051#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000052
Guido van Rossum667d7041995-08-04 04:20:48 +000053/* Short usage message (with %s for argv0) */
54static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000055"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000056
57/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000058static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000059Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000060-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000061-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000062-E : ignore environment variables (such as PYTHONPATH)\n\
63-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000064-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000065 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000066";
67static char *usage_2 = "\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000068-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000069-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000070-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000071-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000072-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000073-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Neal Norwitzce233b42002-07-28 13:53:05 +000074 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075";
76static char *usage_3 = "\
Guido van Rossum7922bd71997-08-29 22:34:47 +000077-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000078-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000079-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000080-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000081file : program read from script file\n\
82- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000083";
Guido van Rossum393661d2001-08-31 17:40:15 +000084static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000085arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086Other environment variables:\n\
87PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000088PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000090PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
91 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000092PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000093";
94
95
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000096static void
97usage(int exitcode, char* program)
98{
Guido van Rossum393661d2001-08-31 17:40:15 +000099 FILE *f = exitcode ? stderr : stdout;
100
101 fprintf(f, usage_line, program);
102 if (exitcode)
103 fprintf(f, "Try `python -h' for more information.\n");
104 else {
105 fprintf(f, usage_1);
106 fprintf(f, usage_2);
107 fprintf(f, usage_3);
108 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
109 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000110#if defined(__VMS)
111 if (exitcode == 0) {
112 /* suppress 'error' message */
113 exit(1);
114 }
115 else {
116 /* STS$M_INHIB_MSG + SS$_ABORT */
117 exit(0x1000002c);
118 }
119#else
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000120 exit(exitcode);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000121#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000122 /*NOTREACHED*/
123}
124
125
Guido van Rossum667d7041995-08-04 04:20:48 +0000126/* Main program */
127
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000128int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000129Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000130{
131 int c;
132 int sts;
133 char *command = NULL;
134 char *filename = NULL;
135 FILE *fp = stdin;
136 char *p;
137 int inspect = 0;
138 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000139 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000140 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000141 int help = 0;
142 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000143 int saw_inspect_flag = 0;
144 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000145 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000146
Guido van Rossum393661d2001-08-31 17:40:15 +0000147 cf.cf_flags = 0;
148
Guido van Rossumac56b031996-07-21 02:33:38 +0000149 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000150 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000151
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000152#ifdef RISCOS
153 Py_RISCOSWimpFlag = 0;
154#endif
155
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000156 PySys_ResetWarnOptions();
157
Jack Jansen21ed16a2002-08-02 14:11:24 +0000158#if defined(WITH_NEXT_FRAMEWORK)
159 /* If we are running from a framework it could be that we are actually
160 ** the main program for an applet. If so, the next call will return the
161 ** filename that we are supposed to run.
162 */
163 filename = PyMac_GetAppletScriptFile();
164 if (filename != NULL) {
165 if ((fp = fopen(filename, "r")) == NULL) {
166 fprintf(stderr, "%s: can't open file '%s'\n",
167 argv[0], filename);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000168#if defined(__VMS)
169 /* STS$M_INHIB_MSG + SS$_ABORT */
170 exit(0x1000002c);
171#else
Jack Jansen21ed16a2002-08-02 14:11:24 +0000172 exit(2);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000173#endif
Jack Jansen21ed16a2002-08-02 14:11:24 +0000174 }
175 }
176 /* Skip option-processing if we are an applet */
177 if (filename == NULL)
178#endif
Guido van Rossumbceccf52001-04-10 22:07:43 +0000179 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000180 if (c == 'c') {
181 /* -c is the last option; following arguments
182 that look like options are left for the
183 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000184 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 if (command == NULL)
186 Py_FatalError(
187 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000188 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000189 strcat(command, "\n");
190 break;
191 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000192
Guido van Rossum667d7041995-08-04 04:20:48 +0000193 switch (c) {
194
195 case 'd':
196 Py_DebugFlag++;
197 break;
198
Guido van Rossum61c345f2001-09-04 03:26:15 +0000199 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000200 if (strcmp(_PyOS_optarg, "old") == 0) {
201 Py_DivisionWarningFlag = 0;
202 break;
203 }
204 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000205 Py_DivisionWarningFlag = 1;
206 break;
207 }
208 if (strcmp(_PyOS_optarg, "warnall") == 0) {
209 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000210 break;
211 }
212 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000213 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000214 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000215 /* And this tells the eval loop to treat
216 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
217 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000218 break;
219 }
220 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000221 "-Q option should be `-Qold', "
222 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Guido van Rossum393661d2001-08-31 17:40:15 +0000223 usage(2, argv[0]);
224 /* NOTREACHED */
225
Guido van Rossum667d7041995-08-04 04:20:48 +0000226 case 'i':
227 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000228 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000229 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 break;
231
Guido van Rossum7614da61997-03-03 19:14:45 +0000232 case 'O':
233 Py_OptimizeFlag++;
234 break;
235
Guido van Rossum7922bd71997-08-29 22:34:47 +0000236 case 'S':
237 Py_NoSiteFlag++;
238 break;
239
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000240 case 'E':
241 Py_IgnoreEnvironmentFlag++;
242 break;
243
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000244 case 't':
245 Py_TabcheckFlag++;
246 break;
247
Guido van Rossum667d7041995-08-04 04:20:48 +0000248 case 'u':
249 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000250 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000251 break;
252
253 case 'v':
254 Py_VerboseFlag++;
255 break;
256
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000257#ifdef RISCOS
258 case 'w':
259 Py_RISCOSWimpFlag = 1;
260 break;
261#endif
262
Guido van Rossuma075ce11997-12-05 21:56:45 +0000263 case 'x':
264 skipfirstline = 1;
265 break;
266
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000267 case 'U':
268 Py_UnicodeFlag++;
269 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000270 case 'h':
271 help++;
272 break;
273 case 'V':
274 version++;
275 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000276
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000277 case 'W':
278 PySys_AddWarnOption(_PyOS_optarg);
279 break;
280
Guido van Rossum667d7041995-08-04 04:20:48 +0000281 /* This space reserved for other options */
282
283 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000284 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000285 /*NOTREACHED*/
286
287 }
288 }
289
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000290 if (help)
291 usage(0, argv[0]);
292
293 if (version) {
294 fprintf(stderr, "Python %s\n", PY_VERSION);
295 exit(0);
296 }
297
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000298 if (!saw_inspect_flag &&
299 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
300 inspect = 1;
301 if (!saw_unbuffered_flag &&
302 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
303 unbuffered = 1;
304
Jack Jansen21ed16a2002-08-02 14:11:24 +0000305 if (command == NULL && filename == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000306 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000307 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000308 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000309 if (filename != NULL) {
310 if ((fp = fopen(filename, "r")) == NULL) {
311 fprintf(stderr, "%s: can't open file '%s'\n",
312 argv[0], filename);
313 exit(2);
314 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000315 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000316 int ch;
317 /* Push back first newline so line numbers
318 remain the same */
319 while ((ch = getc(fp)) != EOF) {
320 if (ch == '\n') {
321 (void)ungetc(ch, fp);
322 break;
323 }
324 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000325 }
Guido van Rossum775af911997-02-14 19:50:32 +0000326 }
327 }
328
329 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
330
Guido van Rossum667d7041995-08-04 04:20:48 +0000331 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000332#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000333 _setmode(fileno(stdin), O_BINARY);
334 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000335#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000336#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000337#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000338 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
339 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
340 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000341#else /* !HAVE_SETVBUF */
342 setbuf(stdin, (char *)NULL);
343 setbuf(stdout, (char *)NULL);
344 setbuf(stderr, (char *)NULL);
345#endif /* !HAVE_SETVBUF */
346#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000347 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000348 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000349 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
350 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000351#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000352 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000353 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000354#ifdef MS_WINDOWS
355 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000356 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000357 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000358#else /* !MS_WINDOWS */
359#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000360 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
361 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000362#endif /* HAVE_SETVBUF */
363#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000364 /* Leave stderr alone - it should be unbuffered anyway. */
365 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000366#ifdef __VMS
367 else {
368 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
369 }
370#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000371
Guido van Rossumed52aac1997-07-19 19:20:32 +0000372 Py_SetProgramName(argv[0]);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000373#ifdef __VMS
374 PyVMS_init(&argc, &argv);
375#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000376 Py_Initialize();
377
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000378#ifdef __VMS
379 /* create an empty string object */
380 pyvms_gr_empty_string = Py_BuildValue("s#", Py_None, (unsigned int)0);
381#endif
382
Guido van Rossum667d7041995-08-04 04:20:48 +0000383 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000384 (command == NULL && filename == NULL && stdin_is_interactive))
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000385#ifndef __VMS
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000386 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000387 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000388#else
389 fprintf(stderr, "Python %s on %s %s (%s_float)\n%s\n",
390 Py_GetVersion(), Py_GetPlatform(),
391# ifdef __ALPHA
392 "Alpha",
393# else
394 "VAX",
395# endif
396# if __IEEE_FLOAT
397 "T",
398# else
399# if __D_FLOAT
400 "D",
401# else
402# if __G_FLOAT
403 "G",
404# endif /* __G_FLOAT */
405# endif /* __D_FLOAT */
406# endif /* __IEEE_FLOAT */
407 COPYRIGHT); /* << @@ defined above in this file */
408/* Py_GetCopyright()); */
409#endif /* __VMS */
Guido van Rossum393661d2001-08-31 17:40:15 +0000410
Guido van Rossum667d7041995-08-04 04:20:48 +0000411 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000412 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
413 _PyOS_optind--;
414 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000415 }
416
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000417 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000418
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000419 if ((inspect || (command == NULL && filename == NULL)) &&
420 isatty(fileno(stdin))) {
421 PyObject *v;
422 v = PyImport_ImportModule("readline");
423 if (v == NULL)
424 PyErr_Clear();
425 else
426 Py_DECREF(v);
427 }
428
Guido van Rossum667d7041995-08-04 04:20:48 +0000429 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000430 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000431 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000432 }
433 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000434 if (filename == NULL && stdin_is_interactive) {
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000435 char *startup = Py_GETENV("PYTHONSTARTUP");
Guido van Rossum667d7041995-08-04 04:20:48 +0000436 if (startup != NULL && startup[0] != '\0') {
437 FILE *fp = fopen(startup, "r");
438 if (fp != NULL) {
439 (void) PyRun_SimpleFile(fp, startup);
440 PyErr_Clear();
441 fclose(fp);
442 }
443 }
444 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000445 /* XXX */
446 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000447 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000448 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000449 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000450 }
451
Guido van Rossum775af911997-02-14 19:50:32 +0000452 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000453 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000454 /* XXX */
455 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000456
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000457 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000458#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000459 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000460 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
461#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000462
463#ifdef __INSURE__
464 /* Insure++ is a memory analysis tool that aids in discovering
465 * memory leaks and other memory problems. On Python exit, the
466 * interned string dictionary is flagged as being in use at exit
467 * (which it is). Under normal circumstances, this is fine because
468 * the memory will be automatically reclaimed by the system. Under
469 * memory debugging, it's a huge source of useless noise, so we
470 * trade off slower shutdown for less distraction in the memory
471 * reports. -baw
472 */
473 _Py_ReleaseInternedStrings();
474#endif /* __INSURE__ */
475
Guido van Rossum05f7c501997-08-02 03:00:42 +0000476 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000477}
478
479
Guido van Rossum667d7041995-08-04 04:20:48 +0000480/* Make the *original* argc/argv available to other modules.
481 This is rare, but it is needed by the secureware extension. */
482
483void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000484Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000485{
486 *argc = orig_argc;
487 *argv = orig_argv;
488}