blob: dd196efc32d1c57502abd3f550f26bbe5d3e9797 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Jeremy Hyltonec97a282005-10-21 14:58:06 +00005#include "code.h" /* For CO_FUTURE_DIVISION */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00006#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00007
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00008#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00009#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000010#endif
11
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000012#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Martin v. Löwisa43190b2006-05-22 09:15:18 +000013#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000014#include <fcntl.h>
15#endif
Martin v. Löwisa43190b2006-05-22 09:15:18 +000016#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000019#define PYTHONHOMEHELP "<prefix>\\lib"
20#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if defined(PYOS_OS2) && defined(PYCC_GCC)
22#define PYTHONHOMEHELP "<prefix>/Lib"
23#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000024#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000025#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000026#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000027
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000028#include "pygetopt.h"
29
Guido van Rossuma22865e2000-09-05 04:41:18 +000030#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000031 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
32 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000033
Anthony Baxterac6bd462006-04-13 02:06:09 +000034#ifdef __cplusplus
35extern "C" {
36#endif
37
Guido van Rossumac56b031996-07-21 02:33:38 +000038/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000039static char **orig_argv;
40static int orig_argc;
41
Guido van Rossumbceccf52001-04-10 22:07:43 +000042/* command line options */
Christian Heimesaf748c32008-05-06 22:41:46 +000043#define BASE_OPTS "3bBc:dEhiJm:OQ:sStuUvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000044
45#ifndef RISCOS
46#define PROGRAM_OPTS BASE_OPTS
47#else /*RISCOS*/
48/* extra option saying that we are running under a special task window
49 frontend; especially my_readline will behave different */
50#define PROGRAM_OPTS BASE_OPTS "w"
51/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000052extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000053#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000054
Guido van Rossum667d7041995-08-04 04:20:48 +000055/* Short usage message (with %s for argv0) */
56static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000057"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000058
59/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000060static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000061Options and arguments (and corresponding environment variables):\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000062-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000063-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000064-d : debug output from parser; also PYTHONDEBUG=x\n\
Georg Brandlaed6c662008-01-07 17:25:53 +000065-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000066-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000067-i : inspect interactively after running script; forces a prompt even\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000070 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000071-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000072-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000073-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000074-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Christian Heimesaf748c32008-05-06 22:41:46 +000075-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000076-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000077-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000078";
79static char *usage_3 = "\
Georg Brandl2da0fce2008-01-07 17:09:35 +000080-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000081 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000082-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
83 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000084-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000085-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000086-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000087";
88static char *usage_4 = "\
Georg Brandlc04c2892009-01-14 00:00:17 +000089-3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090file : program read from script file\n\
91- : program read from stdin (default; interactive mode if a tty)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000092arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000093Other environment variables:\n\
94PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000095PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000096 default module search path. The result is sys.path.\n\
Georg Brandl2da0fce2008-01-07 17:09:35 +000097";
98static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +000099PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
100 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000101PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis99815892008-06-01 07:20:46 +0000102PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000103";
104
105
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000106static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000107usage(int exitcode, char* program)
108{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000109 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000110
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000111 fprintf(f, usage_line, program);
112 if (exitcode)
113 fprintf(f, "Try `python -h' for more information.\n");
114 else {
115 fputs(usage_1, f);
116 fputs(usage_2, f);
117 fputs(usage_3, f);
118 fprintf(f, usage_4, DELIM);
119 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
120 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000121#if defined(__VMS)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000122 if (exitcode == 0) {
123 /* suppress 'error' message */
124 return 1;
125 }
126 else {
127 /* STS$M_INHIB_MSG + SS$_ABORT */
128 return 0x1000002c;
129 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000130#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000131 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000132#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000133 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000134}
135
Martin v. Löwis6caea372003-11-18 19:46:25 +0000136static void RunStartupFile(PyCompilerFlags *cf)
137{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000138 char *startup = Py_GETENV("PYTHONSTARTUP");
139 if (startup != NULL && startup[0] != '\0') {
140 FILE *fp = fopen(startup, "r");
141 if (fp != NULL) {
142 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
143 PyErr_Clear();
144 fclose(fp);
145 } else {
146 int save_errno;
147 save_errno = errno;
148 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
149 errno = save_errno;
150 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
151 startup);
152 PyErr_Print();
153 PyErr_Clear();
154 }
155 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000156}
157
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000158
Nick Coghlan327a39b2007-11-18 11:56:28 +0000159static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000160{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000161 PyObject *runpy, *runmodule, *runargs, *result;
162 runpy = PyImport_ImportModule("runpy");
163 if (runpy == NULL) {
164 fprintf(stderr, "Could not import runpy module\n");
165 return -1;
166 }
167 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
168 if (runmodule == NULL) {
169 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
170 Py_DECREF(runpy);
171 return -1;
172 }
173 runargs = Py_BuildValue("(si)", module, set_argv0);
174 if (runargs == NULL) {
175 fprintf(stderr,
176 "Could not create arguments for runpy._run_module_as_main\n");
177 Py_DECREF(runpy);
178 Py_DECREF(runmodule);
179 return -1;
180 }
181 result = PyObject_Call(runmodule, runargs, NULL);
182 if (result == NULL) {
183 PyErr_Print();
184 }
185 Py_DECREF(runpy);
186 Py_DECREF(runmodule);
187 Py_DECREF(runargs);
188 if (result == NULL) {
189 return -1;
190 }
191 Py_DECREF(result);
192 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000193}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000194
Nick Coghlan327a39b2007-11-18 11:56:28 +0000195static int RunMainFromImporter(char *filename)
196{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000197 PyObject *argv0 = NULL, *importer = NULL;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000198
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000199 if ((argv0 = PyString_FromString(filename)) &&
200 (importer = PyImport_GetImporter(argv0)) &&
201 (importer->ob_type != &PyNullImporter_Type))
202 {
203 /* argv0 is usable as an import source, so
204 put it in sys.path[0] and import __main__ */
205 PyObject *sys_path = NULL;
206 if ((sys_path = PySys_GetObject("path")) &&
207 !PyList_SetItem(sys_path, 0, argv0))
208 {
209 Py_INCREF(argv0);
210 Py_DECREF(importer);
211 sys_path = NULL;
212 return RunModule("__main__", 0) != 0;
213 }
214 }
215 Py_XDECREF(argv0);
216 Py_XDECREF(importer);
217 if (PyErr_Occurred()) {
218 PyErr_Print();
219 return 1;
220 }
221 return -1;
Nick Coghlan327a39b2007-11-18 11:56:28 +0000222}
223
224
Guido van Rossum667d7041995-08-04 04:20:48 +0000225/* Main program */
226
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000227int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000228Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000229{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000230 int c;
231 int sts;
232 char *command = NULL;
233 char *filename = NULL;
234 char *module = NULL;
235 FILE *fp = stdin;
236 char *p;
237 int unbuffered = 0;
238 int skipfirstline = 0;
239 int stdin_is_interactive = 0;
240 int help = 0;
241 int version = 0;
242 int saw_unbuffered_flag = 0;
243 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000244
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000245 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000246
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000247 orig_argc = argc; /* For Py_GetArgcArgv() */
248 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000249
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000250#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000251 Py_RISCOSWimpFlag = 0;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000252#endif
253
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000254 PySys_ResetWarnOptions();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000255
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000256 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
257 if (c == 'c') {
258 /* -c is the last option; following arguments
259 that look like options are left for the
260 command to interpret. */
261 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
262 if (command == NULL)
263 Py_FatalError(
264 "not enough memory to copy -c argument");
265 strcpy(command, _PyOS_optarg);
266 strcat(command, "\n");
267 break;
268 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000269
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000270 if (c == 'm') {
271 /* -m is the last option; following arguments
272 that look like options are left for the
273 module to interpret. */
274 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
275 if (module == NULL)
276 Py_FatalError(
277 "not enough memory to copy -m argument");
278 strcpy(module, _PyOS_optarg);
279 break;
280 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000281
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000282 switch (c) {
283 case 'b':
284 Py_BytesWarningFlag++;
285 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000286
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000287 case 'd':
288 Py_DebugFlag++;
289 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000290
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000291 case '3':
292 Py_Py3kWarningFlag++;
293 if (!Py_DivisionWarningFlag)
294 Py_DivisionWarningFlag = 1;
295 break;
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +0000296
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000297 case 'Q':
298 if (strcmp(_PyOS_optarg, "old") == 0) {
299 Py_DivisionWarningFlag = 0;
300 break;
301 }
302 if (strcmp(_PyOS_optarg, "warn") == 0) {
303 Py_DivisionWarningFlag = 1;
304 break;
305 }
306 if (strcmp(_PyOS_optarg, "warnall") == 0) {
307 Py_DivisionWarningFlag = 2;
308 break;
309 }
310 if (strcmp(_PyOS_optarg, "new") == 0) {
311 /* This only affects __main__ */
312 cf.cf_flags |= CO_FUTURE_DIVISION;
313 /* And this tells the eval loop to treat
314 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
315 _Py_QnewFlag = 1;
316 break;
317 }
318 fprintf(stderr,
319 "-Q option should be `-Qold', "
320 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
321 return usage(2, argv[0]);
322 /* NOTREACHED */
Guido van Rossum393661d2001-08-31 17:40:15 +0000323
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000324 case 'i':
325 Py_InspectFlag++;
326 Py_InteractiveFlag++;
327 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000328
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000329 /* case 'J': reserved for Jython */
Christian Heimes7a98d272008-04-12 13:03:03 +0000330
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000331 case 'O':
332 Py_OptimizeFlag++;
333 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000334
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000335 case 'B':
336 Py_DontWriteBytecodeFlag++;
337 break;
Georg Brandl2da0fce2008-01-07 17:09:35 +0000338
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000339 case 's':
340 Py_NoUserSiteDirectory++;
341 break;
Christian Heimesaf748c32008-05-06 22:41:46 +0000342
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000343 case 'S':
344 Py_NoSiteFlag++;
345 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000346
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000347 case 'E':
348 Py_IgnoreEnvironmentFlag++;
349 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000350
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000351 case 't':
352 Py_TabcheckFlag++;
353 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000354
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000355 case 'u':
356 unbuffered++;
357 saw_unbuffered_flag = 1;
358 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000359
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000360 case 'v':
361 Py_VerboseFlag++;
362 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000363
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000364#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000365 case 'w':
366 Py_RISCOSWimpFlag = 1;
367 break;
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000368#endif
369
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000370 case 'x':
371 skipfirstline = 1;
372 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000373
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000374 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes7a98d272008-04-12 13:03:03 +0000375
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000376 case 'U':
377 Py_UnicodeFlag++;
378 break;
379 case 'h':
380 case '?':
381 help++;
382 break;
383 case 'V':
384 version++;
385 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000386
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000387 case 'W':
388 PySys_AddWarnOption(_PyOS_optarg);
389 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000390
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000391 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000392
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000393 default:
394 return usage(2, argv[0]);
395 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000396
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000397 }
398 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000399
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000400 if (help)
401 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000402
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000403 if (version) {
404 fprintf(stderr, "Python %s\n", PY_VERSION);
405 return 0;
406 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000407
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000408 if (!Py_InspectFlag &&
409 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
410 Py_InspectFlag = 1;
411 if (!saw_unbuffered_flag &&
412 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
413 unbuffered = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000414
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000415 if (!Py_NoUserSiteDirectory &&
416 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
417 Py_NoUserSiteDirectory = 1;
Christian Heimesaf748c32008-05-06 22:41:46 +0000418
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000419 if (command == NULL && module == NULL && _PyOS_optind < argc &&
420 strcmp(argv[_PyOS_optind], "-") != 0)
421 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000422#ifdef __VMS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000423 filename = decc$translate_vms(argv[_PyOS_optind]);
424 if (filename == (char *)0 || filename == (char *)-1)
425 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000426
427#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000428 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000429#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000430 }
Guido van Rossum775af911997-02-14 19:50:32 +0000431
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000432 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000433
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000434 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000435#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000436 _setmode(fileno(stdin), O_BINARY);
437 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000438#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000439#ifdef HAVE_SETVBUF
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000440 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
441 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
442 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000443#else /* !HAVE_SETVBUF */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000444 setbuf(stdin, (char *)NULL);
445 setbuf(stdout, (char *)NULL);
446 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000447#endif /* !HAVE_SETVBUF */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000448 }
449 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000450#ifdef MS_WINDOWS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000451 /* Doesn't have to have line-buffered -- use unbuffered */
452 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
453 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000454#else /* !MS_WINDOWS */
455#ifdef HAVE_SETVBUF
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000456 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
457 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000458#endif /* HAVE_SETVBUF */
459#endif /* !MS_WINDOWS */
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000460 /* Leave stderr alone - it should be unbuffered anyway. */
461 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000462#ifdef __VMS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000463 else {
464 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
465 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000466#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000467
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000468#ifdef __APPLE__
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000469 /* On MacOS X, when the Python interpreter is embedded in an
470 application bundle, it gets executed by a bootstrapping script
471 that does os.execve() with an argv[0] that's different from the
472 actual Python executable. This is needed to keep the Finder happy,
473 or rather, to work around Apple's overly strict requirements of
474 the process name. However, we still need a usable sys.executable,
475 so the actual executable path is passed in an environment variable.
476 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
477 script. */
478 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
479 Py_SetProgramName(p);
480 else
481 Py_SetProgramName(argv[0]);
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000482#else
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000483 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000484#endif
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000485 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000486
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000487 if (Py_VerboseFlag ||
488 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
489 fprintf(stderr, "Python %s on %s\n",
490 Py_GetVersion(), Py_GetPlatform());
491 if (!Py_NoSiteFlag)
492 fprintf(stderr, "%s\n", COPYRIGHT);
493 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000494
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000495 if (command != NULL) {
496 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
497 _PyOS_optind--;
498 argv[_PyOS_optind] = "-c";
499 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000500
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000501 if (module != NULL) {
502 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
503 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
504 _PyOS_optind--;
505 argv[_PyOS_optind] = "-c";
506 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000507
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000508 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000509
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000510 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
511 isatty(fileno(stdin))) {
512 PyObject *v;
513 v = PyImport_ImportModule("readline");
514 if (v == NULL)
515 PyErr_Clear();
516 else
517 Py_DECREF(v);
518 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000519
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000520 if (command) {
521 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
522 free(command);
523 } else if (module) {
524 sts = RunModule(module, 1);
525 free(module);
526 }
527 else {
Nick Coghlan327a39b2007-11-18 11:56:28 +0000528
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000529 if (filename == NULL && stdin_is_interactive) {
530 Py_InspectFlag = 0; /* do exit on SystemExit */
531 RunStartupFile(&cf);
532 }
533 /* XXX */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000534
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000535 sts = -1; /* keep track of whether we've already run __main__ */
Nick Coghlan327a39b2007-11-18 11:56:28 +0000536
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000537 if (filename != NULL) {
538 sts = RunMainFromImporter(filename);
539 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000540
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000541 if (sts==-1 && filename!=NULL) {
542 if ((fp = fopen(filename, "r")) == NULL) {
543 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
544 argv[0], filename, errno, strerror(errno));
Brett Cannon10ed0f52008-03-18 15:35:58 +0000545
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000546 return 2;
547 }
548 else if (skipfirstline) {
549 int ch;
550 /* Push back first newline so line numbers
551 remain the same */
552 while ((ch = getc(fp)) != EOF) {
553 if (ch == '\n') {
554 (void)ungetc(ch, fp);
555 break;
556 }
557 }
558 }
559 {
560 /* XXX: does this work on Win/Win64? (see posix_fstat) */
561 struct stat sb;
562 if (fstat(fileno(fp), &sb) == 0 &&
563 S_ISDIR(sb.st_mode)) {
564 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
565 fclose(fp);
566 return 1;
567 }
568 }
569 }
Nick Coghlan327a39b2007-11-18 11:56:28 +0000570
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000571 if (sts==-1) {
572 /* call pending calls like signal handlers (SIGINT) */
573 if (Py_MakePendingCalls() == -1) {
574 PyErr_Print();
575 sts = 1;
576 } else {
577 sts = PyRun_AnyFileExFlags(
578 fp,
579 filename == NULL ? "<stdin>" : filename,
580 filename != NULL, &cf) != 0;
581 }
582 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000583
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000584 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000585
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000586 /* Check this environment variable at the end, to give programs the
587 * opportunity to set it from Python.
588 */
589 if (!Py_InspectFlag &&
590 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
591 {
592 Py_InspectFlag = 1;
593 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000594
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000595 if (Py_InspectFlag && stdin_is_interactive &&
596 (filename != NULL || command != NULL || module != NULL)) {
597 Py_InspectFlag = 0;
598 /* XXX */
599 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
600 }
601
602 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000603#ifdef RISCOS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000604 if (Py_RISCOSWimpFlag)
605 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000606#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000607
608#ifdef __INSURE__
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000609 /* Insure++ is a memory analysis tool that aids in discovering
610 * memory leaks and other memory problems. On Python exit, the
611 * interned string dictionary is flagged as being in use at exit
612 * (which it is). Under normal circumstances, this is fine because
613 * the memory will be automatically reclaimed by the system. Under
614 * memory debugging, it's a huge source of useless noise, so we
615 * trade off slower shutdown for less distraction in the memory
616 * reports. -baw
617 */
618 _Py_ReleaseInternedStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000619#endif /* __INSURE__ */
620
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000621 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000622}
623
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000624/* this is gonna seem *real weird*, but if you put some other code between
625 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
626 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000627
Guido van Rossum667d7041995-08-04 04:20:48 +0000628/* Make the *original* argc/argv available to other modules.
629 This is rare, but it is needed by the secureware extension. */
630
631void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000632Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000633{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000634 *argc = orig_argc;
635 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000636}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000637
638#ifdef __cplusplus
639}
640#endif
641