blob: ac6c02742098ce7213ebfb98b2d35c6194ef3216 [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__)
Steve Dowerf85dbfc2016-12-28 15:41:09 -080013#ifdef HAVE_IO_H
14#include <io.h>
15#endif
Martin v. Löwisa43190b2006-05-22 09:15:18 +000016#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017#include <fcntl.h>
18#endif
Martin v. Löwisa43190b2006-05-22 09:15:18 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000022#define PYTHONHOMEHELP "<prefix>\\lib"
23#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000024#if defined(PYOS_OS2) && defined(PYCC_GCC)
25#define PYTHONHOMEHELP "<prefix>/Lib"
26#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000027#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000028#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000029#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000030
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000031#include "pygetopt.h"
32
Guido van Rossuma22865e2000-09-05 04:41:18 +000033#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000034 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
35 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000036
Anthony Baxterac6bd462006-04-13 02:06:09 +000037#ifdef __cplusplus
38extern "C" {
39#endif
40
Guido van Rossumac56b031996-07-21 02:33:38 +000041/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000042static char **orig_argv;
43static int orig_argc;
44
Guido van Rossumbceccf52001-04-10 22:07:43 +000045/* command line options */
Barry Warsaw1e13eb02012-02-20 20:42:21 -050046#define BASE_OPTS "3bBc:dEhiJm:OQ:RsStuUvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000047
48#ifndef RISCOS
49#define PROGRAM_OPTS BASE_OPTS
50#else /*RISCOS*/
51/* extra option saying that we are running under a special task window
52 frontend; especially my_readline will behave different */
53#define PROGRAM_OPTS BASE_OPTS "w"
54/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000055extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000056#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000057
Guido van Rossum667d7041995-08-04 04:20:48 +000058/* Short usage message (with %s for argv0) */
59static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000060"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000061
62/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000063static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000064Options and arguments (and corresponding environment variables):\n\
Zachary Waref6c6d1e2017-05-13 09:30:20 -050065-b : issue warnings about comparing bytearray with unicode\n\
66 (-bb: issue errors)\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000067-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000068-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000069-d : debug output from parser; also PYTHONDEBUG=x\n\
Georg Brandlaed6c662008-01-07 17:25:53 +000070-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000071-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000072-i : inspect interactively after running script; forces a prompt even\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000073";
74static char *usage_2 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000075 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000076-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000077-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000078-OO : remove doc-strings in addition to the -O optimizations\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -050079-R : use a pseudo-random salt to make hash() values of various types be\n\
80 unpredictable between separate invocations of the interpreter, as\n\
81 a defense against denial-of-service attacks\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000082-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Christian Heimesaf748c32008-05-06 22:41:46 +000083-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000084-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000085-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000086";
87static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000088-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000089 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000090-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
91 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000092-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000093-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenveyaebbaeb2010-04-06 23:24:45 +000094 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000095-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000096";
97static char *usage_4 = "\
Benjamin Petersonf902a942009-01-09 03:07:27 +000098-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000099file : program read from script file\n\
100- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +0000101arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000102Other environment variables:\n\
103PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000104PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000105 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +0000106";
107static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000108PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
109 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000110PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000111PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000112";
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500113static char *usage_6 = "\
Georg Brandl3aec5682012-02-21 22:36:27 +0100114PYTHONHASHSEED: if this variable is set to 'random', the effect is the same\n\
115 as specifying the -R option: a random value is used to seed the hashes of\n\
116 str, bytes and datetime objects. It can also be set to an integer\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500117 in the range [0,4294967295] to get hash values with a predictable seed.\n\
118";
Guido van Rossum667d7041995-08-04 04:20:48 +0000119
120
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000121static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000122usage(int exitcode, char* program)
123{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000125
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000126 fprintf(f, usage_line, program);
127 if (exitcode)
128 fprintf(f, "Try `python -h' for more information.\n");
129 else {
130 fputs(usage_1, f);
131 fputs(usage_2, f);
132 fputs(usage_3, f);
133 fprintf(f, usage_4, DELIM);
134 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500135 fputs(usage_6, f);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000137#if defined(__VMS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 if (exitcode == 0) {
139 /* suppress 'error' message */
140 return 1;
141 }
142 else {
143 /* STS$M_INHIB_MSG + SS$_ABORT */
144 return 0x1000002c;
145 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000146#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000148#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000150}
151
Steven Moreland71ec9402019-09-24 18:43:59 -0700152// TODO(b/141583221): stop leaks
153const char *__asan_default_options() {
154 return "detect_leaks=0";
155}
156
Martin v. Löwis6caea372003-11-18 19:46:25 +0000157static void RunStartupFile(PyCompilerFlags *cf)
158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 char *startup = Py_GETENV("PYTHONSTARTUP");
160 if (startup != NULL && startup[0] != '\0') {
161 FILE *fp = fopen(startup, "r");
162 if (fp != NULL) {
163 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
164 PyErr_Clear();
165 fclose(fp);
Martin Panterca56dd42016-09-17 07:54:55 +0000166 } else {
167 int save_errno;
168 save_errno = errno;
169 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
170 errno = save_errno;
171 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
172 startup);
173 PyErr_Print();
174 PyErr_Clear();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 }
176 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000177}
178
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000179
Nick Coghlan327a39b2007-11-18 11:56:28 +0000180static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000181{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 PyObject *runpy, *runmodule, *runargs, *result;
183 runpy = PyImport_ImportModule("runpy");
184 if (runpy == NULL) {
185 fprintf(stderr, "Could not import runpy module\n");
186 return -1;
187 }
188 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
189 if (runmodule == NULL) {
190 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
191 Py_DECREF(runpy);
192 return -1;
193 }
194 runargs = Py_BuildValue("(si)", module, set_argv0);
195 if (runargs == NULL) {
196 fprintf(stderr,
197 "Could not create arguments for runpy._run_module_as_main\n");
198 Py_DECREF(runpy);
199 Py_DECREF(runmodule);
200 return -1;
201 }
202 result = PyObject_Call(runmodule, runargs, NULL);
203 if (result == NULL) {
204 PyErr_Print();
205 }
206 Py_DECREF(runpy);
207 Py_DECREF(runmodule);
208 Py_DECREF(runargs);
209 if (result == NULL) {
210 return -1;
211 }
212 Py_DECREF(result);
213 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000214}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000215
Nick Coghlan327a39b2007-11-18 11:56:28 +0000216static int RunMainFromImporter(char *filename)
217{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 PyObject *argv0 = NULL, *importer = NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 if ((argv0 = PyString_FromString(filename)) &&
221 (importer = PyImport_GetImporter(argv0)) &&
222 (importer->ob_type != &PyNullImporter_Type))
223 {
224 /* argv0 is usable as an import source, so
225 put it in sys.path[0] and import __main__ */
226 PyObject *sys_path = NULL;
227 if ((sys_path = PySys_GetObject("path")) &&
228 !PyList_SetItem(sys_path, 0, argv0))
229 {
230 Py_INCREF(argv0);
231 Py_DECREF(importer);
232 sys_path = NULL;
233 return RunModule("__main__", 0) != 0;
234 }
235 }
236 Py_XDECREF(argv0);
237 Py_XDECREF(importer);
238 if (PyErr_Occurred()) {
239 PyErr_Print();
240 return 1;
241 }
242 return -1;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000243}
244
245
Guido van Rossum667d7041995-08-04 04:20:48 +0000246/* Main program */
247
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000248int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000249Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000250{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 int c;
252 int sts;
253 char *command = NULL;
254 char *filename = NULL;
255 char *module = NULL;
256 FILE *fp = stdin;
257 char *p;
258 int unbuffered = 0;
259 int skipfirstline = 0;
260 int stdin_is_interactive = 0;
261 int help = 0;
262 int version = 0;
263 int saw_unbuffered_flag = 0;
264 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000265
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000267
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 orig_argc = argc; /* For Py_GetArgcArgv() */
269 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000270
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000271#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 Py_RISCOSWimpFlag = 0;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000273#endif
274
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100275 /* Hash randomization needed early for all string operations
276 (including -W and -X options). */
Ezio Melottiec6486d2012-11-23 18:46:11 +0200277 _PyOS_opterr = 0; /* prevent printing the error in 1st pass */
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100278 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
279 if (c == 'm' || c == 'c') {
280 /* -c / -m is the last option: following arguments are
281 not interpreter options. */
282 break;
283 }
284 switch (c) {
285 case 'E':
286 Py_IgnoreEnvironmentFlag++;
287 break;
288 case 'R':
289 Py_HashRandomizationFlag++;
290 break;
291 }
292 }
293 /* The variable is only tested for existence here; _PyRandom_Init will
294 check its value further. */
295 if (!Py_HashRandomizationFlag &&
296 (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
297 Py_HashRandomizationFlag = 1;
298
299 _PyRandom_Init();
300
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PySys_ResetWarnOptions();
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100302 _PyOS_ResetGetOpt();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
305 if (c == 'c') {
306 /* -c is the last option; following arguments
307 that look like options are left for the
308 command to interpret. */
309 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
310 if (command == NULL)
311 Py_FatalError(
312 "not enough memory to copy -c argument");
313 strcpy(command, _PyOS_optarg);
314 strcat(command, "\n");
315 break;
316 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000317
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000318 if (c == 'm') {
319 /* -m is the last option; following arguments
320 that look like options are left for the
321 module to interpret. */
322 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
323 if (module == NULL)
324 Py_FatalError(
325 "not enough memory to copy -m argument");
326 strcpy(module, _PyOS_optarg);
327 break;
328 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000329
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000330 switch (c) {
331 case 'b':
332 Py_BytesWarningFlag++;
333 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 case 'd':
336 Py_DebugFlag++;
337 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000338
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 case '3':
340 Py_Py3kWarningFlag++;
341 if (!Py_DivisionWarningFlag)
342 Py_DivisionWarningFlag = 1;
343 break;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000344
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 case 'Q':
346 if (strcmp(_PyOS_optarg, "old") == 0) {
347 Py_DivisionWarningFlag = 0;
348 break;
349 }
350 if (strcmp(_PyOS_optarg, "warn") == 0) {
351 Py_DivisionWarningFlag = 1;
352 break;
353 }
354 if (strcmp(_PyOS_optarg, "warnall") == 0) {
355 Py_DivisionWarningFlag = 2;
356 break;
357 }
358 if (strcmp(_PyOS_optarg, "new") == 0) {
359 /* This only affects __main__ */
360 cf.cf_flags |= CO_FUTURE_DIVISION;
361 /* And this tells the eval loop to treat
362 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
363 _Py_QnewFlag = 1;
364 break;
365 }
366 fprintf(stderr,
367 "-Q option should be `-Qold', "
368 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
369 return usage(2, argv[0]);
370 /* NOTREACHED */
Guido van Rossum393661d2001-08-31 17:40:15 +0000371
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372 case 'i':
373 Py_InspectFlag++;
374 Py_InteractiveFlag++;
375 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000376
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 /* case 'J': reserved for Jython */
Christian Heimes7a98d272008-04-12 13:03:03 +0000378
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000379 case 'O':
380 Py_OptimizeFlag++;
381 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000382
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000383 case 'B':
384 Py_DontWriteBytecodeFlag++;
385 break;
Georg Brandl2da0fce2008-01-07 17:09:35 +0000386
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 case 's':
388 Py_NoUserSiteDirectory++;
389 break;
Christian Heimesaf748c32008-05-06 22:41:46 +0000390
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391 case 'S':
392 Py_NoSiteFlag++;
393 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000394
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000395 case 'E':
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100396 /* Already handled above */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000397 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000398
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 case 't':
400 Py_TabcheckFlag++;
401 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000402
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000403 case 'u':
404 unbuffered++;
405 saw_unbuffered_flag = 1;
406 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000407
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 case 'v':
409 Py_VerboseFlag++;
410 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000411
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000412#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 case 'w':
414 Py_RISCOSWimpFlag = 1;
415 break;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000416#endif
417
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 case 'x':
419 skipfirstline = 1;
420 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000421
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000422 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000423
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000424 case 'U':
425 Py_UnicodeFlag++;
426 break;
427 case 'h':
428 case '?':
429 help++;
430 break;
431 case 'V':
432 version++;
433 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000434
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000435 case 'W':
436 PySys_AddWarnOption(_PyOS_optarg);
437 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000438
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500439 case 'R':
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100440 /* Already handled above */
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500441 break;
442
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000443 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000444
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 default:
446 return usage(2, argv[0]);
447 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000448
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 }
450 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 if (help)
453 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000454
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 if (version) {
456 fprintf(stderr, "Python %s\n", PY_VERSION);
457 return 0;
458 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000459
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000460 if (Py_Py3kWarningFlag && !Py_TabcheckFlag)
461 /* -3 implies -t (but not -tt) */
462 Py_TabcheckFlag = 1;
Georg Brandl3c4a5462009-04-12 12:08:12 +0000463
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 if (!Py_InspectFlag &&
465 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
466 Py_InspectFlag = 1;
467 if (!saw_unbuffered_flag &&
468 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
469 unbuffered = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000470
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 if (!Py_NoUserSiteDirectory &&
472 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
473 Py_NoUserSiteDirectory = 1;
Christian Heimesaf748c32008-05-06 22:41:46 +0000474
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
476 char *buf, *warning;
Philip Jenveycdd98fb2010-04-10 20:27:15 +0000477
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000478 buf = (char *)malloc(strlen(p) + 1);
479 if (buf == NULL)
480 Py_FatalError(
481 "not enough memory to copy PYTHONWARNINGS");
482 strcpy(buf, p);
483 for (warning = strtok(buf, ",");
484 warning != NULL;
485 warning = strtok(NULL, ","))
486 PySys_AddWarnOption(warning);
487 free(buf);
488 }
Philip Jenveyaebbaeb2010-04-06 23:24:45 +0000489
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 if (command == NULL && module == NULL && _PyOS_optind < argc &&
491 strcmp(argv[_PyOS_optind], "-") != 0)
492 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000493#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 filename = decc$translate_vms(argv[_PyOS_optind]);
495 if (filename == (char *)0 || filename == (char *)-1)
496 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000497
498#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000499 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000500#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 }
Guido van Rossum775af911997-02-14 19:50:32 +0000502
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000504
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000505 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000506#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 _setmode(fileno(stdin), O_BINARY);
508 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000509#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000510#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
512 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
513 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000514#else /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 setbuf(stdin, (char *)NULL);
516 setbuf(stdout, (char *)NULL);
517 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000518#endif /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 }
520 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000521#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000522 /* Doesn't have to have line-buffered -- use unbuffered */
523 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
524 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000525#else /* !MS_WINDOWS */
526#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
528 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000529#endif /* HAVE_SETVBUF */
530#endif /* !MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 /* Leave stderr alone - it should be unbuffered anyway. */
532 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000533#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000534 else {
535 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
536 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000537#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000538
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000539#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000540 /* On MacOS X, when the Python interpreter is embedded in an
541 application bundle, it gets executed by a bootstrapping script
542 that does os.execve() with an argv[0] that's different from the
543 actual Python executable. This is needed to keep the Finder happy,
544 or rather, to work around Apple's overly strict requirements of
545 the process name. However, we still need a usable sys.executable,
546 so the actual executable path is passed in an environment variable.
547 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
548 script. */
549 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
550 Py_SetProgramName(p);
551 else
552 Py_SetProgramName(argv[0]);
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000553#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000555#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000557
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 if (Py_VerboseFlag ||
559 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
560 fprintf(stderr, "Python %s on %s\n",
561 Py_GetVersion(), Py_GetPlatform());
562 if (!Py_NoSiteFlag)
563 fprintf(stderr, "%s\n", COPYRIGHT);
564 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 if (command != NULL) {
567 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
568 _PyOS_optind--;
569 argv[_PyOS_optind] = "-c";
570 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000571
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 if (module != NULL) {
Nick Coghlan8842c352010-06-13 06:50:39 +0000573 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
574 so that PySys_SetArgv correctly sets sys.path[0] to ''
575 rather than looking for a file called "-m". See
576 tracker issue #8202 for details. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 _PyOS_optind--;
Nick Coghlan8842c352010-06-13 06:50:39 +0000578 argv[_PyOS_optind] = "-c";
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000580
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000582
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000583 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
584 isatty(fileno(stdin))) {
585 PyObject *v;
586 v = PyImport_ImportModule("readline");
587 if (v == NULL)
588 PyErr_Clear();
589 else
590 Py_DECREF(v);
591 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000592
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000593 if (command) {
594 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
595 free(command);
596 } else if (module) {
Senthil Kumaran3b30b192012-07-04 19:50:29 -0700597 sts = (RunModule(module, 1) != 0);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 free(module);
599 }
600 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000601
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 if (filename == NULL && stdin_is_interactive) {
603 Py_InspectFlag = 0; /* do exit on SystemExit */
604 RunStartupFile(&cf);
605 }
606 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000607
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 sts = -1; /* keep track of whether we've already run __main__ */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000609
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000610 if (filename != NULL) {
611 sts = RunMainFromImporter(filename);
612 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000613
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000614 if (sts==-1 && filename!=NULL) {
615 if ((fp = fopen(filename, "r")) == NULL) {
616 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
617 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000618
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619 return 2;
620 }
621 else if (skipfirstline) {
622 int ch;
623 /* Push back first newline so line numbers
624 remain the same */
625 while ((ch = getc(fp)) != EOF) {
626 if (ch == '\n') {
627 (void)ungetc(ch, fp);
628 break;
629 }
630 }
631 }
632 {
633 /* XXX: does this work on Win/Win64? (see posix_fstat) */
634 struct stat sb;
635 if (fstat(fileno(fp), &sb) == 0 &&
636 S_ISDIR(sb.st_mode)) {
637 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
638 fclose(fp);
639 return 1;
640 }
641 }
642 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000643
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 if (sts==-1) {
645 /* call pending calls like signal handlers (SIGINT) */
646 if (Py_MakePendingCalls() == -1) {
647 PyErr_Print();
648 sts = 1;
649 } else {
650 sts = PyRun_AnyFileExFlags(
651 fp,
652 filename == NULL ? "<stdin>" : filename,
653 filename != NULL, &cf) != 0;
654 }
655 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000656
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000658
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 /* Check this environment variable at the end, to give programs the
660 * opportunity to set it from Python.
661 */
662 if (!Py_InspectFlag &&
663 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
664 {
665 Py_InspectFlag = 1;
666 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000667
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000668 if (Py_InspectFlag && stdin_is_interactive &&
669 (filename != NULL || command != NULL || module != NULL)) {
670 Py_InspectFlag = 0;
671 /* XXX */
672 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
673 }
674
675 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000676#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000677 if (Py_RISCOSWimpFlag)
678 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000679#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000680
681#ifdef __INSURE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000682 /* Insure++ is a memory analysis tool that aids in discovering
683 * memory leaks and other memory problems. On Python exit, the
684 * interned string dictionary is flagged as being in use at exit
685 * (which it is). Under normal circumstances, this is fine because
686 * the memory will be automatically reclaimed by the system. Under
687 * memory debugging, it's a huge source of useless noise, so we
688 * trade off slower shutdown for less distraction in the memory
689 * reports. -baw
690 */
691 _Py_ReleaseInternedStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000692#endif /* __INSURE__ */
693
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000695}
696
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000697/* this is gonna seem *real weird*, but if you put some other code between
698 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
699 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000700
Guido van Rossum667d7041995-08-04 04:20:48 +0000701/* Make the *original* argc/argv available to other modules.
702 This is rare, but it is needed by the secureware extension. */
703
704void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000705Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000706{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 *argc = orig_argc;
708 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000709}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000710
711#ifdef __cplusplus
712}
713#endif
714