1 /* Python interpreter main program */
5 #include "compile.h" /* For CO_FUTURE_DIVISION */
11 #if defined(PYOS_OS2) || defined(MS_WINDOWS)
12 #define PYTHONHOMEHELP "<prefix>\\lib"
14 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
20 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
21 "for more information."
23 /* For Py_GetArgcArgv(); set by main() */
24 static char **orig_argv
;
27 /* command line options */
28 #define BASE_OPTS "c:dEhiOQ:StuUvVW:xX"
31 #define PROGRAM_OPTS BASE_OPTS
33 /* extra option saying that we are running under a special task window
34 frontend; especially my_readline will behave different */
35 #define PROGRAM_OPTS BASE_OPTS "w"
36 /* corresponding flag */
37 extern int Py_RISCOSWimpFlag
;
40 /* Short usage message (with %s for argv0) */
41 static char *usage_line
=
42 "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
44 /* Long usage message, split into parts < 512 bytes */
45 static char *usage_1
= "\
46 Options and arguments (and corresponding environment variables):\n\
47 -c cmd : program passed in as string (terminates option list)\n\
48 -d : debug output from parser (also PYTHONDEBUG=x)\n\
49 -E : ignore environment variables (such as PYTHONPATH)\n\
50 -h : print this help message and exit\n\
51 -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
52 and force prompts, even if stdin does not appear to be a terminal\n\
54 static char *usage_2
= "\
55 -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
56 -OO : remove doc-strings in addition to the -O optimizations\n\
57 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
58 -S : don't imply 'import site' on initialization\n\
59 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
60 -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
62 static char *usage_3
= "\
63 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
64 -V : print the Python version number and exit\n\
65 -W arg : warning control (arg is action:message:category:module:lineno)\n\
66 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
67 file : program read from script file\n\
68 - : program read from stdin (default; interactive mode if a tty)\n\
70 static char *usage_4
= "\
71 arg ...: arguments passed to program in sys.argv[1:]\n\
72 Other environment variables:\n\
73 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
74 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
75 default module search path. The result is sys.path.\n\
76 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
77 The default module search path uses %s.\n\
78 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
83 usage(int exitcode
, char* program
)
85 FILE *f
= exitcode
? stderr
: stdout
;
87 fprintf(f
, usage_line
, program
);
89 fprintf(f
, "Try `python -h' for more information.\n");
94 fprintf(f
, usage_4
, DELIM
, DELIM
, PYTHONHOMEHELP
);
104 Py_Main(int argc
, char **argv
)
108 char *command
= NULL
;
109 char *filename
= NULL
;
114 int skipfirstline
= 0;
115 int stdin_is_interactive
= 0;
118 int saw_inspect_flag
= 0;
119 int saw_unbuffered_flag
= 0;
124 orig_argc
= argc
; /* For Py_GetArgcArgv() */
128 Py_RISCOSWimpFlag
= 0;
131 PySys_ResetWarnOptions();
133 while ((c
= _PyOS_GetOpt(argc
, argv
, PROGRAM_OPTS
)) != EOF
) {
135 /* -c is the last option; following arguments
136 that look like options are left for the
137 the command to interpret. */
138 command
= malloc(strlen(_PyOS_optarg
) + 2);
141 "not enough memory to copy -c argument");
142 strcpy(command
, _PyOS_optarg
);
143 strcat(command
, "\n");
154 if (strcmp(_PyOS_optarg
, "old") == 0) {
155 Py_DivisionWarningFlag
= 0;
158 if (strcmp(_PyOS_optarg
, "warn") == 0) {
159 Py_DivisionWarningFlag
= 1;
162 if (strcmp(_PyOS_optarg
, "warnall") == 0) {
163 Py_DivisionWarningFlag
= 2;
166 if (strcmp(_PyOS_optarg
, "new") == 0) {
167 /* This only affects __main__ */
168 cf
.cf_flags
|= CO_FUTURE_DIVISION
;
169 /* And this tells the eval loop to treat
170 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
175 "-Q option should be `-Qold', "
176 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
182 saw_inspect_flag
= 1;
183 Py_InteractiveFlag
++;
195 Py_IgnoreEnvironmentFlag
++;
204 saw_unbuffered_flag
= 1;
213 Py_RISCOSWimpFlag
= 1;
232 PySys_AddWarnOption(_PyOS_optarg
);
235 /* This space reserved for other options */
248 fprintf(stderr
, "Python %s\n", PY_VERSION
);
252 if (!saw_inspect_flag
&&
253 (p
= Py_GETENV("PYTHONINSPECT")) && *p
!= '\0')
255 if (!saw_unbuffered_flag
&&
256 (p
= Py_GETENV("PYTHONUNBUFFERED")) && *p
!= '\0')
259 if (command
== NULL
&& _PyOS_optind
< argc
&&
260 strcmp(argv
[_PyOS_optind
], "-") != 0)
262 filename
= argv
[_PyOS_optind
];
263 if (filename
!= NULL
) {
264 if ((fp
= fopen(filename
, "r")) == NULL
) {
265 fprintf(stderr
, "%s: can't open file '%s'\n",
269 else if (skipfirstline
) {
271 /* Push back first newline so line numbers
273 while ((ch
= getc(fp
)) != EOF
) {
275 (void)ungetc(ch
, fp
);
283 stdin_is_interactive
= Py_FdIsInteractive(stdin
, (char *)0);
287 _setmode(fileno(stdin
), O_BINARY
);
288 _setmode(fileno(stdout
), O_BINARY
);
292 setvbuf(stdin
, (char *)NULL
, _IONBF
, BUFSIZ
);
293 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
294 setvbuf(stderr
, (char *)NULL
, _IONBF
, BUFSIZ
);
295 #else /* !HAVE_SETVBUF */
296 setbuf(stdin
, (char *)NULL
);
297 setbuf(stdout
, (char *)NULL
);
298 setbuf(stderr
, (char *)NULL
);
299 #endif /* !HAVE_SETVBUF */
301 /* On MPW (3.2) unbuffered seems to hang */
302 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
303 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
304 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
307 else if (Py_InteractiveFlag
) {
309 /* Doesn't have to have line-buffered -- use unbuffered */
310 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
311 setvbuf(stdout
, (char *)NULL
, _IONBF
, BUFSIZ
);
312 #else /* !MS_WINDOWS */
314 setvbuf(stdin
, (char *)NULL
, _IOLBF
, BUFSIZ
);
315 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
316 #endif /* HAVE_SETVBUF */
317 #endif /* !MS_WINDOWS */
318 /* Leave stderr alone - it should be unbuffered anyway. */
321 Py_SetProgramName(argv
[0]);
324 if (Py_VerboseFlag
||
325 (command
== NULL
&& filename
== NULL
&& stdin_is_interactive
))
326 fprintf(stderr
, "Python %s on %s\n%s\n",
327 Py_GetVersion(), Py_GetPlatform(), COPYRIGHT
);
329 if (command
!= NULL
) {
330 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
332 argv
[_PyOS_optind
] = "-c";
335 PySys_SetArgv(argc
-_PyOS_optind
, argv
+_PyOS_optind
);
337 if ((inspect
|| (command
== NULL
&& filename
== NULL
)) &&
338 isatty(fileno(stdin
))) {
340 v
= PyImport_ImportModule("readline");
348 sts
= PyRun_SimpleStringFlags(command
, &cf
) != 0;
352 if (filename
== NULL
&& stdin_is_interactive
) {
353 char *startup
= Py_GETENV("PYTHONSTARTUP");
354 if (startup
!= NULL
&& startup
[0] != '\0') {
355 FILE *fp
= fopen(startup
, "r");
357 (void) PyRun_SimpleFile(fp
, startup
);
364 sts
= PyRun_AnyFileExFlags(
366 filename
== NULL
? "<stdin>" : filename
,
367 filename
!= NULL
, &cf
) != 0;
370 if (inspect
&& stdin_is_interactive
&&
371 (filename
!= NULL
|| command
!= NULL
))
373 sts
= PyRun_AnyFileFlags(stdin
, "<stdin>", &cf
) != 0;
377 if(Py_RISCOSWimpFlag
)
378 fprintf(stderr
, "\x0cq\x0c"); /* make frontend quit */
382 /* Insure++ is a memory analysis tool that aids in discovering
383 * memory leaks and other memory problems. On Python exit, the
384 * interned string dictionary is flagged as being in use at exit
385 * (which it is). Under normal circumstances, this is fine because
386 * the memory will be automatically reclaimed by the system. Under
387 * memory debugging, it's a huge source of useless noise, so we
388 * trade off slower shutdown for less distraction in the memory
391 _Py_ReleaseInternedStrings();
392 #endif /* __INSURE__ */
398 /* Make the *original* argc/argv available to other modules.
399 This is rare, but it is needed by the secureware extension. */
402 Py_GetArgcArgv(int *argc
, char ***argv
)