blob: 23a760fe0d8584b07ab3c50036421a54260dfb9a [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 */
Guido van Rossum45aecf42006-03-15 04:58:47 +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\
64-h : print this help message and exit\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\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000079-V : print the Python version number and exit\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':
284 help++;
285 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000286
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000287 case 'V':
288 version++;
289 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000290
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000291 case 'W':
292 PySys_AddWarnOption(_PyOS_optarg);
293 break;
294
Guido van Rossum667d7041995-08-04 04:20:48 +0000295 /* This space reserved for other options */
296
297 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000298 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000299 /*NOTREACHED*/
300
301 }
302 }
303
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000304 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000305 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000306
307 if (version) {
308 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000309 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000310 }
311
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000312 if (!saw_inspect_flag &&
313 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
314 inspect = 1;
315 if (!saw_unbuffered_flag &&
316 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
317 unbuffered = 1;
318
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000319 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000320 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000321 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000322#ifdef __VMS
323 filename = decc$translate_vms(argv[_PyOS_optind]);
324 if (filename == (char *)0 || filename == (char *)-1)
325 filename = argv[_PyOS_optind];
326
327#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000328 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000329#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000330 if (filename != NULL) {
331 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000332#ifdef HAVE_STRERROR
333 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
334 argv[0], filename, errno, strerror(errno));
335#else
336 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
337 argv[0], filename, errno);
338#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000339 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000340 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000341 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000342 int ch;
343 /* Push back first newline so line numbers
344 remain the same */
345 while ((ch = getc(fp)) != EOF) {
346 if (ch == '\n') {
347 (void)ungetc(ch, fp);
348 break;
349 }
350 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000351 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000352 {
353 /* XXX: does this work on Win/Win64? (see posix_fstat) */
354 struct stat sb;
355 if (fstat(fileno(fp), &sb) == 0 &&
356 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000357 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
358 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000359 }
360 }
Guido van Rossum775af911997-02-14 19:50:32 +0000361 }
362 }
363
364 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
365
Guido van Rossum667d7041995-08-04 04:20:48 +0000366 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000367#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000368 _setmode(fileno(stdin), O_BINARY);
369 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000370#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000371#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000372 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
373 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
374 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000375#else /* !HAVE_SETVBUF */
376 setbuf(stdin, (char *)NULL);
377 setbuf(stdout, (char *)NULL);
378 setbuf(stderr, (char *)NULL);
379#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000380 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000381 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000382#ifdef MS_WINDOWS
383 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000384 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000385 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000386#else /* !MS_WINDOWS */
387#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000388 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
389 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000390#endif /* HAVE_SETVBUF */
391#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000392 /* Leave stderr alone - it should be unbuffered anyway. */
393 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000394#ifdef __VMS
395 else {
396 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
397 }
398#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000399
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000400#ifdef __APPLE__
401 /* On MacOS X, when the Python interpreter is embedded in an
402 application bundle, it gets executed by a bootstrapping script
403 that does os.execve() with an argv[0] that's different from the
404 actual Python executable. This is needed to keep the Finder happy,
405 or rather, to work around Apple's overly strict requirements of
406 the process name. However, we still need a usable sys.executable,
407 so the actual executable path is passed in an environment variable.
408 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
409 script. */
410 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
411 Py_SetProgramName(p);
412 else
413 Py_SetProgramName(argv[0]);
414#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000415 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000416#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000417 Py_Initialize();
418
Guido van Rossum667d7041995-08-04 04:20:48 +0000419 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000420 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000421 fprintf(stderr, "Python %s on %s\n",
422 Py_GetVersion(), Py_GetPlatform());
423 if (!Py_NoSiteFlag)
424 fprintf(stderr, "%s\n", COPYRIGHT);
425 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000426
Guido van Rossum667d7041995-08-04 04:20:48 +0000427 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000428 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
429 _PyOS_optind--;
430 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000431 }
432
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000433 if (module != NULL) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000434 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000435 _PyOS_optind--;
Thomas Woutersa9773292006-04-21 09:43:23 +0000436 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000437 }
438
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000439 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000440
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000441 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000442 isatty(fileno(stdin))) {
443 PyObject *v;
444 v = PyImport_ImportModule("readline");
445 if (v == NULL)
446 PyErr_Clear();
447 else
448 Py_DECREF(v);
449 }
450
Guido van Rossum667d7041995-08-04 04:20:48 +0000451 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000452 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000453 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000454 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000455 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000456 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000457 }
458 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000459 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000460 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000461 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000462 /* XXX */
463 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000464 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000465 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000466 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000467 }
468
Barry Warsawd86dcd32003-06-29 17:07:06 +0000469 /* Check this environment variable at the end, to give programs the
470 * opportunity to set it from Python.
471 */
472 if (!saw_inspect_flag &&
473 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
474 {
475 inspect = 1;
476 }
477
Guido van Rossum775af911997-02-14 19:50:32 +0000478 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000479 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000480 /* XXX */
481 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000482
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000483 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000484#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000485 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000486 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
487#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000488
489#ifdef __INSURE__
490 /* Insure++ is a memory analysis tool that aids in discovering
491 * memory leaks and other memory problems. On Python exit, the
492 * interned string dictionary is flagged as being in use at exit
493 * (which it is). Under normal circumstances, this is fine because
494 * the memory will be automatically reclaimed by the system. Under
495 * memory debugging, it's a huge source of useless noise, so we
496 * trade off slower shutdown for less distraction in the memory
497 * reports. -baw
498 */
499 _Py_ReleaseInternedStrings();
500#endif /* __INSURE__ */
501
Guido van Rossum05f7c501997-08-02 03:00:42 +0000502 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000503}
504
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000505/* this is gonna seem *real weird*, but if you put some other code between
506 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
507 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000508
Guido van Rossum667d7041995-08-04 04:20:48 +0000509/* Make the *original* argc/argv available to other modules.
510 This is rare, but it is needed by the secureware extension. */
511
512void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000513Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000514{
515 *argc = orig_argc;
516 *argv = orig_argv;
517}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518
519#ifdef __cplusplus
520}
521#endif
522