blob: 6d1ae9ed68761d807ca6fd7424ddfc71342d3ef1 [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\
Philip Jenveyaebbaeb2010-04-06 23:24:45 +000089 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000090-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000091";
92static char *usage_4 = "\
Benjamin Petersonf902a942009-01-09 03:07:27 +000093-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000094file : program read from script file\n\
95- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000096arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000097Other environment variables:\n\
98PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000099PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000100 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +0000101";
102static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000103PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
104 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000105PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000106PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000107";
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500108static char *usage_6 = "\
109PYTHONHASHSEED: if this variable is set to ``random``, the effect is the same \n\
110 as specifying the :option:`-R` option: a random value is used to seed the\n\
111 hashes of str, bytes and datetime objects. It can also be set to an integer\n\
112 in the range [0,4294967295] to get hash values with a predictable seed.\n\
113";
Guido van Rossum667d7041995-08-04 04:20:48 +0000114
115
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000116static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000117usage(int exitcode, char* program)
118{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000120
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 fprintf(f, usage_line, program);
122 if (exitcode)
123 fprintf(f, "Try `python -h' for more information.\n");
124 else {
125 fputs(usage_1, f);
126 fputs(usage_2, f);
127 fputs(usage_3, f);
128 fprintf(f, usage_4, DELIM);
129 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500130 fputs(usage_6, f);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#if defined(__VMS)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 if (exitcode == 0) {
134 /* suppress 'error' message */
135 return 1;
136 }
137 else {
138 /* STS$M_INHIB_MSG + SS$_ABORT */
139 return 0x1000002c;
140 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000141#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000142 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000143#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000144 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000145}
146
Martin v. Löwis6caea372003-11-18 19:46:25 +0000147static void RunStartupFile(PyCompilerFlags *cf)
148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 char *startup = Py_GETENV("PYTHONSTARTUP");
150 if (startup != NULL && startup[0] != '\0') {
151 FILE *fp = fopen(startup, "r");
152 if (fp != NULL) {
153 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
154 PyErr_Clear();
155 fclose(fp);
156 } else {
157 int save_errno;
158 save_errno = errno;
159 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
160 errno = save_errno;
161 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
162 startup);
163 PyErr_Print();
164 PyErr_Clear();
165 }
166 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000167}
168
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000169
Nick Coghlan327a39b2007-11-18 11:56:28 +0000170static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 PyObject *runpy, *runmodule, *runargs, *result;
173 runpy = PyImport_ImportModule("runpy");
174 if (runpy == NULL) {
175 fprintf(stderr, "Could not import runpy module\n");
176 return -1;
177 }
178 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
179 if (runmodule == NULL) {
180 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
181 Py_DECREF(runpy);
182 return -1;
183 }
184 runargs = Py_BuildValue("(si)", module, set_argv0);
185 if (runargs == NULL) {
186 fprintf(stderr,
187 "Could not create arguments for runpy._run_module_as_main\n");
188 Py_DECREF(runpy);
189 Py_DECREF(runmodule);
190 return -1;
191 }
192 result = PyObject_Call(runmodule, runargs, NULL);
193 if (result == NULL) {
194 PyErr_Print();
195 }
196 Py_DECREF(runpy);
197 Py_DECREF(runmodule);
198 Py_DECREF(runargs);
199 if (result == NULL) {
200 return -1;
201 }
202 Py_DECREF(result);
203 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000204}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000205
Nick Coghlan327a39b2007-11-18 11:56:28 +0000206static int RunMainFromImporter(char *filename)
207{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000208 PyObject *argv0 = NULL, *importer = NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000209
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000210 if ((argv0 = PyString_FromString(filename)) &&
211 (importer = PyImport_GetImporter(argv0)) &&
212 (importer->ob_type != &PyNullImporter_Type))
213 {
214 /* argv0 is usable as an import source, so
215 put it in sys.path[0] and import __main__ */
216 PyObject *sys_path = NULL;
217 if ((sys_path = PySys_GetObject("path")) &&
218 !PyList_SetItem(sys_path, 0, argv0))
219 {
220 Py_INCREF(argv0);
221 Py_DECREF(importer);
222 sys_path = NULL;
223 return RunModule("__main__", 0) != 0;
224 }
225 }
226 Py_XDECREF(argv0);
227 Py_XDECREF(importer);
228 if (PyErr_Occurred()) {
229 PyErr_Print();
230 return 1;
231 }
232 return -1;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000233}
234
235
Guido van Rossum667d7041995-08-04 04:20:48 +0000236/* Main program */
237
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000238int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000239Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000240{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 int c;
242 int sts;
243 char *command = NULL;
244 char *filename = NULL;
245 char *module = NULL;
246 FILE *fp = stdin;
247 char *p;
248 int unbuffered = 0;
249 int skipfirstline = 0;
250 int stdin_is_interactive = 0;
251 int help = 0;
252 int version = 0;
253 int saw_unbuffered_flag = 0;
254 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000255
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000256 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000257
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 orig_argc = argc; /* For Py_GetArgcArgv() */
259 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000260
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000261#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000262 Py_RISCOSWimpFlag = 0;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000263#endif
264
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 PySys_ResetWarnOptions();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000266
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
268 if (c == 'c') {
269 /* -c is the last option; following arguments
270 that look like options are left for the
271 command to interpret. */
272 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
273 if (command == NULL)
274 Py_FatalError(
275 "not enough memory to copy -c argument");
276 strcpy(command, _PyOS_optarg);
277 strcat(command, "\n");
278 break;
279 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281 if (c == 'm') {
282 /* -m is the last option; following arguments
283 that look like options are left for the
284 module to interpret. */
285 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
286 if (module == NULL)
287 Py_FatalError(
288 "not enough memory to copy -m argument");
289 strcpy(module, _PyOS_optarg);
290 break;
291 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000292
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000293 switch (c) {
294 case 'b':
295 Py_BytesWarningFlag++;
296 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000297
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 case 'd':
299 Py_DebugFlag++;
300 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000301
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 case '3':
303 Py_Py3kWarningFlag++;
304 if (!Py_DivisionWarningFlag)
305 Py_DivisionWarningFlag = 1;
306 break;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000307
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000308 case 'Q':
309 if (strcmp(_PyOS_optarg, "old") == 0) {
310 Py_DivisionWarningFlag = 0;
311 break;
312 }
313 if (strcmp(_PyOS_optarg, "warn") == 0) {
314 Py_DivisionWarningFlag = 1;
315 break;
316 }
317 if (strcmp(_PyOS_optarg, "warnall") == 0) {
318 Py_DivisionWarningFlag = 2;
319 break;
320 }
321 if (strcmp(_PyOS_optarg, "new") == 0) {
322 /* This only affects __main__ */
323 cf.cf_flags |= CO_FUTURE_DIVISION;
324 /* And this tells the eval loop to treat
325 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
326 _Py_QnewFlag = 1;
327 break;
328 }
329 fprintf(stderr,
330 "-Q option should be `-Qold', "
331 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
332 return usage(2, argv[0]);
333 /* NOTREACHED */
Guido van Rossum393661d2001-08-31 17:40:15 +0000334
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 case 'i':
336 Py_InspectFlag++;
337 Py_InteractiveFlag++;
338 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000339
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000340 /* case 'J': reserved for Jython */
Christian Heimes7a98d272008-04-12 13:03:03 +0000341
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 case 'O':
343 Py_OptimizeFlag++;
344 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000345
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 case 'B':
347 Py_DontWriteBytecodeFlag++;
348 break;
Georg Brandl2da0fce2008-01-07 17:09:35 +0000349
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 case 's':
351 Py_NoUserSiteDirectory++;
352 break;
Christian Heimesaf748c32008-05-06 22:41:46 +0000353
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 case 'S':
355 Py_NoSiteFlag++;
356 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000357
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 case 'E':
359 Py_IgnoreEnvironmentFlag++;
360 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000361
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 case 't':
363 Py_TabcheckFlag++;
364 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000365
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000366 case 'u':
367 unbuffered++;
368 saw_unbuffered_flag = 1;
369 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000370
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371 case 'v':
372 Py_VerboseFlag++;
373 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000374
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000375#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 case 'w':
377 Py_RISCOSWimpFlag = 1;
378 break;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000379#endif
380
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000381 case 'x':
382 skipfirstline = 1;
383 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000384
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000386
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000387 case 'U':
388 Py_UnicodeFlag++;
389 break;
390 case 'h':
391 case '?':
392 help++;
393 break;
394 case 'V':
395 version++;
396 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000397
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 case 'W':
399 PySys_AddWarnOption(_PyOS_optarg);
400 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000401
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500402 case 'R':
403 Py_HashRandomizationFlag++;
404 break;
405
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000406 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000407
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000408 default:
409 return usage(2, argv[0]);
410 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000411
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 }
413 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000414
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 if (help)
416 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000417
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 if (version) {
419 fprintf(stderr, "Python %s\n", PY_VERSION);
420 return 0;
421 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000422
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000423 if (Py_Py3kWarningFlag && !Py_TabcheckFlag)
424 /* -3 implies -t (but not -tt) */
425 Py_TabcheckFlag = 1;
Georg Brandl3c4a5462009-04-12 12:08:12 +0000426
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 if (!Py_InspectFlag &&
428 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
429 Py_InspectFlag = 1;
430 if (!saw_unbuffered_flag &&
431 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
432 unbuffered = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 if (!Py_NoUserSiteDirectory &&
435 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
436 Py_NoUserSiteDirectory = 1;
Christian Heimesaf748c32008-05-06 22:41:46 +0000437
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000438 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
439 char *buf, *warning;
Philip Jenveycdd98fb2010-04-10 20:27:15 +0000440
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000441 buf = (char *)malloc(strlen(p) + 1);
442 if (buf == NULL)
443 Py_FatalError(
444 "not enough memory to copy PYTHONWARNINGS");
445 strcpy(buf, p);
446 for (warning = strtok(buf, ",");
447 warning != NULL;
448 warning = strtok(NULL, ","))
449 PySys_AddWarnOption(warning);
450 free(buf);
451 }
Philip Jenveyaebbaeb2010-04-06 23:24:45 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 if (command == NULL && module == NULL && _PyOS_optind < argc &&
454 strcmp(argv[_PyOS_optind], "-") != 0)
455 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000456#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 filename = decc$translate_vms(argv[_PyOS_optind]);
458 if (filename == (char *)0 || filename == (char *)-1)
459 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000460
461#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000463#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 }
Guido van Rossum775af911997-02-14 19:50:32 +0000465
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000466 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000469#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 _setmode(fileno(stdin), O_BINARY);
471 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000472#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000473#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000474 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
475 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
476 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000477#else /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000478 setbuf(stdin, (char *)NULL);
479 setbuf(stdout, (char *)NULL);
480 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000481#endif /* !HAVE_SETVBUF */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 }
483 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000484#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 /* Doesn't have to have line-buffered -- use unbuffered */
486 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
487 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000488#else /* !MS_WINDOWS */
489#ifdef HAVE_SETVBUF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
491 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000492#endif /* HAVE_SETVBUF */
493#endif /* !MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 /* Leave stderr alone - it should be unbuffered anyway. */
495 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000496#ifdef __VMS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 else {
498 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
499 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000500#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000501
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000502#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 /* On MacOS X, when the Python interpreter is embedded in an
504 application bundle, it gets executed by a bootstrapping script
505 that does os.execve() with an argv[0] that's different from the
506 actual Python executable. This is needed to keep the Finder happy,
507 or rather, to work around Apple's overly strict requirements of
508 the process name. However, we still need a usable sys.executable,
509 so the actual executable path is passed in an environment variable.
510 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
511 script. */
512 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
513 Py_SetProgramName(p);
514 else
515 Py_SetProgramName(argv[0]);
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000516#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000517 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000518#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000520
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 if (Py_VerboseFlag ||
522 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
523 fprintf(stderr, "Python %s on %s\n",
524 Py_GetVersion(), Py_GetPlatform());
525 if (!Py_NoSiteFlag)
526 fprintf(stderr, "%s\n", COPYRIGHT);
527 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000528
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 if (command != NULL) {
530 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
531 _PyOS_optind--;
532 argv[_PyOS_optind] = "-c";
533 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000534
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 if (module != NULL) {
Nick Coghlan8842c352010-06-13 06:50:39 +0000536 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
537 so that PySys_SetArgv correctly sets sys.path[0] to ''
538 rather than looking for a file called "-m". See
539 tracker issue #8202 for details. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000540 _PyOS_optind--;
Nick Coghlan8842c352010-06-13 06:50:39 +0000541 argv[_PyOS_optind] = "-c";
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000542 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000543
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000544 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000545
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
547 isatty(fileno(stdin))) {
548 PyObject *v;
549 v = PyImport_ImportModule("readline");
550 if (v == NULL)
551 PyErr_Clear();
552 else
553 Py_DECREF(v);
554 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000555
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 if (command) {
557 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
558 free(command);
559 } else if (module) {
560 sts = RunModule(module, 1);
561 free(module);
562 }
563 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000564
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 if (filename == NULL && stdin_is_interactive) {
566 Py_InspectFlag = 0; /* do exit on SystemExit */
567 RunStartupFile(&cf);
568 }
569 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000570
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 sts = -1; /* keep track of whether we've already run __main__ */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000572
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000573 if (filename != NULL) {
574 sts = RunMainFromImporter(filename);
575 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000576
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 if (sts==-1 && filename!=NULL) {
578 if ((fp = fopen(filename, "r")) == NULL) {
579 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
580 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000581
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000582 return 2;
583 }
584 else if (skipfirstline) {
585 int ch;
586 /* Push back first newline so line numbers
587 remain the same */
588 while ((ch = getc(fp)) != EOF) {
589 if (ch == '\n') {
590 (void)ungetc(ch, fp);
591 break;
592 }
593 }
594 }
595 {
596 /* XXX: does this work on Win/Win64? (see posix_fstat) */
597 struct stat sb;
598 if (fstat(fileno(fp), &sb) == 0 &&
599 S_ISDIR(sb.st_mode)) {
600 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
601 fclose(fp);
602 return 1;
603 }
604 }
605 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000606
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 if (sts==-1) {
608 /* call pending calls like signal handlers (SIGINT) */
609 if (Py_MakePendingCalls() == -1) {
610 PyErr_Print();
611 sts = 1;
612 } else {
613 sts = PyRun_AnyFileExFlags(
614 fp,
615 filename == NULL ? "<stdin>" : filename,
616 filename != NULL, &cf) != 0;
617 }
618 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000619
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000621
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 /* Check this environment variable at the end, to give programs the
623 * opportunity to set it from Python.
624 */
625 if (!Py_InspectFlag &&
626 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
627 {
628 Py_InspectFlag = 1;
629 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000630
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 if (Py_InspectFlag && stdin_is_interactive &&
632 (filename != NULL || command != NULL || module != NULL)) {
633 Py_InspectFlag = 0;
634 /* XXX */
635 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
636 }
637
638 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000639#ifdef RISCOS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000640 if (Py_RISCOSWimpFlag)
641 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000642#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000643
644#ifdef __INSURE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000645 /* Insure++ is a memory analysis tool that aids in discovering
646 * memory leaks and other memory problems. On Python exit, the
647 * interned string dictionary is flagged as being in use at exit
648 * (which it is). Under normal circumstances, this is fine because
649 * the memory will be automatically reclaimed by the system. Under
650 * memory debugging, it's a huge source of useless noise, so we
651 * trade off slower shutdown for less distraction in the memory
652 * reports. -baw
653 */
654 _Py_ReleaseInternedStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000655#endif /* __INSURE__ */
656
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000658}
659
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000660/* this is gonna seem *real weird*, but if you put some other code between
661 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
662 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000663
Guido van Rossum667d7041995-08-04 04:20:48 +0000664/* Make the *original* argc/argv available to other modules.
665 This is rare, but it is needed by the secureware extension. */
666
667void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000668Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000669{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000670 *argc = orig_argc;
671 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000672}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000673
674#ifdef __cplusplus
675}
676#endif
677