blob: 26f8780e72b5a5c49dececcb83cf322fbc70a402 [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 */
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +000043#define BASE_OPTS "3c: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\
Guido van Rossum393661d2001-08-31 17:40:15 +000062-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000063-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000064-E : ignore environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000065-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +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\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000070-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000071-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000072-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000073-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000074-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000075-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000076-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000079 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000080-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
81 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000082-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000083-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000084-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +000085-3 : warn about Python 3.x incompatibilities\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086file : program read from script file\n\
87- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000088";
Guido van Rossum393661d2001-08-31 17:40:15 +000089static char *usage_4 = "\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000090arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000091Other environment variables:\n\
92PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000093PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000094 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000095PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
96 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000097PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000098";
99
100
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000101static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000102usage(int exitcode, char* program)
103{
Guido van Rossum393661d2001-08-31 17:40:15 +0000104 FILE *f = exitcode ? stderr : stdout;
105
106 fprintf(f, usage_line, program);
107 if (exitcode)
108 fprintf(f, "Try `python -h' for more information.\n");
109 else {
110 fprintf(f, usage_1);
111 fprintf(f, usage_2);
112 fprintf(f, usage_3);
113 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
114 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115#if defined(__VMS)
116 if (exitcode == 0) {
117 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000118 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000119 }
120 else {
121 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000123 }
124#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000125 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000126#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000127 /*NOTREACHED*/
128}
129
Martin v. Löwis6caea372003-11-18 19:46:25 +0000130static void RunStartupFile(PyCompilerFlags *cf)
131{
132 char *startup = Py_GETENV("PYTHONSTARTUP");
133 if (startup != NULL && startup[0] != '\0') {
134 FILE *fp = fopen(startup, "r");
135 if (fp != NULL) {
136 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
137 PyErr_Clear();
138 fclose(fp);
139 }
140 }
141}
142
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000143
Nick Coghlan327a39b2007-11-18 11:56:28 +0000144static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000145{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000146 PyObject *runpy, *runmodule, *runargs, *result;
147 runpy = PyImport_ImportModule("runpy");
148 if (runpy == NULL) {
149 fprintf(stderr, "Could not import runpy module\n");
150 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000151 }
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000152 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000153 if (runmodule == NULL) {
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000154 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000155 Py_DECREF(runpy);
156 return -1;
157 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000158 runargs = Py_BuildValue("(si)", module, set_argv0);
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000159 if (runargs == NULL) {
160 fprintf(stderr,
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000161 "Could not create arguments for runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000162 Py_DECREF(runpy);
163 Py_DECREF(runmodule);
164 return -1;
165 }
166 result = PyObject_Call(runmodule, runargs, NULL);
167 if (result == NULL) {
168 PyErr_Print();
169 }
170 Py_DECREF(runpy);
171 Py_DECREF(runmodule);
172 Py_DECREF(runargs);
173 if (result == NULL) {
174 return -1;
175 }
176 Py_DECREF(result);
177 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000178}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000179
Nick Coghlan327a39b2007-11-18 11:56:28 +0000180static int RunMainFromImporter(char *filename)
181{
182 PyObject *argv0 = NULL, *importer = NULL;
183
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000184 if ((argv0 = PyString_FromString(filename)) &&
185 (importer = PyImport_GetImporter(argv0)) &&
186 (importer->ob_type != &PyNullImporter_Type))
Nick Coghlan327a39b2007-11-18 11:56:28 +0000187 {
188 /* argv0 is usable as an import source, so
189 put it in sys.path[0] and import __main__ */
190 PyObject *sys_path = NULL;
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000191 if ((sys_path = PySys_GetObject("path")) &&
192 !PyList_SetItem(sys_path, 0, argv0))
193 {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000194 Py_INCREF(argv0);
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000195 Py_DECREF(importer);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000196 sys_path = NULL;
197 return RunModule("__main__", 0) != 0;
198 }
199 }
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000200 Py_XDECREF(argv0);
201 Py_XDECREF(importer);
202 if (PyErr_Occurred()) {
203 PyErr_Print();
204 return 1;
205 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000206 return -1;
207}
208
209
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000210/* Wait until threading._shutdown completes, provided
211 the threading module was imported in the first place.
212 The shutdown routine will wait until all non-daemon
213 "threading" threads have completed. */
214#include "abstract.h"
215static void
Brett Cannond14ef772007-01-05 21:45:09 +0000216WaitForThreadShutdown(void)
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000217{
218#ifdef WITH_THREAD
219 PyObject *result;
220 PyThreadState *tstate = PyThreadState_GET();
221 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
222 "threading");
223 if (threading == NULL) {
224 /* threading not imported */
225 PyErr_Clear();
226 return;
227 }
228 result = PyObject_CallMethod(threading, "_shutdown", "");
229 if (result == NULL)
230 PyErr_WriteUnraisable(threading);
231 else
232 Py_DECREF(result);
233 Py_DECREF(threading);
234#endif
235}
236
Guido van Rossum667d7041995-08-04 04:20:48 +0000237/* Main program */
238
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000239int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000240Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000241{
242 int c;
243 int sts;
244 char *command = NULL;
245 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000246 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000247 FILE *fp = stdin;
248 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000249 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000250 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000251 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000252 int help = 0;
253 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000254 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000255 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000256
Guido van Rossum393661d2001-08-31 17:40:15 +0000257 cf.cf_flags = 0;
258
Guido van Rossumac56b031996-07-21 02:33:38 +0000259 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000260 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000261
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000262#ifdef RISCOS
263 Py_RISCOSWimpFlag = 0;
264#endif
265
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000266 PySys_ResetWarnOptions();
267
Guido van Rossumbceccf52001-04-10 22:07:43 +0000268 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000269 if (c == 'c') {
270 /* -c is the last option; following arguments
271 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000272 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000273 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000274 if (command == NULL)
275 Py_FatalError(
276 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000277 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000278 strcat(command, "\n");
279 break;
280 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000281
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000282 if (c == 'm') {
283 /* -m is the last option; following arguments
284 that look like options are left for the
285 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000286 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000287 if (module == NULL)
288 Py_FatalError(
289 "not enough memory to copy -m argument");
290 strcpy(module, _PyOS_optarg);
291 break;
292 }
293
Guido van Rossum667d7041995-08-04 04:20:48 +0000294 switch (c) {
295
296 case 'd':
297 Py_DebugFlag++;
298 break;
299
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000300 case '3':
301 Py_Py3kWarningFlag++;
302 break;
303
Guido van Rossum61c345f2001-09-04 03:26:15 +0000304 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000305 if (strcmp(_PyOS_optarg, "old") == 0) {
306 Py_DivisionWarningFlag = 0;
307 break;
308 }
309 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000310 Py_DivisionWarningFlag = 1;
311 break;
312 }
313 if (strcmp(_PyOS_optarg, "warnall") == 0) {
314 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000315 break;
316 }
317 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000318 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000319 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000320 /* And this tells the eval loop to treat
321 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
322 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000323 break;
324 }
325 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000326 "-Q option should be `-Qold', "
327 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000328 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000329 /* NOTREACHED */
330
Guido van Rossum667d7041995-08-04 04:20:48 +0000331 case 'i':
Georg Brandl49aafc92007-03-07 00:34:46 +0000332 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000333 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000334 break;
335
Guido van Rossum7614da61997-03-03 19:14:45 +0000336 case 'O':
337 Py_OptimizeFlag++;
338 break;
339
Guido van Rossum7922bd71997-08-29 22:34:47 +0000340 case 'S':
341 Py_NoSiteFlag++;
342 break;
343
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000344 case 'E':
345 Py_IgnoreEnvironmentFlag++;
346 break;
347
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000348 case 't':
349 Py_TabcheckFlag++;
350 break;
351
Guido van Rossum667d7041995-08-04 04:20:48 +0000352 case 'u':
353 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000354 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000355 break;
356
357 case 'v':
358 Py_VerboseFlag++;
359 break;
360
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000361#ifdef RISCOS
362 case 'w':
363 Py_RISCOSWimpFlag = 1;
364 break;
365#endif
366
Guido van Rossuma075ce11997-12-05 21:56:45 +0000367 case 'x':
368 skipfirstline = 1;
369 break;
370
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000371 case 'U':
372 Py_UnicodeFlag++;
373 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000374 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000375 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000376 help++;
377 break;
378 case 'V':
379 version++;
380 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000381
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000382 case 'W':
383 PySys_AddWarnOption(_PyOS_optarg);
384 break;
385
Guido van Rossum667d7041995-08-04 04:20:48 +0000386 /* This space reserved for other options */
387
388 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000389 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000390 /*NOTREACHED*/
391
392 }
393 }
394
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000395 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000396 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000397
398 if (version) {
399 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000400 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000401 }
402
Georg Brandl49aafc92007-03-07 00:34:46 +0000403 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000404 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Georg Brandl49aafc92007-03-07 00:34:46 +0000405 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000406 if (!saw_unbuffered_flag &&
407 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
408 unbuffered = 1;
409
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000410 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000411 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000412 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000413#ifdef __VMS
414 filename = decc$translate_vms(argv[_PyOS_optind]);
415 if (filename == (char *)0 || filename == (char *)-1)
416 filename = argv[_PyOS_optind];
417
418#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000419 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000420#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000421 }
422
423 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
424
Guido van Rossum667d7041995-08-04 04:20:48 +0000425 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000426#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000427 _setmode(fileno(stdin), O_BINARY);
428 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000429#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000430#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000431 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
432 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
433 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000434#else /* !HAVE_SETVBUF */
435 setbuf(stdin, (char *)NULL);
436 setbuf(stdout, (char *)NULL);
437 setbuf(stderr, (char *)NULL);
438#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000439 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000440 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000441#ifdef MS_WINDOWS
442 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000443 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000444 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000445#else /* !MS_WINDOWS */
446#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000447 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
448 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000449#endif /* HAVE_SETVBUF */
450#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000451 /* Leave stderr alone - it should be unbuffered anyway. */
452 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000453#ifdef __VMS
454 else {
455 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
456 }
457#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000458
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000459#ifdef __APPLE__
460 /* On MacOS X, when the Python interpreter is embedded in an
461 application bundle, it gets executed by a bootstrapping script
462 that does os.execve() with an argv[0] that's different from the
463 actual Python executable. This is needed to keep the Finder happy,
464 or rather, to work around Apple's overly strict requirements of
465 the process name. However, we still need a usable sys.executable,
466 so the actual executable path is passed in an environment variable.
467 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
468 script. */
469 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
470 Py_SetProgramName(p);
471 else
472 Py_SetProgramName(argv[0]);
473#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000474 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000475#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000476 Py_Initialize();
477
Guido van Rossum667d7041995-08-04 04:20:48 +0000478 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000479 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000480 fprintf(stderr, "Python %s on %s\n",
481 Py_GetVersion(), Py_GetPlatform());
482 if (!Py_NoSiteFlag)
483 fprintf(stderr, "%s\n", COPYRIGHT);
484 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000485
Guido van Rossum667d7041995-08-04 04:20:48 +0000486 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000487 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
488 _PyOS_optind--;
489 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000490 }
491
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000492 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000493 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
494 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000495 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000496 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000497 }
498
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000499 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000500
Georg Brandl49aafc92007-03-07 00:34:46 +0000501 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000502 isatty(fileno(stdin))) {
503 PyObject *v;
504 v = PyImport_ImportModule("readline");
505 if (v == NULL)
506 PyErr_Clear();
507 else
508 Py_DECREF(v);
509 }
510
Guido van Rossum667d7041995-08-04 04:20:48 +0000511 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000512 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000513 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000514 } else if (module) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000515 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000516 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000517 }
518 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000519
Guido van Rossum775af911997-02-14 19:50:32 +0000520 if (filename == NULL && stdin_is_interactive) {
Georg Brandl49aafc92007-03-07 00:34:46 +0000521 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000522 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000523 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000524 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000525
526 sts = -1; /* keep track of whether we've already run __main__ */
527
528 if (filename != NULL) {
529 sts = RunMainFromImporter(filename);
530 }
531
532 if (sts==-1 && filename!=NULL) {
533 if ((fp = fopen(filename, "r")) == NULL) {
534#ifdef HAVE_STRERROR
535 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
536 argv[0], filename, errno, strerror(errno));
537#else
538 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
539 argv[0], filename, errno);
540#endif
541 return 2;
542 }
543 else if (skipfirstline) {
544 int ch;
545 /* Push back first newline so line numbers
546 remain the same */
547 while ((ch = getc(fp)) != EOF) {
548 if (ch == '\n') {
549 (void)ungetc(ch, fp);
550 break;
551 }
552 }
553 }
554 {
555 /* XXX: does this work on Win/Win64? (see posix_fstat) */
556 struct stat sb;
557 if (fstat(fileno(fp), &sb) == 0 &&
558 S_ISDIR(sb.st_mode)) {
559 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
560 return 1;
561 }
562 }
563 }
564
565 if (sts==-1) {
566 sts = PyRun_AnyFileExFlags(
567 fp,
568 filename == NULL ? "<stdin>" : filename,
569 filename != NULL, &cf) != 0;
570 }
571
Guido van Rossum667d7041995-08-04 04:20:48 +0000572 }
573
Barry Warsawd86dcd32003-06-29 17:07:06 +0000574 /* Check this environment variable at the end, to give programs the
575 * opportunity to set it from Python.
576 */
Georg Brandl49aafc92007-03-07 00:34:46 +0000577 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000578 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
579 {
Georg Brandl49aafc92007-03-07 00:34:46 +0000580 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000581 }
582
Georg Brandl49aafc92007-03-07 00:34:46 +0000583 if (Py_InspectFlag && stdin_is_interactive &&
584 (filename != NULL || command != NULL || module != NULL)) {
585 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000586 /* XXX */
587 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Georg Brandl49aafc92007-03-07 00:34:46 +0000588 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000589
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000590 WaitForThreadShutdown();
591
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000592 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000593#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000594 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000595 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
596#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000597
598#ifdef __INSURE__
599 /* Insure++ is a memory analysis tool that aids in discovering
600 * memory leaks and other memory problems. On Python exit, the
601 * interned string dictionary is flagged as being in use at exit
602 * (which it is). Under normal circumstances, this is fine because
603 * the memory will be automatically reclaimed by the system. Under
604 * memory debugging, it's a huge source of useless noise, so we
605 * trade off slower shutdown for less distraction in the memory
606 * reports. -baw
607 */
608 _Py_ReleaseInternedStrings();
609#endif /* __INSURE__ */
610
Guido van Rossum05f7c501997-08-02 03:00:42 +0000611 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000612}
613
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000614/* this is gonna seem *real weird*, but if you put some other code between
615 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
616 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000617
Guido van Rossum667d7041995-08-04 04:20:48 +0000618/* Make the *original* argc/argv available to other modules.
619 This is rare, but it is needed by the secureware extension. */
620
621void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000622Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000623{
624 *argc = orig_argc;
625 *argv = orig_argv;
626}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000627
628#ifdef __cplusplus
629}
630#endif
631