blob: 9f0b01c99bc9f37795543e798a7ba420a6c84f85 [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__)
Martin v. Löwis945362c2007-08-30 14:57:25 +000012#include <windows.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000014#include <fcntl.h>
15#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000016#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000017
Martin v. Löwis945362c2007-08-30 14:57:25 +000018#ifdef _MSC_VER
19#include <crtdbg.h>
20#endif
21
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000022#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000023#define PYTHONHOMEHELP "<prefix>\\lib"
24#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#if defined(PYOS_OS2) && defined(PYCC_GCC)
26#define PYTHONHOMEHELP "<prefix>/Lib"
27#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000028#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000029#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000030#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000031
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000032#include "pygetopt.h"
33
Guido van Rossuma22865e2000-09-05 04:41:18 +000034#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000035 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
36 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#ifdef __cplusplus
39extern "C" {
40#endif
41
Guido van Rossumac56b031996-07-21 02:33:38 +000042/* For Py_GetArgcArgv(); set by main() */
Guido van Rossum667d7041995-08-04 04:20:48 +000043static char **orig_argv;
44static int orig_argc;
45
Guido van Rossumbceccf52001-04-10 22:07:43 +000046/* command line options */
Christian Heimes790c8232008-01-07 21:14:23 +000047#define BASE_OPTS "bBc:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000048
Guido van Rossumbceccf52001-04-10 22:07:43 +000049#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000050
Guido van Rossum667d7041995-08-04 04:20:48 +000051/* Short usage message (with %s for argv0) */
52static char *usage_line =
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000053"usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000054
55/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000056static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000057Options and arguments (and corresponding environment variables):\n\
Guido van Rossum98297ee2007-11-06 21:34:58 +000058-b : issue warnings about str(bytes_instance), str(buffer_instance)\n\
59 and comparing bytes/buffer with str. (-bb: issue errors)\n\
Christian Heimes790c8232008-01-07 21:14:23 +000060-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\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\
Christian Heimes790c8232008-01-07 21:14:23 +000063-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000065";
66static char *usage_2 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000067-i : inspect interactively after running script; forces a prompt even\n\
68 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
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\
Guido van Rossum393661d2001-08-31 17:40:15 +000074";
75static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000076-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
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 Rossum7922bd71997-08-29 22:34:47 +000083";
Guido van Rossum393661d2001-08-31 17:40:15 +000084static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000085file : program read from script file\n\
86- : program read from stdin (default; interactive mode if a tty)\n\
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\
Christian Heimes790c8232008-01-07 21:14:23 +000092";
93static char *usage_5 = "\
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);
Christian Heimes790c8232008-01-07 21:14:23 +0000112 fprintf(f, usage_4, DELIM);
113 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000114 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000115#if defined(__VMS)
116 if (exitcode == 0) {
117 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000118 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000119 }
120 else {
121 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000123 }
124#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000125 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000126#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000127 /*NOTREACHED*/
128}
129
Martin v. Löwis6caea372003-11-18 19:46:25 +0000130static void RunStartupFile(PyCompilerFlags *cf)
131{
132 char *startup = Py_GETENV("PYTHONSTARTUP");
133 if (startup != NULL && startup[0] != '\0') {
134 FILE *fp = fopen(startup, "r");
135 if (fp != NULL) {
136 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
137 PyErr_Clear();
138 fclose(fp);
Christian Heimese69a08e2007-11-14 16:21:32 +0000139 } else {
140 int save_errno;
141
142 save_errno = errno;
143 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
144 errno = save_errno;
145 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
146 startup);
147 PyErr_Print();
148 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000149 }
150 }
151}
152
Thomas Woutersa9773292006-04-21 09:43:23 +0000153
Christian Heimes9cd17752007-11-18 19:35:23 +0000154static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000155{
Thomas Woutersa9773292006-04-21 09:43:23 +0000156 PyObject *runpy, *runmodule, *runargs, *result;
157 runpy = PyImport_ImportModule("runpy");
158 if (runpy == NULL) {
159 fprintf(stderr, "Could not import runpy module\n");
160 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000161 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000162 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000163 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000164 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000165 Py_DECREF(runpy);
166 return -1;
167 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000168 runargs = Py_BuildValue("(si)", module, set_argv0);
Thomas Woutersa9773292006-04-21 09:43:23 +0000169 if (runargs == NULL) {
170 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000171 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000172 Py_DECREF(runpy);
173 Py_DECREF(runmodule);
174 return -1;
175 }
176 result = PyObject_Call(runmodule, runargs, NULL);
177 if (result == NULL) {
178 PyErr_Print();
179 }
180 Py_DECREF(runpy);
181 Py_DECREF(runmodule);
182 Py_DECREF(runargs);
183 if (result == NULL) {
184 return -1;
185 }
186 Py_DECREF(result);
187 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000188}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000189
Christian Heimes9cd17752007-11-18 19:35:23 +0000190static int RunMainFromImporter(char *filename)
191{
192 PyObject *argv0 = NULL, *importer = NULL;
193
Guido van Rossum74c29c72007-11-19 18:36:41 +0000194 if ((argv0 = PyUnicode_DecodeFSDefault(filename)) &&
195 (importer = PyImport_GetImporter(argv0)) &&
196 (importer->ob_type != &PyNullImporter_Type))
Christian Heimes9cd17752007-11-18 19:35:23 +0000197 {
198 /* argv0 is usable as an import source, so
199 put it in sys.path[0] and import __main__ */
200 PyObject *sys_path = NULL;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000201 if ((sys_path = PySys_GetObject("path")) &&
202 !PyList_SetItem(sys_path, 0, argv0))
203 {
Christian Heimes9cd17752007-11-18 19:35:23 +0000204 Py_INCREF(argv0);
Guido van Rossum74c29c72007-11-19 18:36:41 +0000205 Py_DECREF(importer);
Christian Heimes9cd17752007-11-18 19:35:23 +0000206 sys_path = NULL;
207 return RunModule("__main__", 0) != 0;
208 }
209 }
Guido van Rossum74c29c72007-11-19 18:36:41 +0000210 Py_XDECREF(argv0);
211 Py_XDECREF(importer);
212 if (PyErr_Occurred()) {
213 PyErr_Print();
214 return 1;
215 }
216 else {
217 return -1;
218 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000219}
220
221
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000222/* Wait until threading._shutdown completes, provided
223 the threading module was imported in the first place.
224 The shutdown routine will wait until all non-daemon
225 "threading" threads have completed. */
226#include "abstract.h"
227static void
228WaitForThreadShutdown(void)
229{
230#ifdef WITH_THREAD
231 PyObject *result;
232 PyThreadState *tstate = PyThreadState_GET();
233 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
234 "threading");
235 if (threading == NULL) {
236 /* threading not imported */
237 PyErr_Clear();
238 return;
239 }
240 result = PyObject_CallMethod(threading, "_shutdown", "");
241 if (result == NULL)
242 PyErr_WriteUnraisable(threading);
243 else
244 Py_DECREF(result);
245 Py_DECREF(threading);
246#endif
247}
248
Guido van Rossum667d7041995-08-04 04:20:48 +0000249/* Main program */
250
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000251int
Fredrik Lundh620f3772000-07-09 20:42:34 +0000252Py_Main(int argc, char **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000253{
254 int c;
255 int sts;
256 char *command = NULL;
257 char *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000258 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000259 FILE *fp = stdin;
260 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000261 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000262 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000263 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000264 int help = 0;
265 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000266 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000267 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000268
Guido van Rossum393661d2001-08-31 17:40:15 +0000269 cf.cf_flags = 0;
270
Guido van Rossumac56b031996-07-21 02:33:38 +0000271 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000272 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000273
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000274 PySys_ResetWarnOptions();
275
Guido van Rossumbceccf52001-04-10 22:07:43 +0000276 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000277 if (c == 'c') {
278 /* -c is the last option; following arguments
279 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000280 command to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
Guido van Rossum667d7041995-08-04 04:20:48 +0000282 if (command == NULL)
283 Py_FatalError(
284 "not enough memory to copy -c argument");
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000285 strcpy(command, _PyOS_optarg);
Guido van Rossum667d7041995-08-04 04:20:48 +0000286 strcat(command, "\n");
287 break;
288 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000289
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000290 if (c == 'm') {
291 /* -m is the last option; following arguments
292 that look like options are left for the
293 module to interpret. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000295 if (module == NULL)
296 Py_FatalError(
297 "not enough memory to copy -m argument");
298 strcpy(module, _PyOS_optarg);
299 break;
300 }
301
Guido van Rossum667d7041995-08-04 04:20:48 +0000302 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000303 case 'b':
304 Py_BytesWarningFlag++;
305 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000306
307 case 'd':
308 Py_DebugFlag++;
309 break;
310
311 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000312 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000313 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000314 break;
315
Guido van Rossum7614da61997-03-03 19:14:45 +0000316 case 'O':
317 Py_OptimizeFlag++;
318 break;
319
Christian Heimes790c8232008-01-07 21:14:23 +0000320 case 'B':
321 Py_DontWriteBytecodeFlag++;
322 break;
323
Guido van Rossum7922bd71997-08-29 22:34:47 +0000324 case 'S':
325 Py_NoSiteFlag++;
326 break;
327
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000328 case 'E':
329 Py_IgnoreEnvironmentFlag++;
330 break;
331
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000332 case 't':
333 Py_TabcheckFlag++;
334 break;
335
Guido van Rossum667d7041995-08-04 04:20:48 +0000336 case 'u':
337 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000338 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000339 break;
340
341 case 'v':
342 Py_VerboseFlag++;
343 break;
344
Guido van Rossuma075ce11997-12-05 21:56:45 +0000345 case 'x':
346 skipfirstline = 1;
347 break;
348
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000349 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000350 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000351 help++;
352 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000353
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000354 case 'V':
355 version++;
356 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000357
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000358 case 'W':
359 PySys_AddWarnOption(_PyOS_optarg);
360 break;
361
Guido van Rossum667d7041995-08-04 04:20:48 +0000362 /* This space reserved for other options */
363
364 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000365 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000366 /*NOTREACHED*/
367
368 }
369 }
370
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000371 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000372 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000373
374 if (version) {
375 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000376 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000377 }
378
Guido van Rossumd8faa362007-04-27 19:54:29 +0000379 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000380 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000381 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000382 if (!saw_unbuffered_flag &&
383 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
384 unbuffered = 1;
385
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000386 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000387 strcmp(argv[_PyOS_optind], "-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000388 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000389#ifdef __VMS
390 filename = decc$translate_vms(argv[_PyOS_optind]);
391 if (filename == (char *)0 || filename == (char *)-1)
392 filename = argv[_PyOS_optind];
393
394#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000395 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000396#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000397 }
398
399 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
400
Guido van Rossum667d7041995-08-04 04:20:48 +0000401 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000402#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000403 _setmode(fileno(stdin), O_BINARY);
404 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000405#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000406#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000407 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
408 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
409 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000410#else /* !HAVE_SETVBUF */
411 setbuf(stdin, (char *)NULL);
412 setbuf(stdout, (char *)NULL);
413 setbuf(stderr, (char *)NULL);
414#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000415 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000416 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000417#ifdef MS_WINDOWS
418 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000419 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000420 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000421#else /* !MS_WINDOWS */
422#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000423 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
424 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000425#endif /* HAVE_SETVBUF */
426#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000427 /* Leave stderr alone - it should be unbuffered anyway. */
428 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000429#ifdef __VMS
430 else {
431 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
432 }
433#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000434
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000435#ifdef __APPLE__
436 /* On MacOS X, when the Python interpreter is embedded in an
437 application bundle, it gets executed by a bootstrapping script
438 that does os.execve() with an argv[0] that's different from the
439 actual Python executable. This is needed to keep the Finder happy,
440 or rather, to work around Apple's overly strict requirements of
441 the process name. However, we still need a usable sys.executable,
442 so the actual executable path is passed in an environment variable.
443 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
444 script. */
445 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
446 Py_SetProgramName(p);
447 else
448 Py_SetProgramName(argv[0]);
449#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000450 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000451#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000452 Py_Initialize();
453
Guido van Rossum667d7041995-08-04 04:20:48 +0000454 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000455 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000456 fprintf(stderr, "Python %s on %s\n",
457 Py_GetVersion(), Py_GetPlatform());
458 if (!Py_NoSiteFlag)
459 fprintf(stderr, "%s\n", COPYRIGHT);
460 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000461
Guido van Rossum667d7041995-08-04 04:20:48 +0000462 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000463 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
464 _PyOS_optind--;
465 argv[_PyOS_optind] = "-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000466 }
467
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000468 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
470 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000471 _PyOS_optind--;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000472 argv[_PyOS_optind] = "-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000473 }
474
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000475 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000476
Guido van Rossumd8faa362007-04-27 19:54:29 +0000477 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000478 isatty(fileno(stdin))) {
479 PyObject *v;
480 v = PyImport_ImportModule("readline");
481 if (v == NULL)
482 PyErr_Clear();
483 else
484 Py_DECREF(v);
485 }
486
Guido van Rossum667d7041995-08-04 04:20:48 +0000487 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000488 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000489 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000490 } else if (module) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000491 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000492 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000493 }
494 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000495
Guido van Rossum775af911997-02-14 19:50:32 +0000496 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000497 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000498 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000499 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000500 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000501
502 sts = -1; /* keep track of whether we've already run __main__ */
503
504 if (filename != NULL) {
505 sts = RunMainFromImporter(filename);
506 }
507
508 if (sts==-1 && filename!=NULL) {
509 if ((fp = fopen(filename, "r")) == NULL) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000510 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
511 argv[0], filename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000512
Christian Heimes9cd17752007-11-18 19:35:23 +0000513 return 2;
514 }
515 else if (skipfirstline) {
516 int ch;
517 /* Push back first newline so line numbers
518 remain the same */
519 while ((ch = getc(fp)) != EOF) {
520 if (ch == '\n') {
521 (void)ungetc(ch, fp);
522 break;
523 }
524 }
525 }
526 {
527 /* XXX: does this work on Win/Win64? (see posix_fstat) */
528 struct stat sb;
529 if (fstat(fileno(fp), &sb) == 0 &&
530 S_ISDIR(sb.st_mode)) {
531 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes679db4a2008-01-18 09:56:22 +0000532 fclose(fp);
Christian Heimes9cd17752007-11-18 19:35:23 +0000533 return 1;
534 }
535 }
536 }
537
538 if (sts==-1) {
539 sts = PyRun_AnyFileExFlags(
540 fp,
541 filename == NULL ? "<stdin>" : filename,
542 filename != NULL, &cf) != 0;
543 }
544
Guido van Rossum667d7041995-08-04 04:20:48 +0000545 }
546
Barry Warsawd86dcd32003-06-29 17:07:06 +0000547 /* Check this environment variable at the end, to give programs the
548 * opportunity to set it from Python.
549 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000550 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000551 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
552 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000553 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000554 }
555
Guido van Rossumd8faa362007-04-27 19:54:29 +0000556 if (Py_InspectFlag && stdin_is_interactive &&
557 (filename != NULL || command != NULL || module != NULL)) {
558 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000559 /* XXX */
560 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000561 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000562
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000563 WaitForThreadShutdown();
564
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000565 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000566
567#ifdef __INSURE__
568 /* Insure++ is a memory analysis tool that aids in discovering
569 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000570 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000571 * (which it is). Under normal circumstances, this is fine because
572 * the memory will be automatically reclaimed by the system. Under
573 * memory debugging, it's a huge source of useless noise, so we
574 * trade off slower shutdown for less distraction in the memory
575 * reports. -baw
576 */
577 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000578 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000579#endif /* __INSURE__ */
580
Guido van Rossum05f7c501997-08-02 03:00:42 +0000581 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000582}
583
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000584/* this is gonna seem *real weird*, but if you put some other code between
585 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
586 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000587
Guido van Rossum667d7041995-08-04 04:20:48 +0000588/* Make the *original* argc/argv available to other modules.
589 This is rare, but it is needed by the secureware extension. */
590
591void
Fredrik Lundh620f3772000-07-09 20:42:34 +0000592Py_GetArgcArgv(int *argc, char ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000593{
594 *argc = orig_argc;
595 *argv = orig_argv;
596}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597
598#ifdef __cplusplus
599}
600#endif