blob: 0fd59c98711b7dbb1aa7a832b55fbbdd7a16fd8d [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 */
Christian Heimes33fe8092008-04-13 13:53:33 +000048#define BASE_OPTS L"bBc:dEhiJm: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
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 /* case 'J': reserved for Jython */
349
Guido van Rossum7614da61997-03-03 19:14:45 +0000350 case 'O':
351 Py_OptimizeFlag++;
352 break;
353
Christian Heimes790c8232008-01-07 21:14:23 +0000354 case 'B':
355 Py_DontWriteBytecodeFlag++;
356 break;
357
Guido van Rossum7922bd71997-08-29 22:34:47 +0000358 case 'S':
359 Py_NoSiteFlag++;
360 break;
361
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000362 case 'E':
363 Py_IgnoreEnvironmentFlag++;
364 break;
365
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000366 case 't':
367 Py_TabcheckFlag++;
368 break;
369
Guido van Rossum667d7041995-08-04 04:20:48 +0000370 case 'u':
371 unbuffered++;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000372 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000373 break;
374
375 case 'v':
376 Py_VerboseFlag++;
377 break;
378
Guido van Rossuma075ce11997-12-05 21:56:45 +0000379 case 'x':
380 skipfirstline = 1;
381 break;
382
Neal Norwitz32dde222008-04-15 06:43:13 +0000383 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes33fe8092008-04-13 13:53:33 +0000384
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000385 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000386 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000387 help++;
388 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000389
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000390 case 'V':
391 version++;
392 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000393
Martin v. Löwis790465f2008-04-05 20:41:37 +0000394 case 'W':
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000395 PySys_AddWarnOption(_PyOS_optarg);
396 break;
397
Guido van Rossum667d7041995-08-04 04:20:48 +0000398 /* This space reserved for other options */
399
400 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000401 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000402 /*NOTREACHED*/
403
404 }
405 }
406
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000407 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000408 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000409
410 if (version) {
411 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000412 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000413 }
414
Guido van Rossumd8faa362007-04-27 19:54:29 +0000415 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000416 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000417 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000418 if (!saw_unbuffered_flag &&
419 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
420 unbuffered = 1;
421
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000422 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Martin v. Löwis790465f2008-04-05 20:41:37 +0000423 wcscmp(argv[_PyOS_optind], L"-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000424 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000425#ifdef __VMS
426 filename = decc$translate_vms(argv[_PyOS_optind]);
427 if (filename == (char *)0 || filename == (char *)-1)
428 filename = argv[_PyOS_optind];
429
430#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000431 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000432#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000433 }
434
435 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
436
Guido van Rossum667d7041995-08-04 04:20:48 +0000437 if (unbuffered) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000438#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000439 _setmode(fileno(stdin), O_BINARY);
440 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000441#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000442#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000443 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
444 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
445 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000446#else /* !HAVE_SETVBUF */
447 setbuf(stdin, (char *)NULL);
448 setbuf(stdout, (char *)NULL);
449 setbuf(stderr, (char *)NULL);
450#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000451 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000452 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000453#ifdef MS_WINDOWS
454 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000455 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000456 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000457#else /* !MS_WINDOWS */
458#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000459 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
460 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000461#endif /* HAVE_SETVBUF */
462#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000463 /* Leave stderr alone - it should be unbuffered anyway. */
464 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000465#ifdef __VMS
466 else {
467 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
468 }
469#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000470
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000471#ifdef __APPLE__
472 /* On MacOS X, when the Python interpreter is embedded in an
473 application bundle, it gets executed by a bootstrapping script
474 that does os.execve() with an argv[0] that's different from the
475 actual Python executable. This is needed to keep the Finder happy,
476 or rather, to work around Apple's overly strict requirements of
477 the process name. However, we still need a usable sys.executable,
478 so the actual executable path is passed in an environment variable.
479 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
480 script. */
481 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
482 Py_SetProgramName(p);
483 else
484 Py_SetProgramName(argv[0]);
485#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000486 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000487#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000488 Py_Initialize();
489
Guido van Rossum667d7041995-08-04 04:20:48 +0000490 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000491 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000492 fprintf(stderr, "Python %s on %s\n",
493 Py_GetVersion(), Py_GetPlatform());
494 if (!Py_NoSiteFlag)
495 fprintf(stderr, "%s\n", COPYRIGHT);
496 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000497
Guido van Rossum667d7041995-08-04 04:20:48 +0000498 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000499 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
500 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000501 argv[_PyOS_optind] = L"-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000502 }
503
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000504 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
506 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000507 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000508 argv[_PyOS_optind] = L"-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000509 }
510
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000511 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000512
Guido van Rossumd8faa362007-04-27 19:54:29 +0000513 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000514 isatty(fileno(stdin))) {
515 PyObject *v;
516 v = PyImport_ImportModule("readline");
517 if (v == NULL)
518 PyErr_Clear();
519 else
520 Py_DECREF(v);
521 }
522
Guido van Rossum667d7041995-08-04 04:20:48 +0000523 if (command) {
Guido van Rossum393661d2001-08-31 17:40:15 +0000524 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
Guido van Rossum05f7c501997-08-02 03:00:42 +0000525 free(command);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000526 } else if (module) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000527 sts = RunModule(module, 1);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000528 free(module);
Guido van Rossum667d7041995-08-04 04:20:48 +0000529 }
530 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000531
Guido van Rossum775af911997-02-14 19:50:32 +0000532 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000533 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000534 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000535 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000536 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000537
538 sts = -1; /* keep track of whether we've already run __main__ */
539
540 if (filename != NULL) {
541 sts = RunMainFromImporter(filename);
542 }
543
544 if (sts==-1 && filename!=NULL) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000545 if ((fp = _wfopen(filename, L"r")) == NULL) {
Martin v. Löwis99ddc8c2008-04-06 17:57:16 +0000546 fprintf(stderr, "%ls: can't open file '%ls': [Errno %d] %s\n",
Christian Heimes9cd17752007-11-18 19:35:23 +0000547 argv[0], filename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000548
Christian Heimes9cd17752007-11-18 19:35:23 +0000549 return 2;
550 }
551 else if (skipfirstline) {
552 int ch;
553 /* Push back first newline so line numbers
554 remain the same */
555 while ((ch = getc(fp)) != EOF) {
556 if (ch == '\n') {
557 (void)ungetc(ch, fp);
558 break;
559 }
560 }
561 }
562 {
563 /* XXX: does this work on Win/Win64? (see posix_fstat) */
564 struct stat sb;
565 if (fstat(fileno(fp), &sb) == 0 &&
566 S_ISDIR(sb.st_mode)) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000567 fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes679db4a2008-01-18 09:56:22 +0000568 fclose(fp);
Christian Heimes9cd17752007-11-18 19:35:23 +0000569 return 1;
570 }
571 }
572 }
573
574 if (sts==-1) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000575 char cfilename[PATH_MAX];
576 char *p_cfilename = "<stdin>";
577 if (filename) {
578 size_t r = wcstombs(cfilename, filename, PATH_MAX);
579 p_cfilename = cfilename;
580 if (r == (size_t)-1 || r >= PATH_MAX)
581 p_cfilename = "<decoding error>";
582 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000583 sts = PyRun_AnyFileExFlags(
584 fp,
Martin v. Löwis790465f2008-04-05 20:41:37 +0000585 p_cfilename,
Christian Heimes9cd17752007-11-18 19:35:23 +0000586 filename != NULL, &cf) != 0;
587 }
588
Guido van Rossum667d7041995-08-04 04:20:48 +0000589 }
590
Barry Warsawd86dcd32003-06-29 17:07:06 +0000591 /* Check this environment variable at the end, to give programs the
592 * opportunity to set it from Python.
593 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000594 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000595 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
596 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000597 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000598 }
599
Guido van Rossumd8faa362007-04-27 19:54:29 +0000600 if (Py_InspectFlag && stdin_is_interactive &&
601 (filename != NULL || command != NULL || module != NULL)) {
602 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000603 /* XXX */
604 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000606
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000607 WaitForThreadShutdown();
608
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000609 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000610
611#ifdef __INSURE__
612 /* Insure++ is a memory analysis tool that aids in discovering
613 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000614 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000615 * (which it is). Under normal circumstances, this is fine because
616 * the memory will be automatically reclaimed by the system. Under
617 * memory debugging, it's a huge source of useless noise, so we
618 * trade off slower shutdown for less distraction in the memory
619 * reports. -baw
620 */
621 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000622 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000623#endif /* __INSURE__ */
624
Guido van Rossum05f7c501997-08-02 03:00:42 +0000625 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000626}
627
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000628/* this is gonna seem *real weird*, but if you put some other code between
629 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
630 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000631
Guido van Rossum667d7041995-08-04 04:20:48 +0000632/* Make the *original* argc/argv available to other modules.
633 This is rare, but it is needed by the secureware extension. */
634
635void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000636Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000637{
638 *argc = orig_argc;
639 *argv = orig_argv;
640}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641
642#ifdef __cplusplus
643}
644#endif