blob: 3a8a5cb8564f7638ac144fee2acb20a6fa808779 [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 = "\
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +000089-3 : warn about Python 3.x incompatibilities\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\
Guido van Rossum667d7041995-08-04 04:20:48 +0000102";
103
104
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000105static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000106usage(int exitcode, char* program)
107{
Guido van Rossum393661d2001-08-31 17:40:15 +0000108 FILE *f = exitcode ? stderr : stdout;
109
110 fprintf(f, usage_line, program);
111 if (exitcode)
112 fprintf(f, "Try `python -h' for more information.\n");
113 else {
114 fprintf(f, usage_1);
115 fprintf(f, usage_2);
116 fprintf(f, usage_3);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000117 fprintf(f, usage_4, DELIM);
118 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000119 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000120#if defined(__VMS)
121 if (exitcode == 0) {
122 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000123 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000124 }
125 else {
126 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000127 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000128 }
129#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000130 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000131#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000132 /*NOTREACHED*/
133}
134
Martin v. Löwis6caea372003-11-18 19:46:25 +0000135static void RunStartupFile(PyCompilerFlags *cf)
136{
137 char *startup = Py_GETENV("PYTHONSTARTUP");
138 if (startup != NULL && startup[0] != '\0') {
139 FILE *fp = fopen(startup, "r");
140 if (fp != NULL) {
141 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
142 PyErr_Clear();
143 fclose(fp);
Georg Brandl91e0cda2008-03-29 01:50:06 +0000144 } else {
145 int save_errno;
146 save_errno = errno;
147 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
148 errno = save_errno;
149 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
150 startup);
151 PyErr_Print();
152 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000153 }
154 }
155}
156
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000157
Nick Coghlan327a39b2007-11-18 11:56:28 +0000158static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000159{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000160 PyObject *runpy, *runmodule, *runargs, *result;
161 runpy = PyImport_ImportModule("runpy");
162 if (runpy == NULL) {
163 fprintf(stderr, "Could not import runpy module\n");
164 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000165 }
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000166 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000167 if (runmodule == NULL) {
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000168 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000169 Py_DECREF(runpy);
170 return -1;
171 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000172 runargs = Py_BuildValue("(si)", module, set_argv0);
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000173 if (runargs == NULL) {
174 fprintf(stderr,
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000175 "Could not create arguments for runpy._run_module_as_main\n");
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000176 Py_DECREF(runpy);
177 Py_DECREF(runmodule);
178 return -1;
179 }
180 result = PyObject_Call(runmodule, runargs, NULL);
181 if (result == NULL) {
182 PyErr_Print();
183 }
184 Py_DECREF(runpy);
185 Py_DECREF(runmodule);
186 Py_DECREF(runargs);
187 if (result == NULL) {
188 return -1;
189 }
190 Py_DECREF(result);
191 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000192}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000193
Nick Coghlan327a39b2007-11-18 11:56:28 +0000194static int RunMainFromImporter(char *filename)
195{
196 PyObject *argv0 = NULL, *importer = NULL;
197
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000198 if ((argv0 = PyString_FromString(filename)) &&
199 (importer = PyImport_GetImporter(argv0)) &&
200 (importer->ob_type != &PyNullImporter_Type))
Nick Coghlan327a39b2007-11-18 11:56:28 +0000201 {
202 /* argv0 is usable as an import source, so
203 put it in sys.path[0] and import __main__ */
204 PyObject *sys_path = NULL;
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000205 if ((sys_path = PySys_GetObject("path")) &&
206 !PyList_SetItem(sys_path, 0, argv0))
207 {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000208 Py_INCREF(argv0);
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000209 Py_DECREF(importer);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000210 sys_path = NULL;
211 return RunModule("__main__", 0) != 0;
212 }
213 }
Nick Coghlan8c4592a2007-11-20 14:55:57 +0000214 Py_XDECREF(argv0);
215 Py_XDECREF(importer);
216 if (PyErr_Occurred()) {
217 PyErr_Print();
218 return 1;
219 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000220 return -1;
221}
222
223
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000224/* Wait until threading._shutdown completes, provided
225 the threading module was imported in the first place.
226 The shutdown routine will wait until all non-daemon
227 "threading" threads have completed. */
228#include "abstract.h"
229static void
Brett Cannond14ef772007-01-05 21:45:09 +0000230WaitForThreadShutdown(void)
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000231{
232#ifdef WITH_THREAD
233 PyObject *result;
234 PyThreadState *tstate = PyThreadState_GET();
235 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
236 "threading");
237 if (threading == NULL) {
238 /* threading not imported */
239 PyErr_Clear();
240 return;
241 }
242 result = PyObject_CallMethod(threading, "_shutdown", "");
243 if (result == NULL)
244 PyErr_WriteUnraisable(threading);
245 else
246 Py_DECREF(result);
247 Py_DECREF(threading);
248#endif
249}
250
Guido van Rossum667d7041995-08-04 04:20:48 +0000251/* Main program */
252
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000253int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000254Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000255{
256 int c;
257 int sts;
258 char *command = NULL;
259 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000260 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000261 FILE *fp = stdin;
262 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000263 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000264 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000265 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000266 int help = 0;
267 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000268 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000269 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000270
Guido van Rossum393661d2001-08-31 17:40:15 +0000271 cf.cf_flags = 0;
272
Guido van Rossumac56b031996-07-21 02:33:38 +0000273 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000274 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000275
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000276#ifdef RISCOS
277 Py_RISCOSWimpFlag = 0;
278#endif
279
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000280 PySys_ResetWarnOptions();
281
Guido van Rossumbceccf52001-04-10 22:07:43 +0000282 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000283 if (c == 'c') {
284 /* -c is the last option; following arguments
285 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000286 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000287 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000288 if (command == NULL)
289 Py_FatalError(
290 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000291 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000292 strcat(command, "\n");
293 break;
294 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000295
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000296 if (c == 'm') {
297 /* -m is the last option; following arguments
298 that look like options are left for the
299 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000300 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000301 if (module == NULL)
302 Py_FatalError(
303 "not enough memory to copy -m argument");
304 strcpy(module, _PyOS_optarg);
305 break;
306 }
307
Guido van Rossum667d7041995-08-04 04:20:48 +0000308 switch (c) {
Christian Heimes1a6387e2008-03-26 12:49:49 +0000309 case 'b':
310 Py_BytesWarningFlag++;
311 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000312
313 case 'd':
314 Py_DebugFlag++;
315 break;
316
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000317 case '3':
318 Py_Py3kWarningFlag++;
319 break;
320
Guido van Rossum61c345f2001-09-04 03:26:15 +0000321 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000322 if (strcmp(_PyOS_optarg, "old") == 0) {
323 Py_DivisionWarningFlag = 0;
324 break;
325 }
326 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000327 Py_DivisionWarningFlag = 1;
328 break;
329 }
330 if (strcmp(_PyOS_optarg, "warnall") == 0) {
331 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000332 break;
333 }
334 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000335 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000336 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000337 /* And this tells the eval loop to treat
338 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
339 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000340 break;
341 }
342 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000343 "-Q option should be `-Qold', "
344 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000345 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000346 /* NOTREACHED */
347
Guido van Rossum667d7041995-08-04 04:20:48 +0000348 case 'i':
Georg Brandl49aafc92007-03-07 00:34:46 +0000349 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000350 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000351 break;
352
Christian Heimes7a98d272008-04-12 13:03:03 +0000353 /* case 'J': reserved for Jython */
354
Guido van Rossum7614da61997-03-03 19:14:45 +0000355 case 'O':
356 Py_OptimizeFlag++;
357 break;
358
Georg Brandl2da0fce2008-01-07 17:09:35 +0000359 case 'B':
360 Py_DontWriteBytecodeFlag++;
361 break;
362
Christian Heimesaf748c32008-05-06 22:41:46 +0000363 case 's':
364 Py_NoUserSiteDirectory++;
365 break;
366
Guido van Rossum7922bd71997-08-29 22:34:47 +0000367 case 'S':
368 Py_NoSiteFlag++;
369 break;
370
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000371 case 'E':
372 Py_IgnoreEnvironmentFlag++;
373 break;
374
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000375 case 't':
376 Py_TabcheckFlag++;
377 break;
378
Guido van Rossum667d7041995-08-04 04:20:48 +0000379 case 'u':
380 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000381 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000382 break;
383
384 case 'v':
385 Py_VerboseFlag++;
386 break;
387
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000388#ifdef RISCOS
389 case 'w':
390 Py_RISCOSWimpFlag = 1;
391 break;
392#endif
393
Guido van Rossuma075ce11997-12-05 21:56:45 +0000394 case 'x':
395 skipfirstline = 1;
396 break;
397
Georg Brandl41a0a372008-04-13 20:50:29 +0000398 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000399
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000400 case 'U':
401 Py_UnicodeFlag++;
402 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000403 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000404 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000405 help++;
406 break;
407 case 'V':
408 version++;
409 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000410
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000411 case 'W':
412 PySys_AddWarnOption(_PyOS_optarg);
413 break;
414
Guido van Rossum667d7041995-08-04 04:20:48 +0000415 /* This space reserved for other options */
416
417 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000418 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000419 /*NOTREACHED*/
420
421 }
422 }
423
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000424 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000425 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000426
427 if (version) {
428 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000429 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000430 }
431
Georg Brandl49aafc92007-03-07 00:34:46 +0000432 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000433 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Georg Brandl49aafc92007-03-07 00:34:46 +0000434 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000435 if (!saw_unbuffered_flag &&
436 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
437 unbuffered = 1;
438
Christian Heimesaf748c32008-05-06 22:41:46 +0000439 if (!Py_NoUserSiteDirectory &&
440 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
441 Py_NoUserSiteDirectory = 1;
442
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000443 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000444 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000445 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000446#ifdef __VMS
447 filename = decc$translate_vms(argv[_PyOS_optind]);
448 if (filename == (char *)0 || filename == (char *)-1)
449 filename = argv[_PyOS_optind];
450
451#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000452 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000453#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000454 }
455
456 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
457
Guido van Rossum667d7041995-08-04 04:20:48 +0000458 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000459#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000460 _setmode(fileno(stdin), O_BINARY);
461 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000462#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000463#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000464 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
465 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
466 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000467#else /* !HAVE_SETVBUF */
468 setbuf(stdin, (char *)NULL);
469 setbuf(stdout, (char *)NULL);
470 setbuf(stderr, (char *)NULL);
471#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000472 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000473 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000474#ifdef MS_WINDOWS
475 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000476 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000477 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000478#else /* !MS_WINDOWS */
479#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000480 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
481 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000482#endif /* HAVE_SETVBUF */
483#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000484 /* Leave stderr alone - it should be unbuffered anyway. */
485 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000486#ifdef __VMS
487 else {
488 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
489 }
490#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000491
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000492#ifdef __APPLE__
493 /* On MacOS X, when the Python interpreter is embedded in an
494 application bundle, it gets executed by a bootstrapping script
495 that does os.execve() with an argv[0] that's different from the
496 actual Python executable. This is needed to keep the Finder happy,
497 or rather, to work around Apple's overly strict requirements of
498 the process name. However, we still need a usable sys.executable,
499 so the actual executable path is passed in an environment variable.
500 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
501 script. */
502 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
503 Py_SetProgramName(p);
504 else
505 Py_SetProgramName(argv[0]);
506#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000507 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000508#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000509 Py_Initialize();
510
Guido van Rossum667d7041995-08-04 04:20:48 +0000511 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000512 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000513 fprintf(stderr, "Python %s on %s\n",
514 Py_GetVersion(), Py_GetPlatform());
515 if (!Py_NoSiteFlag)
516 fprintf(stderr, "%s\n", COPYRIGHT);
517 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000518
Guido van Rossum667d7041995-08-04 04:20:48 +0000519 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000520 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
521 _PyOS_optind--;
522 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000523 }
524
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000525 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000526 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
527 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000528 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000529 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000530 }
531
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000532 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000533
Georg Brandl49aafc92007-03-07 00:34:46 +0000534 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000535 isatty(fileno(stdin))) {
536 PyObject *v;
537 v = PyImport_ImportModule("readline");
538 if (v == NULL)
539 PyErr_Clear();
540 else
541 Py_DECREF(v);
542 }
543
Guido van Rossum667d7041995-08-04 04:20:48 +0000544 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000545 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000546 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000547 } else if (module) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000548 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000549 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000550 }
551 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000552
Guido van Rossum775af911997-02-14 19:50:32 +0000553 if (filename == NULL && stdin_is_interactive) {
Georg Brandl49aafc92007-03-07 00:34:46 +0000554 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000555 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000556 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000557 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000558
559 sts = -1; /* keep track of whether we've already run __main__ */
560
561 if (filename != NULL) {
562 sts = RunMainFromImporter(filename);
563 }
564
565 if (sts==-1 && filename!=NULL) {
566 if ((fp = fopen(filename, "r")) == NULL) {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000567 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
568 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000569
Nick Coghlan327a39b2007-11-18 11:56:28 +0000570 return 2;
571 }
572 else if (skipfirstline) {
573 int ch;
574 /* Push back first newline so line numbers
575 remain the same */
576 while ((ch = getc(fp)) != EOF) {
577 if (ch == '\n') {
578 (void)ungetc(ch, fp);
579 break;
580 }
581 }
582 }
583 {
584 /* XXX: does this work on Win/Win64? (see posix_fstat) */
585 struct stat sb;
586 if (fstat(fileno(fp), &sb) == 0 &&
587 S_ISDIR(sb.st_mode)) {
588 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes5cc3f262008-01-18 08:53:45 +0000589 fclose(fp);
Nick Coghlan327a39b2007-11-18 11:56:28 +0000590 return 1;
591 }
592 }
593 }
594
595 if (sts==-1) {
596 sts = PyRun_AnyFileExFlags(
597 fp,
598 filename == NULL ? "<stdin>" : filename,
599 filename != NULL, &cf) != 0;
600 }
601
Guido van Rossum667d7041995-08-04 04:20:48 +0000602 }
603
Barry Warsawd86dcd32003-06-29 17:07:06 +0000604 /* Check this environment variable at the end, to give programs the
605 * opportunity to set it from Python.
606 */
Georg Brandl49aafc92007-03-07 00:34:46 +0000607 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000608 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
609 {
Georg Brandl49aafc92007-03-07 00:34:46 +0000610 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000611 }
612
Georg Brandl49aafc92007-03-07 00:34:46 +0000613 if (Py_InspectFlag && stdin_is_interactive &&
614 (filename != NULL || command != NULL || module != NULL)) {
615 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000616 /* XXX */
617 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Georg Brandl49aafc92007-03-07 00:34:46 +0000618 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000619
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000620 WaitForThreadShutdown();
621
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000622 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000623#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000624 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000625 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
626#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000627
628#ifdef __INSURE__
629 /* Insure++ is a memory analysis tool that aids in discovering
630 * memory leaks and other memory problems. On Python exit, the
631 * interned string dictionary is flagged as being in use at exit
632 * (which it is). Under normal circumstances, this is fine because
633 * the memory will be automatically reclaimed by the system. Under
634 * memory debugging, it's a huge source of useless noise, so we
635 * trade off slower shutdown for less distraction in the memory
636 * reports. -baw
637 */
638 _Py_ReleaseInternedStrings();
639#endif /* __INSURE__ */
640
Guido van Rossum05f7c501997-08-02 03:00:42 +0000641 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000642}
643
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000644/* this is gonna seem *real weird*, but if you put some other code between
645 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
646 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000647
Guido van Rossum667d7041995-08-04 04:20:48 +0000648/* Make the *original* argc/argv available to other modules.
649 This is rare, but it is needed by the secureware extension. */
650
651void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000652Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000653{
654 *argc = orig_argc;
655 *argv = orig_argv;
656}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000657
658#ifdef __cplusplus
659}
660#endif
661