blob: a8cbbfe12f14db7ceb0aef8df0c9695a44423720 [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);
Christian Heimese69a08e2007-11-14 16:21:32 +0000135 } else {
136 int save_errno;
137
138 save_errno = errno;
139 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
140 errno = save_errno;
141 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
142 startup);
143 PyErr_Print();
144 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000145 }
146 }
147}
148
Thomas Woutersa9773292006-04-21 09:43:23 +0000149
Christian Heimes9cd17752007-11-18 19:35:23 +0000150static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000151{
Thomas Woutersa9773292006-04-21 09:43:23 +0000152 PyObject *runpy, *runmodule, *runargs, *result;
153 runpy = PyImport_ImportModule("runpy");
154 if (runpy == NULL) {
155 fprintf(stderr, "Could not import runpy module\n");
156 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000157 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000158 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000159 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000160 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000161 Py_DECREF(runpy);
162 return -1;
163 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000164 runargs = Py_BuildValue("(si)", module, set_argv0);
Thomas Woutersa9773292006-04-21 09:43:23 +0000165 if (runargs == NULL) {
166 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000167 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000168 Py_DECREF(runpy);
169 Py_DECREF(runmodule);
170 return -1;
171 }
172 result = PyObject_Call(runmodule, runargs, NULL);
173 if (result == NULL) {
174 PyErr_Print();
175 }
176 Py_DECREF(runpy);
177 Py_DECREF(runmodule);
178 Py_DECREF(runargs);
179 if (result == NULL) {
180 return -1;
181 }
182 Py_DECREF(result);
183 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000184}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000185
Christian Heimes9cd17752007-11-18 19:35:23 +0000186static int RunMainFromImporter(char *filename)
187{
188 PyObject *argv0 = NULL, *importer = NULL;
189
Guido van Rossum74c29c72007-11-19 18:36:41 +0000190 if ((argv0 = PyUnicode_DecodeFSDefault(filename)) &&
191 (importer = PyImport_GetImporter(argv0)) &&
192 (importer->ob_type != &PyNullImporter_Type))
Christian Heimes9cd17752007-11-18 19:35:23 +0000193 {
194 /* argv0 is usable as an import source, so
195 put it in sys.path[0] and import __main__ */
196 PyObject *sys_path = NULL;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000197 if ((sys_path = PySys_GetObject("path")) &&
198 !PyList_SetItem(sys_path, 0, argv0))
199 {
Christian Heimes9cd17752007-11-18 19:35:23 +0000200 Py_INCREF(argv0);
Guido van Rossum74c29c72007-11-19 18:36:41 +0000201 Py_DECREF(importer);
Christian Heimes9cd17752007-11-18 19:35:23 +0000202 sys_path = NULL;
203 return RunModule("__main__", 0) != 0;
204 }
205 }
Guido van Rossum74c29c72007-11-19 18:36:41 +0000206 Py_XDECREF(argv0);
207 Py_XDECREF(importer);
208 if (PyErr_Occurred()) {
209 PyErr_Print();
210 return 1;
211 }
212 else {
213 return -1;
214 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000215}
216
217
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000218/* Wait until threading._shutdown completes, provided
219 the threading module was imported in the first place.
220 The shutdown routine will wait until all non-daemon
221 "threading" threads have completed. */
222#include "abstract.h"
223static void
224WaitForThreadShutdown(void)
225{
226#ifdef WITH_THREAD
227 PyObject *result;
228 PyThreadState *tstate = PyThreadState_GET();
229 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
230 "threading");
231 if (threading == NULL) {
232 /* threading not imported */
233 PyErr_Clear();
234 return;
235 }
236 result = PyObject_CallMethod(threading, "_shutdown", "");
237 if (result == NULL)
238 PyErr_WriteUnraisable(threading);
239 else
240 Py_DECREF(result);
241 Py_DECREF(threading);
242#endif
243}
244
Guido van Rossum667d7041995-08-04 04:20:48 +0000245/* Main program */
246
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000247int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000248Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000249{
250 int c;
251 int sts;
252 char *command = NULL;
253 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000254 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000255 FILE *fp = stdin;
256 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000257 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000258 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000259 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000260 int help = 0;
261 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000262 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000263 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000264
Guido van Rossum393661d2001-08-31 17:40:15 +0000265 cf.cf_flags = 0;
266
Guido van Rossumac56b031996-07-21 02:33:38 +0000267 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000269
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000270 PySys_ResetWarnOptions();
271
Guido van Rossumbceccf52001-04-10 22:07:43 +0000272 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000273 if (c == 'c') {
274 /* -c is the last option; following arguments
275 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000276 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000278 if (command == NULL)
279 Py_FatalError(
280 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000281 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000282 strcat(command, "\n");
283 break;
284 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000285
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000286 if (c == 'm') {
287 /* -m is the last option; following arguments
288 that look like options are left for the
289 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000290 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000291 if (module == NULL)
292 Py_FatalError(
293 "not enough memory to copy -m argument");
294 strcpy(module, _PyOS_optarg);
295 break;
296 }
297
Guido van Rossum667d7041995-08-04 04:20:48 +0000298 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000299 case 'b':
300 Py_BytesWarningFlag++;
301 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000302
303 case 'd':
304 Py_DebugFlag++;
305 break;
306
307 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000308 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000309 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000310 break;
311
Guido van Rossum7614da61997-03-03 19:14:45 +0000312 case 'O':
313 Py_OptimizeFlag++;
314 break;
315
Guido van Rossum7922bd71997-08-29 22:34:47 +0000316 case 'S':
317 Py_NoSiteFlag++;
318 break;
319
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000320 case 'E':
321 Py_IgnoreEnvironmentFlag++;
322 break;
323
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000324 case 't':
325 Py_TabcheckFlag++;
326 break;
327
Guido van Rossum667d7041995-08-04 04:20:48 +0000328 case 'u':
329 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000330 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000331 break;
332
333 case 'v':
334 Py_VerboseFlag++;
335 break;
336
Guido van Rossuma075ce11997-12-05 21:56:45 +0000337 case 'x':
338 skipfirstline = 1;
339 break;
340
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000341 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000342 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000343 help++;
344 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000345
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000346 case 'V':
347 version++;
348 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000349
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000350 case 'W':
351 PySys_AddWarnOption(_PyOS_optarg);
352 break;
353
Guido van Rossum667d7041995-08-04 04:20:48 +0000354 /* This space reserved for other options */
355
356 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000357 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000358 /*NOTREACHED*/
359
360 }
361 }
362
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000363 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000364 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000365
366 if (version) {
367 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000368 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000369 }
370
Guido van Rossumd8faa362007-04-27 19:54:29 +0000371 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000372 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000373 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000374 if (!saw_unbuffered_flag &&
375 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
376 unbuffered = 1;
377
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000378 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000379 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000380 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000381#ifdef __VMS
382 filename = decc$translate_vms(argv[_PyOS_optind]);
383 if (filename == (char *)0 || filename == (char *)-1)
384 filename = argv[_PyOS_optind];
385
386#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000387 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000388#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000389 }
390
391 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
392
Guido van Rossum667d7041995-08-04 04:20:48 +0000393 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000394#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000395 _setmode(fileno(stdin), O_BINARY);
396 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000397#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000398#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000399 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
400 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
401 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000402#else /* !HAVE_SETVBUF */
403 setbuf(stdin, (char *)NULL);
404 setbuf(stdout, (char *)NULL);
405 setbuf(stderr, (char *)NULL);
406#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000407 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000408 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000409#ifdef MS_WINDOWS
410 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000411 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000412 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000413#else /* !MS_WINDOWS */
414#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000415 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
416 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000417#endif /* HAVE_SETVBUF */
418#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000419 /* Leave stderr alone - it should be unbuffered anyway. */
420 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000421#ifdef __VMS
422 else {
423 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
424 }
425#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000426
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000427#ifdef __APPLE__
428 /* On MacOS X, when the Python interpreter is embedded in an
429 application bundle, it gets executed by a bootstrapping script
430 that does os.execve() with an argv[0] that's different from the
431 actual Python executable. This is needed to keep the Finder happy,
432 or rather, to work around Apple's overly strict requirements of
433 the process name. However, we still need a usable sys.executable,
434 so the actual executable path is passed in an environment variable.
435 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
436 script. */
437 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
438 Py_SetProgramName(p);
439 else
440 Py_SetProgramName(argv[0]);
441#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000442 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000443#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000444 Py_Initialize();
445
Guido van Rossum667d7041995-08-04 04:20:48 +0000446 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000447 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000448 fprintf(stderr, "Python %s on %s\n",
449 Py_GetVersion(), Py_GetPlatform());
450 if (!Py_NoSiteFlag)
451 fprintf(stderr, "%s\n", COPYRIGHT);
452 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000453
Guido van Rossum667d7041995-08-04 04:20:48 +0000454 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000455 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
456 _PyOS_optind--;
457 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000458 }
459
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000460 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
462 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000463 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000464 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000465 }
466
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000467 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000468
Guido van Rossumd8faa362007-04-27 19:54:29 +0000469 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000470 isatty(fileno(stdin))) {
471 PyObject *v;
472 v = PyImport_ImportModule("readline");
473 if (v == NULL)
474 PyErr_Clear();
475 else
476 Py_DECREF(v);
477 }
478
Guido van Rossum667d7041995-08-04 04:20:48 +0000479 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000480 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000481 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000482 } else if (module) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000483 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000484 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000485 }
486 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000487
Guido van Rossum775af911997-02-14 19:50:32 +0000488 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000489 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000490 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000491 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000492 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000493
494 sts = -1; /* keep track of whether we've already run __main__ */
495
496 if (filename != NULL) {
497 sts = RunMainFromImporter(filename);
498 }
499
500 if (sts==-1 && filename!=NULL) {
501 if ((fp = fopen(filename, "r")) == NULL) {
502#ifdef HAVE_STRERROR
503 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
504 argv[0], filename, errno, strerror(errno));
505#else
506 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
507 argv[0], filename, errno);
508#endif
509 return 2;
510 }
511 else if (skipfirstline) {
512 int ch;
513 /* Push back first newline so line numbers
514 remain the same */
515 while ((ch = getc(fp)) != EOF) {
516 if (ch == '\n') {
517 (void)ungetc(ch, fp);
518 break;
519 }
520 }
521 }
522 {
523 /* XXX: does this work on Win/Win64? (see posix_fstat) */
524 struct stat sb;
525 if (fstat(fileno(fp), &sb) == 0 &&
526 S_ISDIR(sb.st_mode)) {
527 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
528 return 1;
529 }
530 }
531 }
532
533 if (sts==-1) {
534 sts = PyRun_AnyFileExFlags(
535 fp,
536 filename == NULL ? "<stdin>" : filename,
537 filename != NULL, &cf) != 0;
538 }
539
Guido van Rossum667d7041995-08-04 04:20:48 +0000540 }
541
Barry Warsawd86dcd32003-06-29 17:07:06 +0000542 /* Check this environment variable at the end, to give programs the
543 * opportunity to set it from Python.
544 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000545 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000546 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
547 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000548 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000549 }
550
Guido van Rossumd8faa362007-04-27 19:54:29 +0000551 if (Py_InspectFlag && stdin_is_interactive &&
552 (filename != NULL || command != NULL || module != NULL)) {
553 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000554 /* XXX */
555 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000556 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000557
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000558 WaitForThreadShutdown();
559
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000560 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000561
562#ifdef __INSURE__
563 /* Insure++ is a memory analysis tool that aids in discovering
564 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000565 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000566 * (which it is). Under normal circumstances, this is fine because
567 * the memory will be automatically reclaimed by the system. Under
568 * memory debugging, it's a huge source of useless noise, so we
569 * trade off slower shutdown for less distraction in the memory
570 * reports. -baw
571 */
572 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000573 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000574#endif /* __INSURE__ */
575
Guido van Rossum05f7c501997-08-02 03:00:42 +0000576 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000577}
578
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000579/* this is gonna seem *real weird*, but if you put some other code between
580 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
581 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000582
Guido van Rossum667d7041995-08-04 04:20:48 +0000583/* Make the *original* argc/argv available to other modules.
584 This is rare, but it is needed by the secureware extension. */
585
586void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000587Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000588{
589 *argc = orig_argc;
590 *argv = orig_argv;
591}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592
593#ifdef __cplusplus
594}
595#endif