blob: 22f5c0a954f5b2e6c938b75b5f36b12782ae0b40 [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
Guido van Rossumbceccf52001-04-10 22:07:43 +000044#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000045
Guido van Rossum667d7041995-08-04 04:20:48 +000046/* Short usage message (with %s for argv0) */
47static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000048"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000049
50/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000051static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000052Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000053-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000054-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000055-E : ignore environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056-h : print this help message and exit (also --help)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000057-i : inspect interactively after running script; forces a prompt even\n\
58 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000059";
60static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000061-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000062-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000063-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000064-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000065-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000066-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000067";
68static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000069 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000070-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
71 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000073-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000074-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000075file : program read from script file\n\
76- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000077";
Guido van Rossum393661d2001-08-31 17:40:15 +000078static char *usage_4 = "\
Thomas Wouters89f507f2006-12-13 04:49:30 +000079arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080Other environment variables:\n\
81PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000082PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000083 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000084PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
85 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000086PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000087";
88
89
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000090static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000091usage(int exitcode, char* program)
92{
Guido van Rossum393661d2001-08-31 17:40:15 +000093 FILE *f = exitcode ? stderr : stdout;
94
95 fprintf(f, usage_line, program);
96 if (exitcode)
97 fprintf(f, "Try `python -h' for more information.\n");
98 else {
99 fprintf(f, usage_1);
100 fprintf(f, usage_2);
101 fprintf(f, usage_3);
102 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
103 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000104#if defined(__VMS)
105 if (exitcode == 0) {
106 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000107 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000108 }
109 else {
110 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000111 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000112 }
113#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000114 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000116 /*NOTREACHED*/
117}
118
Martin v. Löwis6caea372003-11-18 19:46:25 +0000119static void RunStartupFile(PyCompilerFlags *cf)
120{
121 char *startup = Py_GETENV("PYTHONSTARTUP");
122 if (startup != NULL && startup[0] != '\0') {
123 FILE *fp = fopen(startup, "r");
124 if (fp != NULL) {
125 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
126 PyErr_Clear();
127 fclose(fp);
128 }
129 }
130}
131
Thomas Woutersa9773292006-04-21 09:43:23 +0000132
133static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000134{
Thomas Woutersa9773292006-04-21 09:43:23 +0000135 PyObject *runpy, *runmodule, *runargs, *result;
136 runpy = PyImport_ImportModule("runpy");
137 if (runpy == NULL) {
138 fprintf(stderr, "Could not import runpy module\n");
139 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000140 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000141 runmodule = PyObject_GetAttrString(runpy, "run_module");
142 if (runmodule == NULL) {
143 fprintf(stderr, "Could not access runpy.run_module\n");
144 Py_DECREF(runpy);
145 return -1;
146 }
147 runargs = Py_BuildValue("sOsO", module,
148 Py_None, "__main__", Py_True);
149 if (runargs == NULL) {
150 fprintf(stderr,
151 "Could not create arguments for runpy.run_module\n");
152 Py_DECREF(runpy);
153 Py_DECREF(runmodule);
154 return -1;
155 }
156 result = PyObject_Call(runmodule, runargs, NULL);
157 if (result == NULL) {
158 PyErr_Print();
159 }
160 Py_DECREF(runpy);
161 Py_DECREF(runmodule);
162 Py_DECREF(runargs);
163 if (result == NULL) {
164 return -1;
165 }
166 Py_DECREF(result);
167 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000168}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000169
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000170/* Wait until threading._shutdown completes, provided
171 the threading module was imported in the first place.
172 The shutdown routine will wait until all non-daemon
173 "threading" threads have completed. */
174#include "abstract.h"
175static void
176WaitForThreadShutdown(void)
177{
178#ifdef WITH_THREAD
179 PyObject *result;
180 PyThreadState *tstate = PyThreadState_GET();
181 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
182 "threading");
183 if (threading == NULL) {
184 /* threading not imported */
185 PyErr_Clear();
186 return;
187 }
188 result = PyObject_CallMethod(threading, "_shutdown", "");
189 if (result == NULL)
190 PyErr_WriteUnraisable(threading);
191 else
192 Py_DECREF(result);
193 Py_DECREF(threading);
194#endif
195}
196
Guido van Rossum667d7041995-08-04 04:20:48 +0000197/* Main program */
198
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000199int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000200Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000201{
202 int c;
203 int sts;
204 char *command = NULL;
205 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000206 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000207 FILE *fp = stdin;
208 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000209 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000210 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000211 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000212 int help = 0;
213 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000214 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000215 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000216
Guido van Rossum393661d2001-08-31 17:40:15 +0000217 cf.cf_flags = 0;
218
Guido van Rossumac56b031996-07-21 02:33:38 +0000219 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000220 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000221
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000222 PySys_ResetWarnOptions();
223
Guido van Rossumbceccf52001-04-10 22:07:43 +0000224 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000225 if (c == 'c') {
226 /* -c is the last option; following arguments
227 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000228 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 if (command == NULL)
231 Py_FatalError(
232 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000233 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000234 strcat(command, "\n");
235 break;
236 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000237
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000238 if (c == 'm') {
239 /* -m is the last option; following arguments
240 that look like options are left for the
241 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000243 if (module == NULL)
244 Py_FatalError(
245 "not enough memory to copy -m argument");
246 strcpy(module, _PyOS_optarg);
247 break;
248 }
249
Guido van Rossum667d7041995-08-04 04:20:48 +0000250 switch (c) {
251
252 case 'd':
253 Py_DebugFlag++;
254 break;
255
256 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000257 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000258 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 break;
260
Guido van Rossum7614da61997-03-03 19:14:45 +0000261 case 'O':
262 Py_OptimizeFlag++;
263 break;
264
Guido van Rossum7922bd71997-08-29 22:34:47 +0000265 case 'S':
266 Py_NoSiteFlag++;
267 break;
268
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000269 case 'E':
270 Py_IgnoreEnvironmentFlag++;
271 break;
272
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000273 case 't':
274 Py_TabcheckFlag++;
275 break;
276
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 case 'u':
278 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000279 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000280 break;
281
282 case 'v':
283 Py_VerboseFlag++;
284 break;
285
Guido van Rossuma075ce11997-12-05 21:56:45 +0000286 case 'x':
287 skipfirstline = 1;
288 break;
289
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000290 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000292 help++;
293 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000294
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000295 case 'V':
296 version++;
297 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000298
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000299 case 'W':
300 PySys_AddWarnOption(_PyOS_optarg);
301 break;
302
Guido van Rossum667d7041995-08-04 04:20:48 +0000303 /* This space reserved for other options */
304
305 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000306 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000307 /*NOTREACHED*/
308
309 }
310 }
311
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000312 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000313 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000314
315 if (version) {
316 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000317 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000318 }
319
Guido van Rossumd8faa362007-04-27 19:54:29 +0000320 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000321 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000322 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000323 if (!saw_unbuffered_flag &&
324 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
325 unbuffered = 1;
326
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000327 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000328 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000329 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000330#ifdef __VMS
331 filename = decc$translate_vms(argv[_PyOS_optind]);
332 if (filename == (char *)0 || filename == (char *)-1)
333 filename = argv[_PyOS_optind];
334
335#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000336 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000337#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000338 if (filename != NULL) {
339 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000340#ifdef HAVE_STRERROR
341 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
342 argv[0], filename, errno, strerror(errno));
343#else
344 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
345 argv[0], filename, errno);
346#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000347 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000348 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000349 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000350 int ch;
351 /* Push back first newline so line numbers
352 remain the same */
353 while ((ch = getc(fp)) != EOF) {
354 if (ch == '\n') {
355 (void)ungetc(ch, fp);
356 break;
357 }
358 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000359 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000360 {
361 /* XXX: does this work on Win/Win64? (see posix_fstat) */
362 struct stat sb;
363 if (fstat(fileno(fp), &sb) == 0 &&
364 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000365 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
366 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000367 }
368 }
Guido van Rossum775af911997-02-14 19:50:32 +0000369 }
370 }
371
372 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
373
Guido van Rossum667d7041995-08-04 04:20:48 +0000374 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000375#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000376 _setmode(fileno(stdin), O_BINARY);
377 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000378#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000379#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000380 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
381 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
382 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000383#else /* !HAVE_SETVBUF */
384 setbuf(stdin, (char *)NULL);
385 setbuf(stdout, (char *)NULL);
386 setbuf(stderr, (char *)NULL);
387#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000388 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000389 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000390#ifdef MS_WINDOWS
391 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000392 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000393 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000394#else /* !MS_WINDOWS */
395#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000396 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
397 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000398#endif /* HAVE_SETVBUF */
399#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000400 /* Leave stderr alone - it should be unbuffered anyway. */
401 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000402#ifdef __VMS
403 else {
404 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
405 }
406#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000407
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000408#ifdef __APPLE__
409 /* On MacOS X, when the Python interpreter is embedded in an
410 application bundle, it gets executed by a bootstrapping script
411 that does os.execve() with an argv[0] that's different from the
412 actual Python executable. This is needed to keep the Finder happy,
413 or rather, to work around Apple's overly strict requirements of
414 the process name. However, we still need a usable sys.executable,
415 so the actual executable path is passed in an environment variable.
416 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
417 script. */
418 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
419 Py_SetProgramName(p);
420 else
421 Py_SetProgramName(argv[0]);
422#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000423 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000424#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000425 Py_Initialize();
426
Guido van Rossum667d7041995-08-04 04:20:48 +0000427 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000428 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000429 fprintf(stderr, "Python %s on %s\n",
430 Py_GetVersion(), Py_GetPlatform());
431 if (!Py_NoSiteFlag)
432 fprintf(stderr, "%s\n", COPYRIGHT);
433 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000434
Guido van Rossum667d7041995-08-04 04:20:48 +0000435 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000436 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
437 _PyOS_optind--;
438 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000439 }
440
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000441 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
443 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000444 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000445 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000446 }
447
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000448 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000449
Guido van Rossumd8faa362007-04-27 19:54:29 +0000450 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000451 isatty(fileno(stdin))) {
452 PyObject *v;
453 v = PyImport_ImportModule("readline");
454 if (v == NULL)
455 PyErr_Clear();
456 else
457 Py_DECREF(v);
458 }
459
Guido van Rossum667d7041995-08-04 04:20:48 +0000460 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000461 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000462 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000463 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000464 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000465 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000466 }
467 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000468 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000469 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000470 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000471 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000472 /* XXX */
473 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000474 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000475 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000476 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000477 }
478
Barry Warsawd86dcd32003-06-29 17:07:06 +0000479 /* Check this environment variable at the end, to give programs the
480 * opportunity to set it from Python.
481 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000482 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000483 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
484 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000485 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000486 }
487
Guido van Rossumd8faa362007-04-27 19:54:29 +0000488 if (Py_InspectFlag && stdin_is_interactive &&
489 (filename != NULL || command != NULL || module != NULL)) {
490 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000491 /* XXX */
492 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000493 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000494
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000495 WaitForThreadShutdown();
496
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000497 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000498
499#ifdef __INSURE__
500 /* Insure++ is a memory analysis tool that aids in discovering
501 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000502 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000503 * (which it is). Under normal circumstances, this is fine because
504 * the memory will be automatically reclaimed by the system. Under
505 * memory debugging, it's a huge source of useless noise, so we
506 * trade off slower shutdown for less distraction in the memory
507 * reports. -baw
508 */
509 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000510 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000511#endif /* __INSURE__ */
512
Guido van Rossum05f7c501997-08-02 03:00:42 +0000513 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000514}
515
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000516/* this is gonna seem *real weird*, but if you put some other code between
517 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
518 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000519
Guido van Rossum667d7041995-08-04 04:20:48 +0000520/* Make the *original* argc/argv available to other modules.
521 This is rare, but it is needed by the secureware extension. */
522
523void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000524Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000525{
526 *argc = orig_argc;
527 *argv = orig_argv;
528}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529
530#ifdef __cplusplus
531}
532#endif
533