1 /* Python interpreter main program */
5 #include "code.h" /* For CO_FUTURE_DIVISION */
12 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
16 #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
17 #define PYTHONHOMEHELP "<prefix>\\lib"
19 #if defined(PYOS_OS2) && defined(PYCC_GCC)
20 #define PYTHONHOMEHELP "<prefix>/Lib"
22 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
29 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
30 "for more information."
32 /* For Py_GetArgcArgv(); set by main() */
33 static char **orig_argv
;
36 /* command line options */
37 #define BASE_OPTS "c:dEhim:OQ:StuUvVW:xX"
40 #define PROGRAM_OPTS BASE_OPTS
42 /* extra option saying that we are running under a special task window
43 frontend; especially my_readline will behave different */
44 #define PROGRAM_OPTS BASE_OPTS "w"
45 /* corresponding flag */
46 extern int Py_RISCOSWimpFlag
;
49 /* Short usage message (with %s for argv0) */
50 static char *usage_line
=
51 "usage: %s [option] ... [-c cmd | -m mod | file | -] [arg] ...\n";
53 /* Long usage message, split into parts < 512 bytes */
54 static char *usage_1
= "\
55 Options and arguments (and corresponding environment variables):\n\
56 -c cmd : program passed in as string (terminates option list)\n\
57 -d : debug output from parser (also PYTHONDEBUG=x)\n\
58 -E : ignore environment variables (such as PYTHONPATH)\n\
59 -h : print this help message and exit\n\
60 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
61 and force prompts, even if stdin does not appear to be a terminal\n\
63 static char *usage_2
= "\
64 -m mod : run library module as a script (terminates option list)\n\
65 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
66 -OO : remove doc-strings in addition to the -O optimizations\n\
67 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
68 -S : don't imply 'import site' on initialization\n\
69 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
70 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
72 static char *usage_3
= "\
73 see man page for details on internal buffering relating to '-u'\n\
74 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
75 -V : print the Python version number and exit\n\
76 -W arg : warning control (arg is action:message:category:module:lineno)\n\
77 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
78 file : program read from script file\n\
79 - : program read from stdin (default; interactive mode if a tty)\n\
81 static char *usage_4
= "\
82 arg ...: arguments passed to program in sys.argv[1:]\n\
83 Other environment variables:\n\
84 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
85 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
86 default module search path. The result is sys.path.\n\
87 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
88 The default module search path uses %s.\n\
89 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
94 usage(int exitcode
, char* program
)
96 FILE *f
= exitcode
? stderr
: stdout
;
98 fprintf(f
, usage_line
, program
);
100 fprintf(f
, "Try `python -h' for more information.\n");
105 fprintf(f
, usage_4
, DELIM
, DELIM
, PYTHONHOMEHELP
);
109 /* suppress 'error' message */
113 /* STS$M_INHIB_MSG + SS$_ABORT */
122 static void RunStartupFile(PyCompilerFlags
*cf
)
124 char *startup
= Py_GETENV("PYTHONSTARTUP");
125 if (startup
!= NULL
&& startup
[0] != '\0') {
126 FILE *fp
= fopen(startup
, "r");
128 (void) PyRun_SimpleFileExFlags(fp
, startup
, 0, cf
);
135 /* Get the path to a top-level module */
136 static struct filedescr
* FindModule(const char *module
,
137 FILE **fp
, char **filename
)
139 struct filedescr
*fdescr
= NULL
;
141 *filename
= malloc(MAXPATHLEN
);
143 if (*filename
== NULL
)
146 /* Find the actual module source code */
147 fdescr
= _PyImport_FindModule(module
, NULL
,
148 *filename
, MAXPATHLEN
, fp
, NULL
);
150 if (fdescr
== NULL
) {
161 Py_Main(int argc
, char **argv
)
165 char *command
= NULL
;
166 char *filename
= NULL
;
172 int skipfirstline
= 0;
173 int stdin_is_interactive
= 0;
176 int saw_inspect_flag
= 0;
177 int saw_unbuffered_flag
= 0;
182 orig_argc
= argc
; /* For Py_GetArgcArgv() */
186 Py_RISCOSWimpFlag
= 0;
189 PySys_ResetWarnOptions();
191 while ((c
= _PyOS_GetOpt(argc
, argv
, PROGRAM_OPTS
)) != EOF
) {
193 /* -c is the last option; following arguments
194 that look like options are left for the
195 command to interpret. */
196 command
= malloc(strlen(_PyOS_optarg
) + 2);
199 "not enough memory to copy -c argument");
200 strcpy(command
, _PyOS_optarg
);
201 strcat(command
, "\n");
206 /* -m is the last option; following arguments
207 that look like options are left for the
208 module to interpret. */
209 module
= malloc(strlen(_PyOS_optarg
) + 2);
212 "not enough memory to copy -m argument");
213 strcpy(module
, _PyOS_optarg
);
224 if (strcmp(_PyOS_optarg
, "old") == 0) {
225 Py_DivisionWarningFlag
= 0;
228 if (strcmp(_PyOS_optarg
, "warn") == 0) {
229 Py_DivisionWarningFlag
= 1;
232 if (strcmp(_PyOS_optarg
, "warnall") == 0) {
233 Py_DivisionWarningFlag
= 2;
236 if (strcmp(_PyOS_optarg
, "new") == 0) {
237 /* This only affects __main__ */
238 cf
.cf_flags
|= CO_FUTURE_DIVISION
;
239 /* And this tells the eval loop to treat
240 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
245 "-Q option should be `-Qold', "
246 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
247 return usage(2, argv
[0]);
252 saw_inspect_flag
= 1;
253 Py_InteractiveFlag
++;
265 Py_IgnoreEnvironmentFlag
++;
274 saw_unbuffered_flag
= 1;
283 Py_RISCOSWimpFlag
= 1;
302 PySys_AddWarnOption(_PyOS_optarg
);
305 /* This space reserved for other options */
308 return usage(2, argv
[0]);
315 return usage(0, argv
[0]);
318 fprintf(stderr
, "Python %s\n", PY_VERSION
);
322 if (!saw_inspect_flag
&&
323 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
325 if (!saw_unbuffered_flag
&&
326 (p
= Py_GETENV("PYTHONUNBUFFERED")) && *p
!= '\0')
329 if (command
== NULL
&& module
== NULL
&& _PyOS_optind
< argc
&&
330 strcmp(argv
[_PyOS_optind
], "-") != 0)
333 filename
= decc$
translate_vms(argv
[_PyOS_optind
]);
334 if (filename
== (char *)0 || filename
== (char *)-1)
335 filename
= argv
[_PyOS_optind
];
338 filename
= argv
[_PyOS_optind
];
340 if (filename
!= NULL
) {
341 if ((fp
= fopen(filename
, "r")) == NULL
) {
343 fprintf(stderr
, "%s: can't open file '%s': [Errno %d] %s\n",
344 argv
[0], filename
, errno
, strerror(errno
));
346 fprintf(stderr
, "%s: can't open file '%s': Errno %d\n",
347 argv
[0], filename
, errno
);
351 else if (skipfirstline
) {
353 /* Push back first newline so line numbers
355 while ((ch
= getc(fp
)) != EOF
) {
357 (void)ungetc(ch
, fp
);
363 /* XXX: does this work on Win/Win64? (see posix_fstat) */
365 if (fstat(fileno(fp
), &sb
) == 0 &&
366 S_ISDIR(sb
.st_mode
)) {
367 fprintf(stderr
, "%s: warning '%s' is a directory\n", argv
[0], filename
);
373 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
376 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
377 _setmode(fileno(stdin
), O_BINARY
);
378 _setmode(fileno(stdout
), O_BINARY
);
381 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
382 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
383 setvbuf(stderr
, (char *)NULL
, _IONBF
, BUFSIZ
);
384 #else /* !HAVE_SETVBUF */
385 setbuf(stdin
, (char *)NULL
);
386 setbuf(stdout
, (char *)NULL
);
387 setbuf(stderr
, (char *)NULL
);
388 #endif /* !HAVE_SETVBUF */
390 else if (Py_InteractiveFlag
) {
392 /* Doesn't have to have line-buffered -- use unbuffered */
393 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
394 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
395 #else /* !MS_WINDOWS */
397 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
398 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
399 #endif /* HAVE_SETVBUF */
400 #endif /* !MS_WINDOWS */
401 /* Leave stderr alone - it should be unbuffered anyway. */
405 setvbuf (stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
410 /* On MacOS X, when the Python interpreter is embedded in an
411 application bundle, it gets executed by a bootstrapping script
412 that does os.execve() with an argv[0] that's different from the
413 actual Python executable. This is needed to keep the Finder happy,
414 or rather, to work around Apple's overly strict requirements of
415 the process name. However, we still need a usable sys.executable,
416 so the actual executable path is passed in an environment variable.
417 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
419 if ((p
= Py_GETENV("PYTHONEXECUTABLE")) && *p
!= '\0')
420 Py_SetProgramName(p
);
422 Py_SetProgramName(argv
[0]);
424 Py_SetProgramName(argv
[0]);
428 if (Py_VerboseFlag
||
429 (command
== NULL
&& filename
== NULL
&& module
== NULL
&& stdin_is_interactive
)) {
430 fprintf(stderr
, "Python %s on %s\n",
431 Py_GetVersion(), Py_GetPlatform());
433 fprintf(stderr
, "%s\n", COPYRIGHT
);
436 if (command
!= NULL
) {
437 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
439 argv
[_PyOS_optind
] = "-c";
442 if (module
!= NULL
) {
443 /* Backup _PyOS_optind and find the real file */
444 struct filedescr
*fdescr
= NULL
;
446 if ((fdescr
= FindModule(module
, &fp
, &filename
))) {
447 argv
[_PyOS_optind
] = filename
;
449 fprintf(stderr
, "%s: module %s not found\n",
455 "%s: module %s has no associated file\n",
459 if (!_PyImport_IsScript(fdescr
)) {
461 "%s: module %s not usable as script\n (%s)\n",
462 argv
[0], module
, filename
);
467 PySys_SetArgv(argc
-_PyOS_optind
, argv
+_PyOS_optind
);
469 if ((inspect
|| (command
== NULL
&& filename
== NULL
&& module
== NULL
)) &&
470 isatty(fileno(stdin
))) {
472 v
= PyImport_ImportModule("readline");
480 sts
= PyRun_SimpleStringFlags(command
, &cf
) != 0;
483 sts
= PyRun_AnyFileExFlags(fp
, filename
, 1, &cf
) != 0;
488 if (filename
== NULL
&& stdin_is_interactive
) {
492 sts
= PyRun_AnyFileExFlags(
494 filename
== NULL
? "<stdin>" : filename
,
495 filename
!= NULL
, &cf
) != 0;
498 /* Check this environment variable at the end, to give programs the
499 * opportunity to set it from Python.
501 if (!saw_inspect_flag
&&
502 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
507 if (inspect
&& stdin_is_interactive
&&
508 (filename
!= NULL
|| command
!= NULL
|| module
!= NULL
))
510 sts
= PyRun_AnyFileFlags(stdin
, "<stdin>", &cf
) != 0;
514 if (Py_RISCOSWimpFlag
)
515 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
519 /* Insure++ is a memory analysis tool that aids in discovering
520 * memory leaks and other memory problems. On Python exit, the
521 * interned string dictionary is flagged as being in use at exit
522 * (which it is). Under normal circumstances, this is fine because
523 * the memory will be automatically reclaimed by the system. Under
524 * memory debugging, it's a huge source of useless noise, so we
525 * trade off slower shutdown for less distraction in the memory
528 _Py_ReleaseInternedStrings();
529 #endif /* __INSURE__ */
534 /* this is gonna seem *real weird*, but if you put some other code between
535 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
536 while statement in Misc/gdbinit:ppystack */
538 /* Make the *original* argc/argv available to other modules.
539 This is rare, but it is needed by the secureware extension. */
542 Py_GetArgcArgv(int *argc
, char ***argv
)