blob: 4fb4d2d4c3c6c27c1bb2fee357e397d68c11dca3 [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 Woutersed03b412007-08-28 21:37:11 +0000141 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000142 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000143 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000144 Py_DECREF(runpy);
145 return -1;
146 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000147 runargs = Py_BuildValue("(s)", module);
Thomas Woutersa9773292006-04-21 09:43:23 +0000148 if (runargs == NULL) {
149 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000150 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000151 Py_DECREF(runpy);
152 Py_DECREF(runmodule);
153 return -1;
154 }
155 result = PyObject_Call(runmodule, runargs, NULL);
156 if (result == NULL) {
157 PyErr_Print();
158 }
159 Py_DECREF(runpy);
160 Py_DECREF(runmodule);
161 Py_DECREF(runargs);
162 if (result == NULL) {
163 return -1;
164 }
165 Py_DECREF(result);
166 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000167}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000168
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000169/* Wait until threading._shutdown completes, provided
170 the threading module was imported in the first place.
171 The shutdown routine will wait until all non-daemon
172 "threading" threads have completed. */
173#include "abstract.h"
174static void
175WaitForThreadShutdown(void)
176{
177#ifdef WITH_THREAD
178 PyObject *result;
179 PyThreadState *tstate = PyThreadState_GET();
180 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
181 "threading");
182 if (threading == NULL) {
183 /* threading not imported */
184 PyErr_Clear();
185 return;
186 }
187 result = PyObject_CallMethod(threading, "_shutdown", "");
188 if (result == NULL)
189 PyErr_WriteUnraisable(threading);
190 else
191 Py_DECREF(result);
192 Py_DECREF(threading);
193#endif
194}
195
Guido van Rossum667d7041995-08-04 04:20:48 +0000196/* Main program */
197
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000198int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000199Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000200{
201 int c;
202 int sts;
203 char *command = NULL;
204 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000205 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000206 FILE *fp = stdin;
207 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000208 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000209 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000210 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000211 int help = 0;
212 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000213 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000214 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000215
Guido van Rossum393661d2001-08-31 17:40:15 +0000216 cf.cf_flags = 0;
217
Guido van Rossumac56b031996-07-21 02:33:38 +0000218 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000219 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000220
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000221 PySys_ResetWarnOptions();
222
Guido van Rossumbceccf52001-04-10 22:07:43 +0000223 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000224 if (c == 'c') {
225 /* -c is the last option; following arguments
226 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000227 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000229 if (command == NULL)
230 Py_FatalError(
231 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000232 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000233 strcat(command, "\n");
234 break;
235 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000236
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000237 if (c == 'm') {
238 /* -m is the last option; following arguments
239 that look like options are left for the
240 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000242 if (module == NULL)
243 Py_FatalError(
244 "not enough memory to copy -m argument");
245 strcpy(module, _PyOS_optarg);
246 break;
247 }
248
Guido van Rossum667d7041995-08-04 04:20:48 +0000249 switch (c) {
250
251 case 'd':
252 Py_DebugFlag++;
253 break;
254
255 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000256 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000257 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000258 break;
259
Guido van Rossum7614da61997-03-03 19:14:45 +0000260 case 'O':
261 Py_OptimizeFlag++;
262 break;
263
Guido van Rossum7922bd71997-08-29 22:34:47 +0000264 case 'S':
265 Py_NoSiteFlag++;
266 break;
267
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000268 case 'E':
269 Py_IgnoreEnvironmentFlag++;
270 break;
271
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000272 case 't':
273 Py_TabcheckFlag++;
274 break;
275
Guido van Rossum667d7041995-08-04 04:20:48 +0000276 case 'u':
277 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000278 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000279 break;
280
281 case 'v':
282 Py_VerboseFlag++;
283 break;
284
Guido van Rossuma075ce11997-12-05 21:56:45 +0000285 case 'x':
286 skipfirstline = 1;
287 break;
288
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000289 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000290 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000291 help++;
292 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000293
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000294 case 'V':
295 version++;
296 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000297
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000298 case 'W':
299 PySys_AddWarnOption(_PyOS_optarg);
300 break;
301
Guido van Rossum667d7041995-08-04 04:20:48 +0000302 /* This space reserved for other options */
303
304 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000305 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000306 /*NOTREACHED*/
307
308 }
309 }
310
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000311 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000312 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000313
314 if (version) {
315 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000316 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000317 }
318
Guido van Rossumd8faa362007-04-27 19:54:29 +0000319 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000320 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000321 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000322 if (!saw_unbuffered_flag &&
323 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
324 unbuffered = 1;
325
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000326 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000327 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000328 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000329#ifdef __VMS
330 filename = decc$translate_vms(argv[_PyOS_optind]);
331 if (filename == (char *)0 || filename == (char *)-1)
332 filename = argv[_PyOS_optind];
333
334#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000335 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000336#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000337 if (filename != NULL) {
338 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000339#ifdef HAVE_STRERROR
340 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
341 argv[0], filename, errno, strerror(errno));
342#else
343 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
344 argv[0], filename, errno);
345#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000346 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000347 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000348 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000349 int ch;
350 /* Push back first newline so line numbers
351 remain the same */
352 while ((ch = getc(fp)) != EOF) {
353 if (ch == '\n') {
354 (void)ungetc(ch, fp);
355 break;
356 }
357 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000358 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000359 {
360 /* XXX: does this work on Win/Win64? (see posix_fstat) */
361 struct stat sb;
362 if (fstat(fileno(fp), &sb) == 0 &&
363 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000364 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
365 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000366 }
367 }
Guido van Rossum775af911997-02-14 19:50:32 +0000368 }
369 }
370
371 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
372
Guido van Rossum667d7041995-08-04 04:20:48 +0000373 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000374#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000375 _setmode(fileno(stdin), O_BINARY);
376 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000377#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000378#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000379 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
380 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
381 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000382#else /* !HAVE_SETVBUF */
383 setbuf(stdin, (char *)NULL);
384 setbuf(stdout, (char *)NULL);
385 setbuf(stderr, (char *)NULL);
386#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000387 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000388 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000389#ifdef MS_WINDOWS
390 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000391 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000392 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000393#else /* !MS_WINDOWS */
394#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000395 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
396 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000397#endif /* HAVE_SETVBUF */
398#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000399 /* Leave stderr alone - it should be unbuffered anyway. */
400 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000401#ifdef __VMS
402 else {
403 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
404 }
405#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000406
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000407#ifdef __APPLE__
408 /* On MacOS X, when the Python interpreter is embedded in an
409 application bundle, it gets executed by a bootstrapping script
410 that does os.execve() with an argv[0] that's different from the
411 actual Python executable. This is needed to keep the Finder happy,
412 or rather, to work around Apple's overly strict requirements of
413 the process name. However, we still need a usable sys.executable,
414 so the actual executable path is passed in an environment variable.
415 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
416 script. */
417 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
418 Py_SetProgramName(p);
419 else
420 Py_SetProgramName(argv[0]);
421#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000422 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000423#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000424 Py_Initialize();
425
Guido van Rossum667d7041995-08-04 04:20:48 +0000426 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000427 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000428 fprintf(stderr, "Python %s on %s\n",
429 Py_GetVersion(), Py_GetPlatform());
430 if (!Py_NoSiteFlag)
431 fprintf(stderr, "%s\n", COPYRIGHT);
432 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000433
Guido van Rossum667d7041995-08-04 04:20:48 +0000434 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000435 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
436 _PyOS_optind--;
437 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000438 }
439
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000440 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000441 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
442 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000443 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000444 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000445 }
446
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000447 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000448
Guido van Rossumd8faa362007-04-27 19:54:29 +0000449 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000450 isatty(fileno(stdin))) {
451 PyObject *v;
452 v = PyImport_ImportModule("readline");
453 if (v == NULL)
454 PyErr_Clear();
455 else
456 Py_DECREF(v);
457 }
458
Guido van Rossum667d7041995-08-04 04:20:48 +0000459 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000460 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000461 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000462 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000463 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000464 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000465 }
466 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000467 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000468 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000469 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000470 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000471 /* XXX */
472 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000473 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000474 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000475 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000476 }
477
Barry Warsawd86dcd32003-06-29 17:07:06 +0000478 /* Check this environment variable at the end, to give programs the
479 * opportunity to set it from Python.
480 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000481 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000482 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
483 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000484 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000485 }
486
Guido van Rossumd8faa362007-04-27 19:54:29 +0000487 if (Py_InspectFlag && stdin_is_interactive &&
488 (filename != NULL || command != NULL || module != NULL)) {
489 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000490 /* XXX */
491 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000493
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000494 WaitForThreadShutdown();
495
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000496 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000497
498#ifdef __INSURE__
499 /* Insure++ is a memory analysis tool that aids in discovering
500 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000501 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000502 * (which it is). Under normal circumstances, this is fine because
503 * the memory will be automatically reclaimed by the system. Under
504 * memory debugging, it's a huge source of useless noise, so we
505 * trade off slower shutdown for less distraction in the memory
506 * reports. -baw
507 */
508 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000509 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000510#endif /* __INSURE__ */
511
Guido van Rossum05f7c501997-08-02 03:00:42 +0000512 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000513}
514
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000515/* this is gonna seem *real weird*, but if you put some other code between
516 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
517 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000518
Guido van Rossum667d7041995-08-04 04:20:48 +0000519/* Make the *original* argc/argv available to other modules.
520 This is rare, but it is needed by the secureware extension. */
521
522void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000523Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000524{
525 *argc = orig_argc;
526 *argv = orig_argv;
527}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000528
529#ifdef __cplusplus
530}
531#endif
532