Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 1 | /* Python interpreter main program */ |
| 2 | |
| 3 | #include "Python.h" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 4 | #include "osdefs.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 5 | |
Antoine Pitrou | 5651eaa | 2008-09-06 20:46:58 +0000 | [diff] [blame] | 6 | #include <locale.h> |
| 7 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 8 | #ifdef __VMS |
Victor Stinner | b90db4c | 2011-04-26 22:48:24 +0200 | [diff] [blame] | 9 | #error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4" |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 10 | #include <unixlib.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 13 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 14 | #include <windows.h> |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 15 | #ifdef HAVE_FCNTL_H |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 17 | #define PATH_MAX MAXPATHLEN |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 18 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 20 | |
Martin v. Löwis | 945362c | 2007-08-30 14:57:25 +0000 | [diff] [blame] | 21 | #ifdef _MSC_VER |
| 22 | #include <crtdbg.h> |
| 23 | #endif |
| 24 | |
Jesus Cea | ab70e2a | 2012-10-05 01:48:08 +0200 | [diff] [blame] | 25 | #if defined(MS_WINDOWS) |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 26 | #define PYTHONHOMEHELP "<prefix>\\lib" |
| 27 | #else |
Marc-André Lemburg | da4dbc3 | 2001-06-12 16:13:51 +0000 | [diff] [blame] | 28 | #define PYTHONHOMEHELP "<prefix>/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Thomas Wouters | 2cffc7d | 2000-11-03 08:18:37 +0000 | [diff] [blame] | 31 | #include "pygetopt.h" |
| 32 | |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 33 | #define COPYRIGHT \ |
Guido van Rossum | 36002d7 | 2001-07-18 16:59:46 +0000 | [diff] [blame] | 34 | "Type \"help\", \"copyright\", \"credits\" or \"license\" " \ |
| 35 | "for more information." |
Guido van Rossum | a22865e | 2000-09-05 04:41:18 +0000 | [diff] [blame] | 36 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Guido van Rossum | ac56b03 | 1996-07-21 02:33:38 +0000 | [diff] [blame] | 41 | /* For Py_GetArgcArgv(); set by main() */ |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 42 | static wchar_t **orig_argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 43 | static int orig_argc; |
| 44 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 45 | /* command line options */ |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 46 | #define BASE_OPTS L"bBc:dEhiJm:OqRsStuvVW:xX:?" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 48 | #define PROGRAM_OPTS BASE_OPTS |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 50 | /* Short usage message (with %s for argv0) */ |
| 51 | static char *usage_line = |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 52 | "usage: %ls [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 53 | |
| 54 | /* Long usage message, split into parts < 512 bytes */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 55 | static char *usage_1 = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 56 | Options and arguments (and corresponding environment variables):\n\ |
Christian Heimes | 2ab3444 | 2008-09-03 20:31:07 +0000 | [diff] [blame] | 57 | -b : issue warnings about str(bytes_instance), str(bytearray_instance)\n\ |
| 58 | and comparing bytes/bytearray with str. (-bb: issue errors)\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 59 | -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 60 | -c cmd : program passed in as string (terminates option list)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | -d : debug output from parser; also PYTHONDEBUG=x\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 62 | -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | -h : print this help message and exit (also --help)\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 64 | "; |
| 65 | static char *usage_2 = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 66 | -i : inspect interactively after running script; forces a prompt even\n\ |
| 67 | if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\ |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 68 | -m mod : run library module as a script (terminates option list)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 69 | -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 70 | -OO : remove doc-strings in addition to the -O optimizations\n\ |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 71 | -q : don't print version and copyright messages on interactive startup\n\ |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 72 | -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 73 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 74 | "; |
| 75 | static char *usage_3 = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 76 | -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\ |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 77 | see man page for details on internal buffering relating to '-u'\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 78 | -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ |
| 79 | can be supplied multiple times to increase verbosity\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 80 | -V : print the Python version number and exit (also --version)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 81 | -W arg : warning control; arg is action:message:category:module:lineno\n\ |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 82 | also PYTHONWARNINGS=arg\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 83 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 84 | -X opt : set implementation-specific option\n\ |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 85 | "; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 86 | static char *usage_4 = "\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 87 | file : program read from script file\n\ |
| 88 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 89 | arg ...: arguments passed to program in sys.argv[1:]\n\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 90 | Other environment variables:\n\ |
| 91 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 92 | PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 93 | default module search path. The result is sys.path.\n\ |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 94 | "; |
Victor Stinner | 9802b39 | 2010-08-19 11:36:43 +0000 | [diff] [blame] | 95 | static char *usage_5 = |
| 96 | "PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n" |
| 97 | " The default module search path uses %s.\n" |
| 98 | "PYTHONCASEOK : ignore case in 'import' statements (Windows).\n" |
| 99 | "PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n" |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 100 | "PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 101 | "; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 102 | static char *usage_6 = "\ |
Georg Brandl | 42ae472 | 2012-02-21 22:37:36 +0100 | [diff] [blame] | 103 | PYTHONHASHSEED: if this variable is set to 'random', a random value is used\n\ |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 104 | to seed the hashes of str, bytes and datetime objects. It can also be\n\ |
| 105 | set to an integer in the range [0,4294967295] to get hash values with a\n\ |
| 106 | predictable seed.\n\ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 107 | "; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | 852ba7e | 2003-03-30 17:09:58 +0000 | [diff] [blame] | 109 | static int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 110 | usage(int exitcode, wchar_t* program) |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | FILE *f = exitcode ? stderr : stdout; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | fprintf(f, usage_line, program); |
| 115 | if (exitcode) |
| 116 | fprintf(f, "Try `python -h' for more information.\n"); |
| 117 | else { |
| 118 | fputs(usage_1, f); |
| 119 | fputs(usage_2, f); |
| 120 | fputs(usage_3, f); |
| 121 | fprintf(f, usage_4, DELIM); |
| 122 | fprintf(f, usage_5, DELIM, PYTHONHOMEHELP); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 123 | fputs(usage_6, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 125 | #if defined(__VMS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | if (exitcode == 0) { |
| 127 | /* suppress 'error' message */ |
| 128 | return 1; |
| 129 | } |
| 130 | else { |
| 131 | /* STS$M_INHIB_MSG + SS$_ABORT */ |
| 132 | return 0x1000002c; |
| 133 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 134 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | return exitcode; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 136 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | /*NOTREACHED*/ |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Martin v. Löwis | 6caea37 | 2003-11-18 19:46:25 +0000 | [diff] [blame] | 140 | static void RunStartupFile(PyCompilerFlags *cf) |
| 141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | char *startup = Py_GETENV("PYTHONSTARTUP"); |
| 143 | if (startup != NULL && startup[0] != '\0') { |
| 144 | FILE *fp = fopen(startup, "r"); |
| 145 | if (fp != NULL) { |
| 146 | (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); |
| 147 | PyErr_Clear(); |
| 148 | fclose(fp); |
| 149 | } else { |
| 150 | int save_errno; |
| 151 | |
| 152 | save_errno = errno; |
| 153 | PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); |
| 154 | errno = save_errno; |
| 155 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, |
| 156 | startup); |
| 157 | PyErr_Print(); |
| 158 | PyErr_Clear(); |
| 159 | } |
| 160 | } |
Martin v. Löwis | 6caea37 | 2003-11-18 19:46:25 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | 5651eaa | 2008-09-06 20:46:58 +0000 | [diff] [blame] | 164 | static int RunModule(wchar_t *modname, int set_argv0) |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | PyObject *module, *runpy, *runmodule, *runargs, *result; |
| 167 | runpy = PyImport_ImportModule("runpy"); |
| 168 | if (runpy == NULL) { |
| 169 | fprintf(stderr, "Could not import runpy module\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame^] | 170 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | return -1; |
| 172 | } |
| 173 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 174 | if (runmodule == NULL) { |
| 175 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame^] | 176 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | Py_DECREF(runpy); |
| 178 | return -1; |
| 179 | } |
| 180 | module = PyUnicode_FromWideChar(modname, wcslen(modname)); |
| 181 | if (module == NULL) { |
| 182 | fprintf(stderr, "Could not convert module name to unicode\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame^] | 183 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | Py_DECREF(runpy); |
| 185 | Py_DECREF(runmodule); |
| 186 | return -1; |
| 187 | } |
| 188 | runargs = Py_BuildValue("(Oi)", module, set_argv0); |
| 189 | if (runargs == NULL) { |
| 190 | fprintf(stderr, |
| 191 | "Could not create arguments for runpy._run_module_as_main\n"); |
Victor Stinner | 7d36e4f | 2013-04-10 00:27:23 +0200 | [diff] [blame^] | 192 | PyErr_Print(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Py_DECREF(runpy); |
| 194 | Py_DECREF(runmodule); |
| 195 | Py_DECREF(module); |
| 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(module); |
| 205 | Py_DECREF(runargs); |
| 206 | if (result == NULL) { |
| 207 | return -1; |
| 208 | } |
| 209 | Py_DECREF(result); |
| 210 | return 0; |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 211 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 212 | |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 213 | static int |
| 214 | RunMainFromImporter(wchar_t *filename) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 215 | { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 216 | PyObject *argv0 = NULL, *importer, *sys_path; |
| 217 | int sts; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 218 | |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 219 | argv0 = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 220 | if (argv0 == NULL) |
| 221 | goto error; |
| 222 | |
| 223 | importer = PyImport_GetImporter(argv0); |
| 224 | if (importer == NULL) |
| 225 | goto error; |
| 226 | |
Brett Cannon | aa93642 | 2012-04-27 15:30:58 -0400 | [diff] [blame] | 227 | if (importer == Py_None) { |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 228 | Py_DECREF(argv0); |
| 229 | Py_DECREF(importer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | return -1; |
| 231 | } |
Victor Stinner | 4726e40 | 2010-10-06 23:24:57 +0000 | [diff] [blame] | 232 | Py_DECREF(importer); |
| 233 | |
| 234 | /* argv0 is usable as an import source, so put it in sys.path[0] |
| 235 | and import __main__ */ |
| 236 | sys_path = PySys_GetObject("path"); |
| 237 | if (sys_path == NULL) |
| 238 | goto error; |
| 239 | if (PyList_SetItem(sys_path, 0, argv0)) { |
| 240 | argv0 = NULL; |
| 241 | goto error; |
| 242 | } |
| 243 | Py_INCREF(argv0); |
| 244 | |
| 245 | sts = RunModule(L"__main__", 0); |
| 246 | return sts != 0; |
| 247 | |
| 248 | error: |
| 249 | Py_XDECREF(argv0); |
| 250 | PyErr_Print(); |
| 251 | return 1; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 254 | static int |
| 255 | run_command(wchar_t *command, PyCompilerFlags *cf) |
| 256 | { |
| 257 | PyObject *unicode, *bytes; |
| 258 | int ret; |
| 259 | |
| 260 | unicode = PyUnicode_FromWideChar(command, -1); |
| 261 | if (unicode == NULL) |
| 262 | goto error; |
| 263 | bytes = PyUnicode_AsUTF8String(unicode); |
| 264 | Py_DECREF(unicode); |
| 265 | if (bytes == NULL) |
| 266 | goto error; |
| 267 | ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf); |
| 268 | Py_DECREF(bytes); |
| 269 | return ret != 0; |
| 270 | |
| 271 | error: |
Victor Stinner | 398356b | 2010-08-18 22:23:22 +0000 | [diff] [blame] | 272 | PySys_WriteStderr("Unable to decode the command from the command line:\n"); |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 273 | PyErr_Print(); |
| 274 | return 1; |
| 275 | } |
| 276 | |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 277 | static int |
| 278 | run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf) |
| 279 | { |
| 280 | PyObject *unicode, *bytes = NULL; |
| 281 | char *filename_str; |
| 282 | int run; |
| 283 | |
| 284 | /* call pending calls like signal handlers (SIGINT) */ |
| 285 | if (Py_MakePendingCalls() == -1) { |
| 286 | PyErr_Print(); |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | if (filename) { |
| 291 | unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 292 | if (unicode != NULL) { |
Victor Stinner | e0f3268 | 2010-10-17 19:34:51 +0000 | [diff] [blame] | 293 | bytes = PyUnicode_EncodeFSDefault(unicode); |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 294 | Py_DECREF(unicode); |
| 295 | } |
| 296 | if (bytes != NULL) |
| 297 | filename_str = PyBytes_AsString(bytes); |
| 298 | else { |
| 299 | PyErr_Clear(); |
Victor Stinner | e0f3268 | 2010-10-17 19:34:51 +0000 | [diff] [blame] | 300 | filename_str = "<encoding error>"; |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | else |
| 304 | filename_str = "<stdin>"; |
| 305 | |
| 306 | run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf); |
| 307 | Py_XDECREF(bytes); |
| 308 | return run != 0; |
| 309 | } |
| 310 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 312 | /* Main program */ |
| 313 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 314 | int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 315 | Py_Main(int argc, wchar_t **argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | int c; |
| 318 | int sts; |
| 319 | wchar_t *command = NULL; |
| 320 | wchar_t *filename = NULL; |
| 321 | wchar_t *module = NULL; |
| 322 | FILE *fp = stdin; |
| 323 | char *p; |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 324 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | wchar_t *wp; |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 326 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | int skipfirstline = 0; |
| 328 | int stdin_is_interactive = 0; |
| 329 | int help = 0; |
| 330 | int version = 0; |
| 331 | int saw_unbuffered_flag = 0; |
| 332 | PyCompilerFlags cf; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | cf.cf_flags = 0; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
| 337 | orig_argv = argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 339 | /* Hash randomization needed early for all string operations |
| 340 | (including -W and -X options). */ |
Ezio Melotti | a0dd22e | 2012-11-23 18:48:32 +0200 | [diff] [blame] | 341 | _PyOS_opterr = 0; /* prevent printing the error in 1st pass */ |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 342 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
| 343 | if (c == 'm' || c == 'c') { |
| 344 | /* -c / -m is the last option: following arguments are |
| 345 | not interpreter options. */ |
| 346 | break; |
| 347 | } |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 348 | if (c == 'E') { |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 349 | Py_IgnoreEnvironmentFlag++; |
| 350 | break; |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 351 | } |
| 352 | } |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 353 | |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 354 | Py_HashRandomizationFlag = 1; |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 355 | _PyRandom_Init(); |
| 356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | PySys_ResetWarnOptions(); |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 358 | _PyOS_ResetGetOpt(); |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
| 361 | if (c == 'c') { |
| 362 | size_t len; |
| 363 | /* -c is the last option; following arguments |
| 364 | that look like options are left for the |
| 365 | command to interpret. */ |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | len = wcslen(_PyOS_optarg) + 1 + 1; |
| 368 | command = (wchar_t *)malloc(sizeof(wchar_t) * len); |
| 369 | if (command == NULL) |
| 370 | Py_FatalError( |
| 371 | "not enough memory to copy -c argument"); |
| 372 | wcscpy(command, _PyOS_optarg); |
| 373 | command[len - 2] = '\n'; |
| 374 | command[len - 1] = 0; |
| 375 | break; |
| 376 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | if (c == 'm') { |
| 379 | /* -m is the last option; following arguments |
| 380 | that look like options are left for the |
| 381 | module to interpret. */ |
| 382 | module = _PyOS_optarg; |
| 383 | break; |
| 384 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | switch (c) { |
| 387 | case 'b': |
| 388 | Py_BytesWarningFlag++; |
| 389 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | case 'd': |
| 392 | Py_DebugFlag++; |
| 393 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | case 'i': |
| 396 | Py_InspectFlag++; |
| 397 | Py_InteractiveFlag++; |
| 398 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | /* case 'J': reserved for Jython */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | case 'O': |
| 403 | Py_OptimizeFlag++; |
| 404 | break; |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | case 'B': |
| 407 | Py_DontWriteBytecodeFlag++; |
| 408 | break; |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | case 's': |
| 411 | Py_NoUserSiteDirectory++; |
| 412 | break; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | case 'S': |
| 415 | Py_NoSiteFlag++; |
| 416 | break; |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | case 'E': |
Antoine Pitrou | 86838b0 | 2012-02-21 19:03:47 +0100 | [diff] [blame] | 419 | /* Already handled above */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | break; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | case 't': |
| 423 | /* ignored for backwards compatibility */ |
| 424 | break; |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | case 'u': |
| 427 | Py_UnbufferedStdioFlag = 1; |
| 428 | saw_unbuffered_flag = 1; |
| 429 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | case 'v': |
| 432 | Py_VerboseFlag++; |
| 433 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | case 'x': |
| 436 | skipfirstline = 1; |
| 437 | break; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | case 'h': |
| 440 | case '?': |
| 441 | help++; |
| 442 | break; |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | case 'V': |
| 445 | version++; |
| 446 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | case 'W': |
| 449 | PySys_AddWarnOption(_PyOS_optarg); |
| 450 | break; |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 452 | case 'X': |
| 453 | PySys_AddXOption(_PyOS_optarg); |
| 454 | break; |
| 455 | |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 456 | case 'q': |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 457 | Py_QuietFlag++; |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 458 | break; |
| 459 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 460 | case 'R': |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 461 | /* Ignored */ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 462 | break; |
| 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | /* This space reserved for other options */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | default: |
| 467 | return usage(2, argv[0]); |
| 468 | /*NOTREACHED*/ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | if (help) |
| 474 | return usage(0, argv[0]); |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | if (version) { |
| 477 | fprintf(stderr, "Python %s\n", PY_VERSION); |
| 478 | return 0; |
| 479 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | if (!Py_InspectFlag && |
| 482 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 483 | Py_InspectFlag = 1; |
| 484 | if (!saw_unbuffered_flag && |
| 485 | (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 486 | Py_UnbufferedStdioFlag = 1; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (!Py_NoUserSiteDirectory && |
| 489 | (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0') |
| 490 | Py_NoUserSiteDirectory = 1; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 491 | |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 492 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | if (!Py_IgnoreEnvironmentFlag && (wp = _wgetenv(L"PYTHONWARNINGS")) && |
| 494 | *wp != L'\0') { |
| 495 | wchar_t *buf, *warning; |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | buf = (wchar_t *)malloc((wcslen(wp) + 1) * sizeof(wchar_t)); |
| 498 | if (buf == NULL) |
| 499 | Py_FatalError( |
| 500 | "not enough memory to copy PYTHONWARNINGS"); |
| 501 | wcscpy(buf, wp); |
| 502 | for (warning = wcstok(buf, L","); |
| 503 | warning != NULL; |
| 504 | warning = wcstok(NULL, L",")) { |
| 505 | PySys_AddWarnOption(warning); |
| 506 | } |
| 507 | free(buf); |
| 508 | } |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 509 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { |
| 511 | char *buf, *oldloc; |
Victor Stinner | c2d76fd | 2010-09-10 23:13:52 +0000 | [diff] [blame] | 512 | PyObject *unicode; |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | /* settle for strtok here as there's no one standard |
| 515 | C89 wcstok */ |
| 516 | buf = (char *)malloc(strlen(p) + 1); |
| 517 | if (buf == NULL) |
| 518 | Py_FatalError( |
| 519 | "not enough memory to copy PYTHONWARNINGS"); |
| 520 | strcpy(buf, p); |
| 521 | oldloc = strdup(setlocale(LC_ALL, NULL)); |
| 522 | setlocale(LC_ALL, ""); |
| 523 | for (p = strtok(buf, ","); p != NULL; p = strtok(NULL, ",")) { |
Victor Stinner | 5c848a8 | 2010-09-12 08:00:41 +0000 | [diff] [blame] | 524 | #ifdef __APPLE__ |
| 525 | /* Use utf-8 on Mac OS X */ |
| 526 | unicode = PyUnicode_FromString(p); |
| 527 | #else |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 528 | unicode = PyUnicode_DecodeLocale(p, "surrogateescape"); |
Victor Stinner | 5c848a8 | 2010-09-12 08:00:41 +0000 | [diff] [blame] | 529 | #endif |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 530 | if (unicode == NULL) { |
| 531 | /* ignore errors */ |
| 532 | PyErr_Clear(); |
Victor Stinner | c2d76fd | 2010-09-10 23:13:52 +0000 | [diff] [blame] | 533 | continue; |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 534 | } |
Victor Stinner | c2d76fd | 2010-09-10 23:13:52 +0000 | [diff] [blame] | 535 | PySys_AddWarnOptionUnicode(unicode); |
| 536 | Py_DECREF(unicode); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | } |
| 538 | setlocale(LC_ALL, oldloc); |
| 539 | free(oldloc); |
| 540 | free(buf); |
| 541 | } |
Philip Jenvey | e53de3d | 2010-04-14 03:01:39 +0000 | [diff] [blame] | 542 | #endif |
Philip Jenvey | 0805ca3 | 2010-04-07 04:04:10 +0000 | [diff] [blame] | 543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | if (command == NULL && module == NULL && _PyOS_optind < argc && |
| 545 | wcscmp(argv[_PyOS_optind], L"-") != 0) |
| 546 | { |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 547 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | filename = decc$translate_vms(argv[_PyOS_optind]); |
| 549 | if (filename == (char *)0 || filename == (char *)-1) |
| 550 | filename = argv[_PyOS_optind]; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 551 | |
| 552 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | filename = argv[_PyOS_optind]; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 554 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | } |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 558 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 559 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 560 | /* don't translate newlines (\r\n <=> \n) */ |
| 561 | _setmode(fileno(stdin), O_BINARY); |
| 562 | _setmode(fileno(stdout), O_BINARY); |
| 563 | _setmode(fileno(stderr), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 564 | #endif |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 565 | |
| 566 | if (Py_UnbufferedStdioFlag) { |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 567 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 569 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 570 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 571 | #else /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | setbuf(stdin, (char *)NULL); |
| 573 | setbuf(stdout, (char *)NULL); |
| 574 | setbuf(stderr, (char *)NULL); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 575 | #endif /* !HAVE_SETVBUF */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | } |
| 577 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 578 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 580 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
| 581 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 582 | #else /* !MS_WINDOWS */ |
| 583 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 585 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 586 | #endif /* HAVE_SETVBUF */ |
| 587 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 589 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 590 | #ifdef __VMS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | else { |
| 592 | setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 593 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 594 | #endif /* __VMS */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 595 | |
Just van Rossum | 2ac79ef | 2003-03-05 15:46:54 +0000 | [diff] [blame] | 596 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | /* On MacOS X, when the Python interpreter is embedded in an |
| 598 | application bundle, it gets executed by a bootstrapping script |
| 599 | that does os.execve() with an argv[0] that's different from the |
| 600 | actual Python executable. This is needed to keep the Finder happy, |
| 601 | or rather, to work around Apple's overly strict requirements of |
| 602 | the process name. However, we still need a usable sys.executable, |
| 603 | so the actual executable path is passed in an environment variable. |
| 604 | See Lib/plat-mac/bundlebuiler.py for details about the bootstrap |
| 605 | script. */ |
| 606 | if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') { |
| 607 | wchar_t* buffer; |
Ronald Oussoren | eb61f8b | 2012-08-22 14:24:14 +0200 | [diff] [blame] | 608 | size_t len = strlen(p) + 1; |
Ronald Oussoren | 3e264e1 | 2009-02-12 15:55:38 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | buffer = malloc(len * sizeof(wchar_t)); |
| 611 | if (buffer == NULL) { |
| 612 | Py_FatalError( |
| 613 | "not enough memory to copy PYTHONEXECUTABLE"); |
| 614 | } |
Ronald Oussoren | 3e264e1 | 2009-02-12 15:55:38 +0000 | [diff] [blame] | 615 | |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 616 | mbstowcs(buffer, p, len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | Py_SetProgramName(buffer); |
| 618 | /* buffer is now handed off - do not free */ |
| 619 | } else { |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 620 | #ifdef WITH_NEXT_FRAMEWORK |
| 621 | char* pyvenv_launcher = getenv("__PYVENV_LAUNCHER__"); |
| 622 | |
| 623 | if (pyvenv_launcher && *pyvenv_launcher) { |
| 624 | /* Used by Mac/Tools/pythonw.c to forward |
| 625 | * the argv0 of the stub executable |
| 626 | */ |
| 627 | wchar_t* wbuf = _Py_char2wchar(pyvenv_launcher, NULL); |
| 628 | |
| 629 | if (wbuf == NULL) { |
| 630 | Py_FatalError("Cannot decode __PYVENV_LAUNCHER__"); |
| 631 | } |
| 632 | Py_SetProgramName(wbuf); |
| 633 | |
| 634 | /* Don't free wbuf, the argument to Py_SetProgramName |
| 635 | * must remain valid until the Py_Finalize is called. |
| 636 | */ |
| 637 | } else { |
| 638 | Py_SetProgramName(argv[0]); |
| 639 | } |
| 640 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | Py_SetProgramName(argv[0]); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 642 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | } |
Just van Rossum | 2ac79ef | 2003-03-05 15:46:54 +0000 | [diff] [blame] | 644 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | Py_SetProgramName(argv[0]); |
Jack Jansen | fbd861b | 2003-03-05 16:00:15 +0000 | [diff] [blame] | 646 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | Py_Initialize(); |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 648 | |
Georg Brandl | 8aa7e99 | 2010-12-28 18:30:18 +0000 | [diff] [blame] | 649 | if (!Py_QuietFlag && (Py_VerboseFlag || |
Georg Brandl | 9d87119 | 2010-12-04 10:47:18 +0000 | [diff] [blame] | 650 | (command == NULL && filename == NULL && |
| 651 | module == NULL && stdin_is_interactive))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | fprintf(stderr, "Python %s on %s\n", |
| 653 | Py_GetVersion(), Py_GetPlatform()); |
| 654 | if (!Py_NoSiteFlag) |
| 655 | fprintf(stderr, "%s\n", COPYRIGHT); |
| 656 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | if (command != NULL) { |
| 659 | /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ |
| 660 | _PyOS_optind--; |
| 661 | argv[_PyOS_optind] = L"-c"; |
| 662 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | if (module != NULL) { |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 665 | /* Backup _PyOS_optind and force sys.argv[0] = '-m'*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | _PyOS_optind--; |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 667 | argv[_PyOS_optind] = L"-m"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 672 | if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) && |
| 673 | isatty(fileno(stdin))) { |
| 674 | PyObject *v; |
| 675 | v = PyImport_ImportModule("readline"); |
| 676 | if (v == NULL) |
| 677 | PyErr_Clear(); |
| 678 | else |
| 679 | Py_DECREF(v); |
| 680 | } |
Guido van Rossum | 3d26cc9 | 1997-09-16 16:11:28 +0000 | [diff] [blame] | 681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | if (command) { |
Victor Stinner | a62207c | 2010-08-07 10:57:17 +0000 | [diff] [blame] | 683 | sts = run_command(command, &cf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | free(command); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | } else if (module) { |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 686 | sts = (RunModule(module, 1) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | } |
| 688 | else { |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | if (filename == NULL && stdin_is_interactive) { |
| 691 | Py_InspectFlag = 0; /* do exit on SystemExit */ |
| 692 | RunStartupFile(&cf); |
| 693 | } |
| 694 | /* XXX */ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 696 | sts = -1; /* keep track of whether we've already run __main__ */ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | if (filename != NULL) { |
| 699 | sts = RunMainFromImporter(filename); |
| 700 | } |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | if (sts==-1 && filename!=NULL) { |
Victor Stinner | 9a6692f | 2010-10-14 10:51:24 +0000 | [diff] [blame] | 703 | fp = _Py_wfopen(filename, L"r"); |
| 704 | if (fp == NULL) { |
| 705 | char *cfilename_buffer; |
| 706 | const char *cfilename; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 707 | int err = errno; |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 708 | cfilename_buffer = _Py_wchar2char(filename, NULL); |
Victor Stinner | 9a6692f | 2010-10-14 10:51:24 +0000 | [diff] [blame] | 709 | if (cfilename_buffer != NULL) |
| 710 | cfilename = cfilename_buffer; |
| 711 | else |
| 712 | cfilename = "<unprintable file name>"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n", |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 714 | argv[0], cfilename, err, strerror(err)); |
Victor Stinner | 9a6692f | 2010-10-14 10:51:24 +0000 | [diff] [blame] | 715 | if (cfilename_buffer) |
| 716 | PyMem_Free(cfilename_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | return 2; |
| 718 | } |
| 719 | else if (skipfirstline) { |
| 720 | int ch; |
| 721 | /* Push back first newline so line numbers |
| 722 | remain the same */ |
| 723 | while ((ch = getc(fp)) != EOF) { |
| 724 | if (ch == '\n') { |
| 725 | (void)ungetc(ch, fp); |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | { |
| 731 | /* XXX: does this work on Win/Win64? (see posix_fstat) */ |
| 732 | struct stat sb; |
| 733 | if (fstat(fileno(fp), &sb) == 0 && |
| 734 | S_ISDIR(sb.st_mode)) { |
| 735 | fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename); |
| 736 | fclose(fp); |
| 737 | return 1; |
| 738 | } |
| 739 | } |
| 740 | } |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 741 | |
Victor Stinner | 0a3ddad | 2010-08-07 16:34:25 +0000 | [diff] [blame] | 742 | if (sts == -1) |
| 743 | sts = run_file(fp, filename, &cf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | } |
Barry Warsaw | d86dcd3 | 2003-06-29 17:07:06 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | /* Check this environment variable at the end, to give programs the |
| 747 | * opportunity to set it from Python. |
| 748 | */ |
| 749 | if (!Py_InspectFlag && |
| 750 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 751 | { |
| 752 | Py_InspectFlag = 1; |
| 753 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | if (Py_InspectFlag && stdin_is_interactive && |
| 756 | (filename != NULL || command != NULL || module != NULL)) { |
| 757 | Py_InspectFlag = 0; |
| 758 | /* XXX */ |
| 759 | sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; |
| 760 | } |
| 761 | |
| 762 | Py_Finalize(); |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 763 | |
| 764 | #ifdef __INSURE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | /* Insure++ is a memory analysis tool that aids in discovering |
| 766 | * memory leaks and other memory problems. On Python exit, the |
| 767 | * interned string dictionaries are flagged as being in use at exit |
| 768 | * (which it is). Under normal circumstances, this is fine because |
| 769 | * the memory will be automatically reclaimed by the system. Under |
| 770 | * memory debugging, it's a huge source of useless noise, so we |
| 771 | * trade off slower shutdown for less distraction in the memory |
| 772 | * reports. -baw |
| 773 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | _Py_ReleaseInternedUnicodeStrings(); |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 775 | #endif /* __INSURE__ */ |
| 776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | return sts; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 780 | /* this is gonna seem *real weird*, but if you put some other code between |
| 781 | Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the |
| 782 | while statement in Misc/gdbinit:ppystack */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 783 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 784 | /* Make the *original* argc/argv available to other modules. |
| 785 | This is rare, but it is needed by the secureware extension. */ |
| 786 | |
| 787 | void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 788 | Py_GetArgcArgv(int *argc, wchar_t ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | *argc = orig_argc; |
| 791 | *argv = orig_argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 792 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 793 | |
| 794 | #ifdef __cplusplus |
| 795 | } |
| 796 | #endif |