blob: 929991a8c55f89c5f414d05af387d5ad10f194f4 [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 */
Barry Warsaw1e13eb02012-02-20 20:42:21 -050043#define BASE_OPTS "3bBc:dEhiJm:OQ:RsStuUvVW: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\
Barry Warsaw1e13eb02012-02-20 20:42:21 -050074-R : use a pseudo-random salt to make hash() values of various types be\n\
75 unpredictable between separate invocations of the interpreter, as\n\
76 a defense against denial-of-service attacks\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000077-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Christian Heimesaf748c32008-05-06 22:41:46 +000078-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000079-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000080-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000081";
82static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000083-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000084 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000085-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
86 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000087-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000088-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000089-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000090";
91static char *usage_4 = "\
Georg Brandlc04c2892009-01-14 00:00:17 +000092-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000093file : program read from script file\n\
94- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000095arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000096Other environment variables:\n\
97PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000098PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000099 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +0000100";
101static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000102PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
103 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000104PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000105PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000106";
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500107static char *usage_6 = "\
108PYTHONHASHSEED: if this variable is set to ``random``, the effect is the same \n\
109 as specifying the :option:`-R` option: a random value is used to seed the\n\
110 hashes of str, bytes and datetime objects. It can also be set to an integer\n\
111 in the range [0,4294967295] to get hash values with a predictable seed.\n\
112";
Guido van Rossum667d7041995-08-04 04:20:48 +0000113
114
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000115static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000116usage(int exitcode, char* program)
117{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000118 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000119
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000120 fprintf(f, usage_line, program);
121 if (exitcode)
122 fprintf(f, "Try `python -h' for more information.\n");
123 else {
124 fputs(usage_1, f);
125 fputs(usage_2, f);
126 fputs(usage_3, f);
127 fprintf(f, usage_4, DELIM);
128 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500129 fputs(usage_6, f);
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000130 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000131#if defined(__VMS)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000132 if (exitcode == 0) {
133 /* suppress 'error' message */
134 return 1;
135 }
136 else {
137 /* STS$M_INHIB_MSG + SS$_ABORT */
138 return 0x1000002c;
139 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000140#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000141 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000142#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000143 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000144}
145
Martin v. Löwis6caea372003-11-18 19:46:25 +0000146static void RunStartupFile(PyCompilerFlags *cf)
147{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000148 char *startup = Py_GETENV("PYTHONSTARTUP");
149 if (startup != NULL && startup[0] != '\0') {
150 FILE *fp = fopen(startup, "r");
151 if (fp != NULL) {
152 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
153 PyErr_Clear();
154 fclose(fp);
155 } else {
156 int save_errno;
157 save_errno = errno;
158 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
159 errno = save_errno;
160 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
161 startup);
162 PyErr_Print();
163 PyErr_Clear();
164 }
165 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000166}
167
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000168
Nick Coghlan327a39b2007-11-18 11:56:28 +0000169static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000170{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000171 PyObject *runpy, *runmodule, *runargs, *result;
172 runpy = PyImport_ImportModule("runpy");
173 if (runpy == NULL) {
174 fprintf(stderr, "Could not import runpy module\n");
175 return -1;
176 }
177 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
178 if (runmodule == NULL) {
179 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
180 Py_DECREF(runpy);
181 return -1;
182 }
183 runargs = Py_BuildValue("(si)", module, set_argv0);
184 if (runargs == NULL) {
185 fprintf(stderr,
186 "Could not create arguments for runpy._run_module_as_main\n");
187 Py_DECREF(runpy);
188 Py_DECREF(runmodule);
189 return -1;
190 }
191 result = PyObject_Call(runmodule, runargs, NULL);
192 if (result == NULL) {
193 PyErr_Print();
194 }
195 Py_DECREF(runpy);
196 Py_DECREF(runmodule);
197 Py_DECREF(runargs);
198 if (result == NULL) {
199 return -1;
200 }
201 Py_DECREF(result);
202 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000203}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000204
Nick Coghlan327a39b2007-11-18 11:56:28 +0000205static int RunMainFromImporter(char *filename)
206{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000207 PyObject *argv0 = NULL, *importer = NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000208
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000209 if ((argv0 = PyString_FromString(filename)) &&
210 (importer = PyImport_GetImporter(argv0)) &&
211 (importer->ob_type != &PyNullImporter_Type))
212 {
213 /* argv0 is usable as an import source, so
214 put it in sys.path[0] and import __main__ */
215 PyObject *sys_path = NULL;
216 if ((sys_path = PySys_GetObject("path")) &&
217 !PyList_SetItem(sys_path, 0, argv0))
218 {
219 Py_INCREF(argv0);
220 Py_DECREF(importer);
221 sys_path = NULL;
222 return RunModule("__main__", 0) != 0;
223 }
224 }
225 Py_XDECREF(argv0);
226 Py_XDECREF(importer);
227 if (PyErr_Occurred()) {
228 PyErr_Print();
229 return 1;
230 }
231 return -1;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000232}
233
234
Guido van Rossum667d7041995-08-04 04:20:48 +0000235/* Main program */
236
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000237int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000238Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000239{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000240 int c;
241 int sts;
242 char *command = NULL;
243 char *filename = NULL;
244 char *module = NULL;
245 FILE *fp = stdin;
246 char *p;
247 int unbuffered = 0;
248 int skipfirstline = 0;
249 int stdin_is_interactive = 0;
250 int help = 0;
251 int version = 0;
252 int saw_unbuffered_flag = 0;
253 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000254
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000255 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000256
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000257 orig_argc = argc; /* For Py_GetArgcArgv() */
258 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000259
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000260#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000261 Py_RISCOSWimpFlag = 0;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000262#endif
263
Antoine Pitrou776af402012-02-21 20:42:48 +0100264 /* Hash randomization needed early for all string operations
265 (including -W and -X options). */
266 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
267 if (c == 'm' || c == 'c') {
268 /* -c / -m is the last option: following arguments are
269 not interpreter options. */
270 break;
271 }
272 switch (c) {
273 case 'E':
274 Py_IgnoreEnvironmentFlag++;
275 break;
276 case 'R':
277 Py_HashRandomizationFlag++;
278 break;
279 }
280 }
281 /* The variable is only tested for existence here; _PyRandom_Init will
282 check its value further. */
283 if (!Py_HashRandomizationFlag &&
284 (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
285 Py_HashRandomizationFlag = 1;
286
287 _PyRandom_Init();
288
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000289 PySys_ResetWarnOptions();
Antoine Pitrou776af402012-02-21 20:42:48 +0100290 _PyOS_ResetGetOpt();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000291
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000292 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
293 if (c == 'c') {
294 /* -c is the last option; following arguments
295 that look like options are left for the
296 command to interpret. */
297 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
298 if (command == NULL)
299 Py_FatalError(
300 "not enough memory to copy -c argument");
301 strcpy(command, _PyOS_optarg);
302 strcat(command, "\n");
303 break;
304 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000305
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000306 if (c == 'm') {
307 /* -m is the last option; following arguments
308 that look like options are left for the
309 module to interpret. */
310 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
311 if (module == NULL)
312 Py_FatalError(
313 "not enough memory to copy -m argument");
314 strcpy(module, _PyOS_optarg);
315 break;
316 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000317
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000318 switch (c) {
319 case 'b':
320 Py_BytesWarningFlag++;
321 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000322
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000323 case 'd':
324 Py_DebugFlag++;
325 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000326
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000327 case '3':
328 Py_Py3kWarningFlag++;
329 if (!Py_DivisionWarningFlag)
330 Py_DivisionWarningFlag = 1;
331 break;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000332
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000333 case 'Q':
334 if (strcmp(_PyOS_optarg, "old") == 0) {
335 Py_DivisionWarningFlag = 0;
336 break;
337 }
338 if (strcmp(_PyOS_optarg, "warn") == 0) {
339 Py_DivisionWarningFlag = 1;
340 break;
341 }
342 if (strcmp(_PyOS_optarg, "warnall") == 0) {
343 Py_DivisionWarningFlag = 2;
344 break;
345 }
346 if (strcmp(_PyOS_optarg, "new") == 0) {
347 /* This only affects __main__ */
348 cf.cf_flags |= CO_FUTURE_DIVISION;
349 /* And this tells the eval loop to treat
350 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
351 _Py_QnewFlag = 1;
352 break;
353 }
354 fprintf(stderr,
355 "-Q option should be `-Qold', "
356 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
357 return usage(2, argv[0]);
358 /* NOTREACHED */
Guido van Rossum393661d2001-08-31 17:40:15 +0000359
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000360 case 'i':
361 Py_InspectFlag++;
362 Py_InteractiveFlag++;
363 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000364
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000365 /* case 'J': reserved for Jython */
Christian Heimes7a98d272008-04-12 13:03:03 +0000366
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000367 case 'O':
368 Py_OptimizeFlag++;
369 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000370
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000371 case 'B':
372 Py_DontWriteBytecodeFlag++;
373 break;
Georg Brandl2da0fce2008-01-07 17:09:35 +0000374
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000375 case 's':
376 Py_NoUserSiteDirectory++;
377 break;
Christian Heimesaf748c32008-05-06 22:41:46 +0000378
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000379 case 'S':
380 Py_NoSiteFlag++;
381 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000382
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000383 case 'E':
Antoine Pitrou776af402012-02-21 20:42:48 +0100384 /* Already handled above */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000385 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000386
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000387 case 't':
388 Py_TabcheckFlag++;
389 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000390
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000391 case 'u':
392 unbuffered++;
393 saw_unbuffered_flag = 1;
394 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000395
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000396 case 'v':
397 Py_VerboseFlag++;
398 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000399
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000400#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000401 case 'w':
402 Py_RISCOSWimpFlag = 1;
403 break;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000404#endif
405
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000406 case 'x':
407 skipfirstline = 1;
408 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000409
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000410 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000411
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000412 case 'U':
413 Py_UnicodeFlag++;
414 break;
415 case 'h':
416 case '?':
417 help++;
418 break;
419 case 'V':
420 version++;
421 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000422
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000423 case 'W':
424 PySys_AddWarnOption(_PyOS_optarg);
Barry Warsaw8757cad2012-02-21 11:16:06 -0500425 /* Extremely obscure hack: if _PyOS_optarg was one character,
426 PyString_FromString in PySys_AddWarnOption will try to intern
427 it. This is bad because hash randomization has not been setup
428 yet, so the string will get the wrong hash. The following call
429 will cause all the cached characters to be released. */
430 PyString_Fini();
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000431 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000432
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500433 case 'R':
Antoine Pitrou776af402012-02-21 20:42:48 +0100434 /* Already handled above */
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500435 break;
436
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000437 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000438
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000439 default:
440 return usage(2, argv[0]);
441 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000442
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000443 }
444 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000445
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000446 if (help)
447 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000448
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000449 if (version) {
450 fprintf(stderr, "Python %s\n", PY_VERSION);
451 return 0;
452 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000453
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000454 if (!Py_InspectFlag &&
455 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
456 Py_InspectFlag = 1;
457 if (!saw_unbuffered_flag &&
458 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
459 unbuffered = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000460
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000461 if (!Py_NoUserSiteDirectory &&
462 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
463 Py_NoUserSiteDirectory = 1;
Christian Heimesaf748c32008-05-06 22:41:46 +0000464
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000465 if (command == NULL && module == NULL && _PyOS_optind < argc &&
466 strcmp(argv[_PyOS_optind], "-") != 0)
467 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000468#ifdef __VMS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000469 filename = decc$translate_vms(argv[_PyOS_optind]);
470 if (filename == (char *)0 || filename == (char *)-1)
471 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000472
473#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000474 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000475#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000476 }
Guido van Rossum775af911997-02-14 19:50:32 +0000477
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000478 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000479
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000480 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000481#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000482 _setmode(fileno(stdin), O_BINARY);
483 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000484#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000485#ifdef HAVE_SETVBUF
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000486 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
487 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
488 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000489#else /* !HAVE_SETVBUF */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000490 setbuf(stdin, (char *)NULL);
491 setbuf(stdout, (char *)NULL);
492 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000493#endif /* !HAVE_SETVBUF */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000494 }
495 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000496#ifdef MS_WINDOWS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000497 /* Doesn't have to have line-buffered -- use unbuffered */
498 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
499 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000500#else /* !MS_WINDOWS */
501#ifdef HAVE_SETVBUF
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000502 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
503 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000504#endif /* HAVE_SETVBUF */
505#endif /* !MS_WINDOWS */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000506 /* Leave stderr alone - it should be unbuffered anyway. */
507 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000508#ifdef __VMS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000509 else {
510 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
511 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000512#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000513
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000514#ifdef __APPLE__
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000515 /* On MacOS X, when the Python interpreter is embedded in an
516 application bundle, it gets executed by a bootstrapping script
517 that does os.execve() with an argv[0] that's different from the
518 actual Python executable. This is needed to keep the Finder happy,
519 or rather, to work around Apple's overly strict requirements of
520 the process name. However, we still need a usable sys.executable,
521 so the actual executable path is passed in an environment variable.
522 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
523 script. */
524 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
525 Py_SetProgramName(p);
526 else
527 Py_SetProgramName(argv[0]);
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000528#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000529 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000530#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000531 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000532
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000533 if (Py_VerboseFlag ||
534 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
535 fprintf(stderr, "Python %s on %s\n",
536 Py_GetVersion(), Py_GetPlatform());
537 if (!Py_NoSiteFlag)
538 fprintf(stderr, "%s\n", COPYRIGHT);
539 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000540
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000541 if (command != NULL) {
542 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
543 _PyOS_optind--;
544 argv[_PyOS_optind] = "-c";
545 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000546
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000547 if (module != NULL) {
548 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
549 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
550 _PyOS_optind--;
551 argv[_PyOS_optind] = "-c";
552 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000553
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000554 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000555
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000556 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
557 isatty(fileno(stdin))) {
558 PyObject *v;
559 v = PyImport_ImportModule("readline");
560 if (v == NULL)
561 PyErr_Clear();
562 else
563 Py_DECREF(v);
564 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000565
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000566 if (command) {
567 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
568 free(command);
569 } else if (module) {
570 sts = RunModule(module, 1);
571 free(module);
572 }
573 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000574
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000575 if (filename == NULL && stdin_is_interactive) {
576 Py_InspectFlag = 0; /* do exit on SystemExit */
577 RunStartupFile(&cf);
578 }
579 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000580
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000581 sts = -1; /* keep track of whether we've already run __main__ */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000582
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000583 if (filename != NULL) {
584 sts = RunMainFromImporter(filename);
585 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000586
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000587 if (sts==-1 && filename!=NULL) {
588 if ((fp = fopen(filename, "r")) == NULL) {
589 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
590 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000591
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000592 return 2;
593 }
594 else if (skipfirstline) {
595 int ch;
596 /* Push back first newline so line numbers
597 remain the same */
598 while ((ch = getc(fp)) != EOF) {
599 if (ch == '\n') {
600 (void)ungetc(ch, fp);
601 break;
602 }
603 }
604 }
605 {
606 /* XXX: does this work on Win/Win64? (see posix_fstat) */
607 struct stat sb;
608 if (fstat(fileno(fp), &sb) == 0 &&
609 S_ISDIR(sb.st_mode)) {
610 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
611 fclose(fp);
612 return 1;
613 }
614 }
615 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000616
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000617 if (sts==-1) {
618 /* call pending calls like signal handlers (SIGINT) */
619 if (Py_MakePendingCalls() == -1) {
620 PyErr_Print();
621 sts = 1;
622 } else {
623 sts = PyRun_AnyFileExFlags(
624 fp,
625 filename == NULL ? "<stdin>" : filename,
626 filename != NULL, &cf) != 0;
627 }
628 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000629
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000630 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000631
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000632 /* Check this environment variable at the end, to give programs the
633 * opportunity to set it from Python.
634 */
635 if (!Py_InspectFlag &&
636 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
637 {
638 Py_InspectFlag = 1;
639 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000640
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000641 if (Py_InspectFlag && stdin_is_interactive &&
642 (filename != NULL || command != NULL || module != NULL)) {
643 Py_InspectFlag = 0;
644 /* XXX */
645 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
646 }
647
648 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000649#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000650 if (Py_RISCOSWimpFlag)
651 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000652#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000653
654#ifdef __INSURE__
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000655 /* Insure++ is a memory analysis tool that aids in discovering
656 * memory leaks and other memory problems. On Python exit, the
657 * interned string dictionary is flagged as being in use at exit
658 * (which it is). Under normal circumstances, this is fine because
659 * the memory will be automatically reclaimed by the system. Under
660 * memory debugging, it's a huge source of useless noise, so we
661 * trade off slower shutdown for less distraction in the memory
662 * reports. -baw
663 */
664 _Py_ReleaseInternedStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000665#endif /* __INSURE__ */
666
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000667 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000668}
669
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000670/* this is gonna seem *real weird*, but if you put some other code between
671 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
672 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000673
Guido van Rossum667d7041995-08-04 04:20:48 +0000674/* Make the *original* argc/argv available to other modules.
675 This is rare, but it is needed by the secureware extension. */
676
677void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000678Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000679{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000680 *argc = orig_argc;
681 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000682}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000683
684#ifdef __cplusplus
685}
686#endif
687