blob: 2224dfe2f0486c73e12bb93cf364886bc4e9085f [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"
Jeremy Hyltonec97a282005-10-21 14:58:06 +00005#include "code.h" /* For CO_FUTURE_DIVISION */
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +00006#include "import.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00007
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00008#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +00009#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000010#endif
11
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000012#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Martin v. Löwisa43190b2006-05-22 09:15:18 +000013#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000014#include <fcntl.h>
15#endif
Martin v. Löwisa43190b2006-05-22 09:15:18 +000016#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000018#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000019#define PYTHONHOMEHELP "<prefix>\\lib"
20#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000021#if defined(PYOS_OS2) && defined(PYCC_GCC)
22#define PYTHONHOMEHELP "<prefix>/Lib"
23#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000024#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000025#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000026#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000027
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000028#include "pygetopt.h"
29
Guido van Rossuma22865e2000-09-05 04:41:18 +000030#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000031 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
32 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000033
Anthony Baxterac6bd462006-04-13 02:06:09 +000034#ifdef __cplusplus
35extern "C" {
36#endif
37
Guido van Rossumac56b031996-07-21 02:33:38 +000038/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000039static char **orig_argv;
40static int orig_argc;
41
Guido van Rossumbceccf52001-04-10 22:07:43 +000042/* command line options */
Georg Brandl9dceedb2006-07-12 15:31:17 +000043#define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000044
45#ifndef RISCOS
46#define PROGRAM_OPTS BASE_OPTS
47#else /*RISCOS*/
48/* extra option saying that we are running under a special task window
49 frontend; especially my_readline will behave different */
50#define PROGRAM_OPTS BASE_OPTS "w"
51/* corresponding flag */
Guido van Rossum3ed4c152001-03-02 06:18:03 +000052extern int Py_RISCOSWimpFlag;
Guido van Rossumbceccf52001-04-10 22:07:43 +000053#endif /*RISCOS*/
Guido van Rossum3ed4c152001-03-02 06:18:03 +000054
Guido van Rossum667d7041995-08-04 04:20:48 +000055/* Short usage message (with %s for argv0) */
56static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000057"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000058
59/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000060static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000061Options and arguments (and corresponding environment variables):\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000062-c cmd : program passed in as string (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000063-d : debug output from parser; also PYTHONDEBUG=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000064-E : ignore environment variables (such as PYTHONPATH)\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000065-h : print this help message and exit (also --help)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000066-i : inspect interactively after running script; forces a prompt even\n\
67 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000070-m mod : run library module as a script (terminates option list)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000071-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000072-OO : remove doc-strings in addition to the -O optimizations\n\
Guido van Rossum1832de42001-09-04 03:51:09 +000073-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000074-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000075-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000076-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000079 see man page for details on internal buffering relating to '-u'\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000080-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
81 can be supplied multiple times to increase verbosity\n\
Georg Brandl9dceedb2006-07-12 15:31:17 +000082-V : print the Python version number and exit (also --version)\n\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000083-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000084-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000085file : program read from script file\n\
86- : program read from stdin (default; interactive mode if a tty)\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000087";
Guido van Rossum393661d2001-08-31 17:40:15 +000088static char *usage_4 = "\
Andrew M. Kuchlinge2782bb2006-09-14 11:28:50 +000089arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000090Other environment variables:\n\
91PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000092PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000093 default module search path. The result is sys.path.\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000094PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
95 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000096PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000097";
98
99
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000100static int
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000101usage(int exitcode, char* program)
102{
Guido van Rossum393661d2001-08-31 17:40:15 +0000103 FILE *f = exitcode ? stderr : stdout;
104
105 fprintf(f, usage_line, program);
106 if (exitcode)
107 fprintf(f, "Try `python -h' for more information.\n");
108 else {
109 fprintf(f, usage_1);
110 fprintf(f, usage_2);
111 fprintf(f, usage_3);
112 fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP);
113 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000114#if defined(__VMS)
115 if (exitcode == 0) {
116 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000117 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000118 }
119 else {
120 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000121 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000122 }
123#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000124 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000125#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000126 /*NOTREACHED*/
127}
128
Martin v. Löwis6caea372003-11-18 19:46:25 +0000129static void RunStartupFile(PyCompilerFlags *cf)
130{
131 char *startup = Py_GETENV("PYTHONSTARTUP");
132 if (startup != NULL && startup[0] != '\0') {
133 FILE *fp = fopen(startup, "r");
134 if (fp != NULL) {
135 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
136 PyErr_Clear();
137 fclose(fp);
138 }
139 }
140}
141
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000142
143static int RunModule(char *module)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000144{
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000145 PyObject *runpy, *runmodule, *runargs, *result;
146 runpy = PyImport_ImportModule("runpy");
147 if (runpy == NULL) {
148 fprintf(stderr, "Could not import runpy module\n");
149 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000150 }
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000151 runmodule = PyObject_GetAttrString(runpy, "run_module");
152 if (runmodule == NULL) {
153 fprintf(stderr, "Could not access runpy.run_module\n");
154 Py_DECREF(runpy);
155 return -1;
156 }
157 runargs = Py_BuildValue("sOsO", module,
158 Py_None, "__main__", Py_True);
159 if (runargs == NULL) {
160 fprintf(stderr,
161 "Could not create arguments for runpy.run_module\n");
162 Py_DECREF(runpy);
163 Py_DECREF(runmodule);
164 return -1;
165 }
166 result = PyObject_Call(runmodule, runargs, NULL);
167 if (result == NULL) {
168 PyErr_Print();
169 }
170 Py_DECREF(runpy);
171 Py_DECREF(runmodule);
172 Py_DECREF(runargs);
173 if (result == NULL) {
174 return -1;
175 }
176 Py_DECREF(result);
177 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000178}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000179
Guido van Rossum667d7041995-08-04 04:20:48 +0000180/* Main program */
181
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000182int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000183Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000184{
185 int c;
186 int sts;
187 char *command = NULL;
188 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000189 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000190 FILE *fp = stdin;
191 char *p;
192 int inspect = 0;
193 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000194 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000195 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000196 int help = 0;
197 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000198 int saw_inspect_flag = 0;
199 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000200 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000201
Guido van Rossum393661d2001-08-31 17:40:15 +0000202 cf.cf_flags = 0;
203
Guido van Rossumac56b031996-07-21 02:33:38 +0000204 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000205 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000206
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000207#ifdef RISCOS
208 Py_RISCOSWimpFlag = 0;
209#endif
210
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000211 PySys_ResetWarnOptions();
212
Guido van Rossumbceccf52001-04-10 22:07:43 +0000213 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000214 if (c == 'c') {
215 /* -c is the last option; following arguments
216 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000217 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000218 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000219 if (command == NULL)
220 Py_FatalError(
221 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000222 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000223 strcat(command, "\n");
224 break;
225 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000226
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000227 if (c == 'm') {
228 /* -m is the last option; following arguments
229 that look like options are left for the
230 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000231 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000232 if (module == NULL)
233 Py_FatalError(
234 "not enough memory to copy -m argument");
235 strcpy(module, _PyOS_optarg);
236 break;
237 }
238
Guido van Rossum667d7041995-08-04 04:20:48 +0000239 switch (c) {
240
241 case 'd':
242 Py_DebugFlag++;
243 break;
244
Guido van Rossum61c345f2001-09-04 03:26:15 +0000245 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000246 if (strcmp(_PyOS_optarg, "old") == 0) {
247 Py_DivisionWarningFlag = 0;
248 break;
249 }
250 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000251 Py_DivisionWarningFlag = 1;
252 break;
253 }
254 if (strcmp(_PyOS_optarg, "warnall") == 0) {
255 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000256 break;
257 }
258 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000259 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000260 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000261 /* And this tells the eval loop to treat
262 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
263 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000264 break;
265 }
266 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000267 "-Q option should be `-Qold', "
268 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000269 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000270 /* NOTREACHED */
271
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 case 'i':
273 inspect++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000274 saw_inspect_flag = 1;
Guido van Rossum775af911997-02-14 19:50:32 +0000275 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000276 break;
277
Guido van Rossum7614da61997-03-03 19:14:45 +0000278 case 'O':
279 Py_OptimizeFlag++;
280 break;
281
Guido van Rossum7922bd71997-08-29 22:34:47 +0000282 case 'S':
283 Py_NoSiteFlag++;
284 break;
285
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000286 case 'E':
287 Py_IgnoreEnvironmentFlag++;
288 break;
289
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000290 case 't':
291 Py_TabcheckFlag++;
292 break;
293
Guido van Rossum667d7041995-08-04 04:20:48 +0000294 case 'u':
295 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000296 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000297 break;
298
299 case 'v':
300 Py_VerboseFlag++;
301 break;
302
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000303#ifdef RISCOS
304 case 'w':
305 Py_RISCOSWimpFlag = 1;
306 break;
307#endif
308
Guido van Rossuma075ce11997-12-05 21:56:45 +0000309 case 'x':
310 skipfirstline = 1;
311 break;
312
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000313 case 'U':
314 Py_UnicodeFlag++;
315 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000316 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000317 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000318 help++;
319 break;
320 case 'V':
321 version++;
322 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000323
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000324 case 'W':
325 PySys_AddWarnOption(_PyOS_optarg);
326 break;
327
Guido van Rossum667d7041995-08-04 04:20:48 +0000328 /* This space reserved for other options */
329
330 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000331 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000332 /*NOTREACHED*/
333
334 }
335 }
336
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000337 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000338 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000339
340 if (version) {
341 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000342 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000343 }
344
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000345 if (!saw_inspect_flag &&
346 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
347 inspect = 1;
348 if (!saw_unbuffered_flag &&
349 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
350 unbuffered = 1;
351
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000352 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000353 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000354 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000355#ifdef __VMS
356 filename = decc$translate_vms(argv[_PyOS_optind]);
357 if (filename == (char *)0 || filename == (char *)-1)
358 filename = argv[_PyOS_optind];
359
360#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000361 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000362#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000363 if (filename != NULL) {
364 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000365#ifdef HAVE_STRERROR
366 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
367 argv[0], filename, errno, strerror(errno));
368#else
369 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
370 argv[0], filename, errno);
371#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000372 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000373 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000374 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000375 int ch;
376 /* Push back first newline so line numbers
377 remain the same */
378 while ((ch = getc(fp)) != EOF) {
379 if (ch == '\n') {
380 (void)ungetc(ch, fp);
381 break;
382 }
383 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000384 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000385 {
386 /* XXX: does this work on Win/Win64? (see posix_fstat) */
387 struct stat sb;
388 if (fstat(fileno(fp), &sb) == 0 &&
389 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000390 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
391 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000392 }
393 }
Guido van Rossum775af911997-02-14 19:50:32 +0000394 }
395 }
396
397 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
398
Guido van Rossum667d7041995-08-04 04:20:48 +0000399 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000400#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000401 _setmode(fileno(stdin), O_BINARY);
402 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000403#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000404#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000405 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
406 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
407 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000408#else /* !HAVE_SETVBUF */
409 setbuf(stdin, (char *)NULL);
410 setbuf(stdout, (char *)NULL);
411 setbuf(stderr, (char *)NULL);
412#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000413 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000414 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000415#ifdef MS_WINDOWS
416 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000417 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000418 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000419#else /* !MS_WINDOWS */
420#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000421 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
422 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000423#endif /* HAVE_SETVBUF */
424#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000425 /* Leave stderr alone - it should be unbuffered anyway. */
426 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000427#ifdef __VMS
428 else {
429 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
430 }
431#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000432
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000433#ifdef __APPLE__
434 /* On MacOS X, when the Python interpreter is embedded in an
435 application bundle, it gets executed by a bootstrapping script
436 that does os.execve() with an argv[0] that's different from the
437 actual Python executable. This is needed to keep the Finder happy,
438 or rather, to work around Apple's overly strict requirements of
439 the process name. However, we still need a usable sys.executable,
440 so the actual executable path is passed in an environment variable.
441 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
442 script. */
443 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
444 Py_SetProgramName(p);
445 else
446 Py_SetProgramName(argv[0]);
447#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000448 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000449#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000450 Py_Initialize();
451
Guido van Rossum667d7041995-08-04 04:20:48 +0000452 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000453 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000454 fprintf(stderr, "Python %s on %s\n",
455 Py_GetVersion(), Py_GetPlatform());
456 if (!Py_NoSiteFlag)
457 fprintf(stderr, "%s\n", COPYRIGHT);
458 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000459
Guido van Rossum667d7041995-08-04 04:20:48 +0000460 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000461 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
462 _PyOS_optind--;
463 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000464 }
465
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000466 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000467 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
468 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000469 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000470 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000471 }
472
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000473 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000474
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000475 if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000476 isatty(fileno(stdin))) {
477 PyObject *v;
478 v = PyImport_ImportModule("readline");
479 if (v == NULL)
480 PyErr_Clear();
481 else
482 Py_DECREF(v);
483 }
484
Guido van Rossum667d7041995-08-04 04:20:48 +0000485 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000486 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000487 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000488 } else if (module) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000489 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000490 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000491 }
492 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000493 if (filename == NULL && stdin_is_interactive) {
Martin v. Löwis6caea372003-11-18 19:46:25 +0000494 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000495 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000496 /* XXX */
497 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000498 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000499 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000500 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000501 }
502
Barry Warsawd86dcd32003-06-29 17:07:06 +0000503 /* Check this environment variable at the end, to give programs the
504 * opportunity to set it from Python.
505 */
506 if (!saw_inspect_flag &&
507 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
508 {
509 inspect = 1;
510 }
511
Guido van Rossum775af911997-02-14 19:50:32 +0000512 if (inspect && stdin_is_interactive &&
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000513 (filename != NULL || command != NULL || module != NULL))
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000514 /* XXX */
515 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000516
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000517 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000518#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000519 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000520 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
521#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000522
523#ifdef __INSURE__
524 /* Insure++ is a memory analysis tool that aids in discovering
525 * memory leaks and other memory problems. On Python exit, the
526 * interned string dictionary is flagged as being in use at exit
527 * (which it is). Under normal circumstances, this is fine because
528 * the memory will be automatically reclaimed by the system. Under
529 * memory debugging, it's a huge source of useless noise, so we
530 * trade off slower shutdown for less distraction in the memory
531 * reports. -baw
532 */
533 _Py_ReleaseInternedStrings();
534#endif /* __INSURE__ */
535
Guido van Rossum05f7c501997-08-02 03:00:42 +0000536 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000537}
538
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000539/* this is gonna seem *real weird*, but if you put some other code between
540 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
541 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000542
Guido van Rossum667d7041995-08-04 04:20:48 +0000543/* Make the *original* argc/argv available to other modules.
544 This is rare, but it is needed by the secureware extension. */
545
546void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000547Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000548{
549 *argc = orig_argc;
550 *argv = orig_argv;
551}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000552
553#ifdef __cplusplus
554}
555#endif
556