blob: 3f39c9bffcd14340c8a584bbb30b1d1d7ef10f7a [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"
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00005#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00007#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#endif
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000013#include <fcntl.h>
14#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000017#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000018#define PYTHONHOMEHELP "<prefix>\\lib"
19#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000020#if defined(PYOS_OS2) && defined(PYCC_GCC)
21#define PYTHONHOMEHELP "<prefix>/Lib"
22#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000023#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000024#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000026
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000027#include "pygetopt.h"
28
Guido van Rossuma22865e2000-09-05 04:41:18 +000029#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000030 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
31 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Guido van Rossumac56b031996-07-21 02:33:38 +000037/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000038static char **orig_argv;
39static int orig_argc;
40
Guido van Rossumbceccf52001-04-10 22:07:43 +000041/* command line options */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042#define BASE_OPTS "c:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000043
44#ifndef RISCOS
45#define PROGRAM_OPTS BASE_OPTS
46#else /*RISCOS*/
47/* extra option saying that we are running under a special task window
48 frontend; especially my_readline will behave different */
49#define PROGRAM_OPTS BASE_OPTS "w"
50/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000051extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000052#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000053
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000056"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000059static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000060Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000061-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000062-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000063-E : ignore environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064-h : print this help message and exit (also --help)\n\
Guido van Rossum775af911997-02-14 19:50:32 +000065-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000066 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000067";
68static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000069-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000070-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000071-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000073-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000074-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075";
76static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000077 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000078-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079-V : print the Python version number and exit (also --version)\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000080-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000081-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000082file : program read from script file\n\
83- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000084";
Guido van Rossum393661d2001-08-31 17:40:15 +000085static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000086arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000087Other environment variables:\n\
88PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000089PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000091PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
92 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000093PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000094";
95
96
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000097static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000098usage(int exitcode, char* program)
99{
Guido van Rossum393661d2001-08-31 17:40:15 +0000100 FILE *f = exitcode ? stderr : stdout;
101
102 fprintf(f, usage_line, program);
103 if (exitcode)
104 fprintf(f, "Try `python -h' for more information.\n");
105 else {
106 fprintf(f, usage_1);
107 fprintf(f, usage_2);
108 fprintf(f, usage_3);
109 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
110 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000111#if defined(__VMS)
112 if (exitcode == 0) {
113 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000114 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115 }
116 else {
117 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000118 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000119 }
120#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000121 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000122#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000123 /*NOTREACHED*/
124}
125
Martin v. Löwis6caea372003-11-18 19:46:25 +0000126static void RunStartupFile(PyCompilerFlags *cf)
127{
128 char *startup = Py_GETENV("PYTHONSTARTUP");
129 if (startup != NULL && startup[0] != '\0') {
130 FILE *fp = fopen(startup, "r");
131 if (fp != NULL) {
132 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
133 PyErr_Clear();
134 fclose(fp);
135 }
136 }
137}
138
Thomas Woutersa9773292006-04-21 09:43:23 +0000139
140static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000141{
Thomas Woutersa9773292006-04-21 09:43:23 +0000142 PyObject *runpy, *runmodule, *runargs, *result;
143 runpy = PyImport_ImportModule("runpy");
144 if (runpy == NULL) {
145 fprintf(stderr, "Could not import runpy module\n");
146 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000147 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000148 runmodule = PyObject_GetAttrString(runpy, "run_module");
149 if (runmodule == NULL) {
150 fprintf(stderr, "Could not access runpy.run_module\n");
151 Py_DECREF(runpy);
152 return -1;
153 }
154 runargs = Py_BuildValue("sOsO", module,
155 Py_None, "__main__", Py_True);
156 if (runargs == NULL) {
157 fprintf(stderr,
158 "Could not create arguments for runpy.run_module\n");
159 Py_DECREF(runpy);
160 Py_DECREF(runmodule);
161 return -1;
162 }
163 result = PyObject_Call(runmodule, runargs, NULL);
164 if (result == NULL) {
165 PyErr_Print();
166 }
167 Py_DECREF(runpy);
168 Py_DECREF(runmodule);
169 Py_DECREF(runargs);
170 if (result == NULL) {
171 return -1;
172 }
173 Py_DECREF(result);
174 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000175}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000176
Guido van Rossum667d7041995-08-04 04:20:48 +0000177/* Main program */
178
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000179int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000180Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000181{
182 int c;
183 int sts;
184 char *command = NULL;
185 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000186 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000187 FILE *fp = stdin;
188 char *p;
189 int inspect = 0;
190 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000191 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000192 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000193 int help = 0;
194 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000195 int saw_inspect_flag = 0;
196 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000197 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000198
Guido van Rossum393661d2001-08-31 17:40:15 +0000199 cf.cf_flags = 0;
200
Guido van Rossumac56b031996-07-21 02:33:38 +0000201 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000202 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000203
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000204#ifdef RISCOS
205 Py_RISCOSWimpFlag = 0;
206#endif
207
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000208 PySys_ResetWarnOptions();
209
Guido van Rossumbceccf52001-04-10 22:07:43 +0000210 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000211 if (c == 'c') {
212 /* -c is the last option; following arguments
213 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000214 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000216 if (command == NULL)
217 Py_FatalError(
218 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000219 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000220 strcat(command, "\n");
221 break;
222 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000223
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000224 if (c == 'm') {
225 /* -m is the last option; following arguments
226 that look like options are left for the
227 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000229 if (module == NULL)
230 Py_FatalError(
231 "not enough memory to copy -m argument");
232 strcpy(module, _PyOS_optarg);
233 break;
234 }
235
Guido van Rossum667d7041995-08-04 04:20:48 +0000236 switch (c) {
237
238 case 'd':
239 Py_DebugFlag++;
240 break;
241
242 case 'i':
243 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000244 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000245 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 break;
247
Guido van Rossum7614da61997-03-03 19:14:45 +0000248 case 'O':
249 Py_OptimizeFlag++;
250 break;
251
Guido van Rossum7922bd71997-08-29 22:34:47 +0000252 case 'S':
253 Py_NoSiteFlag++;
254 break;
255
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000256 case 'E':
257 Py_IgnoreEnvironmentFlag++;
258 break;
259
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000260 case 't':
261 Py_TabcheckFlag++;
262 break;
263
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 case 'u':
265 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000266 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000267 break;
268
269 case 'v':
270 Py_VerboseFlag++;
271 break;
272
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000273#ifdef RISCOS
274 case 'w':
275 Py_RISCOSWimpFlag = 1;
276 break;
277#endif
278
Guido van Rossuma075ce11997-12-05 21:56:45 +0000279 case 'x':
280 skipfirstline = 1;
281 break;
282
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000283 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000285 help++;
286 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000287
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000288 case 'V':
289 version++;
290 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000291
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000292 case 'W':
293 PySys_AddWarnOption(_PyOS_optarg);
294 break;
295
Guido van Rossum667d7041995-08-04 04:20:48 +0000296 /* This space reserved for other options */
297
298 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000299 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000300 /*NOTREACHED*/
301
302 }
303 }
304
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000305 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000306 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000307
308 if (version) {
309 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000310 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000311 }
312
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000313 if (!saw_inspect_flag &&
314 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
315 inspect = 1;
316 if (!saw_unbuffered_flag &&
317 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
318 unbuffered = 1;
319
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000320 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000321 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000322 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000323#ifdef __VMS
324 filename = decc$translate_vms(argv[_PyOS_optind]);
325 if (filename == (char *)0 || filename == (char *)-1)
326 filename = argv[_PyOS_optind];
327
328#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000329 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000330#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000331 if (filename != NULL) {
332 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000333#ifdef HAVE_STRERROR
334 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
335 argv[0], filename, errno, strerror(errno));
336#else
337 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
338 argv[0], filename, errno);
339#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000340 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000341 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000342 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000343 int ch;
344 /* Push back first newline so line numbers
345 remain the same */
346 while ((ch = getc(fp)) != EOF) {
347 if (ch == '\n') {
348 (void)ungetc(ch, fp);
349 break;
350 }
351 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000352 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000353 {
354 /* XXX: does this work on Win/Win64? (see posix_fstat) */
355 struct stat sb;
356 if (fstat(fileno(fp), &sb) == 0 &&
357 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000358 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
359 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000360 }
361 }
Guido van Rossum775af911997-02-14 19:50:32 +0000362 }
363 }
364
365 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
366
Guido van Rossum667d7041995-08-04 04:20:48 +0000367 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000368#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000369 _setmode(fileno(stdin), O_BINARY);
370 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000371#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000372#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000373 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
374 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
375 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000376#else /* !HAVE_SETVBUF */
377 setbuf(stdin, (char *)NULL);
378 setbuf(stdout, (char *)NULL);
379 setbuf(stderr, (char *)NULL);
380#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000381 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000382 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000383#ifdef MS_WINDOWS
384 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000385 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000386 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000387#else /* !MS_WINDOWS */
388#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000389 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
390 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000391#endif /* HAVE_SETVBUF */
392#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000393 /* Leave stderr alone - it should be unbuffered anyway. */
394 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000395#ifdef __VMS
396 else {
397 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
398 }
399#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000400
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000401#ifdef __APPLE__
402 /* On MacOS X, when the Python interpreter is embedded in an
403 application bundle, it gets executed by a bootstrapping script
404 that does os.execve() with an argv[0] that's different from the
405 actual Python executable. This is needed to keep the Finder happy,
406 or rather, to work around Apple's overly strict requirements of
407 the process name. However, we still need a usable sys.executable,
408 so the actual executable path is passed in an environment variable.
409 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
410 script. */
411 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
412 Py_SetProgramName(p);
413 else
414 Py_SetProgramName(argv[0]);
415#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000416 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000417#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000418 Py_Initialize();
419
Guido van Rossum667d7041995-08-04 04:20:48 +0000420 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000421 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000422 fprintf(stderr, "Python %s on %s\n",
423 Py_GetVersion(), Py_GetPlatform());
424 if (!Py_NoSiteFlag)
425 fprintf(stderr, "%s\n", COPYRIGHT);
426 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000427
Guido van Rossum667d7041995-08-04 04:20:48 +0000428 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000429 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
430 _PyOS_optind--;
431 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000432 }
433
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000434 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000435 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
436 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000437 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000439 }
440
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000441 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000442
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000443 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000444 isatty(fileno(stdin))) {
445 PyObject *v;
446 v = PyImport_ImportModule("readline");
447 if (v == NULL)
448 PyErr_Clear();
449 else
450 Py_DECREF(v);
451 }
452
Guido van Rossum667d7041995-08-04 04:20:48 +0000453 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000454 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000455 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000456 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000457 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000458 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000459 }
460 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000461 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000462 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000463 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000464 /* XXX */
465 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000466 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000467 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000468 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000469 }
470
Barry Warsawd86dcd32003-06-29 17:07:06 +0000471 /* Check this environment variable at the end, to give programs the
472 * opportunity to set it from Python.
473 */
474 if (!saw_inspect_flag &&
475 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
476 {
477 inspect = 1;
478 }
479
Guido van Rossum775af911997-02-14 19:50:32 +0000480 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000481 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000482 /* XXX */
483 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000484
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000485 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000486#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000487 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000488 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
489#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000490
491#ifdef __INSURE__
492 /* Insure++ is a memory analysis tool that aids in discovering
493 * memory leaks and other memory problems. On Python exit, the
494 * interned string dictionary is flagged as being in use at exit
495 * (which it is). Under normal circumstances, this is fine because
496 * the memory will be automatically reclaimed by the system. Under
497 * memory debugging, it's a huge source of useless noise, so we
498 * trade off slower shutdown for less distraction in the memory
499 * reports. -baw
500 */
501 _Py_ReleaseInternedStrings();
502#endif /* __INSURE__ */
503
Guido van Rossum05f7c501997-08-02 03:00:42 +0000504 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000505}
506
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000507/* this is gonna seem *real weird*, but if you put some other code between
508 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
509 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000510
Guido van Rossum667d7041995-08-04 04:20:48 +0000511/* Make the *original* argc/argv available to other modules.
512 This is rare, but it is needed by the secureware extension. */
513
514void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000515Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000516{
517 *argc = orig_argc;
518 *argv = orig_argv;
519}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520
521#ifdef __cplusplus
522}
523#endif
524