blob: 54cfe4a65ddd75e07e8c63dc5e55e8c6a6802d15 [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
Antoine Pitrou5651eaa2008-09-06 20:46:58 +00007#include <locale.h>
8
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00009#ifdef __VMS
Martin v. Löwis7a924e62003-03-05 14:15:21 +000010#include <unixlib.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000011#endif
12
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +000013#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Martin v. Löwis945362c2007-08-30 14:57:25 +000014#include <windows.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000015#ifdef HAVE_FCNTL_H
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000016#include <fcntl.h>
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#define PATH_MAX MAXPATHLEN
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000018#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000019#endif
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +000020
Martin v. Löwis945362c2007-08-30 14:57:25 +000021#ifdef _MSC_VER
22#include <crtdbg.h>
23#endif
24
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000025#if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
Guido van Rossuma075ce11997-12-05 21:56:45 +000026#define PYTHONHOMEHELP "<prefix>\\lib"
27#else
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000028#if defined(PYOS_OS2) && defined(PYCC_GCC)
29#define PYTHONHOMEHELP "<prefix>/Lib"
30#else
Marc-André Lemburgda4dbc32001-06-12 16:13:51 +000031#define PYTHONHOMEHELP "<prefix>/pythonX.X"
Guido van Rossuma075ce11997-12-05 21:56:45 +000032#endif
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000033#endif
Guido van Rossuma075ce11997-12-05 21:56:45 +000034
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000035#include "pygetopt.h"
36
Guido van Rossuma22865e2000-09-05 04:41:18 +000037#define COPYRIGHT \
Guido van Rossum36002d72001-07-18 16:59:46 +000038 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
39 "for more information."
Guido van Rossuma22865e2000-09-05 04:41:18 +000040
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041#ifdef __cplusplus
42extern "C" {
43#endif
44
Guido van Rossumac56b031996-07-21 02:33:38 +000045/* For Py_GetArgcArgv(); set by main() */
Martin v. Löwis790465f2008-04-05 20:41:37 +000046static wchar_t **orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +000047static int orig_argc;
48
Guido van Rossumbceccf52001-04-10 22:07:43 +000049/* command line options */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010050#define BASE_OPTS L"bBc:dEhiJm:ORsStuvVW:xX?"
Guido van Rossumbceccf52001-04-10 22:07:43 +000051
Guido van Rossumbceccf52001-04-10 22:07:43 +000052#define PROGRAM_OPTS BASE_OPTS
Guido van Rossum3ed4c152001-03-02 06:18:03 +000053
Guido van Rossum667d7041995-08-04 04:20:48 +000054/* Short usage message (with %s for argv0) */
55static char *usage_line =
Martin v. Löwis790465f2008-04-05 20:41:37 +000056"usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
Guido van Rossum667d7041995-08-04 04:20:48 +000057
58/* Long usage message, split into parts < 512 bytes */
Guido van Rossum393661d2001-08-31 17:40:15 +000059static char *usage_1 = "\
Guido van Rossum667d7041995-08-04 04:20:48 +000060Options and arguments (and corresponding environment variables):\n\
Christian Heimes2ab34442008-09-03 20:31:07 +000061-b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\
62 and comparing bytes/bytearray with str. (-bb: issue errors)\n\
Christian Heimes790c8232008-01-07 21:14:23 +000063-B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000064-c cmd : program passed in as string (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000065-d : debug output from parser; also PYTHONDEBUG=x\n\
Christian Heimes790c8232008-01-07 21:14:23 +000066-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067-h : print this help message and exit (also --help)\n\
Guido van Rossum61c345f2001-09-04 03:26:15 +000068";
69static char *usage_2 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000070-i : inspect interactively after running script; forces a prompt even\n\
71 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000072-m mod : run library module as a script (terminates option list)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000073-O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
Guido van Rossum6b86a421999-01-28 15:07:47 +000074-OO : remove doc-strings in addition to the -O optimizations\n\
Georg Brandl2daf6ae2012-02-20 19:54:16 +010075-R : use a pseudo-random salt to make hash() values of various types be\n\
76 unpredictable between separate invocations of the interpreter, as\n\
77 a defence against denial-of-service attacks\n\
Christian Heimes8dc226f2008-05-06 23:45:46 +000078-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000079-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000080";
81static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000082-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000083 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000084-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
85 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000086-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000087-W arg : warning control; arg is action:message:category:module:lineno\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000088-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000089";
Guido van Rossum393661d2001-08-31 17:40:15 +000090static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000091file : program read from script file\n\
92- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000093arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000094Other environment variables:\n\
95PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000096PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000097 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +000098";
99static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +0000100PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
101 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000102PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis0f599892008-06-02 11:13:03 +0000103PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000104";
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100105static char *usage_6 = "\
Georg Brandlc9a42072012-02-21 22:36:27 +0100106PYTHONHASHSEED: if this variable is set to 'random', the effect is the same\n\
107 as specifying the -R option: a random value is used to seed the hashes of\n\
108 str, bytes and datetime objects. It can also be set to an integer\n\
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100109 in the range [0,4294967295] to get hash values with a predictable seed.\n\
110";
Guido van Rossum667d7041995-08-04 04:20:48 +0000111
Martin v. Löwis790465f2008-04-05 20:41:37 +0000112#ifndef MS_WINDOWS
113static FILE*
114_wfopen(const wchar_t *path, const wchar_t *mode)
115{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 char cpath[PATH_MAX];
117 char cmode[10];
118 size_t r;
119 r = wcstombs(cpath, path, PATH_MAX);
120 if (r == (size_t)-1 || r >= PATH_MAX) {
121 errno = EINVAL;
122 return NULL;
123 }
124 r = wcstombs(cmode, mode, 10);
125 if (r == (size_t)-1 || r >= 10) {
126 errno = EINVAL;
127 return NULL;
128 }
129 return fopen(cpath, cmode);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000130}
131#endif
132
Guido van Rossum667d7041995-08-04 04:20:48 +0000133
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000134static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000135usage(int exitcode, wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000136{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000137 FILE *f = exitcode ? stderr : stdout;
Guido van Rossum393661d2001-08-31 17:40:15 +0000138
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000139 fprintf(f, usage_line, program);
140 if (exitcode)
141 fprintf(f, "Try `python -h' for more information.\n");
142 else {
143 fputs(usage_1, f);
144 fputs(usage_2, f);
145 fputs(usage_3, f);
146 fprintf(f, usage_4, DELIM);
147 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100148 fputs(usage_6, f);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000150#if defined(__VMS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000151 if (exitcode == 0) {
152 /* suppress 'error' message */
153 return 1;
154 }
155 else {
156 /* STS$M_INHIB_MSG + SS$_ABORT */
157 return 0x1000002c;
158 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000159#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000160 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000161#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000162 /*NOTREACHED*/
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000163}
164
Martin v. Löwis6caea372003-11-18 19:46:25 +0000165static void RunStartupFile(PyCompilerFlags *cf)
166{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000167 char *startup = Py_GETENV("PYTHONSTARTUP");
168 if (startup != NULL && startup[0] != '\0') {
169 FILE *fp = fopen(startup, "r");
170 if (fp != NULL) {
171 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
172 PyErr_Clear();
173 fclose(fp);
174 } else {
175 int save_errno;
176
177 save_errno = errno;
178 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
179 errno = save_errno;
180 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
181 startup);
182 PyErr_Print();
183 PyErr_Clear();
184 }
185 }
Martin v. Löwis6caea372003-11-18 19:46:25 +0000186}
187
Thomas Woutersa9773292006-04-21 09:43:23 +0000188
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000189static int RunModule(wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000190{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000191 PyObject *module, *runpy, *runmodule, *runargs, *result;
192 runpy = PyImport_ImportModule("runpy");
193 if (runpy == NULL) {
194 fprintf(stderr, "Could not import runpy module\n");
195 return -1;
196 }
197 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
198 if (runmodule == NULL) {
199 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
200 Py_DECREF(runpy);
201 return -1;
202 }
203 module = PyUnicode_FromWideChar(modname, wcslen(modname));
204 if (module == NULL) {
205 fprintf(stderr, "Could not convert module name to unicode\n");
206 Py_DECREF(runpy);
207 Py_DECREF(runmodule);
208 return -1;
209 }
210 runargs = Py_BuildValue("(Oi)", module, set_argv0);
211 if (runargs == NULL) {
212 fprintf(stderr,
213 "Could not create arguments for runpy._run_module_as_main\n");
214 Py_DECREF(runpy);
215 Py_DECREF(runmodule);
216 Py_DECREF(module);
217 return -1;
218 }
219 result = PyObject_Call(runmodule, runargs, NULL);
220 if (result == NULL) {
221 PyErr_Print();
222 }
223 Py_DECREF(runpy);
224 Py_DECREF(runmodule);
225 Py_DECREF(module);
226 Py_DECREF(runargs);
227 if (result == NULL) {
228 return -1;
229 }
230 Py_DECREF(result);
231 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000232}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000233
Martin v. Löwis790465f2008-04-05 20:41:37 +0000234static int RunMainFromImporter(wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000235{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 PyObject *argv0 = NULL, *importer = NULL;
Christian Heimes9cd17752007-11-18 19:35:23 +0000237
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
239 (importer = PyImport_GetImporter(argv0)) &&
240 (importer->ob_type != &PyNullImporter_Type))
241 {
242 /* argv0 is usable as an import source, so
243 put it in sys.path[0] and import __main__ */
244 PyObject *sys_path = NULL;
245 if ((sys_path = PySys_GetObject("path")) &&
246 !PyList_SetItem(sys_path, 0, argv0))
247 {
248 Py_INCREF(argv0);
249 Py_DECREF(importer);
250 sys_path = NULL;
251 return RunModule(L"__main__", 0) != 0;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000252 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000253 }
254 Py_XDECREF(argv0);
255 Py_XDECREF(importer);
256 if (PyErr_Occurred()) {
257 PyErr_Print();
258 return 1;
259 }
260 else {
261 return -1;
262 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000263}
264
265
Guido van Rossum667d7041995-08-04 04:20:48 +0000266/* Main program */
267
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000268int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000269Py_Main(int argc, wchar_t **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000270{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000271 int c;
272 int sts;
273 wchar_t *command = NULL;
274 wchar_t *filename = NULL;
275 wchar_t *module = NULL;
276 FILE *fp = stdin;
277 char *p;
278 int skipfirstline = 0;
279 int stdin_is_interactive = 0;
280 int help = 0;
281 int version = 0;
282 int saw_unbuffered_flag = 0;
283 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000284
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 cf.cf_flags = 0;
Guido van Rossum393661d2001-08-31 17:40:15 +0000286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000287 orig_argc = argc; /* For Py_GetArgcArgv() */
288 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000289
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000290 PySys_ResetWarnOptions();
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000291
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000292 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
293 if (c == 'c') {
294 size_t len;
295 /* -c is the last option; following arguments
296 that look like options are left for the
297 command to interpret. */
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000298
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000299 len = wcslen(_PyOS_optarg) + 1 + 1;
300 command = (wchar_t *)malloc(sizeof(wchar_t) * len);
301 if (command == NULL)
302 Py_FatalError(
303 "not enough memory to copy -c argument");
304 wcscpy(command, _PyOS_optarg);
305 command[len - 2] = '\n';
306 command[len - 1] = 0;
307 break;
308 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000309
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000310 if (c == 'm') {
311 /* -m is the last option; following arguments
312 that look like options are left for the
313 module to interpret. */
314 module = _PyOS_optarg;
315 break;
316 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000318 switch (c) {
319 case 'b':
320 Py_BytesWarningFlag++;
321 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000322
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 case 'd':
324 Py_DebugFlag++;
325 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000326
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000327 case 'i':
328 Py_InspectFlag++;
329 Py_InteractiveFlag++;
330 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 /* case 'J': reserved for Jython */
Christian Heimes33fe8092008-04-13 13:53:33 +0000333
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000334 case 'O':
335 Py_OptimizeFlag++;
336 break;
Guido van Rossum7614da61997-03-03 19:14:45 +0000337
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000338 case 'B':
339 Py_DontWriteBytecodeFlag++;
340 break;
Christian Heimes790c8232008-01-07 21:14:23 +0000341
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 case 's':
343 Py_NoUserSiteDirectory++;
344 break;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000345
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000346 case 'S':
347 Py_NoSiteFlag++;
348 break;
Guido van Rossum7922bd71997-08-29 22:34:47 +0000349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350 case 'E':
351 Py_IgnoreEnvironmentFlag++;
352 break;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000353
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000354 case 't':
355 /* ignored for backwards compatibility */
356 break;
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000357
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000358 case 'u':
359 Py_UnbufferedStdioFlag = 1;
360 saw_unbuffered_flag = 1;
361 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000362
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000363 case 'v':
364 Py_VerboseFlag++;
365 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000366
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000367 case 'x':
368 skipfirstline = 1;
369 break;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000370
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes33fe8092008-04-13 13:53:33 +0000372
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000373 case 'h':
374 case '?':
375 help++;
376 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000377
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000378 case 'V':
379 version++;
380 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000381
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000382 case 'W':
383 PySys_AddWarnOption(_PyOS_optarg);
384 break;
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000385
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100386 case 'R':
387 Py_HashRandomizationFlag++;
388 break;
389
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 /* This space reserved for other options */
Guido van Rossum667d7041995-08-04 04:20:48 +0000391
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000392 default:
393 return usage(2, argv[0]);
394 /*NOTREACHED*/
Guido van Rossum667d7041995-08-04 04:20:48 +0000395
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000396 }
397 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000398
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000399 if (help)
400 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000401
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000402 if (version) {
403 fprintf(stderr, "Python %s\n", PY_VERSION);
404 return 0;
405 }
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000406
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000407 if (!Py_InspectFlag &&
408 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
409 Py_InspectFlag = 1;
410 if (!saw_unbuffered_flag &&
411 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
412 Py_UnbufferedStdioFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000413
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000414 if (!Py_NoUserSiteDirectory &&
415 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
416 Py_NoUserSiteDirectory = 1;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000417
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000418 if (command == NULL && module == NULL && _PyOS_optind < argc &&
419 wcscmp(argv[_PyOS_optind], L"-") != 0)
420 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000421#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000422 filename = decc$translate_vms(argv[_PyOS_optind]);
423 if (filename == (char *)0 || filename == (char *)-1)
424 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000425
426#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000427 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000428#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000429 }
Guido van Rossum775af911997-02-14 19:50:32 +0000430
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000431 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
Guido van Rossum775af911997-02-14 19:50:32 +0000432
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000433 if (Py_UnbufferedStdioFlag) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000434#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000443 setbuf(stdin, (char *)NULL);
444 setbuf(stdout, (char *)NULL);
445 setbuf(stderr, (char *)NULL);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000446#endif /* !HAVE_SETVBUF */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 }
448 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000449#ifdef MS_WINDOWS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000450 /* Doesn't have to have line-buffered -- use unbuffered */
451 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
452 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000453#else /* !MS_WINDOWS */
454#ifdef HAVE_SETVBUF
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 /* Leave stderr alone - it should be unbuffered anyway. */
460 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000461#ifdef __VMS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 else {
463 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
464 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000465#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000466
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000467#ifdef __APPLE__
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000468 /* 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 wchar_t* buffer;
479 size_t len = strlen(p);
480 size_t r;
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000481
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 buffer = malloc(len * sizeof(wchar_t));
483 if (buffer == NULL) {
484 Py_FatalError(
485 "not enough memory to copy PYTHONEXECUTABLE");
486 }
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000487
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000488 r = mbstowcs(buffer, p, len);
489 Py_SetProgramName(buffer);
490 /* buffer is now handed off - do not free */
491 } else {
492 Py_SetProgramName(argv[0]);
493 }
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000494#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000496#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000497 Py_Initialize();
Guido van Rossumed52aac1997-07-19 19:20:32 +0000498
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 if (Py_VerboseFlag ||
500 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
501 fprintf(stderr, "Python %s on %s\n",
502 Py_GetVersion(), Py_GetPlatform());
503 if (!Py_NoSiteFlag)
504 fprintf(stderr, "%s\n", COPYRIGHT);
505 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000506
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000507 if (command != NULL) {
508 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
509 _PyOS_optind--;
510 argv[_PyOS_optind] = L"-c";
511 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000512
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000513 if (module != NULL) {
514 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
515 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
516 _PyOS_optind--;
517 argv[_PyOS_optind] = L"-c";
518 }
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000520 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000521
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000522 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
523 isatty(fileno(stdin))) {
524 PyObject *v;
525 v = PyImport_ImportModule("readline");
526 if (v == NULL)
527 PyErr_Clear();
528 else
529 Py_DECREF(v);
530 }
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000531
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000532 if (command) {
Victor Stinner2e71d012010-05-17 09:35:44 +0000533 char *commandStr;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000534 PyObject *commandObj = PyUnicode_FromWideChar(
535 command, wcslen(command));
536 free(command);
Victor Stinner2e71d012010-05-17 09:35:44 +0000537 if (commandObj != NULL)
538 commandStr = _PyUnicode_AsString(commandObj);
539 else
540 commandStr = NULL;
541 if (commandStr != NULL) {
542 sts = PyRun_SimpleStringFlags(commandStr, &cf) != 0;
543 Py_DECREF(commandObj);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000544 }
545 else {
546 PyErr_Print();
547 sts = 1;
548 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000549 } else if (module) {
550 sts = RunModule(module, 1);
551 }
552 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000553
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 if (filename == NULL && stdin_is_interactive) {
555 Py_InspectFlag = 0; /* do exit on SystemExit */
556 RunStartupFile(&cf);
557 }
558 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000559
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 sts = -1; /* keep track of whether we've already run __main__ */
Christian Heimes9cd17752007-11-18 19:35:23 +0000561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 if (filename != NULL) {
563 sts = RunMainFromImporter(filename);
564 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000565
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000566 if (sts==-1 && filename!=NULL) {
567 if ((fp = _wfopen(filename, L"r")) == NULL) {
568 char cfilename[PATH_MAX];
569 size_t r = wcstombs(cfilename, filename, PATH_MAX);
570 if (r == PATH_MAX)
571 /* cfilename is not null-terminated;
572 * forcefully null-terminating it
573 * might break the shift state */
574 strcpy(cfilename, "<file name too long>");
575 if (r == ((size_t)-1))
576 strcpy(cfilename, "<unprintable file name>");
577 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
578 argv[0], cfilename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000579
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000580 return 2;
581 }
582 else if (skipfirstline) {
583 int ch;
584 /* Push back first newline so line numbers
585 remain the same */
586 while ((ch = getc(fp)) != EOF) {
587 if (ch == '\n') {
588 (void)ungetc(ch, fp);
589 break;
590 }
591 }
592 }
593 {
594 /* XXX: does this work on Win/Win64? (see posix_fstat) */
595 struct stat sb;
596 if (fstat(fileno(fp), &sb) == 0 &&
597 S_ISDIR(sb.st_mode)) {
598 fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
599 fclose(fp);
600 return 1;
601 }
602 }
603 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000604
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 if (sts==-1) {
606 PyObject *filenameObj = NULL;
607 char *p_cfilename = "<stdin>";
608 if (filename) {
609 filenameObj = PyUnicode_FromWideChar(
610 filename, wcslen(filename));
611 if (filenameObj != NULL)
612 p_cfilename = _PyUnicode_AsString(filenameObj);
613 else
614 p_cfilename = "<decoding error>";
615 }
616 /* call pending calls like signal handlers (SIGINT) */
617 if (Py_MakePendingCalls() == -1) {
618 PyErr_Print();
619 sts = 1;
620 } else {
621 sts = PyRun_AnyFileExFlags(
622 fp,
623 p_cfilename,
624 filename != NULL, &cf) != 0;
625 }
626 Py_XDECREF(filenameObj);
627 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 }
Barry Warsawd86dcd32003-06-29 17:07:06 +0000630
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 /* Check this environment variable at the end, to give programs the
632 * opportunity to set it from Python.
633 */
634 if (!Py_InspectFlag &&
635 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
636 {
637 Py_InspectFlag = 1;
638 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000639
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 if (Py_InspectFlag && stdin_is_interactive &&
641 (filename != NULL || command != NULL || module != NULL)) {
642 Py_InspectFlag = 0;
643 /* XXX */
644 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
645 }
646
647 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000648
649#ifdef __INSURE__
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 /* Insure++ is a memory analysis tool that aids in discovering
651 * memory leaks and other memory problems. On Python exit, the
652 * interned string dictionaries are flagged as being in use at exit
653 * (which it is). Under normal circumstances, this is fine because
654 * the memory will be automatically reclaimed by the system. Under
655 * memory debugging, it's a huge source of useless noise, so we
656 * trade off slower shutdown for less distraction in the memory
657 * reports. -baw
658 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000660#endif /* __INSURE__ */
661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000663}
664
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000665/* this is gonna seem *real weird*, but if you put some other code between
666 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
667 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000668
Guido van Rossum667d7041995-08-04 04:20:48 +0000669/* Make the *original* argc/argv available to other modules.
670 This is rare, but it is needed by the secureware extension. */
671
672void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000673Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000674{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000675 *argc = orig_argc;
676 *argv = orig_argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000677}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678
679#ifdef __cplusplus
680}
681#endif