blob: 73e87e08fd4a5378ac5689c94850ed5a473bf377 [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\
Georg Brandl2da0fce2008-01-07 17:09:35 +000065-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000066-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000067-d : debug output from parser; also PYTHONDEBUG=x\n\
Georg Brandlaed6c662008-01-07 17:25:53 +000068-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000069-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000070-i : inspect interactively after running script; forces a prompt even\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000071";
72static char *usage_2 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000073 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000074-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000075-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000076-OO : remove doc-strings in addition to the -O optimizations\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -050077-R : use a pseudo-random salt to make hash() values of various types be\n\
78 unpredictable between separate invocations of the interpreter, as\n\
79 a defense against denial-of-service attacks\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000080-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Christian Heimesaf748c32008-05-06 22:41:46 +000081-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000082-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000083-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000084";
85static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000086-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000087 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000088-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
89 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000090-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000091-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenveyaebbaeb2010-04-06 23:24:45 +000092 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000093-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000094";
95static char *usage_4 = "\
Benjamin Petersonf902a942009-01-09 03:07:27 +000096-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000097file : program read from script file\n\
98- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000099arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000100Other environment variables:\n\
101PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000102PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000103 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +0000104";
105static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000106PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
107 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000108PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000109PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000110";
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500111static char *usage_6 = "\
Georg Brandl3aec5682012-02-21 22:36:27 +0100112PYTHONHASHSEED: if this variable is set to 'random', the effect is the same\n\
113 as specifying the -R option: a random value is used to seed the hashes of\n\
114 str, bytes and datetime objects. It can also be set to an integer\n\
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500115 in the range [0,4294967295] to get hash values with a predictable seed.\n\
116";
Guido van Rossum667d7041995-08-04 04:20:48 +0000117
118
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000119static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000120usage(int exitcode, char* program)
121{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000123
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 fprintf(f, usage_line, program);
125 if (exitcode)
126 fprintf(f, "Try `python -h' for more information.\n");
127 else {
128 fputs(usage_1, f);
129 fputs(usage_2, f);
130 fputs(usage_3, f);
131 fprintf(f, usage_4, DELIM);
132 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500133 fputs(usage_6, f);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000134 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000135#if defined(__VMS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 if (exitcode == 0) {
137 /* suppress 'error' message */
138 return 1;
139 }
140 else {
141 /* STS$M_INHIB_MSG + SS$_ABORT */
142 return 0x1000002c;
143 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000144#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000145 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000146#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000148}
149
Martin v. Löwis6caea372003-11-18 19:46:25 +0000150static void RunStartupFile(PyCompilerFlags *cf)
151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 char *startup = Py_GETENV("PYTHONSTARTUP");
153 if (startup != NULL && startup[0] != '\0') {
154 FILE *fp = fopen(startup, "r");
155 if (fp != NULL) {
156 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
157 PyErr_Clear();
158 fclose(fp);
Martin Panterca56dd42016-09-17 07:54:55 +0000159 } else {
160 int save_errno;
161 save_errno = errno;
162 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
163 errno = save_errno;
164 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
165 startup);
166 PyErr_Print();
167 PyErr_Clear();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 }
169 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000170}
171
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000172
Nick Coghlan327a39b2007-11-18 11:56:28 +0000173static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000174{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 PyObject *runpy, *runmodule, *runargs, *result;
176 runpy = PyImport_ImportModule("runpy");
177 if (runpy == NULL) {
178 fprintf(stderr, "Could not import runpy module\n");
179 return -1;
180 }
181 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
182 if (runmodule == NULL) {
183 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
184 Py_DECREF(runpy);
185 return -1;
186 }
187 runargs = Py_BuildValue("(si)", module, set_argv0);
188 if (runargs == NULL) {
189 fprintf(stderr,
190 "Could not create arguments for runpy._run_module_as_main\n");
191 Py_DECREF(runpy);
192 Py_DECREF(runmodule);
193 return -1;
194 }
195 result = PyObject_Call(runmodule, runargs, NULL);
196 if (result == NULL) {
197 PyErr_Print();
198 }
199 Py_DECREF(runpy);
200 Py_DECREF(runmodule);
201 Py_DECREF(runargs);
202 if (result == NULL) {
203 return -1;
204 }
205 Py_DECREF(result);
206 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000207}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000208
Nick Coghlan327a39b2007-11-18 11:56:28 +0000209static int RunMainFromImporter(char *filename)
210{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 PyObject *argv0 = NULL, *importer = NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000212
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 if ((argv0 = PyString_FromString(filename)) &&
214 (importer = PyImport_GetImporter(argv0)) &&
215 (importer->ob_type != &PyNullImporter_Type))
216 {
217 /* argv0 is usable as an import source, so
218 put it in sys.path[0] and import __main__ */
219 PyObject *sys_path = NULL;
220 if ((sys_path = PySys_GetObject("path")) &&
221 !PyList_SetItem(sys_path, 0, argv0))
222 {
223 Py_INCREF(argv0);
224 Py_DECREF(importer);
225 sys_path = NULL;
226 return RunModule("__main__", 0) != 0;
227 }
228 }
229 Py_XDECREF(argv0);
230 Py_XDECREF(importer);
231 if (PyErr_Occurred()) {
232 PyErr_Print();
233 return 1;
234 }
235 return -1;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000236}
237
238
Guido van Rossum667d7041995-08-04 04:20:48 +0000239/* Main program */
240
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000241int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000242Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000243{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244 int c;
245 int sts;
246 char *command = NULL;
247 char *filename = NULL;
248 char *module = NULL;
249 FILE *fp = stdin;
250 char *p;
251 int unbuffered = 0;
252 int skipfirstline = 0;
253 int stdin_is_interactive = 0;
254 int help = 0;
255 int version = 0;
256 int saw_unbuffered_flag = 0;
257 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000258
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000260
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 orig_argc = argc; /* For Py_GetArgcArgv() */
262 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000263
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000264#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 Py_RISCOSWimpFlag = 0;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000266#endif
267
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100268 /* Hash randomization needed early for all string operations
269 (including -W and -X options). */
Ezio Melottiec6486d2012-11-23 18:46:11 +0200270 _PyOS_opterr = 0; /* prevent printing the error in 1st pass */
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100271 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
272 if (c == 'm' || c == 'c') {
273 /* -c / -m is the last option: following arguments are
274 not interpreter options. */
275 break;
276 }
277 switch (c) {
278 case 'E':
279 Py_IgnoreEnvironmentFlag++;
280 break;
281 case 'R':
282 Py_HashRandomizationFlag++;
283 break;
284 }
285 }
286 /* The variable is only tested for existence here; _PyRandom_Init will
287 check its value further. */
288 if (!Py_HashRandomizationFlag &&
289 (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
290 Py_HashRandomizationFlag = 1;
291
292 _PyRandom_Init();
293
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 PySys_ResetWarnOptions();
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100295 _PyOS_ResetGetOpt();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000296
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
298 if (c == 'c') {
299 /* -c is the last option; following arguments
300 that look like options are left for the
301 command to interpret. */
302 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
303 if (command == NULL)
304 Py_FatalError(
305 "not enough memory to copy -c argument");
306 strcpy(command, _PyOS_optarg);
307 strcat(command, "\n");
308 break;
309 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000310
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311 if (c == 'm') {
312 /* -m is the last option; following arguments
313 that look like options are left for the
314 module to interpret. */
315 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
316 if (module == NULL)
317 Py_FatalError(
318 "not enough memory to copy -m argument");
319 strcpy(module, _PyOS_optarg);
320 break;
321 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000322
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 switch (c) {
324 case 'b':
325 Py_BytesWarningFlag++;
326 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000327
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 case 'd':
329 Py_DebugFlag++;
330 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000331
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 case '3':
333 Py_Py3kWarningFlag++;
334 if (!Py_DivisionWarningFlag)
335 Py_DivisionWarningFlag = 1;
336 break;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000337
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000338 case 'Q':
339 if (strcmp(_PyOS_optarg, "old") == 0) {
340 Py_DivisionWarningFlag = 0;
341 break;
342 }
343 if (strcmp(_PyOS_optarg, "warn") == 0) {
344 Py_DivisionWarningFlag = 1;
345 break;
346 }
347 if (strcmp(_PyOS_optarg, "warnall") == 0) {
348 Py_DivisionWarningFlag = 2;
349 break;
350 }
351 if (strcmp(_PyOS_optarg, "new") == 0) {
352 /* This only affects __main__ */
353 cf.cf_flags |= CO_FUTURE_DIVISION;
354 /* And this tells the eval loop to treat
355 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
356 _Py_QnewFlag = 1;
357 break;
358 }
359 fprintf(stderr,
360 "-Q option should be `-Qold', "
361 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
362 return usage(2, argv[0]);
363 /* NOTREACHED */
Guido van Rossum393661d2001-08-31 17:40:15 +0000364
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 case 'i':
366 Py_InspectFlag++;
367 Py_InteractiveFlag++;
368 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370 /* case 'J': reserved for Jython */
Christian Heimes7a98d272008-04-12 13:03:03 +0000371
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372 case 'O':
373 Py_OptimizeFlag++;
374 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000375
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 case 'B':
377 Py_DontWriteBytecodeFlag++;
378 break;
Georg Brandl2da0fce2008-01-07 17:09:35 +0000379
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 case 's':
381 Py_NoUserSiteDirectory++;
382 break;
Christian Heimesaf748c32008-05-06 22:41:46 +0000383
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000384 case 'S':
385 Py_NoSiteFlag++;
386 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000387
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 case 'E':
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100389 /* Already handled above */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000391
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000392 case 't':
393 Py_TabcheckFlag++;
394 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000395
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000396 case 'u':
397 unbuffered++;
398 saw_unbuffered_flag = 1;
399 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000400
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000401 case 'v':
402 Py_VerboseFlag++;
403 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000404
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000405#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 case 'w':
407 Py_RISCOSWimpFlag = 1;
408 break;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000409#endif
410
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 case 'x':
412 skipfirstline = 1;
413 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000414
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000416
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000417 case 'U':
418 Py_UnicodeFlag++;
419 break;
420 case 'h':
421 case '?':
422 help++;
423 break;
424 case 'V':
425 version++;
426 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000427
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 case 'W':
429 PySys_AddWarnOption(_PyOS_optarg);
430 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000431
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500432 case 'R':
Antoine Pitroucc3fa882012-02-21 20:42:48 +0100433 /* Already handled above */
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500434 break;
435
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000437
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000438 default:
439 return usage(2, argv[0]);
440 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000441
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442 }
443 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000444
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000445 if (help)
446 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000447
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000448 if (version) {
449 fprintf(stderr, "Python %s\n", PY_VERSION);
450 return 0;
451 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 if (Py_Py3kWarningFlag && !Py_TabcheckFlag)
454 /* -3 implies -t (but not -tt) */
455 Py_TabcheckFlag = 1;
Georg Brandl3c4a5462009-04-12 12:08:12 +0000456
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 if (!Py_InspectFlag &&
458 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
459 Py_InspectFlag = 1;
460 if (!saw_unbuffered_flag &&
461 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
462 unbuffered = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000463
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 if (!Py_NoUserSiteDirectory &&
465 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
466 Py_NoUserSiteDirectory = 1;
Christian Heimesaf748c32008-05-06 22:41:46 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
469 char *buf, *warning;
Philip Jenveycdd98fb2010-04-10 20:27:15 +0000470
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 buf = (char *)malloc(strlen(p) + 1);
472 if (buf == NULL)
473 Py_FatalError(
474 "not enough memory to copy PYTHONWARNINGS");
475 strcpy(buf, p);
476 for (warning = strtok(buf, ",");
477 warning != NULL;
478 warning = strtok(NULL, ","))
479 PySys_AddWarnOption(warning);
480 free(buf);
481 }
Philip Jenveyaebbaeb2010-04-06 23:24:45 +0000482
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 if (command == NULL && module == NULL && _PyOS_optind < argc &&
484 strcmp(argv[_PyOS_optind], "-") != 0)
485 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000486#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000487 filename = decc$translate_vms(argv[_PyOS_optind]);
488 if (filename == (char *)0 || filename == (char *)-1)
489 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000490
491#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000493#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 }
Guido van Rossum775af911997-02-14 19:50:32 +0000495
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000497
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000498 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000499#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 _setmode(fileno(stdin), O_BINARY);
501 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000502#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000503#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000504 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
505 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
506 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000507#else /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 setbuf(stdin, (char *)NULL);
509 setbuf(stdout, (char *)NULL);
510 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000511#endif /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512 }
513 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000514#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 /* Doesn't have to have line-buffered -- use unbuffered */
516 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
517 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000518#else /* !MS_WINDOWS */
519#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
521 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000522#endif /* HAVE_SETVBUF */
523#endif /* !MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000524 /* Leave stderr alone - it should be unbuffered anyway. */
525 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000526#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527 else {
528 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
529 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000530#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000531
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000532#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 /* On MacOS X, when the Python interpreter is embedded in an
534 application bundle, it gets executed by a bootstrapping script
535 that does os.execve() with an argv[0] that's different from the
536 actual Python executable. This is needed to keep the Finder happy,
537 or rather, to work around Apple's overly strict requirements of
538 the process name. However, we still need a usable sys.executable,
539 so the actual executable path is passed in an environment variable.
540 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
541 script. */
542 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
543 Py_SetProgramName(p);
544 else
545 Py_SetProgramName(argv[0]);
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000546#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000547 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000548#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000550
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 if (Py_VerboseFlag ||
552 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
553 fprintf(stderr, "Python %s on %s\n",
554 Py_GetVersion(), Py_GetPlatform());
555 if (!Py_NoSiteFlag)
556 fprintf(stderr, "%s\n", COPYRIGHT);
557 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000558
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000559 if (command != NULL) {
560 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
561 _PyOS_optind--;
562 argv[_PyOS_optind] = "-c";
563 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000564
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 if (module != NULL) {
Nick Coghlan8842c352010-06-13 06:50:39 +0000566 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
567 so that PySys_SetArgv correctly sets sys.path[0] to ''
568 rather than looking for a file called "-m". See
569 tracker issue #8202 for details. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 _PyOS_optind--;
Nick Coghlan8842c352010-06-13 06:50:39 +0000571 argv[_PyOS_optind] = "-c";
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000573
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000574 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000575
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
577 isatty(fileno(stdin))) {
578 PyObject *v;
579 v = PyImport_ImportModule("readline");
580 if (v == NULL)
581 PyErr_Clear();
582 else
583 Py_DECREF(v);
584 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000585
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 if (command) {
587 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
588 free(command);
589 } else if (module) {
Senthil Kumaran3b30b192012-07-04 19:50:29 -0700590 sts = (RunModule(module, 1) != 0);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000591 free(module);
592 }
593 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000594
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 if (filename == NULL && stdin_is_interactive) {
596 Py_InspectFlag = 0; /* do exit on SystemExit */
597 RunStartupFile(&cf);
598 }
599 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000600
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 sts = -1; /* keep track of whether we've already run __main__ */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000602
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000603 if (filename != NULL) {
604 sts = RunMainFromImporter(filename);
605 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000606
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 if (sts==-1 && filename!=NULL) {
608 if ((fp = fopen(filename, "r")) == NULL) {
609 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
610 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000611
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 return 2;
613 }
614 else if (skipfirstline) {
615 int ch;
616 /* Push back first newline so line numbers
617 remain the same */
618 while ((ch = getc(fp)) != EOF) {
619 if (ch == '\n') {
620 (void)ungetc(ch, fp);
621 break;
622 }
623 }
624 }
625 {
626 /* XXX: does this work on Win/Win64? (see posix_fstat) */
627 struct stat sb;
628 if (fstat(fileno(fp), &sb) == 0 &&
629 S_ISDIR(sb.st_mode)) {
630 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
631 fclose(fp);
632 return 1;
633 }
634 }
635 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000636
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 if (sts==-1) {
638 /* call pending calls like signal handlers (SIGINT) */
639 if (Py_MakePendingCalls() == -1) {
640 PyErr_Print();
641 sts = 1;
642 } else {
643 sts = PyRun_AnyFileExFlags(
644 fp,
645 filename == NULL ? "<stdin>" : filename,
646 filename != NULL, &cf) != 0;
647 }
648 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000649
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000650 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000651
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 /* Check this environment variable at the end, to give programs the
653 * opportunity to set it from Python.
654 */
655 if (!Py_InspectFlag &&
656 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
657 {
658 Py_InspectFlag = 1;
659 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000660
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000661 if (Py_InspectFlag && stdin_is_interactive &&
662 (filename != NULL || command != NULL || module != NULL)) {
663 Py_InspectFlag = 0;
664 /* XXX */
665 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
666 }
667
668 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000669#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000670 if (Py_RISCOSWimpFlag)
671 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000672#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000673
674#ifdef __INSURE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000675 /* Insure++ is a memory analysis tool that aids in discovering
676 * memory leaks and other memory problems. On Python exit, the
677 * interned string dictionary is flagged as being in use at exit
678 * (which it is). Under normal circumstances, this is fine because
679 * the memory will be automatically reclaimed by the system. Under
680 * memory debugging, it's a huge source of useless noise, so we
681 * trade off slower shutdown for less distraction in the memory
682 * reports. -baw
683 */
684 _Py_ReleaseInternedStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000685#endif /* __INSURE__ */
686
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000688}
689
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000690/* this is gonna seem *real weird*, but if you put some other code between
691 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
692 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000693
Guido van Rossum667d7041995-08-04 04:20:48 +0000694/* Make the *original* argc/argv available to other modules.
695 This is rare, but it is needed by the secureware extension. */
696
697void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000698Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000699{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000700 *argc = orig_argc;
701 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000702}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000703
704#ifdef __cplusplus
705}
706#endif
707