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