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