blob: 28d32946277efb5ee100bef0a1105da8585c7841 [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
Anthony Baxterac6bd462006-04-13 02:06:09 +000032#ifdef __cplusplus
33extern "C" {
34#endif
35
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 */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000041#define BASE_OPTS "c:dEhim:OQ: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 =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000055"usage: %s [option] ... [-c cmd | -m mod | 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 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000068-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000069-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000070-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000071-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000073-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000074-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075";
76static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000077 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000078-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000079-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000080-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000081-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000082file : program read from script file\n\
83- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000084";
Guido van Rossum393661d2001-08-31 17:40:15 +000085static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000086arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000087Other environment variables:\n\
88PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000089PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000091PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
92 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000093PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000094";
95
96
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000097static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000098usage(int exitcode, char* program)
99{
Guido van Rossum393661d2001-08-31 17:40:15 +0000100 FILE *f = exitcode ? stderr : stdout;
101
102 fprintf(f, usage_line, program);
103 if (exitcode)
104 fprintf(f, "Try `python -h' for more information.\n");
105 else {
106 fprintf(f, usage_1);
107 fprintf(f, usage_2);
108 fprintf(f, usage_3);
109 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
110 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000111#if defined(__VMS)
112 if (exitcode == 0) {
113 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000114 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115 }
116 else {
117 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000118 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000119 }
120#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000121 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000122#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000123 /*NOTREACHED*/
124}
125
Martin v. Löwis6caea372003-11-18 19:46:25 +0000126static void RunStartupFile(PyCompilerFlags *cf)
127{
128 char *startup = Py_GETENV("PYTHONSTARTUP");
129 if (startup != NULL && startup[0] != '\0') {
130 FILE *fp = fopen(startup, "r");
131 if (fp != NULL) {
132 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
133 PyErr_Clear();
134 fclose(fp);
135 }
136 }
137}
138
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000139
140static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000141{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000142 PyObject *runpy, *runmodule, *runargs, *result;
143 runpy = PyImport_ImportModule("runpy");
144 if (runpy == NULL) {
145 fprintf(stderr, "Could not import runpy module\n");
146 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000147 }
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000148 runmodule = PyObject_GetAttrString(runpy, "run_module");
149 if (runmodule == NULL) {
150 fprintf(stderr, "Could not access runpy.run_module\n");
151 Py_DECREF(runpy);
152 return -1;
153 }
154 runargs = Py_BuildValue("sOsO", module,
155 Py_None, "__main__", Py_True);
156 if (runargs == NULL) {
157 fprintf(stderr,
158 "Could not create arguments for runpy.run_module\n");
159 Py_DECREF(runpy);
160 Py_DECREF(runmodule);
161 return -1;
162 }
163 result = PyObject_Call(runmodule, runargs, NULL);
164 if (result == NULL) {
165 PyErr_Print();
166 }
167 Py_DECREF(runpy);
168 Py_DECREF(runmodule);
169 Py_DECREF(runargs);
170 if (result == NULL) {
171 return -1;
172 }
173 Py_DECREF(result);
174 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000175}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000176
Guido van Rossum667d7041995-08-04 04:20:48 +0000177/* Main program */
178
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000179int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000180Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000181{
182 int c;
183 int sts;
184 char *command = NULL;
185 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000186 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000187 FILE *fp = stdin;
188 char *p;
189 int inspect = 0;
190 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000191 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000192 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000193 int help = 0;
194 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000195 int saw_inspect_flag = 0;
196 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000197 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000198
Guido van Rossum393661d2001-08-31 17:40:15 +0000199 cf.cf_flags = 0;
200
Guido van Rossumac56b031996-07-21 02:33:38 +0000201 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000202 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000203
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000204#ifdef RISCOS
205 Py_RISCOSWimpFlag = 0;
206#endif
207
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000208 PySys_ResetWarnOptions();
209
Guido van Rossumbceccf52001-04-10 22:07:43 +0000210 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000211 if (c == 'c') {
212 /* -c is the last option; following arguments
213 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000214 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000215 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000216 if (command == NULL)
217 Py_FatalError(
218 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000219 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000220 strcat(command, "\n");
221 break;
222 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000223
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000224 if (c == 'm') {
225 /* -m is the last option; following arguments
226 that look like options are left for the
227 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000228 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000229 if (module == NULL)
230 Py_FatalError(
231 "not enough memory to copy -m argument");
232 strcpy(module, _PyOS_optarg);
233 break;
234 }
235
Guido van Rossum667d7041995-08-04 04:20:48 +0000236 switch (c) {
237
238 case 'd':
239 Py_DebugFlag++;
240 break;
241
Guido van Rossum61c345f2001-09-04 03:26:15 +0000242 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000243 if (strcmp(_PyOS_optarg, "old") == 0) {
244 Py_DivisionWarningFlag = 0;
245 break;
246 }
247 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000248 Py_DivisionWarningFlag = 1;
249 break;
250 }
251 if (strcmp(_PyOS_optarg, "warnall") == 0) {
252 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000253 break;
254 }
255 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000256 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000257 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000258 /* And this tells the eval loop to treat
259 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
260 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000261 break;
262 }
263 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000264 "-Q option should be `-Qold', "
265 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000266 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000267 /* NOTREACHED */
268
Guido van Rossum667d7041995-08-04 04:20:48 +0000269 case 'i':
270 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000271 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000272 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000273 break;
274
Guido van Rossum7614da61997-03-03 19:14:45 +0000275 case 'O':
276 Py_OptimizeFlag++;
277 break;
278
Guido van Rossum7922bd71997-08-29 22:34:47 +0000279 case 'S':
280 Py_NoSiteFlag++;
281 break;
282
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000283 case 'E':
284 Py_IgnoreEnvironmentFlag++;
285 break;
286
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000287 case 't':
288 Py_TabcheckFlag++;
289 break;
290
Guido van Rossum667d7041995-08-04 04:20:48 +0000291 case 'u':
292 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000293 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000294 break;
295
296 case 'v':
297 Py_VerboseFlag++;
298 break;
299
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000300#ifdef RISCOS
301 case 'w':
302 Py_RISCOSWimpFlag = 1;
303 break;
304#endif
305
Guido van Rossuma075ce11997-12-05 21:56:45 +0000306 case 'x':
307 skipfirstline = 1;
308 break;
309
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000310 case 'U':
311 Py_UnicodeFlag++;
312 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000313 case 'h':
314 help++;
315 break;
316 case 'V':
317 version++;
318 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000319
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000320 case 'W':
321 PySys_AddWarnOption(_PyOS_optarg);
322 break;
323
Guido van Rossum667d7041995-08-04 04:20:48 +0000324 /* This space reserved for other options */
325
326 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000327 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000328 /*NOTREACHED*/
329
330 }
331 }
332
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000333 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000334 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000335
336 if (version) {
337 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000338 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000339 }
340
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000341 if (!saw_inspect_flag &&
342 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
343 inspect = 1;
344 if (!saw_unbuffered_flag &&
345 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
346 unbuffered = 1;
347
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000348 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000349 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000350 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000351#ifdef __VMS
352 filename = decc$translate_vms(argv[_PyOS_optind]);
353 if (filename == (char *)0 || filename == (char *)-1)
354 filename = argv[_PyOS_optind];
355
356#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000357 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000358#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000359 if (filename != NULL) {
360 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000361#ifdef HAVE_STRERROR
362 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
363 argv[0], filename, errno, strerror(errno));
364#else
365 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
366 argv[0], filename, errno);
367#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000368 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000369 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000370 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000371 int ch;
372 /* Push back first newline so line numbers
373 remain the same */
374 while ((ch = getc(fp)) != EOF) {
375 if (ch == '\n') {
376 (void)ungetc(ch, fp);
377 break;
378 }
379 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000380 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000381 {
382 /* XXX: does this work on Win/Win64? (see posix_fstat) */
383 struct stat sb;
384 if (fstat(fileno(fp), &sb) == 0 &&
385 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000386 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
387 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000388 }
389 }
Guido van Rossum775af911997-02-14 19:50:32 +0000390 }
391 }
392
393 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
394
Guido van Rossum667d7041995-08-04 04:20:48 +0000395 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000396#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000397 _setmode(fileno(stdin), O_BINARY);
398 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000399#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000400#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000401 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
402 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
403 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000404#else /* !HAVE_SETVBUF */
405 setbuf(stdin, (char *)NULL);
406 setbuf(stdout, (char *)NULL);
407 setbuf(stderr, (char *)NULL);
408#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000409 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000410 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000411#ifdef MS_WINDOWS
412 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000413 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000414 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000415#else /* !MS_WINDOWS */
416#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000417 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
418 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000419#endif /* HAVE_SETVBUF */
420#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000421 /* Leave stderr alone - it should be unbuffered anyway. */
422 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000423#ifdef __VMS
424 else {
425 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
426 }
427#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000428
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000429#ifdef __APPLE__
430 /* On MacOS X, when the Python interpreter is embedded in an
431 application bundle, it gets executed by a bootstrapping script
432 that does os.execve() with an argv[0] that's different from the
433 actual Python executable. This is needed to keep the Finder happy,
434 or rather, to work around Apple's overly strict requirements of
435 the process name. However, we still need a usable sys.executable,
436 so the actual executable path is passed in an environment variable.
437 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
438 script. */
439 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
440 Py_SetProgramName(p);
441 else
442 Py_SetProgramName(argv[0]);
443#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000444 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000445#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000446 Py_Initialize();
447
Guido van Rossum667d7041995-08-04 04:20:48 +0000448 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000449 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000450 fprintf(stderr, "Python %s on %s\n",
451 Py_GetVersion(), Py_GetPlatform());
452 if (!Py_NoSiteFlag)
453 fprintf(stderr, "%s\n", COPYRIGHT);
454 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000455
Guido van Rossum667d7041995-08-04 04:20:48 +0000456 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000457 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
458 _PyOS_optind--;
459 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000460 }
461
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000462 if (module != NULL) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000463 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000464 _PyOS_optind--;
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000465 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000466 }
467
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000468 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000469
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000470 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000471 isatty(fileno(stdin))) {
472 PyObject *v;
473 v = PyImport_ImportModule("readline");
474 if (v == NULL)
475 PyErr_Clear();
476 else
477 Py_DECREF(v);
478 }
479
Guido van Rossum667d7041995-08-04 04:20:48 +0000480 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000481 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000482 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000483 } else if (module) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000484 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000485 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000486 }
487 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000488 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000489 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000490 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000491 /* XXX */
492 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000493 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000494 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000495 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000496 }
497
Barry Warsawd86dcd32003-06-29 17:07:06 +0000498 /* Check this environment variable at the end, to give programs the
499 * opportunity to set it from Python.
500 */
501 if (!saw_inspect_flag &&
502 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
503 {
504 inspect = 1;
505 }
506
Guido van Rossum775af911997-02-14 19:50:32 +0000507 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000508 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000509 /* XXX */
510 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000511
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000512 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000513#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000514 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000515 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
516#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000517
518#ifdef __INSURE__
519 /* Insure++ is a memory analysis tool that aids in discovering
520 * memory leaks and other memory problems. On Python exit, the
521 * interned string dictionary is flagged as being in use at exit
522 * (which it is). Under normal circumstances, this is fine because
523 * the memory will be automatically reclaimed by the system. Under
524 * memory debugging, it's a huge source of useless noise, so we
525 * trade off slower shutdown for less distraction in the memory
526 * reports. -baw
527 */
528 _Py_ReleaseInternedStrings();
529#endif /* __INSURE__ */
530
Guido van Rossum05f7c501997-08-02 03:00:42 +0000531 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000532}
533
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000534/* this is gonna seem *real weird*, but if you put some other code between
535 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
536 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000537
Guido van Rossum667d7041995-08-04 04:20:48 +0000538/* Make the *original* argc/argv available to other modules.
539 This is rare, but it is needed by the secureware extension. */
540
541void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000542Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000543{
544 *argc = orig_argc;
545 *argv = orig_argv;
546}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000547
548#ifdef __cplusplus
549}
550#endif
551