blob: 81a3d46d3f3543af0ce409e762c8c0ddcd2a575f [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>
Martin v. Löwis790465f2008-04-05 20:41:37 +000015#define PATH_MAX MAXPATHLEN
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000017#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000018
Martin v. Löwis945362c2007-08-30 14:57:25 +000019#ifdef _MSC_VER
20#include <crtdbg.h>
21#endif
22
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000023#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000024#define PYTHONHOMEHELP "<prefix>\\lib"
25#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000026#if defined(PYOS_OS2) && defined(PYCC_GCC)
27#define PYTHONHOMEHELP "<prefix>/Lib"
28#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000029#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000030#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000031#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000032
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000033#include "pygetopt.h"
34
Guido van Rossuma22865e2000-09-05 04:41:18 +000035#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000036 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
37 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000038
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039#ifdef __cplusplus
40extern "C" {
41#endif
42
Guido van Rossumac56b031996-07-21 02:33:38 +000043/* For Py_GetArgcArgv(); set by main() */
Martin v. Löwis790465f2008-04-05 20:41:37 +000044static wchar_t **orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +000045static int orig_argc;
46
Guido van Rossumbceccf52001-04-10 22:07:43 +000047/* command line options */
Martin v. Löwis790465f2008-04-05 20:41:37 +000048#define BASE_OPTS L"bBc:dEhim:OStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000049
Guido van Rossumbceccf52001-04-10 22:07:43 +000050#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000051
Guido van Rossum667d7041995-08-04 04:20:48 +000052/* Short usage message (with %s for argv0) */
53static char *usage_line =
Martin v. Löwis790465f2008-04-05 20:41:37 +000054"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000055
56/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000057static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000058Options and arguments (and corresponding environment variables):\n\
Guido van Rossum98297ee2007-11-06 21:34:58 +000059-b : issue warnings about str(bytes_instance), str(buffer_instance)\n\
60 and comparing bytes/buffer with str. (-bb: issue errors)\n\
Christian Heimes790c8232008-01-07 21:14:23 +000061-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000062-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000063-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000064-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000066";
67static char *usage_2 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000068-i : inspect interactively after running script; forces a prompt even\n\
69 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000070-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +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 Rossum7922bd71997-08-29 22:34:47 +000073-S : don't imply 'import site' on initialization\n\
Guido van Rossumbba92ca1998-04-10 19:39:15 +000074-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000075";
76static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000077-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000078 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000079-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
80 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000082-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000083-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000084";
Guido van Rossum393661d2001-08-31 17:40:15 +000085static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000086file : program read from script file\n\
87- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000088arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000089Other environment variables:\n\
90PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000091PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +000093";
94static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +000095PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
96 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +000097PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000098";
99
Martin v. Löwis790465f2008-04-05 20:41:37 +0000100#ifndef MS_WINDOWS
101static FILE*
102_wfopen(const wchar_t *path, const wchar_t *mode)
103{
104 char cpath[PATH_MAX];
105 char cmode[10];
106 size_t r;
107 r = wcstombs(cpath, path, PATH_MAX);
108 if (r == (size_t)-1 || r >= PATH_MAX) {
109 errno = EINVAL;
110 return NULL;
111 }
112 r = wcstombs(cmode, mode, 10);
113 if (r == (size_t)-1 || r >= 10) {
114 errno = EINVAL;
115 return NULL;
116 }
117 return fopen(cpath, cmode);
118}
119#endif
120
Guido van Rossum667d7041995-08-04 04:20:48 +0000121
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000122static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000123usage(int exitcode, wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000124{
Guido van Rossum393661d2001-08-31 17:40:15 +0000125 FILE *f = exitcode ? stderr : stdout;
126
127 fprintf(f, usage_line, program);
128 if (exitcode)
129 fprintf(f, "Try `python -h' for more information.\n");
130 else {
131 fprintf(f, usage_1);
132 fprintf(f, usage_2);
133 fprintf(f, usage_3);
Christian Heimes790c8232008-01-07 21:14:23 +0000134 fprintf(f, usage_4, DELIM);
135 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000136 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000137#if defined(__VMS)
138 if (exitcode == 0) {
139 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000140 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000141 }
142 else {
143 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000144 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000145 }
146#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000147 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000148#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000149 /*NOTREACHED*/
150}
151
Martin v. Löwis6caea372003-11-18 19:46:25 +0000152static void RunStartupFile(PyCompilerFlags *cf)
153{
154 char *startup = Py_GETENV("PYTHONSTARTUP");
155 if (startup != NULL && startup[0] != '\0') {
156 FILE *fp = fopen(startup, "r");
157 if (fp != NULL) {
158 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
159 PyErr_Clear();
160 fclose(fp);
Christian Heimese69a08e2007-11-14 16:21:32 +0000161 } else {
162 int save_errno;
163
164 save_errno = errno;
165 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
166 errno = save_errno;
167 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
168 startup);
169 PyErr_Print();
170 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000171 }
172 }
173}
174
Thomas Woutersa9773292006-04-21 09:43:23 +0000175
Christian Heimes9cd17752007-11-18 19:35:23 +0000176static int RunModule(char *module, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000177{
Thomas Woutersa9773292006-04-21 09:43:23 +0000178 PyObject *runpy, *runmodule, *runargs, *result;
179 runpy = PyImport_ImportModule("runpy");
180 if (runpy == NULL) {
181 fprintf(stderr, "Could not import runpy module\n");
182 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000183 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000184 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000185 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000186 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000187 Py_DECREF(runpy);
188 return -1;
189 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000190 runargs = Py_BuildValue("(si)", module, set_argv0);
Thomas Woutersa9773292006-04-21 09:43:23 +0000191 if (runargs == NULL) {
192 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000193 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000194 Py_DECREF(runpy);
195 Py_DECREF(runmodule);
196 return -1;
197 }
198 result = PyObject_Call(runmodule, runargs, NULL);
199 if (result == NULL) {
200 PyErr_Print();
201 }
202 Py_DECREF(runpy);
203 Py_DECREF(runmodule);
204 Py_DECREF(runargs);
205 if (result == NULL) {
206 return -1;
207 }
208 Py_DECREF(result);
209 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000210}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000211
Martin v. Löwis790465f2008-04-05 20:41:37 +0000212static int RunMainFromImporter(wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000213{
214 PyObject *argv0 = NULL, *importer = NULL;
215
Martin v. Löwis790465f2008-04-05 20:41:37 +0000216 if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
Guido van Rossum74c29c72007-11-19 18:36:41 +0000217 (importer = PyImport_GetImporter(argv0)) &&
218 (importer->ob_type != &PyNullImporter_Type))
Christian Heimes9cd17752007-11-18 19:35:23 +0000219 {
220 /* argv0 is usable as an import source, so
221 put it in sys.path[0] and import __main__ */
222 PyObject *sys_path = NULL;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000223 if ((sys_path = PySys_GetObject("path")) &&
224 !PyList_SetItem(sys_path, 0, argv0))
225 {
Christian Heimes9cd17752007-11-18 19:35:23 +0000226 Py_INCREF(argv0);
Guido van Rossum74c29c72007-11-19 18:36:41 +0000227 Py_DECREF(importer);
Christian Heimes9cd17752007-11-18 19:35:23 +0000228 sys_path = NULL;
229 return RunModule("__main__", 0) != 0;
230 }
231 }
Guido van Rossum74c29c72007-11-19 18:36:41 +0000232 Py_XDECREF(argv0);
233 Py_XDECREF(importer);
234 if (PyErr_Occurred()) {
235 PyErr_Print();
236 return 1;
237 }
238 else {
239 return -1;
240 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000241}
242
243
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000244/* Wait until threading._shutdown completes, provided
245 the threading module was imported in the first place.
246 The shutdown routine will wait until all non-daemon
247 "threading" threads have completed. */
248#include "abstract.h"
249static void
250WaitForThreadShutdown(void)
251{
252#ifdef WITH_THREAD
253 PyObject *result;
254 PyThreadState *tstate = PyThreadState_GET();
255 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
256 "threading");
257 if (threading == NULL) {
258 /* threading not imported */
259 PyErr_Clear();
260 return;
261 }
262 result = PyObject_CallMethod(threading, "_shutdown", "");
263 if (result == NULL)
264 PyErr_WriteUnraisable(threading);
265 else
266 Py_DECREF(result);
267 Py_DECREF(threading);
268#endif
269}
270
Guido van Rossum667d7041995-08-04 04:20:48 +0000271/* Main program */
272
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000273int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000274Py_Main(int argc, wchar_t **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000275{
276 int c;
277 int sts;
278 char *command = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000279 wchar_t *filename = NULL;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000280 char *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000281 FILE *fp = stdin;
282 char *p;
Guido van Rossum667d7041995-08-04 04:20:48 +0000283 int unbuffered = 0;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000284 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000285 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000286 int help = 0;
287 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000288 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000289 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000290
Guido van Rossum393661d2001-08-31 17:40:15 +0000291 cf.cf_flags = 0;
292
Guido van Rossumac56b031996-07-21 02:33:38 +0000293 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000294 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000295
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000296 PySys_ResetWarnOptions();
297
Guido van Rossumbceccf52001-04-10 22:07:43 +0000298 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000299 if (c == 'c') {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000300 size_t r1 = wcslen(_PyOS_optarg) + 2;
301 size_t r2;
Guido van Rossum667d7041995-08-04 04:20:48 +0000302 /* -c is the last option; following arguments
303 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000304 command to interpret. */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000305 command = (char *)malloc(r1);
Guido van Rossum667d7041995-08-04 04:20:48 +0000306 if (command == NULL)
307 Py_FatalError(
308 "not enough memory to copy -c argument");
Martin v. Löwis790465f2008-04-05 20:41:37 +0000309 r2 = wcstombs(command, _PyOS_optarg, r1);
310 if (r2 > r1-2)
311 Py_FatalError(
312 "not enough memory to copy -c argument");
Guido van Rossum667d7041995-08-04 04:20:48 +0000313 strcat(command, "\n");
314 break;
315 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000316
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000317 if (c == 'm') {
318 /* -m is the last option; following arguments
319 that look like options are left for the
320 module to interpret. */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000321 size_t r1 = wcslen(_PyOS_optarg) + 1;
322 size_t r2;
323 module = (char *)malloc(r1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000324 if (module == NULL)
325 Py_FatalError(
326 "not enough memory to copy -m argument");
Martin v. Löwis790465f2008-04-05 20:41:37 +0000327 r2 = wcstombs(module, _PyOS_optarg, r1);
328 if (r2 >= r1)
329 Py_FatalError(
330 "not enough memory to copy -m argument");
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000331 break;
332 }
333
Guido van Rossum667d7041995-08-04 04:20:48 +0000334 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000335 case 'b':
336 Py_BytesWarningFlag++;
337 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000338
339 case 'd':
340 Py_DebugFlag++;
341 break;
342
343 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000344 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000345 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000346 break;
347
Guido van Rossum7614da61997-03-03 19:14:45 +0000348 case 'O':
349 Py_OptimizeFlag++;
350 break;
351
Christian Heimes790c8232008-01-07 21:14:23 +0000352 case 'B':
353 Py_DontWriteBytecodeFlag++;
354 break;
355
Guido van Rossum7922bd71997-08-29 22:34:47 +0000356 case 'S':
357 Py_NoSiteFlag++;
358 break;
359
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000360 case 'E':
361 Py_IgnoreEnvironmentFlag++;
362 break;
363
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000364 case 't':
365 Py_TabcheckFlag++;
366 break;
367
Guido van Rossum667d7041995-08-04 04:20:48 +0000368 case 'u':
369 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000370 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000371 break;
372
373 case 'v':
374 Py_VerboseFlag++;
375 break;
376
Guido van Rossuma075ce11997-12-05 21:56:45 +0000377 case 'x':
378 skipfirstline = 1;
379 break;
380
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000381 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000383 help++;
384 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000385
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000386 case 'V':
387 version++;
388 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000389
Martin v. Löwis790465f2008-04-05 20:41:37 +0000390 case 'W':
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000391 PySys_AddWarnOption(_PyOS_optarg);
392 break;
393
Guido van Rossum667d7041995-08-04 04:20:48 +0000394 /* This space reserved for other options */
395
396 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000397 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 /*NOTREACHED*/
399
400 }
401 }
402
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000403 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000404 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000405
406 if (version) {
407 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000408 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000409 }
410
Guido van Rossumd8faa362007-04-27 19:54:29 +0000411 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000412 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000413 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000414 if (!saw_unbuffered_flag &&
415 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
416 unbuffered = 1;
417
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000418 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Martin v. Löwis790465f2008-04-05 20:41:37 +0000419 wcscmp(argv[_PyOS_optind], L"-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000420 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000421#ifdef __VMS
422 filename = decc$translate_vms(argv[_PyOS_optind]);
423 if (filename == (char *)0 || filename == (char *)-1)
424 filename = argv[_PyOS_optind];
425
426#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000427 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000428#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000429 }
430
431 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
432
Guido van Rossum667d7041995-08-04 04:20:48 +0000433 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000434#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000435 _setmode(fileno(stdin), O_BINARY);
436 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000437#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000438#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000439 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
440 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
441 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000442#else /* !HAVE_SETVBUF */
443 setbuf(stdin, (char *)NULL);
444 setbuf(stdout, (char *)NULL);
445 setbuf(stderr, (char *)NULL);
446#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000447 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000448 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000449#ifdef MS_WINDOWS
450 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000451 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000452 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000453#else /* !MS_WINDOWS */
454#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000455 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
456 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000457#endif /* HAVE_SETVBUF */
458#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000459 /* Leave stderr alone - it should be unbuffered anyway. */
460 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000461#ifdef __VMS
462 else {
463 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
464 }
465#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000466
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000467#ifdef __APPLE__
468 /* On MacOS X, when the Python interpreter is embedded in an
469 application bundle, it gets executed by a bootstrapping script
470 that does os.execve() with an argv[0] that's different from the
471 actual Python executable. This is needed to keep the Finder happy,
472 or rather, to work around Apple's overly strict requirements of
473 the process name. However, we still need a usable sys.executable,
474 so the actual executable path is passed in an environment variable.
475 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
476 script. */
477 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
478 Py_SetProgramName(p);
479 else
480 Py_SetProgramName(argv[0]);
481#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000482 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000483#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000484 Py_Initialize();
485
Guido van Rossum667d7041995-08-04 04:20:48 +0000486 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000487 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000488 fprintf(stderr, "Python %s on %s\n",
489 Py_GetVersion(), Py_GetPlatform());
490 if (!Py_NoSiteFlag)
491 fprintf(stderr, "%s\n", COPYRIGHT);
492 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000493
Guido van Rossum667d7041995-08-04 04:20:48 +0000494 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000495 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
496 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000497 argv[_PyOS_optind] = L"-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000498 }
499
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000500 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
502 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000503 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000504 argv[_PyOS_optind] = L"-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000505 }
506
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000507 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000508
Guido van Rossumd8faa362007-04-27 19:54:29 +0000509 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000510 isatty(fileno(stdin))) {
511 PyObject *v;
512 v = PyImport_ImportModule("readline");
513 if (v == NULL)
514 PyErr_Clear();
515 else
516 Py_DECREF(v);
517 }
518
Guido van Rossum667d7041995-08-04 04:20:48 +0000519 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000520 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000521 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000522 } else if (module) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000523 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000524 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000525 }
526 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000527
Guido van Rossum775af911997-02-14 19:50:32 +0000528 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000529 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000530 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000531 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000532 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000533
534 sts = -1; /* keep track of whether we've already run __main__ */
535
536 if (filename != NULL) {
537 sts = RunMainFromImporter(filename);
538 }
539
540 if (sts==-1 && filename!=NULL) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000541 if ((fp = _wfopen(filename, L"r")) == NULL) {
542 fprintf(stderr, "%s: can't open file '%ls': [Errno %d] %s\n",
Christian Heimes9cd17752007-11-18 19:35:23 +0000543 argv[0], filename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000544
Christian Heimes9cd17752007-11-18 19:35:23 +0000545 return 2;
546 }
547 else if (skipfirstline) {
548 int ch;
549 /* Push back first newline so line numbers
550 remain the same */
551 while ((ch = getc(fp)) != EOF) {
552 if (ch == '\n') {
553 (void)ungetc(ch, fp);
554 break;
555 }
556 }
557 }
558 {
559 /* XXX: does this work on Win/Win64? (see posix_fstat) */
560 struct stat sb;
561 if (fstat(fileno(fp), &sb) == 0 &&
562 S_ISDIR(sb.st_mode)) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000563 fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes679db4a2008-01-18 09:56:22 +0000564 fclose(fp);
Christian Heimes9cd17752007-11-18 19:35:23 +0000565 return 1;
566 }
567 }
568 }
569
570 if (sts==-1) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000571 char cfilename[PATH_MAX];
572 char *p_cfilename = "<stdin>";
573 if (filename) {
574 size_t r = wcstombs(cfilename, filename, PATH_MAX);
575 p_cfilename = cfilename;
576 if (r == (size_t)-1 || r >= PATH_MAX)
577 p_cfilename = "<decoding error>";
578 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000579 sts = PyRun_AnyFileExFlags(
580 fp,
Martin v. Löwis790465f2008-04-05 20:41:37 +0000581 p_cfilename,
Christian Heimes9cd17752007-11-18 19:35:23 +0000582 filename != NULL, &cf) != 0;
583 }
584
Guido van Rossum667d7041995-08-04 04:20:48 +0000585 }
586
Barry Warsawd86dcd32003-06-29 17:07:06 +0000587 /* Check this environment variable at the end, to give programs the
588 * opportunity to set it from Python.
589 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000590 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000591 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
592 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000593 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000594 }
595
Guido van Rossumd8faa362007-04-27 19:54:29 +0000596 if (Py_InspectFlag && stdin_is_interactive &&
597 (filename != NULL || command != NULL || module != NULL)) {
598 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000599 /* XXX */
600 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000601 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000602
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000603 WaitForThreadShutdown();
604
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000605 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000606
607#ifdef __INSURE__
608 /* Insure++ is a memory analysis tool that aids in discovering
609 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000610 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000611 * (which it is). Under normal circumstances, this is fine because
612 * the memory will be automatically reclaimed by the system. Under
613 * memory debugging, it's a huge source of useless noise, so we
614 * trade off slower shutdown for less distraction in the memory
615 * reports. -baw
616 */
617 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000618 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000619#endif /* __INSURE__ */
620
Guido van Rossum05f7c501997-08-02 03:00:42 +0000621 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000622}
623
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000624/* this is gonna seem *real weird*, but if you put some other code between
625 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
626 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000627
Guido van Rossum667d7041995-08-04 04:20:48 +0000628/* Make the *original* argc/argv available to other modules.
629 This is rare, but it is needed by the secureware extension. */
630
631void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000632Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000633{
634 *argc = orig_argc;
635 *argv = orig_argv;
636}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637
638#ifdef __cplusplus
639}
640#endif