blob: 2dbdfe9ab1cba6b4e607b3a4d177f4316995d331 [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
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000180/* Wait until threading._shutdown completes, provided
181 the threading module was imported in the first place.
182 The shutdown routine will wait until all non-daemon
183 "threading" threads have completed. */
184#include "abstract.h"
185static void
Brett Cannond14ef772007-01-05 21:45:09 +0000186WaitForThreadShutdown(void)
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000187{
188#ifdef WITH_THREAD
189 PyObject *result;
190 PyThreadState *tstate = PyThreadState_GET();
191 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
192 "threading");
193 if (threading == NULL) {
194 /* threading not imported */
195 PyErr_Clear();
196 return;
197 }
198 result = PyObject_CallMethod(threading, "_shutdown", "");
199 if (result == NULL)
200 PyErr_WriteUnraisable(threading);
201 else
202 Py_DECREF(result);
203 Py_DECREF(threading);
204#endif
205}
206
Guido van Rossum667d7041995-08-04 04:20:48 +0000207/* Main program */
208
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000209int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000210Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000211{
212 int c;
213 int sts;
214 char *command = NULL;
215 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000216 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000217 FILE *fp = stdin;
218 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000219 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000220 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000221 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000222 int help = 0;
223 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000224 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000225 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000226
Guido van Rossum393661d2001-08-31 17:40:15 +0000227 cf.cf_flags = 0;
228
Guido van Rossumac56b031996-07-21 02:33:38 +0000229 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000230 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000231
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000232#ifdef RISCOS
233 Py_RISCOSWimpFlag = 0;
234#endif
235
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000236 PySys_ResetWarnOptions();
237
Guido van Rossumbceccf52001-04-10 22:07:43 +0000238 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000239 if (c == 'c') {
240 /* -c is the last option; following arguments
241 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000242 command to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000243 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000244 if (command == NULL)
245 Py_FatalError(
246 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000247 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000248 strcat(command, "\n");
249 break;
250 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000251
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000252 if (c == 'm') {
253 /* -m is the last option; following arguments
254 that look like options are left for the
255 module to interpret. */
Anthony Baxter64182fe2006-04-11 12:14:09 +0000256 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000257 if (module == NULL)
258 Py_FatalError(
259 "not enough memory to copy -m argument");
260 strcpy(module, _PyOS_optarg);
261 break;
262 }
263
Guido van Rossum667d7041995-08-04 04:20:48 +0000264 switch (c) {
265
266 case 'd':
267 Py_DebugFlag++;
268 break;
269
Guido van Rossum61c345f2001-09-04 03:26:15 +0000270 case 'Q':
Guido van Rossum393661d2001-08-31 17:40:15 +0000271 if (strcmp(_PyOS_optarg, "old") == 0) {
272 Py_DivisionWarningFlag = 0;
273 break;
274 }
275 if (strcmp(_PyOS_optarg, "warn") == 0) {
Guido van Rossum1832de42001-09-04 03:51:09 +0000276 Py_DivisionWarningFlag = 1;
277 break;
278 }
279 if (strcmp(_PyOS_optarg, "warnall") == 0) {
280 Py_DivisionWarningFlag = 2;
Guido van Rossum393661d2001-08-31 17:40:15 +0000281 break;
282 }
283 if (strcmp(_PyOS_optarg, "new") == 0) {
Tim Peters3caca232001-12-06 06:23:26 +0000284 /* This only affects __main__ */
Guido van Rossum393661d2001-08-31 17:40:15 +0000285 cf.cf_flags |= CO_FUTURE_DIVISION;
Tim Peters3caca232001-12-06 06:23:26 +0000286 /* And this tells the eval loop to treat
287 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
288 _Py_QnewFlag = 1;
Guido van Rossum393661d2001-08-31 17:40:15 +0000289 break;
290 }
291 fprintf(stderr,
Guido van Rossum1832de42001-09-04 03:51:09 +0000292 "-Q option should be `-Qold', "
293 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000294 return usage(2, argv[0]);
Guido van Rossum393661d2001-08-31 17:40:15 +0000295 /* NOTREACHED */
296
Guido van Rossum667d7041995-08-04 04:20:48 +0000297 case 'i':
Georg Brandl49aafc92007-03-07 00:34:46 +0000298 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000299 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000300 break;
301
Guido van Rossum7614da61997-03-03 19:14:45 +0000302 case 'O':
303 Py_OptimizeFlag++;
304 break;
305
Guido van Rossum7922bd71997-08-29 22:34:47 +0000306 case 'S':
307 Py_NoSiteFlag++;
308 break;
309
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000310 case 'E':
311 Py_IgnoreEnvironmentFlag++;
312 break;
313
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000314 case 't':
315 Py_TabcheckFlag++;
316 break;
317
Guido van Rossum667d7041995-08-04 04:20:48 +0000318 case 'u':
319 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000320 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000321 break;
322
323 case 'v':
324 Py_VerboseFlag++;
325 break;
326
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000327#ifdef RISCOS
328 case 'w':
329 Py_RISCOSWimpFlag = 1;
330 break;
331#endif
332
Guido van Rossuma075ce11997-12-05 21:56:45 +0000333 case 'x':
334 skipfirstline = 1;
335 break;
336
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000337 case 'U':
338 Py_UnicodeFlag++;
339 break;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000340 case 'h':
Georg Brandl9dceedb2006-07-12 15:31:17 +0000341 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000342 help++;
343 break;
344 case 'V':
345 version++;
346 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000347
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000348 case 'W':
349 PySys_AddWarnOption(_PyOS_optarg);
350 break;
351
Guido van Rossum667d7041995-08-04 04:20:48 +0000352 /* This space reserved for other options */
353
354 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000355 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000356 /*NOTREACHED*/
357
358 }
359 }
360
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000361 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000362 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000363
364 if (version) {
365 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000366 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000367 }
368
Georg Brandl49aafc92007-03-07 00:34:46 +0000369 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000370 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Georg Brandl49aafc92007-03-07 00:34:46 +0000371 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000372 if (!saw_unbuffered_flag &&
373 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
374 unbuffered = 1;
375
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000376 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000377 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000378 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000379#ifdef __VMS
380 filename = decc$translate_vms(argv[_PyOS_optind]);
381 if (filename == (char *)0 || filename == (char *)-1)
382 filename = argv[_PyOS_optind];
383
384#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000385 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000386#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000387 if (filename != NULL) {
388 if ((fp = fopen(filename, "r")) == NULL) {
Martin v. Löwis4d4dfb72004-08-19 11:07:49 +0000389#ifdef HAVE_STRERROR
390 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
391 argv[0], filename, errno, strerror(errno));
392#else
393 fprintf(stderr, "%s: can't open file '%s': Errno %d\n",
394 argv[0], filename, errno);
395#endif
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000396 return 2;
Guido van Rossum775af911997-02-14 19:50:32 +0000397 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000398 else if (skipfirstline) {
Guido van Rossumdc8b5691999-04-19 17:54:19 +0000399 int ch;
400 /* Push back first newline so line numbers
401 remain the same */
402 while ((ch = getc(fp)) != EOF) {
403 if (ch == '\n') {
404 (void)ungetc(ch, fp);
405 break;
406 }
407 }
Guido van Rossuma075ce11997-12-05 21:56:45 +0000408 }
Neal Norwitz11bd1192005-10-03 00:54:56 +0000409 {
410 /* XXX: does this work on Win/Win64? (see posix_fstat) */
411 struct stat sb;
412 if (fstat(fileno(fp), &sb) == 0 &&
413 S_ISDIR(sb.st_mode)) {
Neal Norwitz72c2c062006-03-09 05:58:11 +0000414 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
415 return 1;
Neal Norwitz11bd1192005-10-03 00:54:56 +0000416 }
417 }
Guido van Rossum775af911997-02-14 19:50:32 +0000418 }
419 }
420
421 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
422
Guido van Rossum667d7041995-08-04 04:20:48 +0000423 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000424#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000425 _setmode(fileno(stdin), O_BINARY);
426 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000427#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000428#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000429 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
430 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
431 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000432#else /* !HAVE_SETVBUF */
433 setbuf(stdin, (char *)NULL);
434 setbuf(stdout, (char *)NULL);
435 setbuf(stderr, (char *)NULL);
436#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000437 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000438 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000439#ifdef MS_WINDOWS
440 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000441 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000442 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000443#else /* !MS_WINDOWS */
444#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000445 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
446 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000447#endif /* HAVE_SETVBUF */
448#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000449 /* Leave stderr alone - it should be unbuffered anyway. */
450 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000451#ifdef __VMS
452 else {
453 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
454 }
455#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000456
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000457#ifdef __APPLE__
458 /* On MacOS X, when the Python interpreter is embedded in an
459 application bundle, it gets executed by a bootstrapping script
460 that does os.execve() with an argv[0] that's different from the
461 actual Python executable. This is needed to keep the Finder happy,
462 or rather, to work around Apple's overly strict requirements of
463 the process name. However, we still need a usable sys.executable,
464 so the actual executable path is passed in an environment variable.
465 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
466 script. */
467 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
468 Py_SetProgramName(p);
469 else
470 Py_SetProgramName(argv[0]);
471#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000472 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000473#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000474 Py_Initialize();
475
Guido van Rossum667d7041995-08-04 04:20:48 +0000476 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000477 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000478 fprintf(stderr, "Python %s on %s\n",
479 Py_GetVersion(), Py_GetPlatform());
480 if (!Py_NoSiteFlag)
481 fprintf(stderr, "%s\n", COPYRIGHT);
482 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000483
Guido van Rossum667d7041995-08-04 04:20:48 +0000484 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000485 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
486 _PyOS_optind--;
487 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000488 }
489
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000490 if (module != NULL) {
Nick Coghlan81f444b2006-06-12 10:17:11 +0000491 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
492 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000493 _PyOS_optind--;
Nick Coghlan81f444b2006-06-12 10:17:11 +0000494 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000495 }
496
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000497 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000498
Georg Brandl49aafc92007-03-07 00:34:46 +0000499 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000500 isatty(fileno(stdin))) {
501 PyObject *v;
502 v = PyImport_ImportModule("readline");
503 if (v == NULL)
504 PyErr_Clear();
505 else
506 Py_DECREF(v);
507 }
508
Guido van Rossum667d7041995-08-04 04:20:48 +0000509 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000510 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000511 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000512 } else if (module) {
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000513 sts = RunModule(module);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000514 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000515 }
516 else {
Guido van Rossum775af911997-02-14 19:50:32 +0000517 if (filename == NULL && stdin_is_interactive) {
Georg Brandl49aafc92007-03-07 00:34:46 +0000518 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000519 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000520 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000521 /* XXX */
522 sts = PyRun_AnyFileExFlags(
Guido van Rossum775af911997-02-14 19:50:32 +0000523 fp,
Guido van Rossum0df002c2000-08-27 19:21:52 +0000524 filename == NULL ? "<stdin>" : filename,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000525 filename != NULL, &cf) != 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000526 }
527
Barry Warsawd86dcd32003-06-29 17:07:06 +0000528 /* Check this environment variable at the end, to give programs the
529 * opportunity to set it from Python.
530 */
Georg Brandl49aafc92007-03-07 00:34:46 +0000531 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000532 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
533 {
Georg Brandl49aafc92007-03-07 00:34:46 +0000534 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000535 }
536
Georg Brandl49aafc92007-03-07 00:34:46 +0000537 if (Py_InspectFlag && stdin_is_interactive &&
538 (filename != NULL || command != NULL || module != NULL)) {
539 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000540 /* XXX */
541 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Georg Brandl49aafc92007-03-07 00:34:46 +0000542 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000543
Martin v. Löwis7b7c9d42007-01-04 21:06:12 +0000544 WaitForThreadShutdown();
545
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000546 Py_Finalize();
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000547#ifdef RISCOS
Fred Drake5134a542002-10-17 20:37:50 +0000548 if (Py_RISCOSWimpFlag)
Guido van Rossum3ed4c152001-03-02 06:18:03 +0000549 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
550#endif
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000551
552#ifdef __INSURE__
553 /* Insure++ is a memory analysis tool that aids in discovering
554 * memory leaks and other memory problems. On Python exit, the
555 * interned string dictionary is flagged as being in use at exit
556 * (which it is). Under normal circumstances, this is fine because
557 * the memory will be automatically reclaimed by the system. Under
558 * memory debugging, it's a huge source of useless noise, so we
559 * trade off slower shutdown for less distraction in the memory
560 * reports. -baw
561 */
562 _Py_ReleaseInternedStrings();
563#endif /* __INSURE__ */
564
Guido van Rossum05f7c501997-08-02 03:00:42 +0000565 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000566}
567
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000568/* this is gonna seem *real weird*, but if you put some other code between
569 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
570 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000571
Guido van Rossum667d7041995-08-04 04:20:48 +0000572/* Make the *original* argc/argv available to other modules.
573 This is rare, but it is needed by the secureware extension. */
574
575void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000576Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000577{
578 *argc = orig_argc;
579 *argv = orig_argv;
580}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000581
582#ifdef __cplusplus
583}
584#endif
585