blob: 5a55036ef2d2bca77d8a45cc0bf699bac7ef2cdf [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 */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000043#define BASE_OPTS "c:dEhim:OQ:StuUvVW: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\
Guido van Rossum393661d2001-08-31 17:40:15 +000062-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000063-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000064-E : ignore environment variables (such as PYTHONPATH)\n\
65-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000066-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000067 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000070-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000071-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000072-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000073-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000074-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000075-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000076-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000079 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000080-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000081-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000082-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000083-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000084file : program read from script file\n\
85- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000086";
Guido van Rossum393661d2001-08-31 17:40:15 +000087static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000088arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089Other environment variables:\n\
90PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000091PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000093PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
94 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000095PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000096";
97
98
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000099static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000100usage(int exitcode, char* program)
101{
Guido van Rossum393661d2001-08-31 17:40:15 +0000102 FILE *f = exitcode ? stderr : stdout;
103
104 fprintf(f, usage_line, program);
105 if (exitcode)
106 fprintf(f, "Try `python -h' for more information.\n");
107 else {
108 fprintf(f, usage_1);
109 fprintf(f, usage_2);
110 fprintf(f, usage_3);
111 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
112 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000113#if defined(__VMS)
114 if (exitcode == 0) {
115 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000116 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000117 }
118 else {
119 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000120 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000121 }
122#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000123 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000124#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000125 /*NOTREACHED*/
126}
127
Martin v. Löwis6caea372003-11-18 19:46:25 +0000128static void RunStartupFile(PyCompilerFlags *cf)
129{
130 char *startup = Py_GETENV("PYTHONSTARTUP");
131 if (startup != NULL && startup[0] != '\0') {
132 FILE *fp = fopen(startup, "r");
133 if (fp != NULL) {
134 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
135 PyErr_Clear();
136 fclose(fp);
137 }
138 }
139}
140
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000141
142static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000143{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000144 PyObject *runpy, *runmodule, *runargs, *result;
145 runpy = PyImport_ImportModule("runpy");
146 if (runpy == NULL) {
147 fprintf(stderr, "Could not import runpy module\n");
148 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000149 }
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000150 runmodule = PyObject_GetAttrString(runpy, "run_module");
151 if (runmodule == NULL) {
152 fprintf(stderr, "Could not access runpy.run_module\n");
153 Py_DECREF(runpy);
154 return -1;
155 }
156 runargs = Py_BuildValue("sOsO", module,
157 Py_None, "__main__", Py_True);
158 if (runargs == NULL) {
159 fprintf(stderr,
160 "Could not create arguments for runpy.run_module\n");
161 Py_DECREF(runpy);
162 Py_DECREF(runmodule);
163 return -1;
164 }
165 result = PyObject_Call(runmodule, runargs, NULL);
166 if (result == NULL) {
167 PyErr_Print();
168 }
169 Py_DECREF(runpy);
170 Py_DECREF(runmodule);
171 Py_DECREF(runargs);
172 if (result == NULL) {
173 return -1;
174 }
175 Py_DECREF(result);
176 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000177}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000178
Guido van Rossum667d7041995-08-04 04:20:48 +0000179/* Main program */
180
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000181int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000182Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000183{
184 int c;
185 int sts;
186 char *command = NULL;
187 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000188 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000189 FILE *fp = stdin;
190 char *p;
191 int inspect = 0;
192 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000193 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000194 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000195 int help = 0;
196 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000197 int saw_inspect_flag = 0;
198 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000199 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000200
Guido van Rossum393661d2001-08-31 17:40:15 +0000201 cf.cf_flags = 0;
202
Guido van Rossumac56b031996-07-21 02:33:38 +0000203 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000204 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000205
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000206#ifdef RISCOS
207 Py_RISCOSWimpFlag = 0;
208#endif
209
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000210 PySys_ResetWarnOptions();
211
Guido van Rossumbceccf52001-04-10 22:07:43 +0000212 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000213 if (c == 'c') {
214 /* -c is the last option; following arguments
215 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000216 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000217 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000218 if (command == NULL)
219 Py_FatalError(
220 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000221 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000222 strcat(command, "\n");
223 break;
224 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000225
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000226 if (c == 'm') {
227 /* -m is the last option; following arguments
228 that look like options are left for the
229 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000230 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000231 if (module == NULL)
232 Py_FatalError(
233 "not enough memory to copy -m argument");
234 strcpy(module, _PyOS_optarg);
235 break;
236 }
237
Guido van Rossum667d7041995-08-04 04:20:48 +0000238 switch (c) {
239
240 case 'd':
241 Py_DebugFlag++;
242 break;
243
Guido van Rossum61c345f2001-09-04 03:26:15 +0000244 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000245 if (strcmp(_PyOS_optarg, "old") == 0) {
246 Py_DivisionWarningFlag = 0;
247 break;
248 }
249 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000250 Py_DivisionWarningFlag = 1;
251 break;
252 }
253 if (strcmp(_PyOS_optarg, "warnall") == 0) {
254 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000255 break;
256 }
257 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000258 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000259 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000260 /* And this tells the eval loop to treat
261 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
262 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000263 break;
264 }
265 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000266 "-Q option should be `-Qold', "
267 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000268 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000269 /* NOTREACHED */
270
Guido van Rossum667d7041995-08-04 04:20:48 +0000271 case 'i':
272 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000273 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000274 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000275 break;
276
Guido van Rossum7614da61997-03-03 19:14:45 +0000277 case 'O':
278 Py_OptimizeFlag++;
279 break;
280
Guido van Rossum7922bd71997-08-29 22:34:47 +0000281 case 'S':
282 Py_NoSiteFlag++;
283 break;
284
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000285 case 'E':
286 Py_IgnoreEnvironmentFlag++;
287 break;
288
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000289 case 't':
290 Py_TabcheckFlag++;
291 break;
292
Guido van Rossum667d7041995-08-04 04:20:48 +0000293 case 'u':
294 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000295 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000296 break;
297
298 case 'v':
299 Py_VerboseFlag++;
300 break;
301
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000302#ifdef RISCOS
303 case 'w':
304 Py_RISCOSWimpFlag = 1;
305 break;
306#endif
307
Guido van Rossuma075ce11997-12-05 21:56:45 +0000308 case 'x':
309 skipfirstline = 1;
310 break;
311
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000312 case 'U':
313 Py_UnicodeFlag++;
314 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000315 case 'h':
316 help++;
317 break;
318 case 'V':
319 version++;
320 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000321
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000322 case 'W':
323 PySys_AddWarnOption(_PyOS_optarg);
324 break;
325
Guido van Rossum667d7041995-08-04 04:20:48 +0000326 /* This space reserved for other options */
327
328 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000329 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000330 /*NOTREACHED*/
331
332 }
333 }
334
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000335 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000336 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000337
338 if (version) {
339 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000340 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000341 }
342
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000343 if (!saw_inspect_flag &&
344 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
345 inspect = 1;
346 if (!saw_unbuffered_flag &&
347 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
348 unbuffered = 1;
349
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000350 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000351 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000352 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000353#ifdef __VMS
354 filename = decc$translate_vms(argv[_PyOS_optind]);
355 if (filename == (char *)0 || filename == (char *)-1)
356 filename = argv[_PyOS_optind];
357
358#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000359 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000360#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000361 if (filename != NULL) {
362 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000363#ifdef HAVE_STRERROR
364 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
365 argv[0], filename, errno, strerror(errno));
366#else
367 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
368 argv[0], filename, errno);
369#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000370 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000371 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000372 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000373 int ch;
374 /* Push back first newline so line numbers
375 remain the same */
376 while ((ch = getc(fp)) != EOF) {
377 if (ch == '\n') {
378 (void)ungetc(ch, fp);
379 break;
380 }
381 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000382 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000383 {
384 /* XXX: does this work on Win/Win64? (see posix_fstat) */
385 struct stat sb;
386 if (fstat(fileno(fp), &sb) == 0 &&
387 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000388 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
389 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000390 }
391 }
Guido van Rossum775af911997-02-14 19:50:32 +0000392 }
393 }
394
395 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
396
Guido van Rossum667d7041995-08-04 04:20:48 +0000397 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000398#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000399 _setmode(fileno(stdin), O_BINARY);
400 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000401#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000402#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000403 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
404 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
405 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000406#else /* !HAVE_SETVBUF */
407 setbuf(stdin, (char *)NULL);
408 setbuf(stdout, (char *)NULL);
409 setbuf(stderr, (char *)NULL);
410#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000411 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000412 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000413#ifdef MS_WINDOWS
414 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000415 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000416 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000417#else /* !MS_WINDOWS */
418#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000419 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
420 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000421#endif /* HAVE_SETVBUF */
422#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000423 /* Leave stderr alone - it should be unbuffered anyway. */
424 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000425#ifdef __VMS
426 else {
427 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
428 }
429#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000430
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000431#ifdef __APPLE__
432 /* On MacOS X, when the Python interpreter is embedded in an
433 application bundle, it gets executed by a bootstrapping script
434 that does os.execve() with an argv[0] that's different from the
435 actual Python executable. This is needed to keep the Finder happy,
436 or rather, to work around Apple's overly strict requirements of
437 the process name. However, we still need a usable sys.executable,
438 so the actual executable path is passed in an environment variable.
439 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
440 script. */
441 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
442 Py_SetProgramName(p);
443 else
444 Py_SetProgramName(argv[0]);
445#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000446 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000447#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000448 Py_Initialize();
449
Guido van Rossum667d7041995-08-04 04:20:48 +0000450 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000451 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000452 fprintf(stderr, "Python %s on %s\n",
453 Py_GetVersion(), Py_GetPlatform());
454 if (!Py_NoSiteFlag)
455 fprintf(stderr, "%s\n", COPYRIGHT);
456 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000457
Guido van Rossum667d7041995-08-04 04:20:48 +0000458 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000459 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
460 _PyOS_optind--;
461 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000462 }
463
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000464 if (module != NULL) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000465 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000466 _PyOS_optind--;
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000467 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000468 }
469
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000470 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000471
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000472 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000473 isatty(fileno(stdin))) {
474 PyObject *v;
475 v = PyImport_ImportModule("readline");
476 if (v == NULL)
477 PyErr_Clear();
478 else
479 Py_DECREF(v);
480 }
481
Guido van Rossum667d7041995-08-04 04:20:48 +0000482 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000483 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000484 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000485 } else if (module) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000486 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000487 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000488 }
489 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000490 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000491 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000492 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000493 /* XXX */
494 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000495 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000496 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000497 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000498 }
499
Barry Warsawd86dcd32003-06-29 17:07:06 +0000500 /* Check this environment variable at the end, to give programs the
501 * opportunity to set it from Python.
502 */
503 if (!saw_inspect_flag &&
504 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
505 {
506 inspect = 1;
507 }
508
Guido van Rossum775af911997-02-14 19:50:32 +0000509 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000510 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000511 /* XXX */
512 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000513
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000514 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000515#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000516 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000517 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
518#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000519
520#ifdef __INSURE__
521 /* Insure++ is a memory analysis tool that aids in discovering
522 * memory leaks and other memory problems. On Python exit, the
523 * interned string dictionary is flagged as being in use at exit
524 * (which it is). Under normal circumstances, this is fine because
525 * the memory will be automatically reclaimed by the system. Under
526 * memory debugging, it's a huge source of useless noise, so we
527 * trade off slower shutdown for less distraction in the memory
528 * reports. -baw
529 */
530 _Py_ReleaseInternedStrings();
531#endif /* __INSURE__ */
532
Guido van Rossum05f7c501997-08-02 03:00:42 +0000533 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000534}
535
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000536/* this is gonna seem *real weird*, but if you put some other code between
537 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
538 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000539
Guido van Rossum667d7041995-08-04 04:20:48 +0000540/* Make the *original* argc/argv available to other modules.
541 This is rare, but it is needed by the secureware extension. */
542
543void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000544Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000545{
546 *argc = orig_argc;
547 *argv = orig_argv;
548}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000549
550#ifdef __cplusplus
551}
552#endif
553