blob: 913e82e31962e3700f36d84265d90f081d4f0710 [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"
Jeremy Hyltonec97a282005-10-21 14:58:06 +00005#include "code.h" /* For CO_FUTURE_DIVISION */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00006#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00007
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00008#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00009#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000010#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 */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000037#define BASE_OPTS "c:dEhim:OQ: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 =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000051"usage: %s [option] ... [-c cmd | -m mod | 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 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000064-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000065-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000066-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000067-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000068-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000069-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000070-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000071";
72static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000073 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000074-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000075-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000076-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000078file : program read from script file\n\
79- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000080";
Guido van Rossum393661d2001-08-31 17:40:15 +000081static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000082arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000083Other environment variables:\n\
84PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000085PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000087PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
88 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000089PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090";
91
92
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000093static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000094usage(int exitcode, char* program)
95{
Guido van Rossum393661d2001-08-31 17:40:15 +000096 FILE *f = exitcode ? stderr : stdout;
97
98 fprintf(f, usage_line, program);
99 if (exitcode)
100 fprintf(f, "Try `python -h' for more information.\n");
101 else {
102 fprintf(f, usage_1);
103 fprintf(f, usage_2);
104 fprintf(f, usage_3);
105 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
106 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000107#if defined(__VMS)
108 if (exitcode == 0) {
109 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000110 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000111 }
112 else {
113 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000114 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115 }
116#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000117 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000118#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000119 /*NOTREACHED*/
120}
121
Martin v. Löwis6caea372003-11-18 19:46:25 +0000122static void RunStartupFile(PyCompilerFlags *cf)
123{
124 char *startup = Py_GETENV("PYTHONSTARTUP");
125 if (startup != NULL && startup[0] != '\0') {
126 FILE *fp = fopen(startup, "r");
127 if (fp != NULL) {
128 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
129 PyErr_Clear();
130 fclose(fp);
131 }
132 }
133}
134
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000135
136static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000137{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000138 PyObject *runpy, *runmodule, *runargs, *result;
139 runpy = PyImport_ImportModule("runpy");
140 if (runpy == NULL) {
141 fprintf(stderr, "Could not import runpy module\n");
142 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000143 }
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000144 runmodule = PyObject_GetAttrString(runpy, "run_module");
145 if (runmodule == NULL) {
146 fprintf(stderr, "Could not access runpy.run_module\n");
147 Py_DECREF(runpy);
148 return -1;
149 }
150 runargs = Py_BuildValue("sOsO", module,
151 Py_None, "__main__", Py_True);
152 if (runargs == NULL) {
153 fprintf(stderr,
154 "Could not create arguments for runpy.run_module\n");
155 Py_DECREF(runpy);
156 Py_DECREF(runmodule);
157 return -1;
158 }
159 result = PyObject_Call(runmodule, runargs, NULL);
160 if (result == NULL) {
161 PyErr_Print();
162 }
163 Py_DECREF(runpy);
164 Py_DECREF(runmodule);
165 Py_DECREF(runargs);
166 if (result == NULL) {
167 return -1;
168 }
169 Py_DECREF(result);
170 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000171}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000172
Guido van Rossum667d7041995-08-04 04:20:48 +0000173/* Main program */
174
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000175int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000176Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000177{
178 int c;
179 int sts;
180 char *command = NULL;
181 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000182 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000183 FILE *fp = stdin;
184 char *p;
185 int inspect = 0;
186 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000187 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000188 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000189 int help = 0;
190 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000191 int saw_inspect_flag = 0;
192 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000193 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000194
Guido van Rossum393661d2001-08-31 17:40:15 +0000195 cf.cf_flags = 0;
196
Guido van Rossumac56b031996-07-21 02:33:38 +0000197 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000198 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000199
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000200#ifdef RISCOS
201 Py_RISCOSWimpFlag = 0;
202#endif
203
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000204 PySys_ResetWarnOptions();
205
Guido van Rossumbceccf52001-04-10 22:07:43 +0000206 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000207 if (c == 'c') {
208 /* -c is the last option; following arguments
209 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000210 command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000211 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000212 if (command == NULL)
213 Py_FatalError(
214 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000215 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000216 strcat(command, "\n");
217 break;
218 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000219
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000220 if (c == 'm') {
221 /* -m is the last option; following arguments
222 that look like options are left for the
223 module to interpret. */
224 module = malloc(strlen(_PyOS_optarg) + 2);
225 if (module == NULL)
226 Py_FatalError(
227 "not enough memory to copy -m argument");
228 strcpy(module, _PyOS_optarg);
229 break;
230 }
231
Guido van Rossum667d7041995-08-04 04:20:48 +0000232 switch (c) {
233
234 case 'd':
235 Py_DebugFlag++;
236 break;
237
Guido van Rossum61c345f2001-09-04 03:26:15 +0000238 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000239 if (strcmp(_PyOS_optarg, "old") == 0) {
240 Py_DivisionWarningFlag = 0;
241 break;
242 }
243 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000244 Py_DivisionWarningFlag = 1;
245 break;
246 }
247 if (strcmp(_PyOS_optarg, "warnall") == 0) {
248 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000249 break;
250 }
251 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000252 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000253 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000254 /* And this tells the eval loop to treat
255 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
256 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000257 break;
258 }
259 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000260 "-Q option should be `-Qold', "
261 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000262 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000263 /* NOTREACHED */
264
Guido van Rossum667d7041995-08-04 04:20:48 +0000265 case 'i':
266 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000267 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000268 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000269 break;
270
Guido van Rossum7614da61997-03-03 19:14:45 +0000271 case 'O':
272 Py_OptimizeFlag++;
273 break;
274
Guido van Rossum7922bd71997-08-29 22:34:47 +0000275 case 'S':
276 Py_NoSiteFlag++;
277 break;
278
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000279 case 'E':
280 Py_IgnoreEnvironmentFlag++;
281 break;
282
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000283 case 't':
284 Py_TabcheckFlag++;
285 break;
286
Guido van Rossum667d7041995-08-04 04:20:48 +0000287 case 'u':
288 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000289 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000290 break;
291
292 case 'v':
293 Py_VerboseFlag++;
294 break;
295
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000296#ifdef RISCOS
297 case 'w':
298 Py_RISCOSWimpFlag = 1;
299 break;
300#endif
301
Guido van Rossuma075ce11997-12-05 21:56:45 +0000302 case 'x':
303 skipfirstline = 1;
304 break;
305
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000306 case 'U':
307 Py_UnicodeFlag++;
308 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000309 case 'h':
310 help++;
311 break;
312 case 'V':
313 version++;
314 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000315
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000316 case 'W':
317 PySys_AddWarnOption(_PyOS_optarg);
318 break;
319
Guido van Rossum667d7041995-08-04 04:20:48 +0000320 /* This space reserved for other options */
321
322 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000323 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000324 /*NOTREACHED*/
325
326 }
327 }
328
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000329 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000330 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000331
332 if (version) {
333 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000334 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000335 }
336
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000337 if (!saw_inspect_flag &&
338 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
339 inspect = 1;
340 if (!saw_unbuffered_flag &&
341 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
342 unbuffered = 1;
343
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000344 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000345 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000346 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000347#ifdef __VMS
348 filename = decc$translate_vms(argv[_PyOS_optind]);
349 if (filename == (char *)0 || filename == (char *)-1)
350 filename = argv[_PyOS_optind];
351
352#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000353 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000354#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000355 if (filename != NULL) {
356 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000357#ifdef HAVE_STRERROR
358 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
359 argv[0], filename, errno, strerror(errno));
360#else
361 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
362 argv[0], filename, errno);
363#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000364 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000365 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000366 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000367 int ch;
368 /* Push back first newline so line numbers
369 remain the same */
370 while ((ch = getc(fp)) != EOF) {
371 if (ch == '\n') {
372 (void)ungetc(ch, fp);
373 break;
374 }
375 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000376 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000377 {
378 /* XXX: does this work on Win/Win64? (see posix_fstat) */
379 struct stat sb;
380 if (fstat(fileno(fp), &sb) == 0 &&
381 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000382 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
383 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000384 }
385 }
Guido van Rossum775af911997-02-14 19:50:32 +0000386 }
387 }
388
389 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
390
Guido van Rossum667d7041995-08-04 04:20:48 +0000391 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000392#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000393 _setmode(fileno(stdin), O_BINARY);
394 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000395#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000396#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000397 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
398 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
399 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000400#else /* !HAVE_SETVBUF */
401 setbuf(stdin, (char *)NULL);
402 setbuf(stdout, (char *)NULL);
403 setbuf(stderr, (char *)NULL);
404#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000405 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000406 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000407#ifdef MS_WINDOWS
408 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000409 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000410 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000411#else /* !MS_WINDOWS */
412#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000413 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
414 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000415#endif /* HAVE_SETVBUF */
416#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000417 /* Leave stderr alone - it should be unbuffered anyway. */
418 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000419#ifdef __VMS
420 else {
421 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
422 }
423#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000424
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000425#ifdef __APPLE__
426 /* On MacOS X, when the Python interpreter is embedded in an
427 application bundle, it gets executed by a bootstrapping script
428 that does os.execve() with an argv[0] that's different from the
429 actual Python executable. This is needed to keep the Finder happy,
430 or rather, to work around Apple's overly strict requirements of
431 the process name. However, we still need a usable sys.executable,
432 so the actual executable path is passed in an environment variable.
433 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
434 script. */
435 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
436 Py_SetProgramName(p);
437 else
438 Py_SetProgramName(argv[0]);
439#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000440 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000441#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000442 Py_Initialize();
443
Guido van Rossum667d7041995-08-04 04:20:48 +0000444 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000445 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000446 fprintf(stderr, "Python %s on %s\n",
447 Py_GetVersion(), Py_GetPlatform());
448 if (!Py_NoSiteFlag)
449 fprintf(stderr, "%s\n", COPYRIGHT);
450 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000451
Guido van Rossum667d7041995-08-04 04:20:48 +0000452 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000453 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
454 _PyOS_optind--;
455 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000456 }
457
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000458 if (module != NULL) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000459 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000460 _PyOS_optind--;
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000461 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000462 }
463
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000464 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000465
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000466 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000467 isatty(fileno(stdin))) {
468 PyObject *v;
469 v = PyImport_ImportModule("readline");
470 if (v == NULL)
471 PyErr_Clear();
472 else
473 Py_DECREF(v);
474 }
475
Guido van Rossum667d7041995-08-04 04:20:48 +0000476 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000477 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000478 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000479 } else if (module) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000480 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000481 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000482 }
483 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000484 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000485 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000486 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000487 /* XXX */
488 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000489 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000490 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000491 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000492 }
493
Barry Warsawd86dcd32003-06-29 17:07:06 +0000494 /* Check this environment variable at the end, to give programs the
495 * opportunity to set it from Python.
496 */
497 if (!saw_inspect_flag &&
498 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
499 {
500 inspect = 1;
501 }
502
Guido van Rossum775af911997-02-14 19:50:32 +0000503 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000504 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000505 /* XXX */
506 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000507
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000508 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000509#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000510 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000511 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
512#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000513
514#ifdef __INSURE__
515 /* Insure++ is a memory analysis tool that aids in discovering
516 * memory leaks and other memory problems. On Python exit, the
517 * interned string dictionary is flagged as being in use at exit
518 * (which it is). Under normal circumstances, this is fine because
519 * the memory will be automatically reclaimed by the system. Under
520 * memory debugging, it's a huge source of useless noise, so we
521 * trade off slower shutdown for less distraction in the memory
522 * reports. -baw
523 */
524 _Py_ReleaseInternedStrings();
525#endif /* __INSURE__ */
526
Guido van Rossum05f7c501997-08-02 03:00:42 +0000527 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000528}
529
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000530/* this is gonna seem *real weird*, but if you put some other code between
531 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
532 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000533
Guido van Rossum667d7041995-08-04 04:20:48 +0000534/* Make the *original* argc/argv available to other modules.
535 This is rare, but it is needed by the secureware extension. */
536
537void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000538Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000539{
540 *argc = orig_argc;
541 *argv = orig_argv;
542}