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" |
Jeremy Hylton | ec97a28 | 2005-10-21 14:58:06 +0000 | [diff] [blame] | 5 | #include "code.h" /* For CO_FUTURE_DIVISION */ |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 6 | #include "import.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 7 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 8 | #ifdef __VMS |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 9 | #include <unixlib.h> |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 10 | #endif |
| 11 | |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 12 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Steve Dower | f85dbfc | 2016-12-28 15:41:09 -0800 | [diff] [blame] | 13 | #ifdef HAVE_IO_H |
| 14 | #include <io.h> |
| 15 | #endif |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 16 | #ifdef HAVE_FCNTL_H |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 17 | #include <fcntl.h> |
| 18 | #endif |
Martin v. Löwis | a43190b | 2006-05-22 09:15:18 +0000 | [diff] [blame] | 19 | #endif |
Guido van Rossum | 3e7ae7a | 1997-01-17 22:05:38 +0000 | [diff] [blame] | 20 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 21 | #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS) |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 22 | #define PYTHONHOMEHELP "<prefix>\\lib" |
| 23 | #else |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 24 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 25 | #define PYTHONHOMEHELP "<prefix>/Lib" |
| 26 | #else |
Marc-André Lemburg | da4dbc3 | 2001-06-12 16:13:51 +0000 | [diff] [blame] | 27 | #define PYTHONHOMEHELP "<prefix>/pythonX.X" |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 28 | #endif |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 29 | #endif |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 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 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +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() */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 42 | static char **orig_argv; |
| 43 | static int orig_argc; |
| 44 | |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 45 | /* command line options */ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 46 | #define BASE_OPTS "3bBc:dEhiJm:OQ:RsStuUvVW:xX?" |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 47 | |
| 48 | #ifndef RISCOS |
| 49 | #define PROGRAM_OPTS BASE_OPTS |
| 50 | #else /*RISCOS*/ |
| 51 | /* extra option saying that we are running under a special task window |
| 52 | frontend; especially my_readline will behave different */ |
| 53 | #define PROGRAM_OPTS BASE_OPTS "w" |
| 54 | /* corresponding flag */ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 55 | extern int Py_RISCOSWimpFlag; |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 56 | #endif /*RISCOS*/ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 58 | /* Short usage message (with %s for argv0) */ |
| 59 | static char *usage_line = |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 60 | "usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 61 | |
| 62 | /* Long usage message, split into parts < 512 bytes */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 63 | static char *usage_1 = "\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 64 | Options and arguments (and corresponding environment variables):\n\ |
Zachary Ware | f6c6d1e | 2017-05-13 09:30:20 -0500 | [diff] [blame] | 65 | -b : issue warnings about comparing bytearray with unicode\n\ |
| 66 | (-bb: issue errors)\n\ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 67 | -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] | 68 | -c cmd : program passed in as string (terminates option list)\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 69 | -d : debug output from parser; also PYTHONDEBUG=x\n\ |
Georg Brandl | aed6c66 | 2008-01-07 17:25:53 +0000 | [diff] [blame] | 70 | -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\ |
Georg Brandl | 9dceedb | 2006-07-12 15:31:17 +0000 | [diff] [blame] | 71 | -h : print this help message and exit (also --help)\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 72 | -i : inspect interactively after running script; forces a prompt even\n\ |
Guido van Rossum | 61c345f | 2001-09-04 03:26:15 +0000 | [diff] [blame] | 73 | "; |
| 74 | static char *usage_2 = "\ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 75 | 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] | 76 | -m mod : run library module as a script (terminates option list)\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 77 | -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\ |
Guido van Rossum | 6b86a42 | 1999-01-28 15:07:47 +0000 | [diff] [blame] | 78 | -OO : remove doc-strings in addition to the -O optimizations\n\ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 79 | -R : use a pseudo-random salt to make hash() values of various types be\n\ |
| 80 | unpredictable between separate invocations of the interpreter, as\n\ |
| 81 | a defense against denial-of-service attacks\n\ |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 82 | -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\ |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 83 | -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] | 84 | -S : don't imply 'import site' on initialization\n\ |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 85 | -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 86 | "; |
| 87 | static char *usage_3 = "\ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 88 | -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\ |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 89 | see man page for details on internal buffering relating to '-u'\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 90 | -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\ |
| 91 | can be supplied multiple times to increase verbosity\n\ |
Georg Brandl | 9dceedb | 2006-07-12 15:31:17 +0000 | [diff] [blame] | 92 | -V : print the Python version number and exit (also --version)\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 93 | -W arg : warning control; arg is action:message:category:module:lineno\n\ |
Philip Jenvey | aebbaeb | 2010-04-06 23:24:45 +0000 | [diff] [blame] | 94 | also PYTHONWARNINGS=arg\n\ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 95 | -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 96 | "; |
| 97 | static char *usage_4 = "\ |
Benjamin Peterson | f902a94 | 2009-01-09 03:07:27 +0000 | [diff] [blame] | 98 | -3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 99 | file : program read from script file\n\ |
| 100 | - : program read from stdin (default; interactive mode if a tty)\n\ |
Andrew M. Kuchling | e2782bb | 2006-09-14 11:28:50 +0000 | [diff] [blame] | 101 | arg ...: arguments passed to program in sys.argv[1:]\n\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 102 | Other environment variables:\n\ |
| 103 | PYTHONSTARTUP: file executed on interactive startup (no default)\n\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 104 | PYTHONPATH : '%c'-separated list of directories prefixed to the\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 105 | default module search path. The result is sys.path.\n\ |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 106 | "; |
| 107 | static char *usage_5 = "\ |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 108 | PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\ |
| 109 | The default module search path uses %s.\n\ |
Tim Peters | 793de09 | 2001-02-22 00:39:47 +0000 | [diff] [blame] | 110 | PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 111 | PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 112 | "; |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 113 | static char *usage_6 = "\ |
Georg Brandl | 3aec568 | 2012-02-21 22:36:27 +0100 | [diff] [blame] | 114 | PYTHONHASHSEED: if this variable is set to 'random', the effect is the same\n\ |
| 115 | as specifying the -R option: a random value is used to seed the hashes of\n\ |
| 116 | str, bytes and datetime objects. It can also be set to an integer\n\ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 117 | in the range [0,4294967295] to get hash values with a predictable seed.\n\ |
| 118 | "; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 119 | |
| 120 | |
Martin v. Löwis | 852ba7e | 2003-03-30 17:09:58 +0000 | [diff] [blame] | 121 | static int |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 122 | usage(int exitcode, char* program) |
| 123 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 124 | FILE *f = exitcode ? stderr : stdout; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 126 | fprintf(f, usage_line, program); |
| 127 | if (exitcode) |
| 128 | fprintf(f, "Try `python -h' for more information.\n"); |
| 129 | else { |
| 130 | fputs(usage_1, f); |
| 131 | fputs(usage_2, f); |
| 132 | fputs(usage_3, f); |
| 133 | fprintf(f, usage_4, DELIM); |
| 134 | fprintf(f, usage_5, DELIM, PYTHONHOMEHELP); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 135 | fputs(usage_6, f); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 136 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 137 | #if defined(__VMS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 138 | if (exitcode == 0) { |
| 139 | /* suppress 'error' message */ |
| 140 | return 1; |
| 141 | } |
| 142 | else { |
| 143 | /* STS$M_INHIB_MSG + SS$_ABORT */ |
| 144 | return 0x1000002c; |
| 145 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 146 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 147 | return exitcode; |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 148 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 149 | /*NOTREACHED*/ |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Martin v. Löwis | 6caea37 | 2003-11-18 19:46:25 +0000 | [diff] [blame] | 152 | static void RunStartupFile(PyCompilerFlags *cf) |
| 153 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 154 | char *startup = Py_GETENV("PYTHONSTARTUP"); |
| 155 | if (startup != NULL && startup[0] != '\0') { |
| 156 | FILE *fp = fopen(startup, "r"); |
| 157 | if (fp != NULL) { |
| 158 | (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); |
| 159 | PyErr_Clear(); |
| 160 | fclose(fp); |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 161 | } else { |
| 162 | int save_errno; |
| 163 | save_errno = errno; |
| 164 | PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); |
| 165 | errno = save_errno; |
| 166 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, |
| 167 | startup); |
| 168 | PyErr_Print(); |
| 169 | PyErr_Clear(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
Martin v. Löwis | 6caea37 | 2003-11-18 19:46:25 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Nick Coghlan | e2ebb2d | 2006-03-15 11:00:26 +0000 | [diff] [blame] | 174 | |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 175 | static int RunModule(char *module, int set_argv0) |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 176 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 177 | PyObject *runpy, *runmodule, *runargs, *result; |
| 178 | runpy = PyImport_ImportModule("runpy"); |
| 179 | if (runpy == NULL) { |
| 180 | fprintf(stderr, "Could not import runpy module\n"); |
| 181 | return -1; |
| 182 | } |
| 183 | runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main"); |
| 184 | if (runmodule == NULL) { |
| 185 | fprintf(stderr, "Could not access runpy._run_module_as_main\n"); |
| 186 | Py_DECREF(runpy); |
| 187 | return -1; |
| 188 | } |
| 189 | runargs = Py_BuildValue("(si)", module, set_argv0); |
| 190 | if (runargs == NULL) { |
| 191 | fprintf(stderr, |
| 192 | "Could not create arguments for runpy._run_module_as_main\n"); |
| 193 | Py_DECREF(runpy); |
| 194 | Py_DECREF(runmodule); |
| 195 | return -1; |
| 196 | } |
| 197 | result = PyObject_Call(runmodule, runargs, NULL); |
| 198 | if (result == NULL) { |
| 199 | PyErr_Print(); |
| 200 | } |
| 201 | Py_DECREF(runpy); |
| 202 | Py_DECREF(runmodule); |
| 203 | Py_DECREF(runargs); |
| 204 | if (result == NULL) { |
| 205 | return -1; |
| 206 | } |
| 207 | Py_DECREF(result); |
| 208 | return 0; |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 209 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 210 | |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 211 | static int RunMainFromImporter(char *filename) |
| 212 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 213 | PyObject *argv0 = NULL, *importer = NULL; |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | if ((argv0 = PyString_FromString(filename)) && |
| 216 | (importer = PyImport_GetImporter(argv0)) && |
| 217 | (importer->ob_type != &PyNullImporter_Type)) |
| 218 | { |
| 219 | /* argv0 is usable as an import source, so |
| 220 | put it in sys.path[0] and import __main__ */ |
| 221 | PyObject *sys_path = NULL; |
| 222 | if ((sys_path = PySys_GetObject("path")) && |
| 223 | !PyList_SetItem(sys_path, 0, argv0)) |
| 224 | { |
| 225 | Py_INCREF(argv0); |
| 226 | Py_DECREF(importer); |
| 227 | sys_path = NULL; |
| 228 | return RunModule("__main__", 0) != 0; |
| 229 | } |
| 230 | } |
| 231 | Py_XDECREF(argv0); |
| 232 | Py_XDECREF(importer); |
| 233 | if (PyErr_Occurred()) { |
| 234 | PyErr_Print(); |
| 235 | return 1; |
| 236 | } |
| 237 | return -1; |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 241 | /* Main program */ |
| 242 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 243 | int |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 244 | Py_Main(int argc, char **argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 246 | int c; |
| 247 | int sts; |
| 248 | char *command = NULL; |
| 249 | char *filename = NULL; |
| 250 | char *module = NULL; |
| 251 | FILE *fp = stdin; |
| 252 | char *p; |
| 253 | int unbuffered = 0; |
| 254 | int skipfirstline = 0; |
| 255 | int stdin_is_interactive = 0; |
| 256 | int help = 0; |
| 257 | int version = 0; |
| 258 | int saw_unbuffered_flag = 0; |
| 259 | PyCompilerFlags cf; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 260 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 261 | cf.cf_flags = 0; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | orig_argc = argc; /* For Py_GetArgcArgv() */ |
| 264 | orig_argv = argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 266 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 267 | Py_RISCOSWimpFlag = 0; |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
Antoine Pitrou | cc3fa88 | 2012-02-21 20:42:48 +0100 | [diff] [blame] | 270 | /* Hash randomization needed early for all string operations |
| 271 | (including -W and -X options). */ |
Ezio Melotti | ec6486d | 2012-11-23 18:46:11 +0200 | [diff] [blame] | 272 | _PyOS_opterr = 0; /* prevent printing the error in 1st pass */ |
Antoine Pitrou | cc3fa88 | 2012-02-21 20:42:48 +0100 | [diff] [blame] | 273 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
| 274 | if (c == 'm' || c == 'c') { |
| 275 | /* -c / -m is the last option: following arguments are |
| 276 | not interpreter options. */ |
| 277 | break; |
| 278 | } |
| 279 | switch (c) { |
| 280 | case 'E': |
| 281 | Py_IgnoreEnvironmentFlag++; |
| 282 | break; |
| 283 | case 'R': |
| 284 | Py_HashRandomizationFlag++; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | /* The variable is only tested for existence here; _PyRandom_Init will |
| 289 | check its value further. */ |
| 290 | if (!Py_HashRandomizationFlag && |
| 291 | (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0') |
| 292 | Py_HashRandomizationFlag = 1; |
| 293 | |
| 294 | _PyRandom_Init(); |
| 295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 296 | PySys_ResetWarnOptions(); |
Antoine Pitrou | cc3fa88 | 2012-02-21 20:42:48 +0100 | [diff] [blame] | 297 | _PyOS_ResetGetOpt(); |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 299 | while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { |
| 300 | if (c == 'c') { |
| 301 | /* -c is the last option; following arguments |
| 302 | that look like options are left for the |
| 303 | command to interpret. */ |
| 304 | command = (char *)malloc(strlen(_PyOS_optarg) + 2); |
| 305 | if (command == NULL) |
| 306 | Py_FatalError( |
| 307 | "not enough memory to copy -c argument"); |
| 308 | strcpy(command, _PyOS_optarg); |
| 309 | strcat(command, "\n"); |
| 310 | break; |
| 311 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 313 | if (c == 'm') { |
| 314 | /* -m is the last option; following arguments |
| 315 | that look like options are left for the |
| 316 | module to interpret. */ |
| 317 | module = (char *)malloc(strlen(_PyOS_optarg) + 2); |
| 318 | if (module == NULL) |
| 319 | Py_FatalError( |
| 320 | "not enough memory to copy -m argument"); |
| 321 | strcpy(module, _PyOS_optarg); |
| 322 | break; |
| 323 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 325 | switch (c) { |
| 326 | case 'b': |
| 327 | Py_BytesWarningFlag++; |
| 328 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 330 | case 'd': |
| 331 | Py_DebugFlag++; |
| 332 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | case '3': |
| 335 | Py_Py3kWarningFlag++; |
| 336 | if (!Py_DivisionWarningFlag) |
| 337 | Py_DivisionWarningFlag = 1; |
| 338 | break; |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 340 | case 'Q': |
| 341 | if (strcmp(_PyOS_optarg, "old") == 0) { |
| 342 | Py_DivisionWarningFlag = 0; |
| 343 | break; |
| 344 | } |
| 345 | if (strcmp(_PyOS_optarg, "warn") == 0) { |
| 346 | Py_DivisionWarningFlag = 1; |
| 347 | break; |
| 348 | } |
| 349 | if (strcmp(_PyOS_optarg, "warnall") == 0) { |
| 350 | Py_DivisionWarningFlag = 2; |
| 351 | break; |
| 352 | } |
| 353 | if (strcmp(_PyOS_optarg, "new") == 0) { |
| 354 | /* This only affects __main__ */ |
| 355 | cf.cf_flags |= CO_FUTURE_DIVISION; |
| 356 | /* And this tells the eval loop to treat |
| 357 | BINARY_DIVIDE as BINARY_TRUE_DIVIDE */ |
| 358 | _Py_QnewFlag = 1; |
| 359 | break; |
| 360 | } |
| 361 | fprintf(stderr, |
| 362 | "-Q option should be `-Qold', " |
| 363 | "`-Qwarn', `-Qwarnall', or `-Qnew' only\n"); |
| 364 | return usage(2, argv[0]); |
| 365 | /* NOTREACHED */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 367 | case 'i': |
| 368 | Py_InspectFlag++; |
| 369 | Py_InteractiveFlag++; |
| 370 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 372 | /* case 'J': reserved for Jython */ |
Christian Heimes | 7a98d27 | 2008-04-12 13:03:03 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 374 | case 'O': |
| 375 | Py_OptimizeFlag++; |
| 376 | break; |
Guido van Rossum | 7614da6 | 1997-03-03 19:14:45 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 378 | case 'B': |
| 379 | Py_DontWriteBytecodeFlag++; |
| 380 | break; |
Georg Brandl | 2da0fce | 2008-01-07 17:09:35 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 382 | case 's': |
| 383 | Py_NoUserSiteDirectory++; |
| 384 | break; |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 386 | case 'S': |
| 387 | Py_NoSiteFlag++; |
| 388 | break; |
Guido van Rossum | 7922bd7 | 1997-08-29 22:34:47 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 390 | case 'E': |
Antoine Pitrou | cc3fa88 | 2012-02-21 20:42:48 +0100 | [diff] [blame] | 391 | /* Already handled above */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 392 | break; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 394 | case 't': |
| 395 | Py_TabcheckFlag++; |
| 396 | break; |
Guido van Rossum | bba92ca | 1998-04-10 19:39:15 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 398 | case 'u': |
| 399 | unbuffered++; |
| 400 | saw_unbuffered_flag = 1; |
| 401 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 403 | case 'v': |
| 404 | Py_VerboseFlag++; |
| 405 | break; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 407 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 408 | case 'w': |
| 409 | Py_RISCOSWimpFlag = 1; |
| 410 | break; |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 411 | #endif |
| 412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | case 'x': |
| 414 | skipfirstline = 1; |
| 415 | break; |
Guido van Rossum | a075ce1 | 1997-12-05 21:56:45 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 417 | /* case 'X': reserved for implementation-specific arguments */ |
Christian Heimes | 7a98d27 | 2008-04-12 13:03:03 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 419 | case 'U': |
| 420 | Py_UnicodeFlag++; |
| 421 | break; |
| 422 | case 'h': |
| 423 | case '?': |
| 424 | help++; |
| 425 | break; |
| 426 | case 'V': |
| 427 | version++; |
| 428 | break; |
Guido van Rossum | c15a9a1 | 2000-05-01 17:54:33 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 430 | case 'W': |
| 431 | PySys_AddWarnOption(_PyOS_optarg); |
| 432 | break; |
Guido van Rossum | 47f5fdc | 2000-12-15 22:00:54 +0000 | [diff] [blame] | 433 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 434 | case 'R': |
Antoine Pitrou | cc3fa88 | 2012-02-21 20:42:48 +0100 | [diff] [blame] | 435 | /* Already handled above */ |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 436 | break; |
| 437 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 438 | /* This space reserved for other options */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 440 | default: |
| 441 | return usage(2, argv[0]); |
| 442 | /*NOTREACHED*/ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 443 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 447 | if (help) |
| 448 | return usage(0, argv[0]); |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 450 | if (version) { |
| 451 | fprintf(stderr, "Python %s\n", PY_VERSION); |
| 452 | return 0; |
| 453 | } |
Barry Warsaw | 3b2aedb | 2000-09-15 18:40:42 +0000 | [diff] [blame] | 454 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 455 | if (Py_Py3kWarningFlag && !Py_TabcheckFlag) |
| 456 | /* -3 implies -t (but not -tt) */ |
| 457 | Py_TabcheckFlag = 1; |
Georg Brandl | 3c4a546 | 2009-04-12 12:08:12 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 459 | if (!Py_InspectFlag && |
| 460 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 461 | Py_InspectFlag = 1; |
| 462 | if (!saw_unbuffered_flag && |
| 463 | (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 464 | unbuffered = 1; |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 466 | if (!Py_NoUserSiteDirectory && |
| 467 | (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0') |
| 468 | Py_NoUserSiteDirectory = 1; |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 470 | if ((p = Py_GETENV("PYTHONWARNINGS")) && *p != '\0') { |
| 471 | char *buf, *warning; |
Philip Jenvey | cdd98fb | 2010-04-10 20:27:15 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 473 | buf = (char *)malloc(strlen(p) + 1); |
| 474 | if (buf == NULL) |
| 475 | Py_FatalError( |
| 476 | "not enough memory to copy PYTHONWARNINGS"); |
| 477 | strcpy(buf, p); |
| 478 | for (warning = strtok(buf, ","); |
| 479 | warning != NULL; |
| 480 | warning = strtok(NULL, ",")) |
| 481 | PySys_AddWarnOption(warning); |
| 482 | free(buf); |
| 483 | } |
Philip Jenvey | aebbaeb | 2010-04-06 23:24:45 +0000 | [diff] [blame] | 484 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 485 | if (command == NULL && module == NULL && _PyOS_optind < argc && |
| 486 | strcmp(argv[_PyOS_optind], "-") != 0) |
| 487 | { |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 488 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | filename = decc$translate_vms(argv[_PyOS_optind]); |
| 490 | if (filename == (char *)0 || filename == (char *)-1) |
| 491 | filename = argv[_PyOS_optind]; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 492 | |
| 493 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 494 | filename = argv[_PyOS_optind]; |
Martin v. Löwis | 7a924e6 | 2003-03-05 14:15:21 +0000 | [diff] [blame] | 495 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 496 | } |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 497 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 498 | stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0); |
Guido van Rossum | 775af91 | 1997-02-14 19:50:32 +0000 | [diff] [blame] | 499 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 500 | if (unbuffered) { |
Sjoerd Mullender | 9cf424b | 2002-08-09 13:35:18 +0000 | [diff] [blame] | 501 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 502 | _setmode(fileno(stdin), O_BINARY); |
| 503 | _setmode(fileno(stdout), O_BINARY); |
Guido van Rossum | f22d7e2 | 1997-01-11 19:28:55 +0000 | [diff] [blame] | 504 | #endif |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 505 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 506 | setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ); |
| 507 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
| 508 | setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 509 | #else /* !HAVE_SETVBUF */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 510 | setbuf(stdin, (char *)NULL); |
| 511 | setbuf(stdout, (char *)NULL); |
| 512 | setbuf(stderr, (char *)NULL); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 513 | #endif /* !HAVE_SETVBUF */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 514 | } |
| 515 | else if (Py_InteractiveFlag) { |
Guido van Rossum | b31c7dc | 1997-04-11 22:19:12 +0000 | [diff] [blame] | 516 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 517 | /* Doesn't have to have line-buffered -- use unbuffered */ |
| 518 | /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */ |
| 519 | setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 520 | #else /* !MS_WINDOWS */ |
| 521 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 522 | setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ); |
| 523 | setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ); |
Guido van Rossum | 22ffac1 | 1998-03-06 15:30:39 +0000 | [diff] [blame] | 524 | #endif /* HAVE_SETVBUF */ |
| 525 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 526 | /* Leave stderr alone - it should be unbuffered anyway. */ |
| 527 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 528 | #ifdef __VMS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 529 | else { |
| 530 | setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ); |
| 531 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 532 | #endif /* __VMS */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 533 | |
Just van Rossum | 2ac79ef | 2003-03-05 15:46:54 +0000 | [diff] [blame] | 534 | #ifdef __APPLE__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 535 | /* On MacOS X, when the Python interpreter is embedded in an |
| 536 | application bundle, it gets executed by a bootstrapping script |
| 537 | that does os.execve() with an argv[0] that's different from the |
| 538 | actual Python executable. This is needed to keep the Finder happy, |
| 539 | or rather, to work around Apple's overly strict requirements of |
| 540 | the process name. However, we still need a usable sys.executable, |
| 541 | so the actual executable path is passed in an environment variable. |
| 542 | See Lib/plat-mac/bundlebuiler.py for details about the bootstrap |
| 543 | script. */ |
| 544 | if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') |
| 545 | Py_SetProgramName(p); |
| 546 | else |
| 547 | Py_SetProgramName(argv[0]); |
Just van Rossum | 2ac79ef | 2003-03-05 15:46:54 +0000 | [diff] [blame] | 548 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 549 | Py_SetProgramName(argv[0]); |
Jack Jansen | fbd861b | 2003-03-05 16:00:15 +0000 | [diff] [blame] | 550 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 551 | Py_Initialize(); |
Guido van Rossum | ed52aac | 1997-07-19 19:20:32 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 553 | if (Py_VerboseFlag || |
| 554 | (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) { |
| 555 | fprintf(stderr, "Python %s on %s\n", |
| 556 | Py_GetVersion(), Py_GetPlatform()); |
| 557 | if (!Py_NoSiteFlag) |
| 558 | fprintf(stderr, "%s\n", COPYRIGHT); |
| 559 | } |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 561 | if (command != NULL) { |
| 562 | /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ |
| 563 | _PyOS_optind--; |
| 564 | argv[_PyOS_optind] = "-c"; |
| 565 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 566 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 567 | if (module != NULL) { |
Nick Coghlan | 8842c35 | 2010-06-13 06:50:39 +0000 | [diff] [blame] | 568 | /* Backup _PyOS_optind and force sys.argv[0] = '-c' |
| 569 | so that PySys_SetArgv correctly sets sys.path[0] to '' |
| 570 | rather than looking for a file called "-m". See |
| 571 | tracker issue #8202 for details. */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 572 | _PyOS_optind--; |
Nick Coghlan | 8842c35 | 2010-06-13 06:50:39 +0000 | [diff] [blame] | 573 | argv[_PyOS_optind] = "-c"; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 574 | } |
Raymond Hettinger | db29e0f | 2004-10-07 06:46:25 +0000 | [diff] [blame] | 575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 576 | PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) && |
| 579 | isatty(fileno(stdin))) { |
| 580 | PyObject *v; |
| 581 | v = PyImport_ImportModule("readline"); |
| 582 | if (v == NULL) |
| 583 | PyErr_Clear(); |
| 584 | else |
| 585 | Py_DECREF(v); |
| 586 | } |
Guido van Rossum | 3d26cc9 | 1997-09-16 16:11:28 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 588 | if (command) { |
| 589 | sts = PyRun_SimpleStringFlags(command, &cf) != 0; |
| 590 | free(command); |
| 591 | } else if (module) { |
Senthil Kumaran | 3b30b19 | 2012-07-04 19:50:29 -0700 | [diff] [blame] | 592 | sts = (RunModule(module, 1) != 0); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 593 | free(module); |
| 594 | } |
| 595 | else { |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 597 | if (filename == NULL && stdin_is_interactive) { |
| 598 | Py_InspectFlag = 0; /* do exit on SystemExit */ |
| 599 | RunStartupFile(&cf); |
| 600 | } |
| 601 | /* XXX */ |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 603 | sts = -1; /* keep track of whether we've already run __main__ */ |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 605 | if (filename != NULL) { |
| 606 | sts = RunMainFromImporter(filename); |
| 607 | } |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | if (sts==-1 && filename!=NULL) { |
| 610 | if ((fp = fopen(filename, "r")) == NULL) { |
| 611 | fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n", |
| 612 | argv[0], filename, errno, strerror(errno)); |
Brett Cannon | 10ed0f5 | 2008-03-18 15:35:58 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 614 | return 2; |
| 615 | } |
| 616 | else if (skipfirstline) { |
| 617 | int ch; |
| 618 | /* Push back first newline so line numbers |
| 619 | remain the same */ |
| 620 | while ((ch = getc(fp)) != EOF) { |
| 621 | if (ch == '\n') { |
| 622 | (void)ungetc(ch, fp); |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | { |
| 628 | /* XXX: does this work on Win/Win64? (see posix_fstat) */ |
| 629 | struct stat sb; |
| 630 | if (fstat(fileno(fp), &sb) == 0 && |
| 631 | S_ISDIR(sb.st_mode)) { |
| 632 | fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename); |
| 633 | fclose(fp); |
| 634 | return 1; |
| 635 | } |
| 636 | } |
| 637 | } |
Nick Coghlan | 327a39b | 2007-11-18 11:56:28 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 639 | if (sts==-1) { |
| 640 | /* call pending calls like signal handlers (SIGINT) */ |
| 641 | if (Py_MakePendingCalls() == -1) { |
| 642 | PyErr_Print(); |
| 643 | sts = 1; |
| 644 | } else { |
| 645 | sts = PyRun_AnyFileExFlags( |
| 646 | fp, |
| 647 | filename == NULL ? "<stdin>" : filename, |
| 648 | filename != NULL, &cf) != 0; |
| 649 | } |
| 650 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 652 | } |
Barry Warsaw | d86dcd3 | 2003-06-29 17:07:06 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 654 | /* Check this environment variable at the end, to give programs the |
| 655 | * opportunity to set it from Python. |
| 656 | */ |
| 657 | if (!Py_InspectFlag && |
| 658 | (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 659 | { |
| 660 | Py_InspectFlag = 1; |
| 661 | } |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 662 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | if (Py_InspectFlag && stdin_is_interactive && |
| 664 | (filename != NULL || command != NULL || module != NULL)) { |
| 665 | Py_InspectFlag = 0; |
| 666 | /* XXX */ |
| 667 | sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0; |
| 668 | } |
| 669 | |
| 670 | Py_Finalize(); |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 671 | #ifdef RISCOS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 672 | if (Py_RISCOSWimpFlag) |
| 673 | fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */ |
Guido van Rossum | 3ed4c15 | 2001-03-02 06:18:03 +0000 | [diff] [blame] | 674 | #endif |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 675 | |
| 676 | #ifdef __INSURE__ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 677 | /* Insure++ is a memory analysis tool that aids in discovering |
| 678 | * memory leaks and other memory problems. On Python exit, the |
| 679 | * interned string dictionary is flagged as being in use at exit |
| 680 | * (which it is). Under normal circumstances, this is fine because |
| 681 | * the memory will be automatically reclaimed by the system. Under |
| 682 | * memory debugging, it's a huge source of useless noise, so we |
| 683 | * trade off slower shutdown for less distraction in the memory |
| 684 | * reports. -baw |
| 685 | */ |
| 686 | _Py_ReleaseInternedStrings(); |
Barry Warsaw | 3e13b1e | 2001-02-23 16:46:39 +0000 | [diff] [blame] | 687 | #endif /* __INSURE__ */ |
| 688 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 689 | return sts; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Skip Montanaro | 786ea6b | 2004-03-01 15:44:05 +0000 | [diff] [blame] | 692 | /* this is gonna seem *real weird*, but if you put some other code between |
| 693 | Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the |
| 694 | while statement in Misc/gdbinit:ppystack */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 695 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 696 | /* Make the *original* argc/argv available to other modules. |
| 697 | This is rare, but it is needed by the secureware extension. */ |
| 698 | |
| 699 | void |
Fredrik Lundh | 620f377 | 2000-07-09 20:42:34 +0000 | [diff] [blame] | 700 | Py_GetArgcArgv(int *argc, char ***argv) |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 701 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | *argc = orig_argc; |
| 703 | *argv = orig_argv; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 704 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 705 | |
| 706 | #ifdef __cplusplus |
| 707 | } |
| 708 | #endif |
| 709 | |