blob: b3ce16e3e25bd29e39af23266b104122266ee274 [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__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000012#include <fcntl.h>
13#endif
14
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000015#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000016#define PYTHONHOMEHELP "<prefix>\\lib"
17#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#if defined(PYOS_OS2) && defined(PYCC_GCC)
19#define PYTHONHOMEHELP "<prefix>/Lib"
20#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000021#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000022#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000023#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000024
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000025#include "pygetopt.h"
26
Guido van Rossuma22865e2000-09-05 04:41:18 +000027#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000028 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
29 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000030
Guido van Rossumac56b031996-07-21 02:33:38 +000031/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000032static char **orig_argv;
33static int orig_argc;
34
Guido van Rossumbceccf52001-04-10 22:07:43 +000035/* command line options */
Guido van Rossum45aecf42006-03-15 04:58:47 +000036#define BASE_OPTS "c:dEhim:OStuvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000037
38#ifndef RISCOS
39#define PROGRAM_OPTS BASE_OPTS
40#else /*RISCOS*/
41/* extra option saying that we are running under a special task window
42 frontend; especially my_readline will behave different */
43#define PROGRAM_OPTS BASE_OPTS "w"
44/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000045extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000046#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000047
Guido van Rossum667d7041995-08-04 04:20:48 +000048/* Short usage message (with %s for argv0) */
49static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000050"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000051
52/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000053static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000054Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000055-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000056-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000057-E : ignore environment variables (such as PYTHONPATH)\n\
58-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000059-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000060 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000061";
62static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000063-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000064-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000065-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000066-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000067-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000068-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000069";
70static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000071 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000073-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000074-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000076file : program read from script file\n\
77- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000078";
Guido van Rossum393661d2001-08-31 17:40:15 +000079static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000080arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000081Other environment variables:\n\
82PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000083PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000084 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000085PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
86 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000087PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088";
89
90
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000091static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000092usage(int exitcode, char* program)
93{
Guido van Rossum393661d2001-08-31 17:40:15 +000094 FILE *f = exitcode ? stderr : stdout;
95
96 fprintf(f, usage_line, program);
97 if (exitcode)
98 fprintf(f, "Try `python -h' for more information.\n");
99 else {
100 fprintf(f, usage_1);
101 fprintf(f, usage_2);
102 fprintf(f, usage_3);
103 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
104 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000105#if defined(__VMS)
106 if (exitcode == 0) {
107 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000108 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000109 }
110 else {
111 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000112 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000113 }
114#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000115 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000116#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000117 /*NOTREACHED*/
118}
119
Martin v. Löwis6caea372003-11-18 19:46:25 +0000120static void RunStartupFile(PyCompilerFlags *cf)
121{
122 char *startup = Py_GETENV("PYTHONSTARTUP");
123 if (startup != NULL && startup[0] != '\0') {
124 FILE *fp = fopen(startup, "r");
125 if (fp != NULL) {
126 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
127 PyErr_Clear();
128 fclose(fp);
129 }
130 }
131}
132
Thomas Woutersa9773292006-04-21 09:43:23 +0000133
134static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000135{
Thomas Woutersa9773292006-04-21 09:43:23 +0000136 PyObject *runpy, *runmodule, *runargs, *result;
137 runpy = PyImport_ImportModule("runpy");
138 if (runpy == NULL) {
139 fprintf(stderr, "Could not import runpy module\n");
140 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000141 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000142 runmodule = PyObject_GetAttrString(runpy, "run_module");
143 if (runmodule == NULL) {
144 fprintf(stderr, "Could not access runpy.run_module\n");
145 Py_DECREF(runpy);
146 return -1;
147 }
148 runargs = Py_BuildValue("sOsO", module,
149 Py_None, "__main__", Py_True);
150 if (runargs == NULL) {
151 fprintf(stderr,
152 "Could not create arguments for runpy.run_module\n");
153 Py_DECREF(runpy);
154 Py_DECREF(runmodule);
155 return -1;
156 }
157 result = PyObject_Call(runmodule, runargs, NULL);
158 if (result == NULL) {
159 PyErr_Print();
160 }
161 Py_DECREF(runpy);
162 Py_DECREF(runmodule);
163 Py_DECREF(runargs);
164 if (result == NULL) {
165 return -1;
166 }
167 Py_DECREF(result);
168 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000169}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000170
Guido van Rossum667d7041995-08-04 04:20:48 +0000171/* Main program */
172
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000173int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000174Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000175{
176 int c;
177 int sts;
178 char *command = NULL;
179 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000180 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000181 FILE *fp = stdin;
182 char *p;
183 int inspect = 0;
184 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000185 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000186 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000187 int help = 0;
188 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000189 int saw_inspect_flag = 0;
190 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000191 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000192
Guido van Rossum393661d2001-08-31 17:40:15 +0000193 cf.cf_flags = 0;
194
Guido van Rossumac56b031996-07-21 02:33:38 +0000195 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000196 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000197
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000198#ifdef RISCOS
199 Py_RISCOSWimpFlag = 0;
200#endif
201
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000202 PySys_ResetWarnOptions();
203
Guido van Rossumbceccf52001-04-10 22:07:43 +0000204 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000205 if (c == 'c') {
206 /* -c is the last option; following arguments
207 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000208 command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000209 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000210 if (command == NULL)
211 Py_FatalError(
212 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000213 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000214 strcat(command, "\n");
215 break;
216 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000217
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000218 if (c == 'm') {
219 /* -m is the last option; following arguments
220 that look like options are left for the
221 module to interpret. */
222 module = malloc(strlen(_PyOS_optarg) + 2);
223 if (module == NULL)
224 Py_FatalError(
225 "not enough memory to copy -m argument");
226 strcpy(module, _PyOS_optarg);
227 break;
228 }
229
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 switch (c) {
231
232 case 'd':
233 Py_DebugFlag++;
234 break;
235
236 case 'i':
237 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000238 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000239 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000240 break;
241
Guido van Rossum7614da61997-03-03 19:14:45 +0000242 case 'O':
243 Py_OptimizeFlag++;
244 break;
245
Guido van Rossum7922bd71997-08-29 22:34:47 +0000246 case 'S':
247 Py_NoSiteFlag++;
248 break;
249
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000250 case 'E':
251 Py_IgnoreEnvironmentFlag++;
252 break;
253
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000254 case 't':
255 Py_TabcheckFlag++;
256 break;
257
Guido van Rossum667d7041995-08-04 04:20:48 +0000258 case 'u':
259 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000260 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000261 break;
262
263 case 'v':
264 Py_VerboseFlag++;
265 break;
266
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000267#ifdef RISCOS
268 case 'w':
269 Py_RISCOSWimpFlag = 1;
270 break;
271#endif
272
Guido van Rossuma075ce11997-12-05 21:56:45 +0000273 case 'x':
274 skipfirstline = 1;
275 break;
276
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000277 case 'h':
278 help++;
279 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000280
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000281 case 'V':
282 version++;
283 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000284
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000285 case 'W':
286 PySys_AddWarnOption(_PyOS_optarg);
287 break;
288
Guido van Rossum667d7041995-08-04 04:20:48 +0000289 /* This space reserved for other options */
290
291 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000292 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000293 /*NOTREACHED*/
294
295 }
296 }
297
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000298 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000299 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000300
301 if (version) {
302 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000303 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000304 }
305
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000306 if (!saw_inspect_flag &&
307 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
308 inspect = 1;
309 if (!saw_unbuffered_flag &&
310 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
311 unbuffered = 1;
312
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000313 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000314 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000315 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000316#ifdef __VMS
317 filename = decc$translate_vms(argv[_PyOS_optind]);
318 if (filename == (char *)0 || filename == (char *)-1)
319 filename = argv[_PyOS_optind];
320
321#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000322 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000323#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000324 if (filename != NULL) {
325 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000326#ifdef HAVE_STRERROR
327 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
328 argv[0], filename, errno, strerror(errno));
329#else
330 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
331 argv[0], filename, errno);
332#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000333 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000334 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000335 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000336 int ch;
337 /* Push back first newline so line numbers
338 remain the same */
339 while ((ch = getc(fp)) != EOF) {
340 if (ch == '\n') {
341 (void)ungetc(ch, fp);
342 break;
343 }
344 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000345 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000346 {
347 /* XXX: does this work on Win/Win64? (see posix_fstat) */
348 struct stat sb;
349 if (fstat(fileno(fp), &sb) == 0 &&
350 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000351 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
352 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000353 }
354 }
Guido van Rossum775af911997-02-14 19:50:32 +0000355 }
356 }
357
358 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
359
Guido van Rossum667d7041995-08-04 04:20:48 +0000360 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000361#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000362 _setmode(fileno(stdin), O_BINARY);
363 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000364#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000365#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000366 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
367 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
368 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000369#else /* !HAVE_SETVBUF */
370 setbuf(stdin, (char *)NULL);
371 setbuf(stdout, (char *)NULL);
372 setbuf(stderr, (char *)NULL);
373#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000374 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000375 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000376#ifdef MS_WINDOWS
377 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000378 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000379 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000380#else /* !MS_WINDOWS */
381#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000382 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
383 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000384#endif /* HAVE_SETVBUF */
385#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000386 /* Leave stderr alone - it should be unbuffered anyway. */
387 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000388#ifdef __VMS
389 else {
390 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
391 }
392#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000393
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000394#ifdef __APPLE__
395 /* On MacOS X, when the Python interpreter is embedded in an
396 application bundle, it gets executed by a bootstrapping script
397 that does os.execve() with an argv[0] that's different from the
398 actual Python executable. This is needed to keep the Finder happy,
399 or rather, to work around Apple's overly strict requirements of
400 the process name. However, we still need a usable sys.executable,
401 so the actual executable path is passed in an environment variable.
402 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
403 script. */
404 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
405 Py_SetProgramName(p);
406 else
407 Py_SetProgramName(argv[0]);
408#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000409 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000410#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000411 Py_Initialize();
412
Guido van Rossum667d7041995-08-04 04:20:48 +0000413 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000414 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000415 fprintf(stderr, "Python %s on %s\n",
416 Py_GetVersion(), Py_GetPlatform());
417 if (!Py_NoSiteFlag)
418 fprintf(stderr, "%s\n", COPYRIGHT);
419 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000420
Guido van Rossum667d7041995-08-04 04:20:48 +0000421 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000422 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
423 _PyOS_optind--;
424 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000425 }
426
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000427 if (module != NULL) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000428 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000429 _PyOS_optind--;
Thomas Woutersa9773292006-04-21 09:43:23 +0000430 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000431 }
432
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000433 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000434
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000435 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000436 isatty(fileno(stdin))) {
437 PyObject *v;
438 v = PyImport_ImportModule("readline");
439 if (v == NULL)
440 PyErr_Clear();
441 else
442 Py_DECREF(v);
443 }
444
Guido van Rossum667d7041995-08-04 04:20:48 +0000445 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000446 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000447 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000448 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000449 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000450 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000451 }
452 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000453 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000454 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000455 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000456 /* XXX */
457 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000458 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000459 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000460 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000461 }
462
Barry Warsawd86dcd32003-06-29 17:07:06 +0000463 /* Check this environment variable at the end, to give programs the
464 * opportunity to set it from Python.
465 */
466 if (!saw_inspect_flag &&
467 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
468 {
469 inspect = 1;
470 }
471
Guido van Rossum775af911997-02-14 19:50:32 +0000472 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000473 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000474 /* XXX */
475 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000476
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000477 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000478#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000479 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000480 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
481#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000482
483#ifdef __INSURE__
484 /* Insure++ is a memory analysis tool that aids in discovering
485 * memory leaks and other memory problems. On Python exit, the
486 * interned string dictionary is flagged as being in use at exit
487 * (which it is). Under normal circumstances, this is fine because
488 * the memory will be automatically reclaimed by the system. Under
489 * memory debugging, it's a huge source of useless noise, so we
490 * trade off slower shutdown for less distraction in the memory
491 * reports. -baw
492 */
493 _Py_ReleaseInternedStrings();
494#endif /* __INSURE__ */
495
Guido van Rossum05f7c501997-08-02 03:00:42 +0000496 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000497}
498
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000499/* this is gonna seem *real weird*, but if you put some other code between
500 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
501 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000502
Guido van Rossum667d7041995-08-04 04:20:48 +0000503/* Make the *original* argc/argv available to other modules.
504 This is rare, but it is needed by the secureware extension. */
505
506void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000507Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000508{
509 *argc = orig_argc;
510 *argv = orig_argv;
511}