blob: 2005f0bb8789a0a5b0e9a6e0de29bb1c821e2bf8 [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
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000016#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000017#define PYTHONHOMEHELP "<prefix>\\lib"
18#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000019#if defined(PYOS_OS2) && defined(PYCC_GCC)
20#define PYTHONHOMEHELP "<prefix>/Lib"
21#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000022#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000023#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000024#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000025
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000026#include "pygetopt.h"
27
Guido van Rossuma22865e2000-09-05 04:41:18 +000028#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000029 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
30 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000031
Guido van Rossumac56b031996-07-21 02:33:38 +000032/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000033static char **orig_argv;
34static int orig_argc;
35
Guido van Rossumbceccf52001-04-10 22:07:43 +000036/* command line options */
Guido van Rossum61c345f2001-09-04 03:26:15 +000037#define BASE_OPTS "c:dEhiOQ:StuUvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000038
39#ifndef RISCOS
40#define PROGRAM_OPTS BASE_OPTS
41#else /*RISCOS*/
42/* extra option saying that we are running under a special task window
43 frontend; especially my_readline will behave different */
44#define PROGRAM_OPTS BASE_OPTS "w"
45/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000046extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000047#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000048
Guido van Rossum667d7041995-08-04 04:20:48 +000049/* Short usage message (with %s for argv0) */
50static char *usage_line =
Guido van Rossum6b86a421999-01-28 15:07:47 +000051"usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000052
53/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000054static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000055Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000056-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000057-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000058-E : ignore environment variables (such as PYTHONPATH)\n\
59-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000060-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000061 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000062";
63static char *usage_2 = "\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000064-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000065-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000066-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000067-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000068-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000069-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Neal Norwitzce233b42002-07-28 13:53:05 +000070 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000071";
72static char *usage_3 = "\
Guido van Rossum7922bd71997-08-29 22:34:47 +000073-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000074-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000075-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000076-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000077file : program read from script file\n\
78- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000079";
Guido van Rossum393661d2001-08-31 17:40:15 +000080static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000081arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000082Other environment variables:\n\
83PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000084PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000085 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000086PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
87 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000088PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089";
90
91
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000092static void
93usage(int exitcode, char* program)
94{
Guido van Rossum393661d2001-08-31 17:40:15 +000095 FILE *f = exitcode ? stderr : stdout;
96
97 fprintf(f, usage_line, program);
98 if (exitcode)
99 fprintf(f, "Try `python -h' for more information.\n");
100 else {
101 fprintf(f, usage_1);
102 fprintf(f, usage_2);
103 fprintf(f, usage_3);
104 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
105 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000106#if defined(__VMS)
107 if (exitcode == 0) {
108 /* suppress 'error' message */
109 exit(1);
110 }
111 else {
112 /* STS$M_INHIB_MSG + SS$_ABORT */
113 exit(0x1000002c);
114 }
115#else
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000116 exit(exitcode);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000117#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000118 /*NOTREACHED*/
119}
120
121
Guido van Rossum667d7041995-08-04 04:20:48 +0000122/* Main program */
123
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000124int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000125Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000126{
127 int c;
128 int sts;
129 char *command = NULL;
130 char *filename = NULL;
131 FILE *fp = stdin;
132 char *p;
133 int inspect = 0;
134 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000135 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000136 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000137 int help = 0;
138 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000139 int saw_inspect_flag = 0;
140 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000141 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000142
Guido van Rossum393661d2001-08-31 17:40:15 +0000143 cf.cf_flags = 0;
144
Guido van Rossumac56b031996-07-21 02:33:38 +0000145 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000146 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000147
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000148#ifdef RISCOS
149 Py_RISCOSWimpFlag = 0;
150#endif
151
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000152 PySys_ResetWarnOptions();
153
Jack Jansen21ed16a2002-08-02 14:11:24 +0000154#if defined(WITH_NEXT_FRAMEWORK)
155 /* If we are running from a framework it could be that we are actually
156 ** the main program for an applet. If so, the next call will return the
157 ** filename that we are supposed to run.
158 */
159 filename = PyMac_GetAppletScriptFile();
160 if (filename != NULL) {
161 if ((fp = fopen(filename, "r")) == NULL) {
162 fprintf(stderr, "%s: can't open file '%s'\n",
163 argv[0], filename);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000164#if defined(__VMS)
165 /* STS$M_INHIB_MSG + SS$_ABORT */
166 exit(0x1000002c);
167#else
Jack Jansen21ed16a2002-08-02 14:11:24 +0000168 exit(2);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000169#endif
Jack Jansen21ed16a2002-08-02 14:11:24 +0000170 }
171 }
172 /* Skip option-processing if we are an applet */
173 if (filename == NULL)
174#endif
Guido van Rossumbceccf52001-04-10 22:07:43 +0000175 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000176 if (c == 'c') {
177 /* -c is the last option; following arguments
178 that look like options are left for the
179 the command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000180 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000181 if (command == NULL)
182 Py_FatalError(
183 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000184 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 strcat(command, "\n");
186 break;
187 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000188
Guido van Rossum667d7041995-08-04 04:20:48 +0000189 switch (c) {
190
191 case 'd':
192 Py_DebugFlag++;
193 break;
194
Guido van Rossum61c345f2001-09-04 03:26:15 +0000195 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000196 if (strcmp(_PyOS_optarg, "old") == 0) {
197 Py_DivisionWarningFlag = 0;
198 break;
199 }
200 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000201 Py_DivisionWarningFlag = 1;
202 break;
203 }
204 if (strcmp(_PyOS_optarg, "warnall") == 0) {
205 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000206 break;
207 }
208 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000209 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000210 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000211 /* And this tells the eval loop to treat
212 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
213 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000214 break;
215 }
216 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000217 "-Q option should be `-Qold', "
218 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Guido van Rossum393661d2001-08-31 17:40:15 +0000219 usage(2, argv[0]);
220 /* NOTREACHED */
221
Guido van Rossum667d7041995-08-04 04:20:48 +0000222 case 'i':
223 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000224 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000225 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000226 break;
227
Guido van Rossum7614da61997-03-03 19:14:45 +0000228 case 'O':
229 Py_OptimizeFlag++;
230 break;
231
Guido van Rossum7922bd71997-08-29 22:34:47 +0000232 case 'S':
233 Py_NoSiteFlag++;
234 break;
235
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000236 case 'E':
237 Py_IgnoreEnvironmentFlag++;
238 break;
239
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000240 case 't':
241 Py_TabcheckFlag++;
242 break;
243
Guido van Rossum667d7041995-08-04 04:20:48 +0000244 case 'u':
245 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000246 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000247 break;
248
249 case 'v':
250 Py_VerboseFlag++;
251 break;
252
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000253#ifdef RISCOS
254 case 'w':
255 Py_RISCOSWimpFlag = 1;
256 break;
257#endif
258
Guido van Rossuma075ce11997-12-05 21:56:45 +0000259 case 'x':
260 skipfirstline = 1;
261 break;
262
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000263 case 'U':
264 Py_UnicodeFlag++;
265 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000266 case 'h':
267 help++;
268 break;
269 case 'V':
270 version++;
271 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000272
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000273 case 'W':
274 PySys_AddWarnOption(_PyOS_optarg);
275 break;
276
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 /* This space reserved for other options */
278
279 default:
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000280 usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000281 /*NOTREACHED*/
282
283 }
284 }
285
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000286 if (help)
287 usage(0, argv[0]);
288
289 if (version) {
290 fprintf(stderr, "Python %s\n", PY_VERSION);
291 exit(0);
292 }
293
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000294 if (!saw_inspect_flag &&
295 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
296 inspect = 1;
297 if (!saw_unbuffered_flag &&
298 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
299 unbuffered = 1;
300
Jack Jansen21ed16a2002-08-02 14:11:24 +0000301 if (command == NULL && filename == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000302 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000303 {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000304 filename = argv[_PyOS_optind];
Guido van Rossum775af911997-02-14 19:50:32 +0000305 if (filename != NULL) {
306 if ((fp = fopen(filename, "r")) == NULL) {
307 fprintf(stderr, "%s: can't open file '%s'\n",
308 argv[0], filename);
309 exit(2);
310 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000311 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000312 int ch;
313 /* Push back first newline so line numbers
314 remain the same */
315 while ((ch = getc(fp)) != EOF) {
316 if (ch == '\n') {
317 (void)ungetc(ch, fp);
318 break;
319 }
320 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000321 }
Guido van Rossum775af911997-02-14 19:50:32 +0000322 }
323 }
324
325 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
326
Guido van Rossum667d7041995-08-04 04:20:48 +0000327 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000328#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000329 _setmode(fileno(stdin), O_BINARY);
330 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000331#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000332#ifndef MPW
Guido van Rossum22ffac11998-03-06 15:30:39 +0000333#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000334 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
335 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
336 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000337#else /* !HAVE_SETVBUF */
338 setbuf(stdin, (char *)NULL);
339 setbuf(stdout, (char *)NULL);
340 setbuf(stderr, (char *)NULL);
341#endif /* !HAVE_SETVBUF */
342#else /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000343 /* On MPW (3.2) unbuffered seems to hang */
Guido van Rossum775af911997-02-14 19:50:32 +0000344 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum667d7041995-08-04 04:20:48 +0000345 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
346 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000347#endif /* MPW */
Guido van Rossum667d7041995-08-04 04:20:48 +0000348 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000349 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000350#ifdef MS_WINDOWS
351 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000352 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000353 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000354#else /* !MS_WINDOWS */
355#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000356 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
357 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000358#endif /* HAVE_SETVBUF */
359#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000360 /* Leave stderr alone - it should be unbuffered anyway. */
361 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000362#ifdef __VMS
363 else {
364 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
365 }
366#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000367
Guido van Rossumed52aac1997-07-19 19:20:32 +0000368 Py_SetProgramName(argv[0]);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000369#ifdef __VMS
370 PyVMS_init(&argc, &argv);
371#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000372 Py_Initialize();
373
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000374#ifdef __VMS
375 /* create an empty string object */
376 pyvms_gr_empty_string = Py_BuildValue("s#", Py_None, (unsigned int)0);
377#endif
378
Guido van Rossum667d7041995-08-04 04:20:48 +0000379 if (Py_VerboseFlag ||
Guido van Rossum129e91a1997-02-14 21:00:50 +0000380 (command == NULL && filename == NULL && stdin_is_interactive))
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000381#ifndef __VMS
Guido van Rossumfe4dfc71997-05-19 18:33:01 +0000382 fprintf(stderr, "Python %s on %s\n%s\n",
Guido van Rossuma22865e2000-09-05 04:41:18 +0000383 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000384#else
385 fprintf(stderr, "Python %s on %s %s (%s_float)\n%s\n",
386 Py_GetVersion(), Py_GetPlatform(),
387# ifdef __ALPHA
388 "Alpha",
389# else
390 "VAX",
391# endif
392# if __IEEE_FLOAT
393 "T",
394# else
395# if __D_FLOAT
396 "D",
397# else
398# if __G_FLOAT
399 "G",
400# endif /* __G_FLOAT */
401# endif /* __D_FLOAT */
402# endif /* __IEEE_FLOAT */
403 COPYRIGHT); /* << @@ defined above in this file */
404/* Py_GetCopyright()); */
405#endif /* __VMS */
Guido van Rossum393661d2001-08-31 17:40:15 +0000406
Guido van Rossum667d7041995-08-04 04:20:48 +0000407 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000408 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
409 _PyOS_optind--;
410 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000411 }
412
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000413 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000414
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000415 if ((inspect || (command == NULL && filename == NULL)) &&
416 isatty(fileno(stdin))) {
417 PyObject *v;
418 v = PyImport_ImportModule("readline");
419 if (v == NULL)
420 PyErr_Clear();
421 else
422 Py_DECREF(v);
423 }
424
Guido van Rossum667d7041995-08-04 04:20:48 +0000425 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000426 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000427 free(command);
Guido van Rossum667d7041995-08-04 04:20:48 +0000428 }
429 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000430 if (filename == NULL && stdin_is_interactive) {
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000431 char *startup = Py_GETENV("PYTHONSTARTUP");
Guido van Rossum667d7041995-08-04 04:20:48 +0000432 if (startup != NULL && startup[0] != '\0') {
433 FILE *fp = fopen(startup, "r");
434 if (fp != NULL) {
435 (void) PyRun_SimpleFile(fp, startup);
436 PyErr_Clear();
437 fclose(fp);
438 }
439 }
440 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000441 /* XXX */
442 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000443 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000444 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000445 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000446 }
447
Guido van Rossum775af911997-02-14 19:50:32 +0000448 if (inspect && stdin_is_interactive &&
Guido van Rossum667d7041995-08-04 04:20:48 +0000449 (filename != NULL || command != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000450 /* XXX */
451 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000452
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000453 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000454#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000455 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000456 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
457#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000458
459#ifdef __INSURE__
460 /* Insure++ is a memory analysis tool that aids in discovering
461 * memory leaks and other memory problems. On Python exit, the
462 * interned string dictionary is flagged as being in use at exit
463 * (which it is). Under normal circumstances, this is fine because
464 * the memory will be automatically reclaimed by the system. Under
465 * memory debugging, it's a huge source of useless noise, so we
466 * trade off slower shutdown for less distraction in the memory
467 * reports. -baw
468 */
469 _Py_ReleaseInternedStrings();
470#endif /* __INSURE__ */
471
Guido van Rossum05f7c501997-08-02 03:00:42 +0000472 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000473}
474
475
Guido van Rossum667d7041995-08-04 04:20:48 +0000476/* Make the *original* argc/argv available to other modules.
477 This is rare, but it is needed by the secureware extension. */
478
479void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000480Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000481{
482 *argc = orig_argc;
483 *argv = orig_argv;
484}