blob: 7326a27c9cb6940dd2add154a7d6bf7684bfdc50 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031#ifdef __cplusplus
32extern "C" {
33#endif
34
Guido van Rossumac56b031996-07-21 02:33:38 +000035/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000036static char **orig_argv;
37static int orig_argc;
38
Guido van Rossumbceccf52001-04-10 22:07:43 +000039/* command line options */
Guido van Rossum45aecf42006-03-15 04:58:47 +000040#define BASE_OPTS "c:dEhim:OStuvVW:xX"
Guido van Rossumbceccf52001-04-10 22:07:43 +000041
42#ifndef RISCOS
43#define PROGRAM_OPTS BASE_OPTS
44#else /*RISCOS*/
45/* extra option saying that we are running under a special task window
46 frontend; especially my_readline will behave different */
47#define PROGRAM_OPTS BASE_OPTS "w"
48/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000049extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000050#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000051
Guido van Rossum667d7041995-08-04 04:20:48 +000052/* Short usage message (with %s for argv0) */
53static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000054"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000055
56/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000057static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000058Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000059-c cmd : program passed in as string (terminates option list)\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000060-d : debug output from parser (also PYTHONDEBUG=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000061-E : ignore environment variables (such as PYTHONPATH)\n\
62-h : print this help message and exit\n\
Guido van Rossum775af911997-02-14 19:50:32 +000063-i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000064 and force prompts, even if stdin does not appear to be a terminal\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000065";
66static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000067-m mod : run library module as a script (terminates option list)\n\
Guido van Rossume7adf3e1998-10-07 14:50:06 +000068-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000069-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000070-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000071-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000072-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000073";
74static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000075 see man page for details on internal buffering relating to '-u'\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000076-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000077-V : print the Python version number and exit\n\
Guido van Rossum47f5fdc2000-12-15 22:00:54 +000078-W arg : warning control (arg is action:message:category:module:lineno)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000079-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000080file : program read from script file\n\
81- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000082";
Guido van Rossum393661d2001-08-31 17:40:15 +000083static char *usage_4 = "\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000084arg ...: arguments passed to program in sys.argv[1:]\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000085Other environment variables:\n\
86PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000087PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000089PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
90 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000091PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092";
93
94
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000095static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000096usage(int exitcode, char* program)
97{
Guido van Rossum393661d2001-08-31 17:40:15 +000098 FILE *f = exitcode ? stderr : stdout;
99
100 fprintf(f, usage_line, program);
101 if (exitcode)
102 fprintf(f, "Try `python -h' for more information.\n");
103 else {
104 fprintf(f, usage_1);
105 fprintf(f, usage_2);
106 fprintf(f, usage_3);
107 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
108 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000109#if defined(__VMS)
110 if (exitcode == 0) {
111 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000112 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000113 }
114 else {
115 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000116 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000117 }
118#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000119 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000120#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000121 /*NOTREACHED*/
122}
123
Martin v. Löwis6caea372003-11-18 19:46:25 +0000124static void RunStartupFile(PyCompilerFlags *cf)
125{
126 char *startup = Py_GETENV("PYTHONSTARTUP");
127 if (startup != NULL && startup[0] != '\0') {
128 FILE *fp = fopen(startup, "r");
129 if (fp != NULL) {
130 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
131 PyErr_Clear();
132 fclose(fp);
133 }
134 }
135}
136
Thomas Woutersa9773292006-04-21 09:43:23 +0000137
138static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000139{
Thomas Woutersa9773292006-04-21 09:43:23 +0000140 PyObject *runpy, *runmodule, *runargs, *result;
141 runpy = PyImport_ImportModule("runpy");
142 if (runpy == NULL) {
143 fprintf(stderr, "Could not import runpy module\n");
144 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000145 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000146 runmodule = PyObject_GetAttrString(runpy, "run_module");
147 if (runmodule == NULL) {
148 fprintf(stderr, "Could not access runpy.run_module\n");
149 Py_DECREF(runpy);
150 return -1;
151 }
152 runargs = Py_BuildValue("sOsO", module,
153 Py_None, "__main__", Py_True);
154 if (runargs == NULL) {
155 fprintf(stderr,
156 "Could not create arguments for runpy.run_module\n");
157 Py_DECREF(runpy);
158 Py_DECREF(runmodule);
159 return -1;
160 }
161 result = PyObject_Call(runmodule, runargs, NULL);
162 if (result == NULL) {
163 PyErr_Print();
164 }
165 Py_DECREF(runpy);
166 Py_DECREF(runmodule);
167 Py_DECREF(runargs);
168 if (result == NULL) {
169 return -1;
170 }
171 Py_DECREF(result);
172 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000173}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000174
Guido van Rossum667d7041995-08-04 04:20:48 +0000175/* Main program */
176
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000177int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000178Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000179{
180 int c;
181 int sts;
182 char *command = NULL;
183 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000184 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000185 FILE *fp = stdin;
186 char *p;
187 int inspect = 0;
188 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000189 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000190 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000191 int help = 0;
192 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000193 int saw_inspect_flag = 0;
194 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000195 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000196
Guido van Rossum393661d2001-08-31 17:40:15 +0000197 cf.cf_flags = 0;
198
Guido van Rossumac56b031996-07-21 02:33:38 +0000199 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000200 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000201
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000202#ifdef RISCOS
203 Py_RISCOSWimpFlag = 0;
204#endif
205
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000206 PySys_ResetWarnOptions();
207
Guido van Rossumbceccf52001-04-10 22:07:43 +0000208 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000209 if (c == 'c') {
210 /* -c is the last option; following arguments
211 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000212 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000213 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000214 if (command == NULL)
215 Py_FatalError(
216 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000217 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000218 strcat(command, "\n");
219 break;
220 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000221
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000222 if (c == 'm') {
223 /* -m is the last option; following arguments
224 that look like options are left for the
225 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000227 if (module == NULL)
228 Py_FatalError(
229 "not enough memory to copy -m argument");
230 strcpy(module, _PyOS_optarg);
231 break;
232 }
233
Guido van Rossum667d7041995-08-04 04:20:48 +0000234 switch (c) {
235
236 case 'd':
237 Py_DebugFlag++;
238 break;
239
240 case 'i':
241 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000242 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000243 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000244 break;
245
Guido van Rossum7614da61997-03-03 19:14:45 +0000246 case 'O':
247 Py_OptimizeFlag++;
248 break;
249
Guido van Rossum7922bd71997-08-29 22:34:47 +0000250 case 'S':
251 Py_NoSiteFlag++;
252 break;
253
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000254 case 'E':
255 Py_IgnoreEnvironmentFlag++;
256 break;
257
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000258 case 't':
259 Py_TabcheckFlag++;
260 break;
261
Guido van Rossum667d7041995-08-04 04:20:48 +0000262 case 'u':
263 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000264 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000265 break;
266
267 case 'v':
268 Py_VerboseFlag++;
269 break;
270
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000271#ifdef RISCOS
272 case 'w':
273 Py_RISCOSWimpFlag = 1;
274 break;
275#endif
276
Guido van Rossuma075ce11997-12-05 21:56:45 +0000277 case 'x':
278 skipfirstline = 1;
279 break;
280
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000281 case 'h':
282 help++;
283 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000284
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000285 case 'V':
286 version++;
287 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000288
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000289 case 'W':
290 PySys_AddWarnOption(_PyOS_optarg);
291 break;
292
Guido van Rossum667d7041995-08-04 04:20:48 +0000293 /* This space reserved for other options */
294
295 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000296 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000297 /*NOTREACHED*/
298
299 }
300 }
301
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000302 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000303 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000304
305 if (version) {
306 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000307 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000308 }
309
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000310 if (!saw_inspect_flag &&
311 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
312 inspect = 1;
313 if (!saw_unbuffered_flag &&
314 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
315 unbuffered = 1;
316
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000317 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000318 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000319 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000320#ifdef __VMS
321 filename = decc$translate_vms(argv[_PyOS_optind]);
322 if (filename == (char *)0 || filename == (char *)-1)
323 filename = argv[_PyOS_optind];
324
325#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000326 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000327#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000328 if (filename != NULL) {
329 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000330#ifdef HAVE_STRERROR
331 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
332 argv[0], filename, errno, strerror(errno));
333#else
334 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
335 argv[0], filename, errno);
336#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000337 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000338 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000339 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000340 int ch;
341 /* Push back first newline so line numbers
342 remain the same */
343 while ((ch = getc(fp)) != EOF) {
344 if (ch == '\n') {
345 (void)ungetc(ch, fp);
346 break;
347 }
348 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000349 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000350 {
351 /* XXX: does this work on Win/Win64? (see posix_fstat) */
352 struct stat sb;
353 if (fstat(fileno(fp), &sb) == 0 &&
354 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000355 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
356 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000357 }
358 }
Guido van Rossum775af911997-02-14 19:50:32 +0000359 }
360 }
361
362 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
363
Guido van Rossum667d7041995-08-04 04:20:48 +0000364 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000365#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000366 _setmode(fileno(stdin), O_BINARY);
367 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000368#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000369#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000370 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
371 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
372 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000373#else /* !HAVE_SETVBUF */
374 setbuf(stdin, (char *)NULL);
375 setbuf(stdout, (char *)NULL);
376 setbuf(stderr, (char *)NULL);
377#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000378 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000379 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000380#ifdef MS_WINDOWS
381 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000382 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000383 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000384#else /* !MS_WINDOWS */
385#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000386 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
387 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000388#endif /* HAVE_SETVBUF */
389#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000390 /* Leave stderr alone - it should be unbuffered anyway. */
391 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000392#ifdef __VMS
393 else {
394 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
395 }
396#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000397
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000398#ifdef __APPLE__
399 /* On MacOS X, when the Python interpreter is embedded in an
400 application bundle, it gets executed by a bootstrapping script
401 that does os.execve() with an argv[0] that's different from the
402 actual Python executable. This is needed to keep the Finder happy,
403 or rather, to work around Apple's overly strict requirements of
404 the process name. However, we still need a usable sys.executable,
405 so the actual executable path is passed in an environment variable.
406 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
407 script. */
408 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
409 Py_SetProgramName(p);
410 else
411 Py_SetProgramName(argv[0]);
412#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000413 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000414#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000415 Py_Initialize();
416
Guido van Rossum667d7041995-08-04 04:20:48 +0000417 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000418 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000419 fprintf(stderr, "Python %s on %s\n",
420 Py_GetVersion(), Py_GetPlatform());
421 if (!Py_NoSiteFlag)
422 fprintf(stderr, "%s\n", COPYRIGHT);
423 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000424
Guido van Rossum667d7041995-08-04 04:20:48 +0000425 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000426 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
427 _PyOS_optind--;
428 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000429 }
430
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000431 if (module != NULL) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000432 /* Backup _PyOS_optind and force sys.arv[0] = module */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000433 _PyOS_optind--;
Thomas Woutersa9773292006-04-21 09:43:23 +0000434 argv[_PyOS_optind] = module;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000435 }
436
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000437 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000438
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000439 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000440 isatty(fileno(stdin))) {
441 PyObject *v;
442 v = PyImport_ImportModule("readline");
443 if (v == NULL)
444 PyErr_Clear();
445 else
446 Py_DECREF(v);
447 }
448
Guido van Rossum667d7041995-08-04 04:20:48 +0000449 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000450 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000451 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000452 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000453 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000454 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000455 }
456 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000457 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000458 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000459 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000460 /* XXX */
461 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000462 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000463 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000464 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000465 }
466
Barry Warsawd86dcd32003-06-29 17:07:06 +0000467 /* Check this environment variable at the end, to give programs the
468 * opportunity to set it from Python.
469 */
470 if (!saw_inspect_flag &&
471 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
472 {
473 inspect = 1;
474 }
475
Guido van Rossum775af911997-02-14 19:50:32 +0000476 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000477 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000478 /* XXX */
479 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000480
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000481 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000482#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000483 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000484 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
485#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000486
487#ifdef __INSURE__
488 /* Insure++ is a memory analysis tool that aids in discovering
489 * memory leaks and other memory problems. On Python exit, the
490 * interned string dictionary is flagged as being in use at exit
491 * (which it is). Under normal circumstances, this is fine because
492 * the memory will be automatically reclaimed by the system. Under
493 * memory debugging, it's a huge source of useless noise, so we
494 * trade off slower shutdown for less distraction in the memory
495 * reports. -baw
496 */
497 _Py_ReleaseInternedStrings();
498#endif /* __INSURE__ */
499
Guido van Rossum05f7c501997-08-02 03:00:42 +0000500 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000501}
502
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000503/* this is gonna seem *real weird*, but if you put some other code between
504 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
505 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000506
Guido van Rossum667d7041995-08-04 04:20:48 +0000507/* Make the *original* argc/argv available to other modules.
508 This is rare, but it is needed by the secureware extension. */
509
510void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000511Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000512{
513 *argc = orig_argc;
514 *argv = orig_argv;
515}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000516
517#ifdef __cplusplus
518}
519#endif
520