blob: 95fae17f832a046bed3bd37cf320e3eed726ac92 [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 */
Christian Heimes8dc226f2008-05-06 23:45:46 +000050#define BASE_OPTS L"bBc:dEhiJm:OsStuvVW: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\
Christian Heimes8dc226f2008-05-06 23:45:46 +000075-s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000076-S : don't imply 'import site' on initialization\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000077";
78static char *usage_3 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000079-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +000080 see man page for details on internal buffering relating to '-u'\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000081-v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
82 can be supplied multiple times to increase verbosity\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083-V : print the Python version number and exit (also --version)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000084-W arg : warning control; arg is action:message:category:module:lineno\n\
Philip Jenvey0805ca32010-04-07 04:04:10 +000085 also PYTHONWARNINGS=arg\n\
Guido van Rossum393661d2001-08-31 17:40:15 +000086-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
Guido van Rossum7922bd71997-08-29 22:34:47 +000087";
Guido van Rossum393661d2001-08-31 17:40:15 +000088static char *usage_4 = "\
Guido van Rossum98297ee2007-11-06 21:34:58 +000089file : program read from script file\n\
90- : program read from stdin (default; interactive mode if a tty)\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +000091arg ...: arguments passed to program in sys.argv[1:]\n\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000092Other environment variables:\n\
93PYTHONSTARTUP: file executed on interactive startup (no default)\n\
Guido van Rossuma075ce11997-12-05 21:56:45 +000094PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
Guido van Rossum667d7041995-08-04 04:20:48 +000095 default module search path. The result is sys.path.\n\
Christian Heimes790c8232008-01-07 21:14:23 +000096";
97static char *usage_5 = "\
Guido van Rossuma075ce11997-12-05 21:56:45 +000098PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
99 The default module search path uses %s.\n\
Tim Peters793de092001-02-22 00:39:47 +0000100PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
Martin v. Löwis0f599892008-06-02 11:13:03 +0000101PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
Guido van Rossum667d7041995-08-04 04:20:48 +0000102";
103
Martin v. Löwis790465f2008-04-05 20:41:37 +0000104#ifndef MS_WINDOWS
105static FILE*
106_wfopen(const wchar_t *path, const wchar_t *mode)
107{
108 char cpath[PATH_MAX];
109 char cmode[10];
110 size_t r;
111 r = wcstombs(cpath, path, PATH_MAX);
112 if (r == (size_t)-1 || r >= PATH_MAX) {
113 errno = EINVAL;
114 return NULL;
115 }
116 r = wcstombs(cmode, mode, 10);
117 if (r == (size_t)-1 || r >= 10) {
118 errno = EINVAL;
119 return NULL;
120 }
121 return fopen(cpath, cmode);
122}
123#endif
124
Guido van Rossum667d7041995-08-04 04:20:48 +0000125
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000126static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000127usage(int exitcode, wchar_t* program)
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000128{
Guido van Rossum393661d2001-08-31 17:40:15 +0000129 FILE *f = exitcode ? stderr : stdout;
130
131 fprintf(f, usage_line, program);
132 if (exitcode)
133 fprintf(f, "Try `python -h' for more information.\n");
134 else {
Alexandre Vassalotticf7128c2009-07-17 08:31:44 +0000135 fputs(usage_1, f);
136 fputs(usage_2, f);
137 fputs(usage_3, f);
Christian Heimes790c8232008-01-07 21:14:23 +0000138 fprintf(f, usage_4, DELIM);
139 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
Guido van Rossum393661d2001-08-31 17:40:15 +0000140 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000141#if defined(__VMS)
142 if (exitcode == 0) {
143 /* suppress 'error' message */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000144 return 1;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000145 }
146 else {
147 /* STS$M_INHIB_MSG + SS$_ABORT */
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000148 return 0x1000002c;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000149 }
150#else
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000151 return exitcode;
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000152#endif
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000153 /*NOTREACHED*/
154}
155
Martin v. Löwis6caea372003-11-18 19:46:25 +0000156static void RunStartupFile(PyCompilerFlags *cf)
157{
158 char *startup = Py_GETENV("PYTHONSTARTUP");
159 if (startup != NULL && startup[0] != '\0') {
160 FILE *fp = fopen(startup, "r");
161 if (fp != NULL) {
162 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
163 PyErr_Clear();
164 fclose(fp);
Christian Heimese69a08e2007-11-14 16:21:32 +0000165 } else {
166 int save_errno;
167
168 save_errno = errno;
169 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
170 errno = save_errno;
171 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
172 startup);
173 PyErr_Print();
174 PyErr_Clear();
Martin v. Löwis6caea372003-11-18 19:46:25 +0000175 }
176 }
177}
178
Thomas Woutersa9773292006-04-21 09:43:23 +0000179
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000180static int RunModule(wchar_t *modname, int set_argv0)
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000181{
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000182 PyObject *module, *runpy, *runmodule, *runargs, *result;
Thomas Woutersa9773292006-04-21 09:43:23 +0000183 runpy = PyImport_ImportModule("runpy");
184 if (runpy == NULL) {
185 fprintf(stderr, "Could not import runpy module\n");
186 return -1;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000187 }
Thomas Woutersed03b412007-08-28 21:37:11 +0000188 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
Thomas Woutersa9773292006-04-21 09:43:23 +0000189 if (runmodule == NULL) {
Thomas Woutersed03b412007-08-28 21:37:11 +0000190 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000191 Py_DECREF(runpy);
192 return -1;
193 }
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000194 module = PyUnicode_FromWideChar(modname, wcslen(modname));
195 if (module == NULL) {
196 fprintf(stderr, "Could not convert module name to unicode\n");
197 Py_DECREF(runpy);
198 Py_DECREF(runmodule);
199 return -1;
200 }
201 runargs = Py_BuildValue("(Oi)", module, set_argv0);
Thomas Woutersa9773292006-04-21 09:43:23 +0000202 if (runargs == NULL) {
203 fprintf(stderr,
Thomas Woutersed03b412007-08-28 21:37:11 +0000204 "Could not create arguments for runpy._run_module_as_main\n");
Thomas Woutersa9773292006-04-21 09:43:23 +0000205 Py_DECREF(runpy);
206 Py_DECREF(runmodule);
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000207 Py_DECREF(module);
Thomas Woutersa9773292006-04-21 09:43:23 +0000208 return -1;
209 }
210 result = PyObject_Call(runmodule, runargs, NULL);
211 if (result == NULL) {
212 PyErr_Print();
213 }
214 Py_DECREF(runpy);
215 Py_DECREF(runmodule);
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000216 Py_DECREF(module);
Thomas Woutersa9773292006-04-21 09:43:23 +0000217 Py_DECREF(runargs);
218 if (result == NULL) {
219 return -1;
220 }
221 Py_DECREF(result);
222 return 0;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000223}
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000224
Martin v. Löwis790465f2008-04-05 20:41:37 +0000225static int RunMainFromImporter(wchar_t *filename)
Christian Heimes9cd17752007-11-18 19:35:23 +0000226{
227 PyObject *argv0 = NULL, *importer = NULL;
228
Martin v. Löwis790465f2008-04-05 20:41:37 +0000229 if ((argv0 = PyUnicode_FromWideChar(filename,wcslen(filename))) &&
Guido van Rossum74c29c72007-11-19 18:36:41 +0000230 (importer = PyImport_GetImporter(argv0)) &&
231 (importer->ob_type != &PyNullImporter_Type))
Christian Heimes9cd17752007-11-18 19:35:23 +0000232 {
233 /* argv0 is usable as an import source, so
234 put it in sys.path[0] and import __main__ */
235 PyObject *sys_path = NULL;
Guido van Rossum74c29c72007-11-19 18:36:41 +0000236 if ((sys_path = PySys_GetObject("path")) &&
237 !PyList_SetItem(sys_path, 0, argv0))
238 {
Christian Heimes9cd17752007-11-18 19:35:23 +0000239 Py_INCREF(argv0);
Guido van Rossum74c29c72007-11-19 18:36:41 +0000240 Py_DECREF(importer);
Christian Heimes9cd17752007-11-18 19:35:23 +0000241 sys_path = NULL;
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000242 return RunModule(L"__main__", 0) != 0;
Christian Heimes9cd17752007-11-18 19:35:23 +0000243 }
244 }
Guido van Rossum74c29c72007-11-19 18:36:41 +0000245 Py_XDECREF(argv0);
246 Py_XDECREF(importer);
247 if (PyErr_Occurred()) {
248 PyErr_Print();
249 return 1;
250 }
251 else {
252 return -1;
253 }
Christian Heimes9cd17752007-11-18 19:35:23 +0000254}
255
256
Guido van Rossum667d7041995-08-04 04:20:48 +0000257/* Main program */
258
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000259int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000260Py_Main(int argc, wchar_t **argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000261{
262 int c;
263 int sts;
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000264 wchar_t *command = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000265 wchar_t *filename = NULL;
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000266 wchar_t *module = NULL;
Guido van Rossum667d7041995-08-04 04:20:48 +0000267 FILE *fp = stdin;
268 char *p;
Guido van Rossuma075ce11997-12-05 21:56:45 +0000269 int skipfirstline = 0;
Guido van Rossum775af911997-02-14 19:50:32 +0000270 int stdin_is_interactive = 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000271 int help = 0;
272 int version = 0;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000273 int saw_unbuffered_flag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000274 PyCompilerFlags cf;
Guido van Rossum667d7041995-08-04 04:20:48 +0000275
Guido van Rossum393661d2001-08-31 17:40:15 +0000276 cf.cf_flags = 0;
277
Guido van Rossumac56b031996-07-21 02:33:38 +0000278 orig_argc = argc; /* For Py_GetArgcArgv() */
Guido van Rossum667d7041995-08-04 04:20:48 +0000279 orig_argv = argv;
Guido van Rossum667d7041995-08-04 04:20:48 +0000280
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000281 PySys_ResetWarnOptions();
282
Guido van Rossumbceccf52001-04-10 22:07:43 +0000283 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
Guido van Rossum667d7041995-08-04 04:20:48 +0000284 if (c == 'c') {
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000285 size_t len;
Guido van Rossum667d7041995-08-04 04:20:48 +0000286 /* -c is the last option; following arguments
287 that look like options are left for the
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +0000288 command to interpret. */
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000289
290 len = wcslen(_PyOS_optarg) + 1 + 1;
291 command = (wchar_t *)malloc(sizeof(wchar_t) * len);
Guido van Rossum667d7041995-08-04 04:20:48 +0000292 if (command == NULL)
293 Py_FatalError(
294 "not enough memory to copy -c argument");
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000295 wcscpy(command, _PyOS_optarg);
296 command[len - 2] = '\n';
297 command[len - 1] = 0;
Guido van Rossum667d7041995-08-04 04:20:48 +0000298 break;
299 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000300
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000301 if (c == 'm') {
302 /* -m is the last option; following arguments
303 that look like options are left for the
304 module to interpret. */
Antoine Pitrou5651eaa2008-09-06 20:46:58 +0000305 module = _PyOS_optarg;
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000306 break;
307 }
308
Guido van Rossum667d7041995-08-04 04:20:48 +0000309 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000310 case 'b':
311 Py_BytesWarningFlag++;
312 break;
Guido van Rossum667d7041995-08-04 04:20:48 +0000313
314 case 'd':
315 Py_DebugFlag++;
316 break;
317
318 case 'i':
Guido van Rossumd8faa362007-04-27 19:54:29 +0000319 Py_InspectFlag++;
Guido van Rossum775af911997-02-14 19:50:32 +0000320 Py_InteractiveFlag++;
Guido van Rossum667d7041995-08-04 04:20:48 +0000321 break;
322
Christian Heimes33fe8092008-04-13 13:53:33 +0000323 /* case 'J': reserved for Jython */
324
Guido van Rossum7614da61997-03-03 19:14:45 +0000325 case 'O':
326 Py_OptimizeFlag++;
327 break;
328
Christian Heimes790c8232008-01-07 21:14:23 +0000329 case 'B':
330 Py_DontWriteBytecodeFlag++;
331 break;
332
Christian Heimes8dc226f2008-05-06 23:45:46 +0000333 case 's':
334 Py_NoUserSiteDirectory++;
335 break;
336
Guido van Rossum7922bd71997-08-29 22:34:47 +0000337 case 'S':
338 Py_NoSiteFlag++;
339 break;
340
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000341 case 'E':
342 Py_IgnoreEnvironmentFlag++;
343 break;
344
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000345 case 't':
Georg Brandle1b5ac62008-06-04 13:06:58 +0000346 /* ignored for backwards compatibility */
Guido van Rossumbba92ca1998-04-10 19:39:15 +0000347 break;
348
Guido van Rossum667d7041995-08-04 04:20:48 +0000349 case 'u':
Antoine Pitrou05608432009-01-09 18:53:14 +0000350 Py_UnbufferedStdioFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000351 saw_unbuffered_flag = 1;
Guido van Rossum667d7041995-08-04 04:20:48 +0000352 break;
353
354 case 'v':
355 Py_VerboseFlag++;
356 break;
357
Guido van Rossuma075ce11997-12-05 21:56:45 +0000358 case 'x':
359 skipfirstline = 1;
360 break;
361
Neal Norwitz32dde222008-04-15 06:43:13 +0000362 /* case 'X': reserved for implementation-specific arguments */
Christian Heimes33fe8092008-04-13 13:53:33 +0000363
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000364 case 'h':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365 case '?':
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000366 help++;
367 break;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000368
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000369 case 'V':
370 version++;
371 break;
Guido van Rossumc15a9a12000-05-01 17:54:33 +0000372
Martin v. Löwis790465f2008-04-05 20:41:37 +0000373 case 'W':
Guido van Rossum47f5fdc2000-12-15 22:00:54 +0000374 PySys_AddWarnOption(_PyOS_optarg);
375 break;
376
Guido van Rossum667d7041995-08-04 04:20:48 +0000377 /* This space reserved for other options */
378
379 default:
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000380 return usage(2, argv[0]);
Guido van Rossum667d7041995-08-04 04:20:48 +0000381 /*NOTREACHED*/
382
383 }
384 }
385
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000386 if (help)
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000387 return usage(0, argv[0]);
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000388
389 if (version) {
390 fprintf(stderr, "Python %s\n", PY_VERSION);
Martin v. Löwis852ba7e2003-03-30 17:09:58 +0000391 return 0;
Barry Warsaw3b2aedb2000-09-15 18:40:42 +0000392 }
393
Guido van Rossumd8faa362007-04-27 19:54:29 +0000394 if (!Py_InspectFlag &&
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000395 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000396 Py_InspectFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000397 if (!saw_unbuffered_flag &&
398 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
Antoine Pitrou05608432009-01-09 18:53:14 +0000399 Py_UnbufferedStdioFlag = 1;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000400
Christian Heimes8dc226f2008-05-06 23:45:46 +0000401 if (!Py_NoUserSiteDirectory &&
402 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
403 Py_NoUserSiteDirectory = 1;
404
Philip Jenvey0805ca32010-04-07 04:04:10 +0000405 if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') {
406 char *buf;
407 wchar_t *warning;
408 size_t len;
409
410 for (buf = strtok(p, ",");
411 buf != NULL;
412 buf = strtok(NULL, ",")) {
413 len = strlen(buf);
414 warning = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
415 if (warning == NULL)
416 Py_FatalError(
417 "not enough memory to copy PYTHONWARNINGS");
418 mbstowcs(warning, buf, len);
419 PySys_AddWarnOption(warning);
420 }
421 }
422
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000423 if (command == NULL && module == NULL && _PyOS_optind < argc &&
Martin v. Löwis790465f2008-04-05 20:41:37 +0000424 wcscmp(argv[_PyOS_optind], L"-") != 0)
Guido van Rossum775af911997-02-14 19:50:32 +0000425 {
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000426#ifdef __VMS
427 filename = decc$translate_vms(argv[_PyOS_optind]);
428 if (filename == (char *)0 || filename == (char *)-1)
429 filename = argv[_PyOS_optind];
430
431#else
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000432 filename = argv[_PyOS_optind];
Martin v. Löwis7a924e62003-03-05 14:15:21 +0000433#endif
Guido van Rossum775af911997-02-14 19:50:32 +0000434 }
435
436 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
437
Antoine Pitrou05608432009-01-09 18:53:14 +0000438 if (Py_UnbufferedStdioFlag) {
Sjoerd Mullender9cf424b2002-08-09 13:35:18 +0000439#if defined(MS_WINDOWS) || defined(__CYGWIN__)
Guido van Rossum3e7ae7a1997-01-17 22:05:38 +0000440 _setmode(fileno(stdin), O_BINARY);
441 _setmode(fileno(stdout), O_BINARY);
Guido van Rossumf22d7e21997-01-11 19:28:55 +0000442#endif
Guido van Rossum22ffac11998-03-06 15:30:39 +0000443#ifdef HAVE_SETVBUF
Guido van Rossum775af911997-02-14 19:50:32 +0000444 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
445 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
446 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000447#else /* !HAVE_SETVBUF */
448 setbuf(stdin, (char *)NULL);
449 setbuf(stdout, (char *)NULL);
450 setbuf(stderr, (char *)NULL);
451#endif /* !HAVE_SETVBUF */
Guido van Rossum667d7041995-08-04 04:20:48 +0000452 }
Guido van Rossum2a212191997-04-11 21:57:53 +0000453 else if (Py_InteractiveFlag) {
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000454#ifdef MS_WINDOWS
455 /* Doesn't have to have line-buffered -- use unbuffered */
Guido van Rossum01b7ced1999-02-09 18:36:51 +0000456 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000457 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000458#else /* !MS_WINDOWS */
459#ifdef HAVE_SETVBUF
Guido van Rossumb31c7dc1997-04-11 22:19:12 +0000460 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
461 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
Guido van Rossum22ffac11998-03-06 15:30:39 +0000462#endif /* HAVE_SETVBUF */
463#endif /* !MS_WINDOWS */
Guido van Rossum775af911997-02-14 19:50:32 +0000464 /* Leave stderr alone - it should be unbuffered anyway. */
465 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000466#ifdef __VMS
467 else {
468 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
469 }
470#endif /* __VMS */
Guido van Rossum667d7041995-08-04 04:20:48 +0000471
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000472#ifdef __APPLE__
473 /* On MacOS X, when the Python interpreter is embedded in an
474 application bundle, it gets executed by a bootstrapping script
475 that does os.execve() with an argv[0] that's different from the
476 actual Python executable. This is needed to keep the Finder happy,
477 or rather, to work around Apple's overly strict requirements of
478 the process name. However, we still need a usable sys.executable,
479 so the actual executable path is passed in an environment variable.
480 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
481 script. */
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000482 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') {
483 wchar_t* buffer;
484 size_t len = strlen(p);
485 size_t r;
486
487 buffer = malloc(len * sizeof(wchar_t));
488 if (buffer == NULL) {
489 Py_FatalError(
490 "not enough memory to copy PYTHONEXECUTABLE");
491 }
492
493 r = mbstowcs(buffer, p, len);
494 Py_SetProgramName(buffer);
495 /* buffer is now handed off - do not free */
496 } else {
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000497 Py_SetProgramName(argv[0]);
Ronald Oussoren3e264e12009-02-12 15:55:38 +0000498 }
Just van Rossum2ac79ef2003-03-05 15:46:54 +0000499#else
Guido van Rossumed52aac1997-07-19 19:20:32 +0000500 Py_SetProgramName(argv[0]);
Jack Jansenfbd861b2003-03-05 16:00:15 +0000501#endif
Guido van Rossumed52aac1997-07-19 19:20:32 +0000502 Py_Initialize();
503
Guido van Rossum667d7041995-08-04 04:20:48 +0000504 if (Py_VerboseFlag ||
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000505 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
Martin v. Löwise98922f2003-03-30 17:00:39 +0000506 fprintf(stderr, "Python %s on %s\n",
507 Py_GetVersion(), Py_GetPlatform());
508 if (!Py_NoSiteFlag)
509 fprintf(stderr, "%s\n", COPYRIGHT);
510 }
Guido van Rossum393661d2001-08-31 17:40:15 +0000511
Guido van Rossum667d7041995-08-04 04:20:48 +0000512 if (command != NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000513 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
514 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000515 argv[_PyOS_optind] = L"-c";
Guido van Rossum667d7041995-08-04 04:20:48 +0000516 }
517
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000518 if (module != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
520 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000521 _PyOS_optind--;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000522 argv[_PyOS_optind] = L"-c";
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000523 }
524
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000525 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
Guido van Rossum667d7041995-08-04 04:20:48 +0000526
Guido van Rossumd8faa362007-04-27 19:54:29 +0000527 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
Guido van Rossum3d26cc91997-09-16 16:11:28 +0000528 isatty(fileno(stdin))) {
529 PyObject *v;
530 v = PyImport_ImportModule("readline");
531 if (v == NULL)
532 PyErr_Clear();
533 else
534 Py_DECREF(v);
535 }
536
Guido van Rossum667d7041995-08-04 04:20:48 +0000537 if (command) {
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000538 PyObject *commandObj = PyUnicode_FromWideChar(
539 command, wcslen(command));
Guido van Rossum05f7c501997-08-02 03:00:42 +0000540 free(command);
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000541 if (commandObj != NULL) {
542 sts = PyRun_SimpleStringFlags(
543 _PyUnicode_AsString(commandObj), &cf) != 0;
544 }
545 else {
546 PyErr_Print();
547 sts = 1;
548 }
549 Py_DECREF(commandObj);
Raymond Hettingerdb29e0f2004-10-07 06:46:25 +0000550 } else if (module) {
Christian Heimes9cd17752007-11-18 19:35:23 +0000551 sts = RunModule(module, 1);
Guido van Rossum667d7041995-08-04 04:20:48 +0000552 }
553 else {
Christian Heimes9cd17752007-11-18 19:35:23 +0000554
Guido van Rossum775af911997-02-14 19:50:32 +0000555 if (filename == NULL && stdin_is_interactive) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000556 Py_InspectFlag = 0; /* do exit on SystemExit */
Martin v. Löwis6caea372003-11-18 19:46:25 +0000557 RunStartupFile(&cf);
Guido van Rossum667d7041995-08-04 04:20:48 +0000558 }
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000559 /* XXX */
Christian Heimes9cd17752007-11-18 19:35:23 +0000560
561 sts = -1; /* keep track of whether we've already run __main__ */
562
563 if (filename != NULL) {
564 sts = RunMainFromImporter(filename);
565 }
566
567 if (sts==-1 && filename!=NULL) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000568 if ((fp = _wfopen(filename, L"r")) == NULL) {
Amaury Forgeot d'Arcdd8059f2008-10-30 23:03:32 +0000569 char cfilename[PATH_MAX];
570 size_t r = wcstombs(cfilename, filename, PATH_MAX);
571 if (r == PATH_MAX)
572 /* cfilename is not null-terminated;
573 * forcefully null-terminating it
574 * might break the shift state */
575 strcpy(cfilename, "<file name too long>");
576 if (r == ((size_t)-1))
577 strcpy(cfilename, "<unprintable file name>");
578 fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
579 argv[0], cfilename, errno, strerror(errno));
Christian Heimesada8c3b2008-03-18 18:26:33 +0000580
Christian Heimes9cd17752007-11-18 19:35:23 +0000581 return 2;
582 }
583 else if (skipfirstline) {
584 int ch;
585 /* Push back first newline so line numbers
586 remain the same */
587 while ((ch = getc(fp)) != EOF) {
588 if (ch == '\n') {
589 (void)ungetc(ch, fp);
590 break;
591 }
592 }
593 }
594 {
595 /* XXX: does this work on Win/Win64? (see posix_fstat) */
596 struct stat sb;
597 if (fstat(fileno(fp), &sb) == 0 &&
598 S_ISDIR(sb.st_mode)) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000599 fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename);
Christian Heimes679db4a2008-01-18 09:56:22 +0000600 fclose(fp);
Christian Heimes9cd17752007-11-18 19:35:23 +0000601 return 1;
602 }
603 }
604 }
605
606 if (sts==-1) {
Amaury Forgeot d'Arc374e2202009-01-01 23:05:36 +0000607 PyObject *filenameObj = NULL;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000608 char *p_cfilename = "<stdin>";
609 if (filename) {
Amaury Forgeot d'Arc374e2202009-01-01 23:05:36 +0000610 filenameObj = PyUnicode_FromWideChar(
611 filename, wcslen(filename));
612 if (filenameObj != NULL)
613 p_cfilename = _PyUnicode_AsString(filenameObj);
614 else
Martin v. Löwis790465f2008-04-05 20:41:37 +0000615 p_cfilename = "<decoding error>";
616 }
Victor Stinner52f6dd72010-03-12 14:45:56 +0000617 /* call pending calls like signal handlers (SIGINT) */
618 if (Py_MakePendingCalls() == -1) {
619 PyErr_Print();
620 sts = 1;
621 } else {
622 sts = PyRun_AnyFileExFlags(
623 fp,
624 p_cfilename,
625 filename != NULL, &cf) != 0;
626 }
Amaury Forgeot d'Arc374e2202009-01-01 23:05:36 +0000627 Py_XDECREF(filenameObj);
Christian Heimes9cd17752007-11-18 19:35:23 +0000628 }
629
Guido van Rossum667d7041995-08-04 04:20:48 +0000630 }
631
Barry Warsawd86dcd32003-06-29 17:07:06 +0000632 /* Check this environment variable at the end, to give programs the
633 * opportunity to set it from Python.
634 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000635 if (!Py_InspectFlag &&
Barry Warsawd86dcd32003-06-29 17:07:06 +0000636 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
637 {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000638 Py_InspectFlag = 1;
Barry Warsawd86dcd32003-06-29 17:07:06 +0000639 }
640
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641 if (Py_InspectFlag && stdin_is_interactive &&
642 (filename != NULL || command != NULL || module != NULL)) {
643 Py_InspectFlag = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000644 /* XXX */
645 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000646 }
Guido van Rossum667d7041995-08-04 04:20:48 +0000647
Guido van Rossum5d1770e1997-08-05 02:23:48 +0000648 Py_Finalize();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000649
650#ifdef __INSURE__
651 /* Insure++ is a memory analysis tool that aids in discovering
652 * memory leaks and other memory problems. On Python exit, the
Walter Dörwald16807132007-05-25 13:52:07 +0000653 * interned string dictionaries are flagged as being in use at exit
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000654 * (which it is). Under normal circumstances, this is fine because
655 * the memory will be automatically reclaimed by the system. Under
656 * memory debugging, it's a huge source of useless noise, so we
657 * trade off slower shutdown for less distraction in the memory
658 * reports. -baw
659 */
660 _Py_ReleaseInternedStrings();
Walter Dörwald16807132007-05-25 13:52:07 +0000661 _Py_ReleaseInternedUnicodeStrings();
Barry Warsaw3e13b1e2001-02-23 16:46:39 +0000662#endif /* __INSURE__ */
663
Guido van Rossum05f7c501997-08-02 03:00:42 +0000664 return sts;
Guido van Rossum667d7041995-08-04 04:20:48 +0000665}
666
Skip Montanaro786ea6b2004-03-01 15:44:05 +0000667/* this is gonna seem *real weird*, but if you put some other code between
668 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
669 while statement in Misc/gdbinit:ppystack */
Guido van Rossum667d7041995-08-04 04:20:48 +0000670
Guido van Rossum667d7041995-08-04 04:20:48 +0000671/* Make the *original* argc/argv available to other modules.
672 This is rare, but it is needed by the secureware extension. */
673
674void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000675Py_GetArgcArgv(int *argc, wchar_t ***argv)
Guido van Rossum667d7041995-08-04 04:20:48 +0000676{
677 *argc = orig_argc;
678 *argv = orig_argv;
679}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680
681#ifdef __cplusplus
682}
683#endif