blob: 0bcd0f4fa647b9b21107d79e6525fa80e6f013c4 [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"
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00005#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00007#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#endif
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000013#include <fcntl.h>
14#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000017#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000018#define PYTHONHOMEHELP "<prefix>\\lib"
19#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000020#if defined(PYOS_OS2) && defined(PYCC_GCC)
21#define PYTHONHOMEHELP "<prefix>/Lib"
22#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000023#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000024#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000026
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000027#include "pygetopt.h"
28
Guido van Rossuma22865e2000-09-05 04:41:18 +000029#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000030 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
31 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Guido van Rossumac56b031996-07-21 02:33:38 +000037/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000038static char **orig_argv;
39static int orig_argc;
40
Guido van Rossumbceccf52001-04-10 22:07:43 +000041/* command line options */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042#define BASE_OPTS "c:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000043
44#ifndef RISCOS
45#define PROGRAM_OPTS BASE_OPTS
46#else /*RISCOS*/
47/* extra option saying that we are running under a special task window
48 frontend; especially my_readline will behave different */
49#define PROGRAM_OPTS BASE_OPTS "w"
50/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000051extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000052#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000053
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000056"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000059static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000060Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000061-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000062-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000063-E : ignore environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064-h : print this help message and exit (also --help)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000065-i : inspect interactively after running script; forces a prompt even\n\
66 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000067";
68static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000069-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000070-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000071-OO : remove doc-strings in addition to the -O optimizations\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\
Thomas Wouters89f507f2006-12-13 04:49:30 +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\
Thomas Wouters89f507f2006-12-13 04:49:30 +000078-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
79 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000081-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000082-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000083file : program read from script file\n\
84- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000085";
Guido van Rossum393661d2001-08-31 17:40:15 +000086static char *usage_4 = "\
Thomas Wouters89f507f2006-12-13 04:49:30 +000087arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088Other environment variables:\n\
89PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000090PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000091 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000092PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
93 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000094PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000095";
96
97
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000098static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000099usage(int exitcode, char* program)
100{
Guido van Rossum393661d2001-08-31 17:40:15 +0000101 FILE *f = exitcode ? stderr : stdout;
102
103 fprintf(f, usage_line, program);
104 if (exitcode)
105 fprintf(f, "Try `python -h' for more information.\n");
106 else {
107 fprintf(f, usage_1);
108 fprintf(f, usage_2);
109 fprintf(f, usage_3);
110 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
111 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000112#if defined(__VMS)
113 if (exitcode == 0) {
114 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000115 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000116 }
117 else {
118 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000119 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000120 }
121#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000123#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000124 /*NOTREACHED*/
125}
126
Martin v. Löwis6caea372003-11-18 19:46:25 +0000127static void RunStartupFile(PyCompilerFlags *cf)
128{
129 char *startup = Py_GETENV("PYTHONSTARTUP");
130 if (startup != NULL && startup[0] != '\0') {
131 FILE *fp = fopen(startup, "r");
132 if (fp != NULL) {
133 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
134 PyErr_Clear();
135 fclose(fp);
136 }
137 }
138}
139
Thomas Woutersa9773292006-04-21 09:43:23 +0000140
141static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000142{
Thomas Woutersa9773292006-04-21 09:43:23 +0000143 PyObject *runpy, *runmodule, *runargs, *result;
144 runpy = PyImport_ImportModule("runpy");
145 if (runpy == NULL) {
146 fprintf(stderr, "Could not import runpy module\n");
147 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000148 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000149 runmodule = PyObject_GetAttrString(runpy, "run_module");
150 if (runmodule == NULL) {
151 fprintf(stderr, "Could not access runpy.run_module\n");
152 Py_DECREF(runpy);
153 return -1;
154 }
155 runargs = Py_BuildValue("sOsO", module,
156 Py_None, "__main__", Py_True);
157 if (runargs == NULL) {
158 fprintf(stderr,
159 "Could not create arguments for runpy.run_module\n");
160 Py_DECREF(runpy);
161 Py_DECREF(runmodule);
162 return -1;
163 }
164 result = PyObject_Call(runmodule, runargs, NULL);
165 if (result == NULL) {
166 PyErr_Print();
167 }
168 Py_DECREF(runpy);
169 Py_DECREF(runmodule);
170 Py_DECREF(runargs);
171 if (result == NULL) {
172 return -1;
173 }
174 Py_DECREF(result);
175 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000176}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000177
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000178/* Wait until threading._shutdown completes, provided
179 the threading module was imported in the first place.
180 The shutdown routine will wait until all non-daemon
181 "threading" threads have completed. */
182#include "abstract.h"
183static void
184WaitForThreadShutdown(void)
185{
186#ifdef WITH_THREAD
187 PyObject *result;
188 PyThreadState *tstate = PyThreadState_GET();
189 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
190 "threading");
191 if (threading == NULL) {
192 /* threading not imported */
193 PyErr_Clear();
194 return;
195 }
196 result = PyObject_CallMethod(threading, "_shutdown", "");
197 if (result == NULL)
198 PyErr_WriteUnraisable(threading);
199 else
200 Py_DECREF(result);
201 Py_DECREF(threading);
202#endif
203}
204
Guido van Rossum667d7041995-08-04 04:20:48 +0000205/* Main program */
206
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000207int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000208Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000209{
210 int c;
211 int sts;
212 char *command = NULL;
213 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000214 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000215 FILE *fp = stdin;
216 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000218 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000219 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000220 int help = 0;
221 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000222 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000223 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000224
Guido van Rossum393661d2001-08-31 17:40:15 +0000225 cf.cf_flags = 0;
226
Guido van Rossumac56b031996-07-21 02:33:38 +0000227 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000228 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000229
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000230#ifdef RISCOS
231 Py_RISCOSWimpFlag = 0;
232#endif
233
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000234 PySys_ResetWarnOptions();
235
Guido van Rossumbceccf52001-04-10 22:07:43 +0000236 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000237 if (c == 'c') {
238 /* -c is the last option; following arguments
239 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000240 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000242 if (command == NULL)
243 Py_FatalError(
244 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000245 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 strcat(command, "\n");
247 break;
248 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000249
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000250 if (c == 'm') {
251 /* -m is the last option; following arguments
252 that look like options are left for the
253 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000255 if (module == NULL)
256 Py_FatalError(
257 "not enough memory to copy -m argument");
258 strcpy(module, _PyOS_optarg);
259 break;
260 }
261
Guido van Rossum667d7041995-08-04 04:20:48 +0000262 switch (c) {
263
264 case 'd':
265 Py_DebugFlag++;
266 break;
267
268 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000269 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000270 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000271 break;
272
Guido van Rossum7614da61997-03-03 19:14:45 +0000273 case 'O':
274 Py_OptimizeFlag++;
275 break;
276
Guido van Rossum7922bd71997-08-29 22:34:47 +0000277 case 'S':
278 Py_NoSiteFlag++;
279 break;
280
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000281 case 'E':
282 Py_IgnoreEnvironmentFlag++;
283 break;
284
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000285 case 't':
286 Py_TabcheckFlag++;
287 break;
288
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 case 'u':
290 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000291 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000292 break;
293
294 case 'v':
295 Py_VerboseFlag++;
296 break;
297
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000298#ifdef RISCOS
299 case 'w':
300 Py_RISCOSWimpFlag = 1;
301 break;
302#endif
303
Guido van Rossuma075ce11997-12-05 21:56:45 +0000304 case 'x':
305 skipfirstline = 1;
306 break;
307
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000308 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000310 help++;
311 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000312
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000313 case 'V':
314 version++;
315 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000316
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000317 case 'W':
318 PySys_AddWarnOption(_PyOS_optarg);
319 break;
320
Guido van Rossum667d7041995-08-04 04:20:48 +0000321 /* This space reserved for other options */
322
323 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000324 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000325 /*NOTREACHED*/
326
327 }
328 }
329
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000330 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000331 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000332
333 if (version) {
334 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000335 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000336 }
337
Guido van Rossumd8faa362007-04-27 19:54:29 +0000338 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000339 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000340 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000341 if (!saw_unbuffered_flag &&
342 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
343 unbuffered = 1;
344
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000345 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000346 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000347 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000348#ifdef __VMS
349 filename = decc$translate_vms(argv[_PyOS_optind]);
350 if (filename == (char *)0 || filename == (char *)-1)
351 filename = argv[_PyOS_optind];
352
353#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000354 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000355#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000356 if (filename != NULL) {
357 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000358#ifdef HAVE_STRERROR
359 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
360 argv[0], filename, errno, strerror(errno));
361#else
362 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
363 argv[0], filename, errno);
364#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000365 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000366 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000367 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000368 int ch;
369 /* Push back first newline so line numbers
370 remain the same */
371 while ((ch = getc(fp)) != EOF) {
372 if (ch == '\n') {
373 (void)ungetc(ch, fp);
374 break;
375 }
376 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000377 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000378 {
379 /* XXX: does this work on Win/Win64? (see posix_fstat) */
380 struct stat sb;
381 if (fstat(fileno(fp), &sb) == 0 &&
382 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000383 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
384 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000385 }
386 }
Guido van Rossum775af911997-02-14 19:50:32 +0000387 }
388 }
389
390 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
391
Guido van Rossum667d7041995-08-04 04:20:48 +0000392 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000393#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000394 _setmode(fileno(stdin), O_BINARY);
395 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000396#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000397#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000398 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
399 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
400 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000401#else /* !HAVE_SETVBUF */
402 setbuf(stdin, (char *)NULL);
403 setbuf(stdout, (char *)NULL);
404 setbuf(stderr, (char *)NULL);
405#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000406 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000407 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000408#ifdef MS_WINDOWS
409 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000410 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000411 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000412#else /* !MS_WINDOWS */
413#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000414 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
415 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000416#endif /* HAVE_SETVBUF */
417#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000418 /* Leave stderr alone - it should be unbuffered anyway. */
419 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000420#ifdef __VMS
421 else {
422 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
423 }
424#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000425
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000426#ifdef __APPLE__
427 /* On MacOS X, when the Python interpreter is embedded in an
428 application bundle, it gets executed by a bootstrapping script
429 that does os.execve() with an argv[0] that's different from the
430 actual Python executable. This is needed to keep the Finder happy,
431 or rather, to work around Apple's overly strict requirements of
432 the process name. However, we still need a usable sys.executable,
433 so the actual executable path is passed in an environment variable.
434 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
435 script. */
436 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
437 Py_SetProgramName(p);
438 else
439 Py_SetProgramName(argv[0]);
440#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000441 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000442#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000443 Py_Initialize();
444
Guido van Rossum667d7041995-08-04 04:20:48 +0000445 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000446 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000447 fprintf(stderr, "Python %s on %s\n",
448 Py_GetVersion(), Py_GetPlatform());
449 if (!Py_NoSiteFlag)
450 fprintf(stderr, "%s\n", COPYRIGHT);
451 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000452
Guido van Rossum667d7041995-08-04 04:20:48 +0000453 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000454 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
455 _PyOS_optind--;
456 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000457 }
458
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000459 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000460 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
461 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000462 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000463 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000464 }
465
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000466 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000467
Guido van Rossumd8faa362007-04-27 19:54:29 +0000468 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000469 isatty(fileno(stdin))) {
470 PyObject *v;
471 v = PyImport_ImportModule("readline");
472 if (v == NULL)
473 PyErr_Clear();
474 else
475 Py_DECREF(v);
476 }
477
Guido van Rossum667d7041995-08-04 04:20:48 +0000478 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000479 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000480 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000481 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000482 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000483 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000484 }
485 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000486 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000487 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000488 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000489 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000490 /* XXX */
491 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000492 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000493 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000494 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000495 }
496
Barry Warsawd86dcd32003-06-29 17:07:06 +0000497 /* Check this environment variable at the end, to give programs the
498 * opportunity to set it from Python.
499 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000500 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000501 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
502 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000503 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000504 }
505
Guido van Rossumd8faa362007-04-27 19:54:29 +0000506 if (Py_InspectFlag && stdin_is_interactive &&
507 (filename != NULL || command != NULL || module != NULL)) {
508 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000509 /* XXX */
510 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000511 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000512
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000513 WaitForThreadShutdown();
514
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000515 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000516#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000517 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000518 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
519#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000520
521#ifdef __INSURE__
522 /* Insure++ is a memory analysis tool that aids in discovering
523 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000524 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000525 * (which it is). Under normal circumstances, this is fine because
526 * the memory will be automatically reclaimed by the system. Under
527 * memory debugging, it's a huge source of useless noise, so we
528 * trade off slower shutdown for less distraction in the memory
529 * reports. -baw
530 */
531 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000532 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000533#endif /* __INSURE__ */
534
Guido van Rossum05f7c501997-08-02 03:00:42 +0000535 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000536}
537
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000538/* this is gonna seem *real weird*, but if you put some other code between
539 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
540 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000541
Guido van Rossum667d7041995-08-04 04:20:48 +0000542/* Make the *original* argc/argv available to other modules.
543 This is rare, but it is needed by the secureware extension. */
544
545void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000546Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000547{
548 *argc = orig_argc;
549 *argv = orig_argv;
550}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551
552#ifdef __cplusplus
553}
554#endif
555