blob: c8298fbf768333f94e630bb57595cb280d1cff81 [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
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000133/* Get the path to a top-level module */
134static struct filedescr * FindModule(const char *module,
135 FILE **fp, char **filename)
136{
137 struct filedescr *fdescr = NULL;
138 *fp = NULL;
139 *filename = malloc(MAXPATHLEN);
140
141 if (*filename == NULL)
142 return NULL;
143
144 /* Find the actual module source code */
145 fdescr = _PyImport_FindModule(module, NULL,
146 *filename, MAXPATHLEN, fp, NULL);
147
148 if (fdescr == NULL) {
149 free(*filename);
150 *filename = NULL;
151 }
152
153 return fdescr;
154}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000155
Guido van Rossum667d7041995-08-04 04:20:48 +0000156/* Main program */
157
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000158int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000159Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000160{
161 int c;
162 int sts;
163 char *command = NULL;
164 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000165 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000166 FILE *fp = stdin;
167 char *p;
168 int inspect = 0;
169 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000170 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000171 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000172 int help = 0;
173 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000174 int saw_inspect_flag = 0;
175 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000176 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000177
Guido van Rossum393661d2001-08-31 17:40:15 +0000178 cf.cf_flags = 0;
179
Guido van Rossumac56b031996-07-21 02:33:38 +0000180 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000181 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000182
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000183#ifdef RISCOS
184 Py_RISCOSWimpFlag = 0;
185#endif
186
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000187 PySys_ResetWarnOptions();
188
Guido van Rossumbceccf52001-04-10 22:07:43 +0000189 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000190 if (c == 'c') {
191 /* -c is the last option; following arguments
192 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000193 command to interpret. */
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000194 command = malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000195 if (command == NULL)
196 Py_FatalError(
197 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000198 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000199 strcat(command, "\n");
200 break;
201 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000202
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000203 if (c == 'm') {
204 /* -m is the last option; following arguments
205 that look like options are left for the
206 module to interpret. */
207 module = malloc(strlen(_PyOS_optarg) + 2);
208 if (module == NULL)
209 Py_FatalError(
210 "not enough memory to copy -m argument");
211 strcpy(module, _PyOS_optarg);
212 break;
213 }
214
Guido van Rossum667d7041995-08-04 04:20:48 +0000215 switch (c) {
216
217 case 'd':
218 Py_DebugFlag++;
219 break;
220
221 case 'i':
222 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000223 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000224 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000225 break;
226
Guido van Rossum7614da61997-03-03 19:14:45 +0000227 case 'O':
228 Py_OptimizeFlag++;
229 break;
230
Guido van Rossum7922bd71997-08-29 22:34:47 +0000231 case 'S':
232 Py_NoSiteFlag++;
233 break;
234
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000235 case 'E':
236 Py_IgnoreEnvironmentFlag++;
237 break;
238
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000239 case 't':
240 Py_TabcheckFlag++;
241 break;
242
Guido van Rossum667d7041995-08-04 04:20:48 +0000243 case 'u':
244 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000245 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000246 break;
247
248 case 'v':
249 Py_VerboseFlag++;
250 break;
251
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000252#ifdef RISCOS
253 case 'w':
254 Py_RISCOSWimpFlag = 1;
255 break;
256#endif
257
Guido van Rossuma075ce11997-12-05 21:56:45 +0000258 case 'x':
259 skipfirstline = 1;
260 break;
261
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000262 case 'h':
263 help++;
264 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000265
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000266 case 'V':
267 version++;
268 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000269
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000270 case 'W':
271 PySys_AddWarnOption(_PyOS_optarg);
272 break;
273
Guido van Rossum667d7041995-08-04 04:20:48 +0000274 /* This space reserved for other options */
275
276 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000277 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000278 /*NOTREACHED*/
279
280 }
281 }
282
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000283 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000284 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000285
286 if (version) {
287 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000288 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000289 }
290
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000291 if (!saw_inspect_flag &&
292 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
293 inspect = 1;
294 if (!saw_unbuffered_flag &&
295 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
296 unbuffered = 1;
297
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000298 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000299 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000300 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000301#ifdef __VMS
302 filename = decc$translate_vms(argv[_PyOS_optind]);
303 if (filename == (char *)0 || filename == (char *)-1)
304 filename = argv[_PyOS_optind];
305
306#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000307 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000308#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000309 if (filename != NULL) {
310 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000311#ifdef HAVE_STRERROR
312 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
313 argv[0], filename, errno, strerror(errno));
314#else
315 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
316 argv[0], filename, errno);
317#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000318 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000319 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000320 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000321 int ch;
322 /* Push back first newline so line numbers
323 remain the same */
324 while ((ch = getc(fp)) != EOF) {
325 if (ch == '\n') {
326 (void)ungetc(ch, fp);
327 break;
328 }
329 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000330 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000331 {
332 /* XXX: does this work on Win/Win64? (see posix_fstat) */
333 struct stat sb;
334 if (fstat(fileno(fp), &sb) == 0 &&
335 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000336 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
337 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000338 }
339 }
Guido van Rossum775af911997-02-14 19:50:32 +0000340 }
341 }
342
343 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
344
Guido van Rossum667d7041995-08-04 04:20:48 +0000345 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000346#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000347 _setmode(fileno(stdin), O_BINARY);
348 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000349#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000350#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000351 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
352 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
353 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000354#else /* !HAVE_SETVBUF */
355 setbuf(stdin, (char *)NULL);
356 setbuf(stdout, (char *)NULL);
357 setbuf(stderr, (char *)NULL);
358#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000359 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000360 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000361#ifdef MS_WINDOWS
362 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000363 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000364 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000365#else /* !MS_WINDOWS */
366#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000367 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
368 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000369#endif /* HAVE_SETVBUF */
370#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000371 /* Leave stderr alone - it should be unbuffered anyway. */
372 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000373#ifdef __VMS
374 else {
375 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
376 }
377#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000378
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000379#ifdef __APPLE__
380 /* On MacOS X, when the Python interpreter is embedded in an
381 application bundle, it gets executed by a bootstrapping script
382 that does os.execve() with an argv[0] that's different from the
383 actual Python executable. This is needed to keep the Finder happy,
384 or rather, to work around Apple's overly strict requirements of
385 the process name. However, we still need a usable sys.executable,
386 so the actual executable path is passed in an environment variable.
387 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
388 script. */
389 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
390 Py_SetProgramName(p);
391 else
392 Py_SetProgramName(argv[0]);
393#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000394 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000395#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000396 Py_Initialize();
397
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000399 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000400 fprintf(stderr, "Python %s on %s\n",
401 Py_GetVersion(), Py_GetPlatform());
402 if (!Py_NoSiteFlag)
403 fprintf(stderr, "%s\n", COPYRIGHT);
404 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000405
Guido van Rossum667d7041995-08-04 04:20:48 +0000406 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000407 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
408 _PyOS_optind--;
409 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000410 }
411
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000412 if (module != NULL) {
413 /* Backup _PyOS_optind and find the real file */
414 struct filedescr *fdescr = NULL;
415 _PyOS_optind--;
416 if ((fdescr = FindModule(module, &fp, &filename))) {
417 argv[_PyOS_optind] = filename;
418 } else {
419 fprintf(stderr, "%s: module %s not found\n",
420 argv[0], module);
421 return 2;
422 }
423 if (!fp) {
424 fprintf(stderr,
425 "%s: module %s has no associated file\n",
426 argv[0], module);
427 return 2;
428 }
429 if (!_PyImport_IsScript(fdescr)) {
430 fprintf(stderr,
431 "%s: module %s not usable as script\n (%s)\n",
432 argv[0], module, filename);
433 return 2;
434 }
435 }
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) {
453 sts = PyRun_AnyFileExFlags(fp, filename, 1, &cf) != 0;
454 free(module);
455 free(filename);
Guido van Rossum667d7041995-08-04 04:20:48 +0000456 }
457 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000458 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000459 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000460 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000461 /* XXX */
462 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000463 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000464 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000465 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000466 }
467
Barry Warsawd86dcd32003-06-29 17:07:06 +0000468 /* Check this environment variable at the end, to give programs the
469 * opportunity to set it from Python.
470 */
471 if (!saw_inspect_flag &&
472 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
473 {
474 inspect = 1;
475 }
476
Guido van Rossum775af911997-02-14 19:50:32 +0000477 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000478 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000479 /* XXX */
480 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000481
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000482 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000483#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000484 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000485 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
486#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000487
488#ifdef __INSURE__
489 /* Insure++ is a memory analysis tool that aids in discovering
490 * memory leaks and other memory problems. On Python exit, the
491 * interned string dictionary is flagged as being in use at exit
492 * (which it is). Under normal circumstances, this is fine because
493 * the memory will be automatically reclaimed by the system. Under
494 * memory debugging, it's a huge source of useless noise, so we
495 * trade off slower shutdown for less distraction in the memory
496 * reports. -baw
497 */
498 _Py_ReleaseInternedStrings();
499#endif /* __INSURE__ */
500
Guido van Rossum05f7c501997-08-02 03:00:42 +0000501 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000502}
503
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000504/* this is gonna seem *real weird*, but if you put some other code between
505 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
506 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000507
Guido van Rossum667d7041995-08-04 04:20:48 +0000508/* Make the *original* argc/argv available to other modules.
509 This is rare, but it is needed by the secureware extension. */
510
511void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000512Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000513{
514 *argc = orig_argc;
515 *argv = orig_argv;
516}