blob: d6df92f6843f3746fb85c895e644b8662cbc12aa [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 */
Christian Heimesaf748c32008-05-06 22:41:46 +000043#define BASE_OPTS "3bBc:dEhiJm:OQ:sStuUvVW: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\
Christian Heimesaf748c32008-05-06 22:41:46 +000075-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000076-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000077-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000078";
79static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000080-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000081 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000082-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
83 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000084-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000085-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000086-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000087";
88static char *usage_4 = "\
Benjamin Petersonf902a942009-01-09 03:07:27 +000089-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090file : program read from script file\n\
91- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000092arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000093Other environment variables:\n\
94PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000095PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000096 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000097";
98static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +000099PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
100 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000101PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000102PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000103";
104
105
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000106static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000107usage(int exitcode, char* program)
108{
Guido van Rossum393661d2001-08-31 17:40:15 +0000109 FILE *f = exitcode ? stderr : stdout;
110
111 fprintf(f, usage_line, program);
112 if (exitcode)
113 fprintf(f, "Try `python -h' for more information.\n");
114 else {
115 fprintf(f, usage_1);
116 fprintf(f, usage_2);
117 fprintf(f, usage_3);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000118 fprintf(f, usage_4, DELIM);
119 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000120 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000121#if defined(__VMS)
122 if (exitcode == 0) {
123 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000124 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000125 }
126 else {
127 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000128 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000129 }
130#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000131 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000133 /*NOTREACHED*/
134}
135
Martin v. Löwis6caea372003-11-18 19:46:25 +0000136static void RunStartupFile(PyCompilerFlags *cf)
137{
138 char *startup = Py_GETENV("PYTHONSTARTUP");
139 if (startup != NULL && startup[0] != '\0') {
140 FILE *fp = fopen(startup, "r");
141 if (fp != NULL) {
142 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
143 PyErr_Clear();
144 fclose(fp);
Georg Brandl91e0cda2008-03-29 01:50:06 +0000145 } else {
146 int save_errno;
147 save_errno = errno;
148 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
149 errno = save_errno;
150 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
151 startup);
152 PyErr_Print();
153 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000154 }
155 }
156}
157
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000158
Nick Coghlan327a39b2007-11-18 11:56:28 +0000159static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000160{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000161 PyObject *runpy, *runmodule, *runargs, *result;
162 runpy = PyImport_ImportModule("runpy");
163 if (runpy == NULL) {
164 fprintf(stderr, "Could not import runpy module\n");
165 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000166 }
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000167 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000168 if (runmodule == NULL) {
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000169 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000170 Py_DECREF(runpy);
171 return -1;
172 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000173 runargs = Py_BuildValue("(si)", module, set_argv0);
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000174 if (runargs == NULL) {
175 fprintf(stderr,
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000176 "Could not create arguments for runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000177 Py_DECREF(runpy);
178 Py_DECREF(runmodule);
179 return -1;
180 }
181 result = PyObject_Call(runmodule, runargs, NULL);
182 if (result == NULL) {
183 PyErr_Print();
184 }
185 Py_DECREF(runpy);
186 Py_DECREF(runmodule);
187 Py_DECREF(runargs);
188 if (result == NULL) {
189 return -1;
190 }
191 Py_DECREF(result);
192 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000193}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000194
Nick Coghlan327a39b2007-11-18 11:56:28 +0000195static int RunMainFromImporter(char *filename)
196{
197 PyObject *argv0 = NULL, *importer = NULL;
198
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000199 if ((argv0 = PyString_FromString(filename)) &&
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000200 (importer = PyImport_GetImporter(argv0)) &&
201 (importer->ob_type != &PyNullImporter_Type))
Nick Coghlan327a39b2007-11-18 11:56:28 +0000202 {
203 /* argv0 is usable as an import source, so
204 put it in sys.path[0] and import __main__ */
205 PyObject *sys_path = NULL;
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000206 if ((sys_path = PySys_GetObject("path")) &&
207 !PyList_SetItem(sys_path, 0, argv0))
208 {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000209 Py_INCREF(argv0);
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000210 Py_DECREF(importer);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000211 sys_path = NULL;
212 return RunModule("__main__", 0) != 0;
213 }
214 }
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000215 Py_XDECREF(argv0);
216 Py_XDECREF(importer);
217 if (PyErr_Occurred()) {
218 PyErr_Print();
219 return 1;
220 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000221 return -1;
222}
223
224
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000225/* Wait until threading._shutdown completes, provided
226 the threading module was imported in the first place.
227 The shutdown routine will wait until all non-daemon
228 "threading" threads have completed. */
229#include "abstract.h"
230static void
Brett Cannond14ef772007-01-05 21:45:09 +0000231WaitForThreadShutdown(void)
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000232{
233#ifdef WITH_THREAD
234 PyObject *result;
235 PyThreadState *tstate = PyThreadState_GET();
236 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
237 "threading");
238 if (threading == NULL) {
239 /* threading not imported */
240 PyErr_Clear();
241 return;
242 }
243 result = PyObject_CallMethod(threading, "_shutdown", "");
244 if (result == NULL)
245 PyErr_WriteUnraisable(threading);
246 else
247 Py_DECREF(result);
248 Py_DECREF(threading);
249#endif
250}
251
Guido van Rossum667d7041995-08-04 04:20:48 +0000252/* Main program */
253
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000254int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000255Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000256{
257 int c;
258 int sts;
259 char *command = NULL;
260 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000261 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000262 FILE *fp = stdin;
263 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000265 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000266 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000267 int help = 0;
268 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000269 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000270 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000271
Guido van Rossum393661d2001-08-31 17:40:15 +0000272 cf.cf_flags = 0;
273
Guido van Rossumac56b031996-07-21 02:33:38 +0000274 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000275 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000276
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000277#ifdef RISCOS
278 Py_RISCOSWimpFlag = 0;
279#endif
280
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000281 PySys_ResetWarnOptions();
282
Guido van Rossumbceccf52001-04-10 22:07:43 +0000283 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000284 if (c == 'c') {
285 /* -c is the last option; following arguments
286 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000287 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000288 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 if (command == NULL)
290 Py_FatalError(
291 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000292 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000293 strcat(command, "\n");
294 break;
295 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000296
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000297 if (c == 'm') {
298 /* -m is the last option; following arguments
299 that look like options are left for the
300 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000301 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000302 if (module == NULL)
303 Py_FatalError(
304 "not enough memory to copy -m argument");
305 strcpy(module, _PyOS_optarg);
306 break;
307 }
308
Guido van Rossum667d7041995-08-04 04:20:48 +0000309 switch (c) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000310 case 'b':
311 Py_BytesWarningFlag++;
312 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000313
314 case 'd':
315 Py_DebugFlag++;
316 break;
317
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000318 case '3':
319 Py_Py3kWarningFlag++;
Raymond Hettinger08259e82009-02-18 23:10:19 +0000320 if (!Py_DivisionWarningFlag)
321 Py_DivisionWarningFlag = 1;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000322 break;
323
Guido van Rossum61c345f2001-09-04 03:26:15 +0000324 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000325 if (strcmp(_PyOS_optarg, "old") == 0) {
326 Py_DivisionWarningFlag = 0;
327 break;
328 }
329 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000330 Py_DivisionWarningFlag = 1;
331 break;
332 }
333 if (strcmp(_PyOS_optarg, "warnall") == 0) {
334 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000335 break;
336 }
337 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000338 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000339 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000340 /* And this tells the eval loop to treat
341 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
342 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000343 break;
344 }
345 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000346 "-Q option should be `-Qold', "
347 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000348 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000349 /* NOTREACHED */
350
Guido van Rossum667d7041995-08-04 04:20:48 +0000351 case 'i':
Georg Brandl49aafc92007-03-07 00:34:46 +0000352 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000353 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000354 break;
355
Christian Heimes7a98d272008-04-12 13:03:03 +0000356 /* case 'J': reserved for Jython */
357
Guido van Rossum7614da61997-03-03 19:14:45 +0000358 case 'O':
359 Py_OptimizeFlag++;
360 break;
361
Georg Brandl2da0fce2008-01-07 17:09:35 +0000362 case 'B':
363 Py_DontWriteBytecodeFlag++;
364 break;
365
Christian Heimesaf748c32008-05-06 22:41:46 +0000366 case 's':
367 Py_NoUserSiteDirectory++;
368 break;
369
Guido van Rossum7922bd71997-08-29 22:34:47 +0000370 case 'S':
371 Py_NoSiteFlag++;
372 break;
373
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000374 case 'E':
375 Py_IgnoreEnvironmentFlag++;
376 break;
377
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000378 case 't':
379 Py_TabcheckFlag++;
380 break;
381
Guido van Rossum667d7041995-08-04 04:20:48 +0000382 case 'u':
383 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000384 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000385 break;
386
387 case 'v':
388 Py_VerboseFlag++;
389 break;
390
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000391#ifdef RISCOS
392 case 'w':
393 Py_RISCOSWimpFlag = 1;
394 break;
395#endif
396
Guido van Rossuma075ce11997-12-05 21:56:45 +0000397 case 'x':
398 skipfirstline = 1;
399 break;
400
Georg Brandl41a0a372008-04-13 20:50:29 +0000401 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000402
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000403 case 'U':
404 Py_UnicodeFlag++;
405 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000406 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000407 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000408 help++;
409 break;
410 case 'V':
411 version++;
412 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000413
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000414 case 'W':
415 PySys_AddWarnOption(_PyOS_optarg);
416 break;
417
Guido van Rossum667d7041995-08-04 04:20:48 +0000418 /* This space reserved for other options */
419
420 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000421 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000422 /*NOTREACHED*/
423
424 }
425 }
426
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000427 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000428 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000429
430 if (version) {
431 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000432 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000433 }
434
Georg Brandl3c4a5462009-04-12 12:08:12 +0000435 if (Py_Py3kWarningFlag && !Py_TabcheckFlag)
436 /* -3 implies -t (but not -tt) */
437 Py_TabcheckFlag = 1;
438
Georg Brandl49aafc92007-03-07 00:34:46 +0000439 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000440 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Georg Brandl49aafc92007-03-07 00:34:46 +0000441 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000442 if (!saw_unbuffered_flag &&
443 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
444 unbuffered = 1;
445
Christian Heimesaf748c32008-05-06 22:41:46 +0000446 if (!Py_NoUserSiteDirectory &&
447 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
448 Py_NoUserSiteDirectory = 1;
449
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000450 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000451 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000452 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000453#ifdef __VMS
454 filename = decc$translate_vms(argv[_PyOS_optind]);
455 if (filename == (char *)0 || filename == (char *)-1)
456 filename = argv[_PyOS_optind];
457
458#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000459 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000460#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000461 }
462
463 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
464
Guido van Rossum667d7041995-08-04 04:20:48 +0000465 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000466#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000467 _setmode(fileno(stdin), O_BINARY);
468 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000469#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000470#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000471 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
472 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
473 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000474#else /* !HAVE_SETVBUF */
475 setbuf(stdin, (char *)NULL);
476 setbuf(stdout, (char *)NULL);
477 setbuf(stderr, (char *)NULL);
478#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000479 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000480 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000481#ifdef MS_WINDOWS
482 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000483 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000484 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000485#else /* !MS_WINDOWS */
486#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000487 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
488 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000489#endif /* HAVE_SETVBUF */
490#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000491 /* Leave stderr alone - it should be unbuffered anyway. */
492 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000493#ifdef __VMS
494 else {
495 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
496 }
497#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000498
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000499#ifdef __APPLE__
500 /* On MacOS X, when the Python interpreter is embedded in an
501 application bundle, it gets executed by a bootstrapping script
502 that does os.execve() with an argv[0] that's different from the
503 actual Python executable. This is needed to keep the Finder happy,
504 or rather, to work around Apple's overly strict requirements of
505 the process name. However, we still need a usable sys.executable,
506 so the actual executable path is passed in an environment variable.
507 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
508 script. */
509 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
510 Py_SetProgramName(p);
511 else
512 Py_SetProgramName(argv[0]);
513#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000514 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000515#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000516 Py_Initialize();
517
Guido van Rossum667d7041995-08-04 04:20:48 +0000518 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000519 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000520 fprintf(stderr, "Python %s on %s\n",
521 Py_GetVersion(), Py_GetPlatform());
522 if (!Py_NoSiteFlag)
523 fprintf(stderr, "%s\n", COPYRIGHT);
524 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000525
Guido van Rossum667d7041995-08-04 04:20:48 +0000526 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000527 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
528 _PyOS_optind--;
529 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000530 }
531
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000532 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000533 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
534 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000535 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000536 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000537 }
538
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000539 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000540
Georg Brandl49aafc92007-03-07 00:34:46 +0000541 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000542 isatty(fileno(stdin))) {
543 PyObject *v;
544 v = PyImport_ImportModule("readline");
545 if (v == NULL)
546 PyErr_Clear();
547 else
548 Py_DECREF(v);
549 }
550
Guido van Rossum667d7041995-08-04 04:20:48 +0000551 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000552 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000553 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000554 } else if (module) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000555 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000556 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000557 }
558 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000559
Guido van Rossum775af911997-02-14 19:50:32 +0000560 if (filename == NULL && stdin_is_interactive) {
Georg Brandl49aafc92007-03-07 00:34:46 +0000561 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000562 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000563 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000564 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000565
566 sts = -1; /* keep track of whether we've already run __main__ */
567
568 if (filename != NULL) {
569 sts = RunMainFromImporter(filename);
570 }
571
572 if (sts==-1 && filename!=NULL) {
573 if ((fp = fopen(filename, "r")) == NULL) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000574 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
575 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000576
Nick Coghlan327a39b2007-11-18 11:56:28 +0000577 return 2;
578 }
579 else if (skipfirstline) {
580 int ch;
581 /* Push back first newline so line numbers
582 remain the same */
583 while ((ch = getc(fp)) != EOF) {
584 if (ch == '\n') {
585 (void)ungetc(ch, fp);
586 break;
587 }
588 }
589 }
590 {
591 /* XXX: does this work on Win/Win64? (see posix_fstat) */
592 struct stat sb;
593 if (fstat(fileno(fp), &sb) == 0 &&
594 S_ISDIR(sb.st_mode)) {
595 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes5cc3f262008-01-18 08:53:45 +0000596 fclose(fp);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000597 return 1;
598 }
599 }
600 }
601
602 if (sts==-1) {
603 sts = PyRun_AnyFileExFlags(
604 fp,
605 filename == NULL ? "<stdin>" : filename,
606 filename != NULL, &cf) != 0;
607 }
608
Guido van Rossum667d7041995-08-04 04:20:48 +0000609 }
610
Barry Warsawd86dcd32003-06-29 17:07:06 +0000611 /* Check this environment variable at the end, to give programs the
612 * opportunity to set it from Python.
613 */
Georg Brandl49aafc92007-03-07 00:34:46 +0000614 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000615 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
616 {
Georg Brandl49aafc92007-03-07 00:34:46 +0000617 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000618 }
619
Georg Brandl49aafc92007-03-07 00:34:46 +0000620 if (Py_InspectFlag && stdin_is_interactive &&
621 (filename != NULL || command != NULL || module != NULL)) {
622 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000623 /* XXX */
624 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Georg Brandl49aafc92007-03-07 00:34:46 +0000625 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000626
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000627 WaitForThreadShutdown();
628
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000629 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000630#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000631 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000632 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
633#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000634
635#ifdef __INSURE__
636 /* Insure++ is a memory analysis tool that aids in discovering
637 * memory leaks and other memory problems. On Python exit, the
638 * interned string dictionary is flagged as being in use at exit
639 * (which it is). Under normal circumstances, this is fine because
640 * the memory will be automatically reclaimed by the system. Under
641 * memory debugging, it's a huge source of useless noise, so we
642 * trade off slower shutdown for less distraction in the memory
643 * reports. -baw
644 */
645 _Py_ReleaseInternedStrings();
646#endif /* __INSURE__ */
647
Guido van Rossum05f7c501997-08-02 03:00:42 +0000648 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000649}
650
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000651/* this is gonna seem *real weird*, but if you put some other code between
652 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
653 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000654
Guido van Rossum667d7041995-08-04 04:20:48 +0000655/* Make the *original* argc/argv available to other modules.
656 This is rare, but it is needed by the secureware extension. */
657
658void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000659Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000660{
661 *argc = orig_argc;
662 *argv = orig_argv;
663}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000664
665#ifdef __cplusplus
666}
667#endif
668