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 (also --help)\n\
66 -i : inspect interactively after running script; forces a prompt even\n\
67 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
69 static char *usage_2
= "\
70 -m mod : run library module as a script (terminates option list)\n\
71 -O : optimize generated bytecode slightly; 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 can be supplied multiple times to increase verbosity\n\
82 -V : print the Python version number and exit (also --version)\n\
83 -W arg : warning control; arg is action:message:category:module:lineno\n\
84 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
85 file : program read from script file\n\
86 - : program read from stdin (default; interactive mode if a tty)\n\
88 static char *usage_4
= "\
89 arg ...: arguments passed to program in sys.argv[1:]\n\n\
90 Other environment variables:\n\
91 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
92 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
93 default module search path. The result is sys.path.\n\
94 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
95 The default module search path uses %s.\n\
96 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
101 usage(int exitcode
, char* program
)
103 FILE *f
= exitcode
? stderr
: stdout
;
105 fprintf(f
, usage_line
, program
);
107 fprintf(f
, "Try `python -h' for more information.\n");
112 fprintf(f
, usage_4
, DELIM
, DELIM
, PYTHONHOMEHELP
);
116 /* suppress 'error' message */
120 /* STS$M_INHIB_MSG + SS$_ABORT */
129 static void RunStartupFile(PyCompilerFlags
*cf
)
131 char *startup
= Py_GETENV("PYTHONSTARTUP");
132 if (startup
!= NULL
&& startup
[0] != '\0') {
133 FILE *fp
= fopen(startup
, "r");
135 (void) PyRun_SimpleFileExFlags(fp
, startup
, 0, cf
);
143 static int RunModule(char *module
)
145 PyObject
*runpy
, *runmodule
, *runargs
, *result
;
146 runpy
= PyImport_ImportModule("runpy");
148 fprintf(stderr
, "Could not import runpy module\n");
151 runmodule
= PyObject_GetAttrString(runpy
, "run_module");
152 if (runmodule
== NULL
) {
153 fprintf(stderr
, "Could not access runpy.run_module\n");
157 runargs
= Py_BuildValue("sOsO", module
,
158 Py_None
, "__main__", Py_True
);
159 if (runargs
== NULL
) {
161 "Could not create arguments for runpy.run_module\n");
163 Py_DECREF(runmodule
);
166 result
= PyObject_Call(runmodule
, runargs
, NULL
);
167 if (result
== NULL
) {
171 Py_DECREF(runmodule
);
173 if (result
== NULL
) {
183 Py_Main(int argc
, char **argv
)
187 char *command
= NULL
;
188 char *filename
= NULL
;
194 int skipfirstline
= 0;
195 int stdin_is_interactive
= 0;
198 int saw_inspect_flag
= 0;
199 int saw_unbuffered_flag
= 0;
204 orig_argc
= argc
; /* For Py_GetArgcArgv() */
208 Py_RISCOSWimpFlag
= 0;
211 PySys_ResetWarnOptions();
213 while ((c
= _PyOS_GetOpt(argc
, argv
, PROGRAM_OPTS
)) != EOF
) {
215 /* -c is the last option; following arguments
216 that look like options are left for the
217 command to interpret. */
218 command
= (char *)malloc(strlen(_PyOS_optarg
) + 2);
221 "not enough memory to copy -c argument");
222 strcpy(command
, _PyOS_optarg
);
223 strcat(command
, "\n");
228 /* -m is the last option; following arguments
229 that look like options are left for the
230 module to interpret. */
231 module
= (char *)malloc(strlen(_PyOS_optarg
) + 2);
234 "not enough memory to copy -m argument");
235 strcpy(module
, _PyOS_optarg
);
246 if (strcmp(_PyOS_optarg
, "old") == 0) {
247 Py_DivisionWarningFlag
= 0;
250 if (strcmp(_PyOS_optarg
, "warn") == 0) {
251 Py_DivisionWarningFlag
= 1;
254 if (strcmp(_PyOS_optarg
, "warnall") == 0) {
255 Py_DivisionWarningFlag
= 2;
258 if (strcmp(_PyOS_optarg
, "new") == 0) {
259 /* This only affects __main__ */
260 cf
.cf_flags
|= CO_FUTURE_DIVISION
;
261 /* And this tells the eval loop to treat
262 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
267 "-Q option should be `-Qold', "
268 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
269 return usage(2, argv
[0]);
274 saw_inspect_flag
= 1;
275 Py_InteractiveFlag
++;
287 Py_IgnoreEnvironmentFlag
++;
296 saw_unbuffered_flag
= 1;
305 Py_RISCOSWimpFlag
= 1;
325 PySys_AddWarnOption(_PyOS_optarg
);
328 /* This space reserved for other options */
331 return usage(2, argv
[0]);
338 return usage(0, argv
[0]);
341 fprintf(stderr
, "Python %s\n", PY_VERSION
);
345 if (!saw_inspect_flag
&&
346 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
348 if (!saw_unbuffered_flag
&&
349 (p
= Py_GETENV("PYTHONUNBUFFERED")) && *p
!= '\0')
352 if (command
== NULL
&& module
== NULL
&& _PyOS_optind
< argc
&&
353 strcmp(argv
[_PyOS_optind
], "-") != 0)
356 filename
= decc$
translate_vms(argv
[_PyOS_optind
]);
357 if (filename
== (char *)0 || filename
== (char *)-1)
358 filename
= argv
[_PyOS_optind
];
361 filename
= argv
[_PyOS_optind
];
363 if (filename
!= NULL
) {
364 if ((fp
= fopen(filename
, "r")) == NULL
) {
366 fprintf(stderr
, "%s: can't open file '%s': [Errno %d] %s\n",
367 argv
[0], filename
, errno
, strerror(errno
));
369 fprintf(stderr
, "%s: can't open file '%s': Errno %d\n",
370 argv
[0], filename
, errno
);
374 else if (skipfirstline
) {
376 /* Push back first newline so line numbers
378 while ((ch
= getc(fp
)) != EOF
) {
380 (void)ungetc(ch
, fp
);
386 /* XXX: does this work on Win/Win64? (see posix_fstat) */
388 if (fstat(fileno(fp
), &sb
) == 0 &&
389 S_ISDIR(sb
.st_mode
)) {
390 fprintf(stderr
, "%s: '%s' is a directory, cannot continue\n", argv
[0], filename
);
397 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
400 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
401 _setmode(fileno(stdin
), O_BINARY
);
402 _setmode(fileno(stdout
), O_BINARY
);
405 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
406 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
407 setvbuf(stderr
, (char *)NULL
, _IONBF
, BUFSIZ
);
408 #else /* !HAVE_SETVBUF */
409 setbuf(stdin
, (char *)NULL
);
410 setbuf(stdout
, (char *)NULL
);
411 setbuf(stderr
, (char *)NULL
);
412 #endif /* !HAVE_SETVBUF */
414 else if (Py_InteractiveFlag
) {
416 /* Doesn't have to have line-buffered -- use unbuffered */
417 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
418 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
419 #else /* !MS_WINDOWS */
421 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
422 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
423 #endif /* HAVE_SETVBUF */
424 #endif /* !MS_WINDOWS */
425 /* Leave stderr alone - it should be unbuffered anyway. */
429 setvbuf (stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
434 /* On MacOS X, when the Python interpreter is embedded in an
435 application bundle, it gets executed by a bootstrapping script
436 that does os.execve() with an argv[0] that's different from the
437 actual Python executable. This is needed to keep the Finder happy,
438 or rather, to work around Apple's overly strict requirements of
439 the process name. However, we still need a usable sys.executable,
440 so the actual executable path is passed in an environment variable.
441 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
443 if ((p
= Py_GETENV("PYTHONEXECUTABLE")) && *p
!= '\0')
444 Py_SetProgramName(p
);
446 Py_SetProgramName(argv
[0]);
448 Py_SetProgramName(argv
[0]);
452 if (Py_VerboseFlag
||
453 (command
== NULL
&& filename
== NULL
&& module
== NULL
&& stdin_is_interactive
)) {
454 fprintf(stderr
, "Python %s on %s\n",
455 Py_GetVersion(), Py_GetPlatform());
457 fprintf(stderr
, "%s\n", COPYRIGHT
);
460 if (command
!= NULL
) {
461 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
463 argv
[_PyOS_optind
] = "-c";
466 if (module
!= NULL
) {
467 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
468 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
470 argv
[_PyOS_optind
] = "-c";
473 PySys_SetArgv(argc
-_PyOS_optind
, argv
+_PyOS_optind
);
475 if ((inspect
|| (command
== NULL
&& filename
== NULL
&& module
== NULL
)) &&
476 isatty(fileno(stdin
))) {
478 v
= PyImport_ImportModule("readline");
486 sts
= PyRun_SimpleStringFlags(command
, &cf
) != 0;
489 sts
= RunModule(module
);
493 if (filename
== NULL
&& stdin_is_interactive
) {
497 sts
= PyRun_AnyFileExFlags(
499 filename
== NULL
? "<stdin>" : filename
,
500 filename
!= NULL
, &cf
) != 0;
503 /* Check this environment variable at the end, to give programs the
504 * opportunity to set it from Python.
506 if (!saw_inspect_flag
&&
507 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
512 if (inspect
&& stdin_is_interactive
&&
513 (filename
!= NULL
|| command
!= NULL
|| module
!= NULL
))
515 sts
= PyRun_AnyFileFlags(stdin
, "<stdin>", &cf
) != 0;
519 if (Py_RISCOSWimpFlag
)
520 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
524 /* Insure++ is a memory analysis tool that aids in discovering
525 * memory leaks and other memory problems. On Python exit, the
526 * interned string dictionary is flagged as being in use at exit
527 * (which it is). Under normal circumstances, this is fine because
528 * the memory will be automatically reclaimed by the system. Under
529 * memory debugging, it's a huge source of useless noise, so we
530 * trade off slower shutdown for less distraction in the memory
533 _Py_ReleaseInternedStrings();
534 #endif /* __INSURE__ */
539 /* this is gonna seem *real weird*, but if you put some other code between
540 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
541 while statement in Misc/gdbinit:ppystack */
543 /* Make the *original* argc/argv available to other modules.
544 This is rare, but it is needed by the secureware extension. */
547 Py_GetArgcArgv(int *argc
, char ***argv
)