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