blob: ee4a1b8ab021f43309d3d38f7eb73d828f200a6d [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__)
Martin v. Löwis945362c2007-08-30 14:57:25 +000012#include <windows.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000014#include <fcntl.h>
15#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017
Martin v. Löwis945362c2007-08-30 14:57:25 +000018#ifdef _MSC_VER
19#include <crtdbg.h>
20#endif
21
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000022#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000023#define PYTHONHOMEHELP "<prefix>\\lib"
24#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#if defined(PYOS_OS2) && defined(PYCC_GCC)
26#define PYTHONHOMEHELP "<prefix>/Lib"
27#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000028#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000029#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000030#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000031
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000032#include "pygetopt.h"
33
Guido van Rossuma22865e2000-09-05 04:41:18 +000034#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000035 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
36 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Guido van Rossumac56b031996-07-21 02:33:38 +000042/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000043static char **orig_argv;
44static int orig_argc;
45
Guido van Rossumbceccf52001-04-10 22:07:43 +000046/* command line options */
Guido van Rossum98297ee2007-11-06 21:34:58 +000047#define BASE_OPTS "bc:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000048
Guido van Rossumbceccf52001-04-10 22:07:43 +000049#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000050
Guido van Rossum667d7041995-08-04 04:20:48 +000051/* Short usage message (with %s for argv0) */
52static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000053"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000054
55/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000056static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000057Options and arguments (and corresponding environment variables):\n\
Guido van Rossum98297ee2007-11-06 21:34:58 +000058-b : issue warnings about str(bytes_instance), str(buffer_instance)\n\
59 and comparing bytes/buffer with str. (-bb: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000060-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +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\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000064";
65static char *usage_2 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000066-i : inspect interactively after running script; forces a prompt even\n\
67 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000068-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000069-O : optimize generated bytecode slightly; 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 Rossum7922bd71997-08-29 22:34:47 +000071-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000072-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000073";
74static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000075-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000076 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000077-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
78 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +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 Rossum7922bd71997-08-29 22:34:47 +000082";
Guido van Rossum393661d2001-08-31 17:40:15 +000083static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000084file : program read from script file\n\
85- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000086arg ...: arguments passed to program in sys.argv[1:]\n\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
Thomas Woutersa9773292006-04-21 09:43:23 +0000139
140static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000141{
Thomas Woutersa9773292006-04-21 09:43:23 +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 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000148 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000149 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000150 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000151 Py_DECREF(runpy);
152 return -1;
153 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000154 runargs = Py_BuildValue("(s)", module);
Thomas Woutersa9773292006-04-21 09:43:23 +0000155 if (runargs == NULL) {
156 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000157 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000158 Py_DECREF(runpy);
159 Py_DECREF(runmodule);
160 return -1;
161 }
162 result = PyObject_Call(runmodule, runargs, NULL);
163 if (result == NULL) {
164 PyErr_Print();
165 }
166 Py_DECREF(runpy);
167 Py_DECREF(runmodule);
168 Py_DECREF(runargs);
169 if (result == NULL) {
170 return -1;
171 }
172 Py_DECREF(result);
173 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000174}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000175
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000176/* Wait until threading._shutdown completes, provided
177 the threading module was imported in the first place.
178 The shutdown routine will wait until all non-daemon
179 "threading" threads have completed. */
180#include "abstract.h"
181static void
182WaitForThreadShutdown(void)
183{
184#ifdef WITH_THREAD
185 PyObject *result;
186 PyThreadState *tstate = PyThreadState_GET();
187 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
188 "threading");
189 if (threading == NULL) {
190 /* threading not imported */
191 PyErr_Clear();
192 return;
193 }
194 result = PyObject_CallMethod(threading, "_shutdown", "");
195 if (result == NULL)
196 PyErr_WriteUnraisable(threading);
197 else
198 Py_DECREF(result);
199 Py_DECREF(threading);
200#endif
201}
202
Guido van Rossum667d7041995-08-04 04:20:48 +0000203/* Main program */
204
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000205int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000206Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000207{
208 int c;
209 int sts;
210 char *command = NULL;
211 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000212 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000213 FILE *fp = stdin;
214 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000215 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000216 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000217 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000218 int help = 0;
219 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000220 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000221 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000222
Guido van Rossum393661d2001-08-31 17:40:15 +0000223 cf.cf_flags = 0;
224
Guido van Rossumac56b031996-07-21 02:33:38 +0000225 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000226 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000227
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000228 PySys_ResetWarnOptions();
229
Guido van Rossumbceccf52001-04-10 22:07:43 +0000230 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000231 if (c == 'c') {
232 /* -c is the last option; following arguments
233 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000234 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000236 if (command == NULL)
237 Py_FatalError(
238 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000239 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000240 strcat(command, "\n");
241 break;
242 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000243
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000244 if (c == 'm') {
245 /* -m is the last option; following arguments
246 that look like options are left for the
247 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000249 if (module == NULL)
250 Py_FatalError(
251 "not enough memory to copy -m argument");
252 strcpy(module, _PyOS_optarg);
253 break;
254 }
255
Guido van Rossum667d7041995-08-04 04:20:48 +0000256 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000257 case 'b':
258 Py_BytesWarningFlag++;
259 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000260
261 case 'd':
262 Py_DebugFlag++;
263 break;
264
265 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000266 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000267 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 break;
269
Guido van Rossum7614da61997-03-03 19:14:45 +0000270 case 'O':
271 Py_OptimizeFlag++;
272 break;
273
Guido van Rossum7922bd71997-08-29 22:34:47 +0000274 case 'S':
275 Py_NoSiteFlag++;
276 break;
277
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000278 case 'E':
279 Py_IgnoreEnvironmentFlag++;
280 break;
281
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000282 case 't':
283 Py_TabcheckFlag++;
284 break;
285
Guido van Rossum667d7041995-08-04 04:20:48 +0000286 case 'u':
287 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000288 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 break;
290
291 case 'v':
292 Py_VerboseFlag++;
293 break;
294
Guido van Rossuma075ce11997-12-05 21:56:45 +0000295 case 'x':
296 skipfirstline = 1;
297 break;
298
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000299 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000301 help++;
302 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000303
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000304 case 'V':
305 version++;
306 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000307
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000308 case 'W':
309 PySys_AddWarnOption(_PyOS_optarg);
310 break;
311
Guido van Rossum667d7041995-08-04 04:20:48 +0000312 /* This space reserved for other options */
313
314 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000315 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000316 /*NOTREACHED*/
317
318 }
319 }
320
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000321 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000322 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000323
324 if (version) {
325 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000326 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000327 }
328
Guido van Rossumd8faa362007-04-27 19:54:29 +0000329 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000330 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000331 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000332 if (!saw_unbuffered_flag &&
333 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
334 unbuffered = 1;
335
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000336 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000337 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000338 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000339#ifdef __VMS
340 filename = decc$translate_vms(argv[_PyOS_optind]);
341 if (filename == (char *)0 || filename == (char *)-1)
342 filename = argv[_PyOS_optind];
343
344#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000345 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000346#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000347 if (filename != NULL) {
348 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000349#ifdef HAVE_STRERROR
350 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
351 argv[0], filename, errno, strerror(errno));
352#else
353 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
354 argv[0], filename, errno);
355#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000356 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000357 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000358 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000359 int ch;
360 /* Push back first newline so line numbers
361 remain the same */
362 while ((ch = getc(fp)) != EOF) {
363 if (ch == '\n') {
364 (void)ungetc(ch, fp);
365 break;
366 }
367 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000368 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000369 {
370 /* XXX: does this work on Win/Win64? (see posix_fstat) */
371 struct stat sb;
372 if (fstat(fileno(fp), &sb) == 0 &&
373 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000374 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000375 fclose(fp);
Neal Norwitz72c2c062006-03-09 05:58:11 +0000376 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000377 }
378 }
Guido van Rossum775af911997-02-14 19:50:32 +0000379 }
380 }
381
382 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
383
Guido van Rossum667d7041995-08-04 04:20:48 +0000384 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000385#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000386 _setmode(fileno(stdin), O_BINARY);
387 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000388#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000389#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000390 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
391 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
392 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000393#else /* !HAVE_SETVBUF */
394 setbuf(stdin, (char *)NULL);
395 setbuf(stdout, (char *)NULL);
396 setbuf(stderr, (char *)NULL);
397#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000399 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000400#ifdef MS_WINDOWS
401 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000402 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000403 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000404#else /* !MS_WINDOWS */
405#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000406 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
407 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000408#endif /* HAVE_SETVBUF */
409#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000410 /* Leave stderr alone - it should be unbuffered anyway. */
411 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000412#ifdef __VMS
413 else {
414 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
415 }
416#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000417
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000418#ifdef __APPLE__
419 /* On MacOS X, when the Python interpreter is embedded in an
420 application bundle, it gets executed by a bootstrapping script
421 that does os.execve() with an argv[0] that's different from the
422 actual Python executable. This is needed to keep the Finder happy,
423 or rather, to work around Apple's overly strict requirements of
424 the process name. However, we still need a usable sys.executable,
425 so the actual executable path is passed in an environment variable.
426 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
427 script. */
428 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
429 Py_SetProgramName(p);
430 else
431 Py_SetProgramName(argv[0]);
432#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000433 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000434#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000435 Py_Initialize();
436
Guido van Rossum667d7041995-08-04 04:20:48 +0000437 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000438 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000439 fprintf(stderr, "Python %s on %s\n",
440 Py_GetVersion(), Py_GetPlatform());
441 if (!Py_NoSiteFlag)
442 fprintf(stderr, "%s\n", COPYRIGHT);
443 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000444
Guido van Rossum667d7041995-08-04 04:20:48 +0000445 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000446 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
447 _PyOS_optind--;
448 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000449 }
450
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000451 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000452 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
453 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000454 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000456 }
457
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000458 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000459
Guido van Rossumd8faa362007-04-27 19:54:29 +0000460 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000461 isatty(fileno(stdin))) {
462 PyObject *v;
463 v = PyImport_ImportModule("readline");
464 if (v == NULL)
465 PyErr_Clear();
466 else
467 Py_DECREF(v);
468 }
469
Guido van Rossum667d7041995-08-04 04:20:48 +0000470 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000471 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000472 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000473 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000474 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000475 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000476 }
477 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000478 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000479 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000480 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000481 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000482 /* XXX */
483 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000484 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000485 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000486 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000487 }
488
Barry Warsawd86dcd32003-06-29 17:07:06 +0000489 /* Check this environment variable at the end, to give programs the
490 * opportunity to set it from Python.
491 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000493 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
494 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000495 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000496 }
497
Guido van Rossumd8faa362007-04-27 19:54:29 +0000498 if (Py_InspectFlag && stdin_is_interactive &&
499 (filename != NULL || command != NULL || module != NULL)) {
500 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000501 /* XXX */
502 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000503 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000504
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000505 WaitForThreadShutdown();
506
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000507 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000508
509#ifdef __INSURE__
510 /* Insure++ is a memory analysis tool that aids in discovering
511 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000512 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000513 * (which it is). Under normal circumstances, this is fine because
514 * the memory will be automatically reclaimed by the system. Under
515 * memory debugging, it's a huge source of useless noise, so we
516 * trade off slower shutdown for less distraction in the memory
517 * reports. -baw
518 */
519 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000520 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000521#endif /* __INSURE__ */
522
Guido van Rossum05f7c501997-08-02 03:00:42 +0000523 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000524}
525
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000526/* this is gonna seem *real weird*, but if you put some other code between
527 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
528 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000529
Guido van Rossum667d7041995-08-04 04:20:48 +0000530/* Make the *original* argc/argv available to other modules.
531 This is rare, but it is needed by the secureware extension. */
532
533void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000534Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000535{
536 *argc = orig_argc;
537 *argv = orig_argv;
538}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539
540#ifdef __cplusplus
541}
542#endif
543