blob: 0cd879de6df9f72d535d4a350ffbdd90bbe9509c [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"
Jeremy Hyltonec97a282005-10-21 14:58:06 +00005#include "code.h" /* For CO_FUTURE_DIVISION */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00006#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00007
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00008#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00009#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000010#endif
11
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000012#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Martin v. Löwisa43190b2006-05-22 09:15:18 +000013#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000014#include <fcntl.h>
15#endif
Martin v. Löwisa43190b2006-05-22 09:15:18 +000016#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000019#define PYTHONHOMEHELP "<prefix>\\lib"
20#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if defined(PYOS_OS2) && defined(PYCC_GCC)
22#define PYTHONHOMEHELP "<prefix>/Lib"
23#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000024#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000025#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000026#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000027
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000028#include "pygetopt.h"
29
Guido van Rossuma22865e2000-09-05 04:41:18 +000030#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000031 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
32 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000033
Anthony Baxterac6bd462006-04-13 02:06:09 +000034#ifdef __cplusplus
35extern "C" {
36#endif
37
Guido van Rossumac56b031996-07-21 02:33:38 +000038/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000039static char **orig_argv;
40static int orig_argc;
41
Guido van Rossumbceccf52001-04-10 22:07:43 +000042/* command line options */
Georg Brandl2da0fce2008-01-07 17:09:35 +000043#define BASE_OPTS "3Bc:dEhim:OQ:StuUvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000044
45#ifndef RISCOS
46#define PROGRAM_OPTS BASE_OPTS
47#else /*RISCOS*/
48/* extra option saying that we are running under a special task window
49 frontend; especially my_readline will behave different */
50#define PROGRAM_OPTS BASE_OPTS "w"
51/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000052extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000053#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000054
Guido van Rossum667d7041995-08-04 04:20:48 +000055/* Short usage message (with %s for argv0) */
56static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000057"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000058
59/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000060static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000061Options and arguments (and corresponding environment variables):\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000062-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000063-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000064-d : debug output from parser; also PYTHONDEBUG=x\n\
Georg Brandlaed6c662008-01-07 17:25:53 +000065-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000066-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000067-i : inspect interactively after running script; forces a prompt even\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000070 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000071-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000072-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000073-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000074-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000075-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000076-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000079-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000080 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000081-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
82 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000083-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000084-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000085-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000086";
87static char *usage_4 = "\
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +000088-3 : warn about Python 3.x incompatibilities\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089file : program read from script file\n\
90- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000091arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092Other environment variables:\n\
93PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000094PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000095 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000096";
97static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +000098PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
99 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000100PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000101";
102
103
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000104static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000105usage(int exitcode, char* program)
106{
Guido van Rossum393661d2001-08-31 17:40:15 +0000107 FILE *f = exitcode ? stderr : stdout;
108
109 fprintf(f, usage_line, program);
110 if (exitcode)
111 fprintf(f, "Try `python -h' for more information.\n");
112 else {
113 fprintf(f, usage_1);
114 fprintf(f, usage_2);
115 fprintf(f, usage_3);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000116 fprintf(f, usage_4, DELIM);
117 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000118 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000119#if defined(__VMS)
120 if (exitcode == 0) {
121 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000123 }
124 else {
125 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000126 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000127 }
128#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000129 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000130#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000131 /*NOTREACHED*/
132}
133
Martin v. Löwis6caea372003-11-18 19:46:25 +0000134static void RunStartupFile(PyCompilerFlags *cf)
135{
136 char *startup = Py_GETENV("PYTHONSTARTUP");
137 if (startup != NULL && startup[0] != '\0') {
138 FILE *fp = fopen(startup, "r");
139 if (fp != NULL) {
140 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
141 PyErr_Clear();
142 fclose(fp);
143 }
144 }
145}
146
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000147
Nick Coghlan327a39b2007-11-18 11:56:28 +0000148static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000149{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000150 PyObject *runpy, *runmodule, *runargs, *result;
151 runpy = PyImport_ImportModule("runpy");
152 if (runpy == NULL) {
153 fprintf(stderr, "Could not import runpy module\n");
154 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000155 }
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000156 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000157 if (runmodule == NULL) {
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000158 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000159 Py_DECREF(runpy);
160 return -1;
161 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000162 runargs = Py_BuildValue("(si)", module, set_argv0);
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000163 if (runargs == NULL) {
164 fprintf(stderr,
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000165 "Could not create arguments for runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000166 Py_DECREF(runpy);
167 Py_DECREF(runmodule);
168 return -1;
169 }
170 result = PyObject_Call(runmodule, runargs, NULL);
171 if (result == NULL) {
172 PyErr_Print();
173 }
174 Py_DECREF(runpy);
175 Py_DECREF(runmodule);
176 Py_DECREF(runargs);
177 if (result == NULL) {
178 return -1;
179 }
180 Py_DECREF(result);
181 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000182}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000183
Nick Coghlan327a39b2007-11-18 11:56:28 +0000184static int RunMainFromImporter(char *filename)
185{
186 PyObject *argv0 = NULL, *importer = NULL;
187
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000188 if ((argv0 = PyString_FromString(filename)) &&
189 (importer = PyImport_GetImporter(argv0)) &&
190 (importer->ob_type != &PyNullImporter_Type))
Nick Coghlan327a39b2007-11-18 11:56:28 +0000191 {
192 /* argv0 is usable as an import source, so
193 put it in sys.path[0] and import __main__ */
194 PyObject *sys_path = NULL;
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000195 if ((sys_path = PySys_GetObject("path")) &&
196 !PyList_SetItem(sys_path, 0, argv0))
197 {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000198 Py_INCREF(argv0);
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000199 Py_DECREF(importer);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000200 sys_path = NULL;
201 return RunModule("__main__", 0) != 0;
202 }
203 }
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000204 Py_XDECREF(argv0);
205 Py_XDECREF(importer);
206 if (PyErr_Occurred()) {
207 PyErr_Print();
208 return 1;
209 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000210 return -1;
211}
212
213
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000214/* Wait until threading._shutdown completes, provided
215 the threading module was imported in the first place.
216 The shutdown routine will wait until all non-daemon
217 "threading" threads have completed. */
218#include "abstract.h"
219static void
Brett Cannond14ef772007-01-05 21:45:09 +0000220WaitForThreadShutdown(void)
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000221{
222#ifdef WITH_THREAD
223 PyObject *result;
224 PyThreadState *tstate = PyThreadState_GET();
225 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
226 "threading");
227 if (threading == NULL) {
228 /* threading not imported */
229 PyErr_Clear();
230 return;
231 }
232 result = PyObject_CallMethod(threading, "_shutdown", "");
233 if (result == NULL)
234 PyErr_WriteUnraisable(threading);
235 else
236 Py_DECREF(result);
237 Py_DECREF(threading);
238#endif
239}
240
Guido van Rossum667d7041995-08-04 04:20:48 +0000241/* Main program */
242
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000243int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000244Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000245{
246 int c;
247 int sts;
248 char *command = NULL;
249 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000250 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000251 FILE *fp = stdin;
252 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000253 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000254 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000255 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000256 int help = 0;
257 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000258 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000259 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000260
Guido van Rossum393661d2001-08-31 17:40:15 +0000261 cf.cf_flags = 0;
262
Guido van Rossumac56b031996-07-21 02:33:38 +0000263 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000265
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000266#ifdef RISCOS
267 Py_RISCOSWimpFlag = 0;
268#endif
269
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. */
Anthony Baxter64182fe2006-04-11 12:14:09 +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. */
Anthony Baxter64182fe2006-04-11 12:14:09 +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) {
299
300 case 'd':
301 Py_DebugFlag++;
302 break;
303
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000304 case '3':
305 Py_Py3kWarningFlag++;
306 break;
307
Guido van Rossum61c345f2001-09-04 03:26:15 +0000308 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000309 if (strcmp(_PyOS_optarg, "old") == 0) {
310 Py_DivisionWarningFlag = 0;
311 break;
312 }
313 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000314 Py_DivisionWarningFlag = 1;
315 break;
316 }
317 if (strcmp(_PyOS_optarg, "warnall") == 0) {
318 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000319 break;
320 }
321 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000322 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000323 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000324 /* And this tells the eval loop to treat
325 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
326 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000327 break;
328 }
329 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000330 "-Q option should be `-Qold', "
331 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000332 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000333 /* NOTREACHED */
334
Guido van Rossum667d7041995-08-04 04:20:48 +0000335 case 'i':
Georg Brandl49aafc92007-03-07 00:34:46 +0000336 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000337 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000338 break;
339
Guido van Rossum7614da61997-03-03 19:14:45 +0000340 case 'O':
341 Py_OptimizeFlag++;
342 break;
343
Georg Brandl2da0fce2008-01-07 17:09:35 +0000344 case 'B':
345 Py_DontWriteBytecodeFlag++;
346 break;
347
Guido van Rossum7922bd71997-08-29 22:34:47 +0000348 case 'S':
349 Py_NoSiteFlag++;
350 break;
351
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000352 case 'E':
353 Py_IgnoreEnvironmentFlag++;
354 break;
355
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000356 case 't':
357 Py_TabcheckFlag++;
358 break;
359
Guido van Rossum667d7041995-08-04 04:20:48 +0000360 case 'u':
361 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000362 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000363 break;
364
365 case 'v':
366 Py_VerboseFlag++;
367 break;
368
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000369#ifdef RISCOS
370 case 'w':
371 Py_RISCOSWimpFlag = 1;
372 break;
373#endif
374
Guido van Rossuma075ce11997-12-05 21:56:45 +0000375 case 'x':
376 skipfirstline = 1;
377 break;
378
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000379 case 'U':
380 Py_UnicodeFlag++;
381 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000382 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000383 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000384 help++;
385 break;
386 case 'V':
387 version++;
388 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000389
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000390 case 'W':
391 PySys_AddWarnOption(_PyOS_optarg);
392 break;
393
Guido van Rossum667d7041995-08-04 04:20:48 +0000394 /* This space reserved for other options */
395
396 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000397 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 /*NOTREACHED*/
399
400 }
401 }
402
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000403 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000404 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000405
406 if (version) {
407 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000408 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000409 }
410
Georg Brandl49aafc92007-03-07 00:34:46 +0000411 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000412 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Georg Brandl49aafc92007-03-07 00:34:46 +0000413 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000414 if (!saw_unbuffered_flag &&
415 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
416 unbuffered = 1;
417
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000418 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000419 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000420 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000421#ifdef __VMS
422 filename = decc$translate_vms(argv[_PyOS_optind]);
423 if (filename == (char *)0 || filename == (char *)-1)
424 filename = argv[_PyOS_optind];
425
426#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000427 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000428#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000429 }
430
431 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
432
Guido van Rossum667d7041995-08-04 04:20:48 +0000433 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000434#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000435 _setmode(fileno(stdin), O_BINARY);
436 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000437#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000438#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000439 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
440 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
441 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000442#else /* !HAVE_SETVBUF */
443 setbuf(stdin, (char *)NULL);
444 setbuf(stdout, (char *)NULL);
445 setbuf(stderr, (char *)NULL);
446#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000447 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000448 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000449#ifdef MS_WINDOWS
450 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000451 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000452 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000453#else /* !MS_WINDOWS */
454#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000455 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
456 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000457#endif /* HAVE_SETVBUF */
458#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000459 /* Leave stderr alone - it should be unbuffered anyway. */
460 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000461#ifdef __VMS
462 else {
463 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
464 }
465#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000466
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000467#ifdef __APPLE__
468 /* On MacOS X, when the Python interpreter is embedded in an
469 application bundle, it gets executed by a bootstrapping script
470 that does os.execve() with an argv[0] that's different from the
471 actual Python executable. This is needed to keep the Finder happy,
472 or rather, to work around Apple's overly strict requirements of
473 the process name. However, we still need a usable sys.executable,
474 so the actual executable path is passed in an environment variable.
475 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
476 script. */
477 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
478 Py_SetProgramName(p);
479 else
480 Py_SetProgramName(argv[0]);
481#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000482 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000483#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000484 Py_Initialize();
485
Guido van Rossum667d7041995-08-04 04:20:48 +0000486 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000487 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000488 fprintf(stderr, "Python %s on %s\n",
489 Py_GetVersion(), Py_GetPlatform());
490 if (!Py_NoSiteFlag)
491 fprintf(stderr, "%s\n", COPYRIGHT);
492 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000493
Guido van Rossum667d7041995-08-04 04:20:48 +0000494 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000495 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
496 _PyOS_optind--;
497 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000498 }
499
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000500 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000501 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
502 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000503 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000504 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000505 }
506
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000507 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000508
Georg Brandl49aafc92007-03-07 00:34:46 +0000509 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000510 isatty(fileno(stdin))) {
511 PyObject *v;
512 v = PyImport_ImportModule("readline");
513 if (v == NULL)
514 PyErr_Clear();
515 else
516 Py_DECREF(v);
517 }
518
Guido van Rossum667d7041995-08-04 04:20:48 +0000519 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000520 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000521 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000522 } else if (module) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000523 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000524 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000525 }
526 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000527
Guido van Rossum775af911997-02-14 19:50:32 +0000528 if (filename == NULL && stdin_is_interactive) {
Georg Brandl49aafc92007-03-07 00:34:46 +0000529 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000530 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000531 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000532 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000533
534 sts = -1; /* keep track of whether we've already run __main__ */
535
536 if (filename != NULL) {
537 sts = RunMainFromImporter(filename);
538 }
539
540 if (sts==-1 && filename!=NULL) {
541 if ((fp = fopen(filename, "r")) == NULL) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000542 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
543 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000544
Nick Coghlan327a39b2007-11-18 11:56:28 +0000545 return 2;
546 }
547 else if (skipfirstline) {
548 int ch;
549 /* Push back first newline so line numbers
550 remain the same */
551 while ((ch = getc(fp)) != EOF) {
552 if (ch == '\n') {
553 (void)ungetc(ch, fp);
554 break;
555 }
556 }
557 }
558 {
559 /* XXX: does this work on Win/Win64? (see posix_fstat) */
560 struct stat sb;
561 if (fstat(fileno(fp), &sb) == 0 &&
562 S_ISDIR(sb.st_mode)) {
563 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes5cc3f262008-01-18 08:53:45 +0000564 fclose(fp);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000565 return 1;
566 }
567 }
568 }
569
570 if (sts==-1) {
571 sts = PyRun_AnyFileExFlags(
572 fp,
573 filename == NULL ? "<stdin>" : filename,
574 filename != NULL, &cf) != 0;
575 }
576
Guido van Rossum667d7041995-08-04 04:20:48 +0000577 }
578
Barry Warsawd86dcd32003-06-29 17:07:06 +0000579 /* Check this environment variable at the end, to give programs the
580 * opportunity to set it from Python.
581 */
Georg Brandl49aafc92007-03-07 00:34:46 +0000582 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000583 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
584 {
Georg Brandl49aafc92007-03-07 00:34:46 +0000585 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000586 }
587
Georg Brandl49aafc92007-03-07 00:34:46 +0000588 if (Py_InspectFlag && stdin_is_interactive &&
589 (filename != NULL || command != NULL || module != NULL)) {
590 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000591 /* XXX */
592 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Georg Brandl49aafc92007-03-07 00:34:46 +0000593 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000594
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000595 WaitForThreadShutdown();
596
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000597 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000598#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000599 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000600 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
601#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000602
603#ifdef __INSURE__
604 /* Insure++ is a memory analysis tool that aids in discovering
605 * memory leaks and other memory problems. On Python exit, the
606 * interned string dictionary is flagged as being in use at exit
607 * (which it is). Under normal circumstances, this is fine because
608 * the memory will be automatically reclaimed by the system. Under
609 * memory debugging, it's a huge source of useless noise, so we
610 * trade off slower shutdown for less distraction in the memory
611 * reports. -baw
612 */
613 _Py_ReleaseInternedStrings();
614#endif /* __INSURE__ */
615
Guido van Rossum05f7c501997-08-02 03:00:42 +0000616 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000617}
618
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000619/* this is gonna seem *real weird*, but if you put some other code between
620 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
621 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000622
Guido van Rossum667d7041995-08-04 04:20:48 +0000623/* Make the *original* argc/argv available to other modules.
624 This is rare, but it is needed by the secureware extension. */
625
626void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000627Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000628{
629 *argc = orig_argc;
630 *argv = orig_argv;
631}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000632
633#ifdef __cplusplus
634}
635#endif
636