1 /* Python interpreter main program */
5 #include "code.h" /* For CO_FUTURE_DIVISION */
12 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
18 #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
19 #define PYTHONHOMEHELP "<prefix>\\lib"
21 #if defined(PYOS_OS2) && defined(PYCC_GCC)
22 #define PYTHONHOMEHELP "<prefix>/Lib"
24 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
31 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
32 "for more information."
38 /* For Py_GetArgcArgv(); set by main() */
39 static char **orig_argv
;
42 /* command line options */
43 #define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX"
46 #define PROGRAM_OPTS BASE_OPTS
48 /* extra option saying that we are running under a special task window
49 frontend; especially my_readline will behave different */
50 #define PROGRAM_OPTS BASE_OPTS "w"
51 /* corresponding flag */
52 extern int Py_RISCOSWimpFlag
;
55 /* Short usage message (with %s for argv0) */
56 static char *usage_line
=
57 "usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
59 /* Long usage message, split into parts < 512 bytes */
60 static char *usage_1
= "\
61 Options and arguments (and corresponding environment variables):\n\
62 -c cmd : program passed in as string (terminates option list)\n\
63 -d : debug output from parser (also PYTHONDEBUG=x)\n\
64 -E : ignore environment variables (such as PYTHONPATH)\n\
65 -h : print this help message and exit\n\
66 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
67 and force prompts, even if stdin does not appear to be a terminal\n\
69 static char *usage_2
= "\
70 -m mod : run library module as a script (terminates option list)\n\
71 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
72 -OO : remove doc-strings in addition to the -O optimizations\n\
73 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
74 -S : don't imply 'import site' on initialization\n\
75 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
76 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
78 static char *usage_3
= "\
79 see man page for details on internal buffering relating to '-u'\n\
80 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
81 -V : print the Python version number and exit\n\
82 -W arg : warning control (arg is action:message:category:module:lineno)\n\
83 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
84 file : program read from script file\n\
85 - : program read from stdin (default; interactive mode if a tty)\n\
87 static char *usage_4
= "\
88 arg ...: arguments passed to program in sys.argv[1:]\n\
89 Other environment variables:\n\
90 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
91 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
92 default module search path. The result is sys.path.\n\
93 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
94 The default module search path uses %s.\n\
95 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
100 usage(int exitcode
, char* program
)
102 FILE *f
= exitcode
? stderr
: stdout
;
104 fprintf(f
, usage_line
, program
);
106 fprintf(f
, "Try `python -h' for more information.\n");
111 fprintf(f
, usage_4
, DELIM
, DELIM
, PYTHONHOMEHELP
);
115 /* suppress 'error' message */
119 /* STS$M_INHIB_MSG + SS$_ABORT */
128 static void RunStartupFile(PyCompilerFlags
*cf
)
130 char *startup
= Py_GETENV("PYTHONSTARTUP");
131 if (startup
!= NULL
&& startup
[0] != '\0') {
132 FILE *fp
= fopen(startup
, "r");
134 (void) PyRun_SimpleFileExFlags(fp
, startup
, 0, cf
);
142 static int RunModule(char *module
)
144 PyObject
*runpy
, *runmodule
, *runargs
, *result
;
145 runpy
= PyImport_ImportModule("runpy");
147 fprintf(stderr
, "Could not import runpy module\n");
150 runmodule
= PyObject_GetAttrString(runpy
, "run_module");
151 if (runmodule
== NULL
) {
152 fprintf(stderr
, "Could not access runpy.run_module\n");
156 runargs
= Py_BuildValue("sOsO", module
,
157 Py_None
, "__main__", Py_True
);
158 if (runargs
== NULL
) {
160 "Could not create arguments for runpy.run_module\n");
162 Py_DECREF(runmodule
);
165 result
= PyObject_Call(runmodule
, runargs
, NULL
);
166 if (result
== NULL
) {
170 Py_DECREF(runmodule
);
172 if (result
== NULL
) {
182 Py_Main(int argc
, char **argv
)
186 char *command
= NULL
;
187 char *filename
= NULL
;
193 int skipfirstline
= 0;
194 int stdin_is_interactive
= 0;
197 int saw_inspect_flag
= 0;
198 int saw_unbuffered_flag
= 0;
203 orig_argc
= argc
; /* For Py_GetArgcArgv() */
207 Py_RISCOSWimpFlag
= 0;
210 PySys_ResetWarnOptions();
212 while ((c
= _PyOS_GetOpt(argc
, argv
, PROGRAM_OPTS
)) != EOF
) {
214 /* -c is the last option; following arguments
215 that look like options are left for the
216 command to interpret. */
217 command
= (char *)malloc(strlen(_PyOS_optarg
) + 2);
220 "not enough memory to copy -c argument");
221 strcpy(command
, _PyOS_optarg
);
222 strcat(command
, "\n");
227 /* -m is the last option; following arguments
228 that look like options are left for the
229 module to interpret. */
230 module
= (char *)malloc(strlen(_PyOS_optarg
) + 2);
233 "not enough memory to copy -m argument");
234 strcpy(module
, _PyOS_optarg
);
245 if (strcmp(_PyOS_optarg
, "old") == 0) {
246 Py_DivisionWarningFlag
= 0;
249 if (strcmp(_PyOS_optarg
, "warn") == 0) {
250 Py_DivisionWarningFlag
= 1;
253 if (strcmp(_PyOS_optarg
, "warnall") == 0) {
254 Py_DivisionWarningFlag
= 2;
257 if (strcmp(_PyOS_optarg
, "new") == 0) {
258 /* This only affects __main__ */
259 cf
.cf_flags
|= CO_FUTURE_DIVISION
;
260 /* And this tells the eval loop to treat
261 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
266 "-Q option should be `-Qold', "
267 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
268 return usage(2, argv
[0]);
273 saw_inspect_flag
= 1;
274 Py_InteractiveFlag
++;
286 Py_IgnoreEnvironmentFlag
++;
295 saw_unbuffered_flag
= 1;
304 Py_RISCOSWimpFlag
= 1;
323 PySys_AddWarnOption(_PyOS_optarg
);
326 /* This space reserved for other options */
329 return usage(2, argv
[0]);
336 return usage(0, argv
[0]);
339 fprintf(stderr
, "Python %s\n", PY_VERSION
);
343 if (!saw_inspect_flag
&&
344 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
346 if (!saw_unbuffered_flag
&&
347 (p
= Py_GETENV("PYTHONUNBUFFERED")) && *p
!= '\0')
350 if (command
== NULL
&& module
== NULL
&& _PyOS_optind
< argc
&&
351 strcmp(argv
[_PyOS_optind
], "-") != 0)
354 filename
= decc$
translate_vms(argv
[_PyOS_optind
]);
355 if (filename
== (char *)0 || filename
== (char *)-1)
356 filename
= argv
[_PyOS_optind
];
359 filename
= argv
[_PyOS_optind
];
361 if (filename
!= NULL
) {
362 if ((fp
= fopen(filename
, "r")) == NULL
) {
364 fprintf(stderr
, "%s: can't open file '%s': [Errno %d] %s\n",
365 argv
[0], filename
, errno
, strerror(errno
));
367 fprintf(stderr
, "%s: can't open file '%s': Errno %d\n",
368 argv
[0], filename
, errno
);
372 else if (skipfirstline
) {
374 /* Push back first newline so line numbers
376 while ((ch
= getc(fp
)) != EOF
) {
378 (void)ungetc(ch
, fp
);
384 /* XXX: does this work on Win/Win64? (see posix_fstat) */
386 if (fstat(fileno(fp
), &sb
) == 0 &&
387 S_ISDIR(sb
.st_mode
)) {
388 fprintf(stderr
, "%s: '%s' is a directory, cannot continue\n", argv
[0], filename
);
395 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
398 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
399 _setmode(fileno(stdin
), O_BINARY
);
400 _setmode(fileno(stdout
), O_BINARY
);
403 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
404 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
405 setvbuf(stderr
, (char *)NULL
, _IONBF
, BUFSIZ
);
406 #else /* !HAVE_SETVBUF */
407 setbuf(stdin
, (char *)NULL
);
408 setbuf(stdout
, (char *)NULL
);
409 setbuf(stderr
, (char *)NULL
);
410 #endif /* !HAVE_SETVBUF */
412 else if (Py_InteractiveFlag
) {
414 /* Doesn't have to have line-buffered -- use unbuffered */
415 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
416 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
417 #else /* !MS_WINDOWS */
419 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
420 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
421 #endif /* HAVE_SETVBUF */
422 #endif /* !MS_WINDOWS */
423 /* Leave stderr alone - it should be unbuffered anyway. */
427 setvbuf (stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
432 /* On MacOS X, when the Python interpreter is embedded in an
433 application bundle, it gets executed by a bootstrapping script
434 that does os.execve() with an argv[0] that's different from the
435 actual Python executable. This is needed to keep the Finder happy,
436 or rather, to work around Apple's overly strict requirements of
437 the process name. However, we still need a usable sys.executable,
438 so the actual executable path is passed in an environment variable.
439 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
441 if ((p
= Py_GETENV("PYTHONEXECUTABLE")) && *p
!= '\0')
442 Py_SetProgramName(p
);
444 Py_SetProgramName(argv
[0]);
446 Py_SetProgramName(argv
[0]);
450 if (Py_VerboseFlag
||
451 (command
== NULL
&& filename
== NULL
&& module
== NULL
&& stdin_is_interactive
)) {
452 fprintf(stderr
, "Python %s on %s\n",
453 Py_GetVersion(), Py_GetPlatform());
455 fprintf(stderr
, "%s\n", COPYRIGHT
);
458 if (command
!= NULL
) {
459 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
461 argv
[_PyOS_optind
] = "-c";
464 if (module
!= NULL
) {
465 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
466 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
468 argv
[_PyOS_optind
] = "-c";
471 PySys_SetArgv(argc
-_PyOS_optind
, argv
+_PyOS_optind
);
473 if ((inspect
|| (command
== NULL
&& filename
== NULL
&& module
== NULL
)) &&
474 isatty(fileno(stdin
))) {
476 v
= PyImport_ImportModule("readline");
484 sts
= PyRun_SimpleStringFlags(command
, &cf
) != 0;
487 sts
= RunModule(module
);
491 if (filename
== NULL
&& stdin_is_interactive
) {
495 sts
= PyRun_AnyFileExFlags(
497 filename
== NULL
? "<stdin>" : filename
,
498 filename
!= NULL
, &cf
) != 0;
501 /* Check this environment variable at the end, to give programs the
502 * opportunity to set it from Python.
504 if (!saw_inspect_flag
&&
505 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
510 if (inspect
&& stdin_is_interactive
&&
511 (filename
!= NULL
|| command
!= NULL
|| module
!= NULL
))
513 sts
= PyRun_AnyFileFlags(stdin
, "<stdin>", &cf
) != 0;
517 if (Py_RISCOSWimpFlag
)
518 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
522 /* Insure++ is a memory analysis tool that aids in discovering
523 * memory leaks and other memory problems. On Python exit, the
524 * interned string dictionary is flagged as being in use at exit
525 * (which it is). Under normal circumstances, this is fine because
526 * the memory will be automatically reclaimed by the system. Under
527 * memory debugging, it's a huge source of useless noise, so we
528 * trade off slower shutdown for less distraction in the memory
531 _Py_ReleaseInternedStrings();
532 #endif /* __INSURE__ */
537 /* this is gonna seem *real weird*, but if you put some other code between
538 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
539 while statement in Misc/gdbinit:ppystack */
541 /* Make the *original* argc/argv available to other modules.
542 This is rare, but it is needed by the secureware extension. */
545 Py_GetArgcArgv(int *argc
, char ***argv
)