blob: 1005b94e224935641a3e789bb1766144fecf8725 [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"
Guido van Rossum393661d2001-08-31 17:40:15 +00005#include "compile.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__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000013#include <fcntl.h>
14#endif
15
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000016#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000017#define PYTHONHOMEHELP "<prefix>\\lib"
18#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000019#if defined(PYOS_OS2) && defined(PYCC_GCC)
20#define PYTHONHOMEHELP "<prefix>/Lib"
21#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000022#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000023#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000024#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000025
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000026#include "pygetopt.h"
27
Guido van Rossuma22865e2000-09-05 04:41:18 +000028#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000029 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
30 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000031
Guido van Rossumac56b031996-07-21 02:33:38 +000032/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000033static char **orig_argv;
34static int orig_argc;
35
Guido van Rossumbceccf52001-04-10 22:07:43 +000036/* command line options */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000037#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000038
39#ifndef RISCOS
40#define PROGRAM_OPTS BASE_OPTS
41#else /*RISCOS*/
42/* extra option saying that we are running under a special task window
43 frontend; especially my_readline will behave different */
44#define PROGRAM_OPTS BASE_OPTS "w"
45/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000046extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000047#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000048
Guido van Rossum667d7041995-08-04 04:20:48 +000049/* Short usage message (with %s for argv0) */
50static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000051"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000052
53/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000054static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000055Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000056-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000057-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000058-E : ignore environment variables (such as PYTHONPATH)\n\
59-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000060-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000061 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000062";
63static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000064-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000065-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000066-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000067-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000068-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000069-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000070-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000071";
72static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000073 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000074-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000075-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000076-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000078file : program read from script file\n\
79- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000080";
Guido van Rossum393661d2001-08-31 17:40:15 +000081static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000082arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000083Other environment variables:\n\
84PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000085PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000086 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000087PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
88 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000089PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090";
91
92
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000093static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000094usage(int exitcode, char* program)
95{
Guido van Rossum393661d2001-08-31 17:40:15 +000096 FILE *f = exitcode ? stderr : stdout;
97
98 fprintf(f, usage_line, program);
99 if (exitcode)
100 fprintf(f, "Try `python -h' for more information.\n");
101 else {
102 fprintf(f, usage_1);
103 fprintf(f, usage_2);
104 fprintf(f, usage_3);
105 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
106 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000107#if defined(__VMS)
108 if (exitcode == 0) {
109 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000110 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000111 }
112 else {
113 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000114 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115 }
116#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000117 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000118#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000119 /*NOTREACHED*/
120}
121
Martin v. Löwis6caea372003-11-18 19:46:25 +0000122static void RunStartupFile(PyCompilerFlags *cf)
123{
124 char *startup = Py_GETENV("PYTHONSTARTUP");
125 if (startup != NULL && startup[0] != '\0') {
126 FILE *fp = fopen(startup, "r");
127 if (fp != NULL) {
128 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
129 PyErr_Clear();
130 fclose(fp);
131 }
132 }
133}
134
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000135/* Get the path to a top-level module */
136static struct filedescr * FindModule(const char *module,
137 FILE **fp, char **filename)
138{
139 struct filedescr *fdescr = NULL;
140 *fp = NULL;
141 *filename = malloc(MAXPATHLEN);
142
143 if (*filename == NULL)
144 return NULL;
145
146 /* Find the actual module source code */
147 fdescr = _PyImport_FindModule(module, NULL,
148 *filename, MAXPATHLEN, fp, NULL);
149
150 if (fdescr == NULL) {
151 free(*filename);
152 *filename = NULL;
153 }
154
155 return fdescr;
156}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000157
Guido van Rossum667d7041995-08-04 04:20:48 +0000158/* Main program */
159
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000160int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000161Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000162{
163 int c;
164 int sts;
165 char *command = NULL;
166 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000167 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000168 FILE *fp = stdin;
169 char *p;
170 int inspect = 0;
171 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000172 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000173 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000174 int help = 0;
175 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000176 int saw_inspect_flag = 0;
177 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000178 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000179
Guido van Rossum393661d2001-08-31 17:40:15 +0000180 cf.cf_flags = 0;
181
Guido van Rossumac56b031996-07-21 02:33:38 +0000182 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000183 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000184
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000185#ifdef RISCOS
186 Py_RISCOSWimpFlag = 0;
187#endif
188
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000189 PySys_ResetWarnOptions();
190
Guido van Rossumbceccf52001-04-10 22:07:43 +0000191 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000192 if (c == 'c') {
193 /* -c is the last option; following arguments
194 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000195 command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000196 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000197 if (command == NULL)
198 Py_FatalError(
199 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000200 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000201 strcat(command, "\n");
202 break;
203 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000204
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000205 if (c == 'm') {
206 /* -m is the last option; following arguments
207 that look like options are left for the
208 module to interpret. */
209 module = malloc(strlen(_PyOS_optarg) + 2);
210 if (module == NULL)
211 Py_FatalError(
212 "not enough memory to copy -m argument");
213 strcpy(module, _PyOS_optarg);
214 break;
215 }
216
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 switch (c) {
218
219 case 'd':
220 Py_DebugFlag++;
221 break;
222
Guido van Rossum61c345f2001-09-04 03:26:15 +0000223 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000224 if (strcmp(_PyOS_optarg, "old") == 0) {
225 Py_DivisionWarningFlag = 0;
226 break;
227 }
228 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000229 Py_DivisionWarningFlag = 1;
230 break;
231 }
232 if (strcmp(_PyOS_optarg, "warnall") == 0) {
233 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000234 break;
235 }
236 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000237 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000238 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000239 /* And this tells the eval loop to treat
240 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
241 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000242 break;
243 }
244 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000245 "-Q option should be `-Qold', "
246 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000247 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000248 /* NOTREACHED */
249
Guido van Rossum667d7041995-08-04 04:20:48 +0000250 case 'i':
251 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000252 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000253 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000254 break;
255
Guido van Rossum7614da61997-03-03 19:14:45 +0000256 case 'O':
257 Py_OptimizeFlag++;
258 break;
259
Guido van Rossum7922bd71997-08-29 22:34:47 +0000260 case 'S':
261 Py_NoSiteFlag++;
262 break;
263
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000264 case 'E':
265 Py_IgnoreEnvironmentFlag++;
266 break;
267
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000268 case 't':
269 Py_TabcheckFlag++;
270 break;
271
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 case 'u':
273 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000274 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000275 break;
276
277 case 'v':
278 Py_VerboseFlag++;
279 break;
280
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000281#ifdef RISCOS
282 case 'w':
283 Py_RISCOSWimpFlag = 1;
284 break;
285#endif
286
Guido van Rossuma075ce11997-12-05 21:56:45 +0000287 case 'x':
288 skipfirstline = 1;
289 break;
290
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000291 case 'U':
292 Py_UnicodeFlag++;
293 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000294 case 'h':
295 help++;
296 break;
297 case 'V':
298 version++;
299 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000300
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000301 case 'W':
302 PySys_AddWarnOption(_PyOS_optarg);
303 break;
304
Guido van Rossum667d7041995-08-04 04:20:48 +0000305 /* This space reserved for other options */
306
307 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000308 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000309 /*NOTREACHED*/
310
311 }
312 }
313
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000314 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000315 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000316
317 if (version) {
318 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000319 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000320 }
321
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000322 if (!saw_inspect_flag &&
323 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
324 inspect = 1;
325 if (!saw_unbuffered_flag &&
326 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
327 unbuffered = 1;
328
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000329 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000330 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000331 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000332#ifdef __VMS
333 filename = decc$translate_vms(argv[_PyOS_optind]);
334 if (filename == (char *)0 || filename == (char *)-1)
335 filename = argv[_PyOS_optind];
336
337#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000338 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000339#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000340 if (filename != NULL) {
341 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000342#ifdef HAVE_STRERROR
343 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
344 argv[0], filename, errno, strerror(errno));
345#else
346 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
347 argv[0], filename, errno);
348#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000349 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000350 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000351 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000352 int ch;
353 /* Push back first newline so line numbers
354 remain the same */
355 while ((ch = getc(fp)) != EOF) {
356 if (ch == '\n') {
357 (void)ungetc(ch, fp);
358 break;
359 }
360 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000361 }
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) {
435 /* Backup _PyOS_optind and find the real file */
436 struct filedescr *fdescr = NULL;
437 _PyOS_optind--;
438 if ((fdescr = FindModule(module, &fp, &filename))) {
439 argv[_PyOS_optind] = filename;
440 } else {
441 fprintf(stderr, "%s: module %s not found\n",
442 argv[0], module);
443 return 2;
444 }
445 if (!fp) {
446 fprintf(stderr,
447 "%s: module %s has no associated file\n",
448 argv[0], module);
449 return 2;
450 }
451 if (!_PyImport_IsScript(fdescr)) {
452 fprintf(stderr,
453 "%s: module %s not usable as script\n (%s)\n",
454 argv[0], module, filename);
455 return 2;
456 }
457 }
458
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000459 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000460
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000461 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000462 isatty(fileno(stdin))) {
463 PyObject *v;
464 v = PyImport_ImportModule("readline");
465 if (v == NULL)
466 PyErr_Clear();
467 else
468 Py_DECREF(v);
469 }
470
Guido van Rossum667d7041995-08-04 04:20:48 +0000471 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000472 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000473 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000474 } else if (module) {
475 sts = PyRun_AnyFileExFlags(fp, filename, 1, &cf) != 0;
476 free(module);
477 free(filename);
Guido van Rossum667d7041995-08-04 04:20:48 +0000478 }
479 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000480 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000481 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000482 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000483 /* XXX */
484 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000485 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000486 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000487 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000488 }
489
Barry Warsawd86dcd32003-06-29 17:07:06 +0000490 /* Check this environment variable at the end, to give programs the
491 * opportunity to set it from Python.
492 */
493 if (!saw_inspect_flag &&
494 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
495 {
496 inspect = 1;
497 }
498
Guido van Rossum775af911997-02-14 19:50:32 +0000499 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000500 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000501 /* XXX */
502 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000503
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000504 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000505#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000506 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000507 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
508#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000509
510#ifdef __INSURE__
511 /* Insure++ is a memory analysis tool that aids in discovering
512 * memory leaks and other memory problems. On Python exit, the
513 * interned string dictionary is flagged as being in use at exit
514 * (which it is). Under normal circumstances, this is fine because
515 * the memory will be automatically reclaimed by the system. Under
516 * memory debugging, it's a huge source of useless noise, so we
517 * trade off slower shutdown for less distraction in the memory
518 * reports. -baw
519 */
520 _Py_ReleaseInternedStrings();
521#endif /* __INSURE__ */
522
Guido van Rossum05f7c501997-08-02 03:00:42 +0000523 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000524}
525
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000526/* this is gonna seem *real weird*, but if you put some other code between
527 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
528 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000529
Guido van Rossum667d7041995-08-04 04:20:48 +0000530/* Make the *original* argc/argv available to other modules.
531 This is rare, but it is needed by the secureware extension. */
532
533void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000534Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000535{
536 *argc = orig_argc;
537 *argv = orig_argv;
538}