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