blob: d604879a28c4d4b78996d9640a26892ed4776625 [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;
217 int inspect = 0;
218 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000219 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000220 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000221 int help = 0;
222 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000223 int saw_inspect_flag = 0;
224 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000225 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000226
Guido van Rossum393661d2001-08-31 17:40:15 +0000227 cf.cf_flags = 0;
228
Guido van Rossumac56b031996-07-21 02:33:38 +0000229 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000231
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000232#ifdef RISCOS
233 Py_RISCOSWimpFlag = 0;
234#endif
235
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000236 PySys_ResetWarnOptions();
237
Guido van Rossumbceccf52001-04-10 22:07:43 +0000238 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000239 if (c == 'c') {
240 /* -c is the last option; following arguments
241 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000242 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000244 if (command == NULL)
245 Py_FatalError(
246 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000247 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000248 strcat(command, "\n");
249 break;
250 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000251
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000252 if (c == 'm') {
253 /* -m is the last option; following arguments
254 that look like options are left for the
255 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000257 if (module == NULL)
258 Py_FatalError(
259 "not enough memory to copy -m argument");
260 strcpy(module, _PyOS_optarg);
261 break;
262 }
263
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 switch (c) {
265
266 case 'd':
267 Py_DebugFlag++;
268 break;
269
270 case 'i':
271 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000272 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000273 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000274 break;
275
Guido van Rossum7614da61997-03-03 19:14:45 +0000276 case 'O':
277 Py_OptimizeFlag++;
278 break;
279
Guido van Rossum7922bd71997-08-29 22:34:47 +0000280 case 'S':
281 Py_NoSiteFlag++;
282 break;
283
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000284 case 'E':
285 Py_IgnoreEnvironmentFlag++;
286 break;
287
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000288 case 't':
289 Py_TabcheckFlag++;
290 break;
291
Guido van Rossum667d7041995-08-04 04:20:48 +0000292 case 'u':
293 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000294 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000295 break;
296
297 case 'v':
298 Py_VerboseFlag++;
299 break;
300
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000301#ifdef RISCOS
302 case 'w':
303 Py_RISCOSWimpFlag = 1;
304 break;
305#endif
306
Guido van Rossuma075ce11997-12-05 21:56:45 +0000307 case 'x':
308 skipfirstline = 1;
309 break;
310
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000311 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000312 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000313 help++;
314 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000315
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000316 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) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000463 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
464 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000465 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000466 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000467 }
468
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000469 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000470
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000471 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000472 isatty(fileno(stdin))) {
473 PyObject *v;
474 v = PyImport_ImportModule("readline");
475 if (v == NULL)
476 PyErr_Clear();
477 else
478 Py_DECREF(v);
479 }
480
Guido van Rossum667d7041995-08-04 04:20:48 +0000481 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000482 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000483 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000484 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000485 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000486 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000487 }
488 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000489 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000490 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000491 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000492 /* XXX */
493 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000494 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000495 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000496 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000497 }
498
Barry Warsawd86dcd32003-06-29 17:07:06 +0000499 /* Check this environment variable at the end, to give programs the
500 * opportunity to set it from Python.
501 */
502 if (!saw_inspect_flag &&
503 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
504 {
505 inspect = 1;
506 }
507
Guido van Rossum775af911997-02-14 19:50:32 +0000508 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000509 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000510 /* XXX */
511 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
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
524 * interned string dictionary is flagged as being in use at exit
525 * (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();
532#endif /* __INSURE__ */
533
Guido van Rossum05f7c501997-08-02 03:00:42 +0000534 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000535}
536
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000537/* this is gonna seem *real weird*, but if you put some other code between
538 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
539 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000540
Guido van Rossum667d7041995-08-04 04:20:48 +0000541/* Make the *original* argc/argv available to other modules.
542 This is rare, but it is needed by the secureware extension. */
543
544void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000545Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000546{
547 *argc = orig_argc;
548 *argv = orig_argv;
549}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550
551#ifdef __cplusplus
552}
553#endif
554