blob: ac6b38dccd09382df55833d50c7728e3093e24e1 [file] [log] [blame]
Guido van Rossum667d7041995-08-04 04:20:48 +00001/* Python interpreter main program */
2
3#include "Python.h"
Guido van Rossuma075ce11997-12-05 21:56:45 +00004#include "osdefs.h"
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00005#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00006
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00007#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00008#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#endif
10
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000011#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Thomas Wouters477c8d52006-05-27 19:21:47 +000012#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000013#include <fcntl.h>
14#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000017#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000018#define PYTHONHOMEHELP "<prefix>\\lib"
19#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000020#if defined(PYOS_OS2) && defined(PYCC_GCC)
21#define PYTHONHOMEHELP "<prefix>/Lib"
22#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000023#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000024#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000026
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000027#include "pygetopt.h"
28
Guido van Rossuma22865e2000-09-05 04:41:18 +000029#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000030 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
31 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000032
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Guido van Rossumac56b031996-07-21 02:33:38 +000037/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000038static char **orig_argv;
39static int orig_argc;
40
Guido van Rossumbceccf52001-04-10 22:07:43 +000041/* command line options */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042#define BASE_OPTS "c:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000043
44#ifndef RISCOS
45#define PROGRAM_OPTS BASE_OPTS
46#else /*RISCOS*/
47/* extra option saying that we are running under a special task window
48 frontend; especially my_readline will behave different */
49#define PROGRAM_OPTS BASE_OPTS "w"
50/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000051extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000052#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000053
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000056"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000059static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000060Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000061-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000062-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000063-E : ignore environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064-h : print this help message and exit (also --help)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000065-i : inspect interactively after running script; forces a prompt even\n\
66 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000067";
68static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000069-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000070-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000071-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000072-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000073-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000074-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075";
76static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000077 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000078-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
79 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000081-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000082-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000083file : program read from script file\n\
84- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000085";
Guido van Rossum393661d2001-08-31 17:40:15 +000086static char *usage_4 = "\
Thomas Wouters89f507f2006-12-13 04:49:30 +000087arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000088Other environment variables:\n\
89PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000090PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000091 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000092PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
93 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000094PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000095";
96
97
Martin v. Löwis852ba7e2003-03-30 17:09:58 +000098static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +000099usage(int exitcode, char* program)
100{
Guido van Rossum393661d2001-08-31 17:40:15 +0000101 FILE *f = exitcode ? stderr : stdout;
102
103 fprintf(f, usage_line, program);
104 if (exitcode)
105 fprintf(f, "Try `python -h' for more information.\n");
106 else {
107 fprintf(f, usage_1);
108 fprintf(f, usage_2);
109 fprintf(f, usage_3);
110 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
111 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000112#if defined(__VMS)
113 if (exitcode == 0) {
114 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000115 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000116 }
117 else {
118 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000119 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000120 }
121#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000123#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000124 /*NOTREACHED*/
125}
126
Martin v. Löwis6caea372003-11-18 19:46:25 +0000127static void RunStartupFile(PyCompilerFlags *cf)
128{
129 char *startup = Py_GETENV("PYTHONSTARTUP");
130 if (startup != NULL && startup[0] != '\0') {
131 FILE *fp = fopen(startup, "r");
132 if (fp != NULL) {
133 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
134 PyErr_Clear();
135 fclose(fp);
136 }
137 }
138}
139
Thomas Woutersa9773292006-04-21 09:43:23 +0000140
141static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000142{
Thomas Woutersa9773292006-04-21 09:43:23 +0000143 PyObject *runpy, *runmodule, *runargs, *result;
144 runpy = PyImport_ImportModule("runpy");
145 if (runpy == NULL) {
146 fprintf(stderr, "Could not import runpy module\n");
147 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000148 }
Thomas Woutersa9773292006-04-21 09:43:23 +0000149 runmodule = PyObject_GetAttrString(runpy, "run_module");
150 if (runmodule == NULL) {
151 fprintf(stderr, "Could not access runpy.run_module\n");
152 Py_DECREF(runpy);
153 return -1;
154 }
155 runargs = Py_BuildValue("sOsO", module,
156 Py_None, "__main__", Py_True);
157 if (runargs == NULL) {
158 fprintf(stderr,
159 "Could not create arguments for runpy.run_module\n");
160 Py_DECREF(runpy);
161 Py_DECREF(runmodule);
162 return -1;
163 }
164 result = PyObject_Call(runmodule, runargs, NULL);
165 if (result == NULL) {
166 PyErr_Print();
167 }
168 Py_DECREF(runpy);
169 Py_DECREF(runmodule);
170 Py_DECREF(runargs);
171 if (result == NULL) {
172 return -1;
173 }
174 Py_DECREF(result);
175 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000176}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000177
Guido van Rossum667d7041995-08-04 04:20:48 +0000178/* Main program */
179
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000180int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000181Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000182{
183 int c;
184 int sts;
185 char *command = NULL;
186 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000187 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000188 FILE *fp = stdin;
189 char *p;
190 int inspect = 0;
191 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000192 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000193 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000194 int help = 0;
195 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000196 int saw_inspect_flag = 0;
197 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000198 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000199
Guido van Rossum393661d2001-08-31 17:40:15 +0000200 cf.cf_flags = 0;
201
Guido van Rossumac56b031996-07-21 02:33:38 +0000202 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000203 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000204
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000205#ifdef RISCOS
206 Py_RISCOSWimpFlag = 0;
207#endif
208
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000209 PySys_ResetWarnOptions();
210
Guido van Rossumbceccf52001-04-10 22:07:43 +0000211 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000212 if (c == 'c') {
213 /* -c is the last option; following arguments
214 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000215 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 if (command == NULL)
218 Py_FatalError(
219 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000220 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000221 strcat(command, "\n");
222 break;
223 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000224
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000225 if (c == 'm') {
226 /* -m is the last option; following arguments
227 that look like options are left for the
228 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000230 if (module == NULL)
231 Py_FatalError(
232 "not enough memory to copy -m argument");
233 strcpy(module, _PyOS_optarg);
234 break;
235 }
236
Guido van Rossum667d7041995-08-04 04:20:48 +0000237 switch (c) {
238
239 case 'd':
240 Py_DebugFlag++;
241 break;
242
243 case 'i':
244 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000245 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000246 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000247 break;
248
Guido van Rossum7614da61997-03-03 19:14:45 +0000249 case 'O':
250 Py_OptimizeFlag++;
251 break;
252
Guido van Rossum7922bd71997-08-29 22:34:47 +0000253 case 'S':
254 Py_NoSiteFlag++;
255 break;
256
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000257 case 'E':
258 Py_IgnoreEnvironmentFlag++;
259 break;
260
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000261 case 't':
262 Py_TabcheckFlag++;
263 break;
264
Guido van Rossum667d7041995-08-04 04:20:48 +0000265 case 'u':
266 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000267 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000268 break;
269
270 case 'v':
271 Py_VerboseFlag++;
272 break;
273
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000274#ifdef RISCOS
275 case 'w':
276 Py_RISCOSWimpFlag = 1;
277 break;
278#endif
279
Guido van Rossuma075ce11997-12-05 21:56:45 +0000280 case 'x':
281 skipfirstline = 1;
282 break;
283
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000284 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000286 help++;
287 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000288
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000289 case 'V':
290 version++;
291 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000292
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000293 case 'W':
294 PySys_AddWarnOption(_PyOS_optarg);
295 break;
296
Guido van Rossum667d7041995-08-04 04:20:48 +0000297 /* This space reserved for other options */
298
299 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000300 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000301 /*NOTREACHED*/
302
303 }
304 }
305
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000306 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000307 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000308
309 if (version) {
310 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000311 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000312 }
313
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000314 if (!saw_inspect_flag &&
315 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
316 inspect = 1;
317 if (!saw_unbuffered_flag &&
318 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
319 unbuffered = 1;
320
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000321 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000322 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000323 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000324#ifdef __VMS
325 filename = decc$translate_vms(argv[_PyOS_optind]);
326 if (filename == (char *)0 || filename == (char *)-1)
327 filename = argv[_PyOS_optind];
328
329#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000330 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000331#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000332 if (filename != NULL) {
333 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000334#ifdef HAVE_STRERROR
335 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
336 argv[0], filename, errno, strerror(errno));
337#else
338 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
339 argv[0], filename, errno);
340#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000341 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000342 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000343 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000344 int ch;
345 /* Push back first newline so line numbers
346 remain the same */
347 while ((ch = getc(fp)) != EOF) {
348 if (ch == '\n') {
349 (void)ungetc(ch, fp);
350 break;
351 }
352 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000353 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000354 {
355 /* XXX: does this work on Win/Win64? (see posix_fstat) */
356 struct stat sb;
357 if (fstat(fileno(fp), &sb) == 0 &&
358 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000359 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
360 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000361 }
362 }
Guido van Rossum775af911997-02-14 19:50:32 +0000363 }
364 }
365
366 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
367
Guido van Rossum667d7041995-08-04 04:20:48 +0000368 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000369#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000370 _setmode(fileno(stdin), O_BINARY);
371 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000372#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000373#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000374 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
375 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
376 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000377#else /* !HAVE_SETVBUF */
378 setbuf(stdin, (char *)NULL);
379 setbuf(stdout, (char *)NULL);
380 setbuf(stderr, (char *)NULL);
381#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000382 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000383 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000384#ifdef MS_WINDOWS
385 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000386 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000387 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000388#else /* !MS_WINDOWS */
389#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000390 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
391 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000392#endif /* HAVE_SETVBUF */
393#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000394 /* Leave stderr alone - it should be unbuffered anyway. */
395 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000396#ifdef __VMS
397 else {
398 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
399 }
400#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000401
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000402#ifdef __APPLE__
403 /* On MacOS X, when the Python interpreter is embedded in an
404 application bundle, it gets executed by a bootstrapping script
405 that does os.execve() with an argv[0] that's different from the
406 actual Python executable. This is needed to keep the Finder happy,
407 or rather, to work around Apple's overly strict requirements of
408 the process name. However, we still need a usable sys.executable,
409 so the actual executable path is passed in an environment variable.
410 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
411 script. */
412 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
413 Py_SetProgramName(p);
414 else
415 Py_SetProgramName(argv[0]);
416#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000417 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000418#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000419 Py_Initialize();
420
Guido van Rossum667d7041995-08-04 04:20:48 +0000421 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000422 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000423 fprintf(stderr, "Python %s on %s\n",
424 Py_GetVersion(), Py_GetPlatform());
425 if (!Py_NoSiteFlag)
426 fprintf(stderr, "%s\n", COPYRIGHT);
427 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000428
Guido van Rossum667d7041995-08-04 04:20:48 +0000429 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000430 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
431 _PyOS_optind--;
432 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000433 }
434
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000435 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
437 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000438 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000439 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000440 }
441
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000442 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000443
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000444 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000445 isatty(fileno(stdin))) {
446 PyObject *v;
447 v = PyImport_ImportModule("readline");
448 if (v == NULL)
449 PyErr_Clear();
450 else
451 Py_DECREF(v);
452 }
453
Guido van Rossum667d7041995-08-04 04:20:48 +0000454 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000455 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000456 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000457 } else if (module) {
Thomas Woutersa9773292006-04-21 09:43:23 +0000458 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000459 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000460 }
461 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000462 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000463 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000464 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000465 /* XXX */
466 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000467 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000468 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000469 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000470 }
471
Barry Warsawd86dcd32003-06-29 17:07:06 +0000472 /* Check this environment variable at the end, to give programs the
473 * opportunity to set it from Python.
474 */
475 if (!saw_inspect_flag &&
476 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
477 {
478 inspect = 1;
479 }
480
Guido van Rossum775af911997-02-14 19:50:32 +0000481 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000482 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000483 /* XXX */
484 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000485
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000486 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000487#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000488 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000489 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
490#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000491
492#ifdef __INSURE__
493 /* Insure++ is a memory analysis tool that aids in discovering
494 * memory leaks and other memory problems. On Python exit, the
495 * interned string dictionary is flagged as being in use at exit
496 * (which it is). Under normal circumstances, this is fine because
497 * the memory will be automatically reclaimed by the system. Under
498 * memory debugging, it's a huge source of useless noise, so we
499 * trade off slower shutdown for less distraction in the memory
500 * reports. -baw
501 */
502 _Py_ReleaseInternedStrings();
503#endif /* __INSURE__ */
504
Guido van Rossum05f7c501997-08-02 03:00:42 +0000505 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000506}
507
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000508/* this is gonna seem *real weird*, but if you put some other code between
509 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
510 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000511
Guido van Rossum667d7041995-08-04 04:20:48 +0000512/* Make the *original* argc/argv available to other modules.
513 This is rare, but it is needed by the secureware extension. */
514
515void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000516Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000517{
518 *argc = orig_argc;
519 *argv = orig_argv;
520}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521
522#ifdef __cplusplus
523}
524#endif
525