blob: e1e7b255cc86f6ab0cbe75b2f31979a5a92c7233 [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 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047#define BASE_OPTS "c: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 Rossum393661d2001-08-31 17:40:15 +000058-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000059-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000060-E : ignore environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061-h : print this help message and exit (also --help)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000062-i : inspect interactively after running script; forces a prompt even\n\
63 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000064";
65static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000066-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000067-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000068-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000069-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000070-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000071-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000072";
73static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000074 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000075-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
76 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000078-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000079-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080file : program read from script file\n\
81- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000082";
Guido van Rossum393661d2001-08-31 17:40:15 +000083static char *usage_4 = "\
Thomas Wouters89f507f2006-12-13 04:49:30 +000084arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000085Other environment variables:\n\
86PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000087PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000089PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
90 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000091PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092";
93
94
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000095static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000096usage(int exitcode, char* program)
97{
Guido van Rossum393661d2001-08-31 17:40:15 +000098 FILE *f = exitcode ? stderr : stdout;
99
100 fprintf(f, usage_line, program);
101 if (exitcode)
102 fprintf(f, "Try `python -h' for more information.\n");
103 else {
104 fprintf(f, usage_1);
105 fprintf(f, usage_2);
106 fprintf(f, usage_3);
107 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
108 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000109#if defined(__VMS)
110 if (exitcode == 0) {
111 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000112 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000113 }
114 else {
115 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000116 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000117 }
118#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000119 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000120#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000121 /*NOTREACHED*/
122}
123
Martin v. Löwis6caea372003-11-18 19:46:25 +0000124static void RunStartupFile(PyCompilerFlags *cf)
125{
126 char *startup = Py_GETENV("PYTHONSTARTUP");
127 if (startup != NULL && startup[0] != '\0') {
128 FILE *fp = fopen(startup, "r");
129 if (fp != NULL) {
130 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
131 PyErr_Clear();
132 fclose(fp);
133 }
134 }
135}
136
Thomas Woutersa9773292006-04-21 09:43:23 +0000137
138static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000139{
Thomas Woutersa9773292006-04-21 09:43:23 +0000140 PyObject *runpy, *runmodule, *runargs, *result;
141 runpy = PyImport_ImportModule("runpy");
142 if (runpy == NULL) {
143 fprintf(stderr, "Could not import runpy module\n");
144 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000145 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000146 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000147 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000148 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000149 Py_DECREF(runpy);
150 return -1;
151 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000152 runargs = Py_BuildValue("(s)", module);
Thomas Woutersa9773292006-04-21 09:43:23 +0000153 if (runargs == NULL) {
154 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000155 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000156 Py_DECREF(runpy);
157 Py_DECREF(runmodule);
158 return -1;
159 }
160 result = PyObject_Call(runmodule, runargs, NULL);
161 if (result == NULL) {
162 PyErr_Print();
163 }
164 Py_DECREF(runpy);
165 Py_DECREF(runmodule);
166 Py_DECREF(runargs);
167 if (result == NULL) {
168 return -1;
169 }
170 Py_DECREF(result);
171 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000172}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000173
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000174/* Wait until threading._shutdown completes, provided
175 the threading module was imported in the first place.
176 The shutdown routine will wait until all non-daemon
177 "threading" threads have completed. */
178#include "abstract.h"
179static void
180WaitForThreadShutdown(void)
181{
182#ifdef WITH_THREAD
183 PyObject *result;
184 PyThreadState *tstate = PyThreadState_GET();
185 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
186 "threading");
187 if (threading == NULL) {
188 /* threading not imported */
189 PyErr_Clear();
190 return;
191 }
192 result = PyObject_CallMethod(threading, "_shutdown", "");
193 if (result == NULL)
194 PyErr_WriteUnraisable(threading);
195 else
196 Py_DECREF(result);
197 Py_DECREF(threading);
198#endif
199}
200
Guido van Rossum667d7041995-08-04 04:20:48 +0000201/* Main program */
202
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000203int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000204Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000205{
206 int c;
207 int sts;
208 char *command = NULL;
209 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000210 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000211 FILE *fp = stdin;
212 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000213 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000214 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000215 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000216 int help = 0;
217 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000218 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000219 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000220
Guido van Rossum393661d2001-08-31 17:40:15 +0000221 cf.cf_flags = 0;
222
Guido van Rossumac56b031996-07-21 02:33:38 +0000223 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000224 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000225
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000226 PySys_ResetWarnOptions();
227
Guido van Rossumbceccf52001-04-10 22:07:43 +0000228 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000229 if (c == 'c') {
230 /* -c is the last option; following arguments
231 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000232 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000234 if (command == NULL)
235 Py_FatalError(
236 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000237 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000238 strcat(command, "\n");
239 break;
240 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000241
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000242 if (c == 'm') {
243 /* -m is the last option; following arguments
244 that look like options are left for the
245 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000247 if (module == NULL)
248 Py_FatalError(
249 "not enough memory to copy -m argument");
250 strcpy(module, _PyOS_optarg);
251 break;
252 }
253
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 switch (c) {
255
256 case 'd':
257 Py_DebugFlag++;
258 break;
259
260 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000261 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000262 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000263 break;
264
Guido van Rossum7614da61997-03-03 19:14:45 +0000265 case 'O':
266 Py_OptimizeFlag++;
267 break;
268
Guido van Rossum7922bd71997-08-29 22:34:47 +0000269 case 'S':
270 Py_NoSiteFlag++;
271 break;
272
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000273 case 'E':
274 Py_IgnoreEnvironmentFlag++;
275 break;
276
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000277 case 't':
278 Py_TabcheckFlag++;
279 break;
280
Guido van Rossum667d7041995-08-04 04:20:48 +0000281 case 'u':
282 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000283 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000284 break;
285
286 case 'v':
287 Py_VerboseFlag++;
288 break;
289
Guido van Rossuma075ce11997-12-05 21:56:45 +0000290 case 'x':
291 skipfirstline = 1;
292 break;
293
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000294 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000296 help++;
297 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000298
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000299 case 'V':
300 version++;
301 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000302
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000303 case 'W':
304 PySys_AddWarnOption(_PyOS_optarg);
305 break;
306
Guido van Rossum667d7041995-08-04 04:20:48 +0000307 /* This space reserved for other options */
308
309 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000310 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000311 /*NOTREACHED*/
312
313 }
314 }
315
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000316 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000317 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000318
319 if (version) {
320 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000321 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000322 }
323
Guido van Rossumd8faa362007-04-27 19:54:29 +0000324 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000325 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000326 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000327 if (!saw_unbuffered_flag &&
328 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
329 unbuffered = 1;
330
Martin v. Löwis945362c2007-08-30 14:57:25 +0000331#ifdef MS_WINDOWS
332 if ((p = Py_GETENV("PYTHONNOERRORWINDOW")) && *p != '\0') {
333 /* Disable all error windows created by the sytem
334 or the CRT. */
335#if defined(_DEBUG) && defined(_MSC_VER)
336 int types[] = {_CRT_WARN, _CRT_ERROR, _CRT_ASSERT};
337 int i;
338 for (i = 0; i < sizeof(types)/sizeof(types[0]); i++) {
339 _CrtSetReportFile(types[i], _CRTDBG_FILE_STDERR);
340 _CrtSetReportMode(types[i], _CRTDBG_MODE_FILE);
341 }
342 _set_error_mode(_OUT_TO_STDERR);
343#endif
344 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
345 SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
346 }
347#endif
348
349
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000350 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000351 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000352 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000353#ifdef __VMS
354 filename = decc$translate_vms(argv[_PyOS_optind]);
355 if (filename == (char *)0 || filename == (char *)-1)
356 filename = argv[_PyOS_optind];
357
358#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000359 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000360#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000361 if (filename != NULL) {
362 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000363#ifdef HAVE_STRERROR
364 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
365 argv[0], filename, errno, strerror(errno));
366#else
367 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
368 argv[0], filename, errno);
369#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000370 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000371 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000372 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000373 int ch;
374 /* Push back first newline so line numbers
375 remain the same */
376 while ((ch = getc(fp)) != EOF) {
377 if (ch == '\n') {
378 (void)ungetc(ch, fp);
379 break;
380 }
381 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000382 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000383 {
384 /* XXX: does this work on Win/Win64? (see posix_fstat) */
385 struct stat sb;
386 if (fstat(fileno(fp), &sb) == 0 &&
387 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000388 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
389 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000390 }
391 }
Guido van Rossum775af911997-02-14 19:50:32 +0000392 }
393 }
394
395 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
396
Guido van Rossum667d7041995-08-04 04:20:48 +0000397 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000398#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000399 _setmode(fileno(stdin), O_BINARY);
400 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000401#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000402#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000403 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
404 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
405 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000406#else /* !HAVE_SETVBUF */
407 setbuf(stdin, (char *)NULL);
408 setbuf(stdout, (char *)NULL);
409 setbuf(stderr, (char *)NULL);
410#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000411 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000412 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000413#ifdef MS_WINDOWS
414 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000415 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000416 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000417#else /* !MS_WINDOWS */
418#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000419 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
420 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000421#endif /* HAVE_SETVBUF */
422#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000423 /* Leave stderr alone - it should be unbuffered anyway. */
424 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000425#ifdef __VMS
426 else {
427 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
428 }
429#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000430
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000431#ifdef __APPLE__
432 /* On MacOS X, when the Python interpreter is embedded in an
433 application bundle, it gets executed by a bootstrapping script
434 that does os.execve() with an argv[0] that's different from the
435 actual Python executable. This is needed to keep the Finder happy,
436 or rather, to work around Apple's overly strict requirements of
437 the process name. However, we still need a usable sys.executable,
438 so the actual executable path is passed in an environment variable.
439 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
440 script. */
441 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
442 Py_SetProgramName(p);
443 else
444 Py_SetProgramName(argv[0]);
445#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000446 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000447#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000448 Py_Initialize();
449
Guido van Rossum667d7041995-08-04 04:20:48 +0000450 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000451 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000452 fprintf(stderr, "Python %s on %s\n",
453 Py_GetVersion(), Py_GetPlatform());
454 if (!Py_NoSiteFlag)
455 fprintf(stderr, "%s\n", COPYRIGHT);
456 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000457
Guido van Rossum667d7041995-08-04 04:20:48 +0000458 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000459 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
460 _PyOS_optind--;
461 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000462 }
463
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000464 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
466 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000467 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000469 }
470
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000471 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000472
Guido van Rossumd8faa362007-04-27 19:54:29 +0000473 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000474 isatty(fileno(stdin))) {
475 PyObject *v;
476 v = PyImport_ImportModule("readline");
477 if (v == NULL)
478 PyErr_Clear();
479 else
480 Py_DECREF(v);
481 }
482
Guido van Rossum667d7041995-08-04 04:20:48 +0000483 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000484 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000485 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000486 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000487 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000488 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000489 }
490 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000491 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000493 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000494 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000495 /* XXX */
496 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000497 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000498 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000499 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000500 }
501
Barry Warsawd86dcd32003-06-29 17:07:06 +0000502 /* Check this environment variable at the end, to give programs the
503 * opportunity to set it from Python.
504 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000505 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000506 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
507 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000508 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000509 }
510
Guido van Rossumd8faa362007-04-27 19:54:29 +0000511 if (Py_InspectFlag && stdin_is_interactive &&
512 (filename != NULL || command != NULL || module != NULL)) {
513 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000514 /* XXX */
515 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000517
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000518 WaitForThreadShutdown();
519
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000520 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000521
522#ifdef __INSURE__
523 /* Insure++ is a memory analysis tool that aids in discovering
524 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000525 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000526 * (which it is). Under normal circumstances, this is fine because
527 * the memory will be automatically reclaimed by the system. Under
528 * memory debugging, it's a huge source of useless noise, so we
529 * trade off slower shutdown for less distraction in the memory
530 * reports. -baw
531 */
532 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000533 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000534#endif /* __INSURE__ */
535
Guido van Rossum05f7c501997-08-02 03:00:42 +0000536 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000537}
538
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000539/* this is gonna seem *real weird*, but if you put some other code between
540 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
541 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000542
Guido van Rossum667d7041995-08-04 04:20:48 +0000543/* Make the *original* argc/argv available to other modules.
544 This is rare, but it is needed by the secureware extension. */
545
546void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000547Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000548{
549 *argc = orig_argc;
550 *argv = orig_argv;
551}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552
553#ifdef __cplusplus
554}
555#endif
556