Issue #7051: Clarify behaviour of 'g' and 'G'-style formatting.
[python.git] / Modules / main.c
blob014ea5f9a0b1b5f4f22858c60ab1dbec84e2d45c
1 /* Python interpreter main program */
3 #include "Python.h"
4 #include "osdefs.h"
5 #include "code.h" /* For CO_FUTURE_DIVISION */
6 #include "import.h"
8 #ifdef __VMS
9 #include <unixlib.h>
10 #endif
12 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
13 #ifdef HAVE_FCNTL_H
14 #include <fcntl.h>
15 #endif
16 #endif
18 #if (defined(PYOS_OS2) && !defined(PYCC_GCC)) || defined(MS_WINDOWS)
19 #define PYTHONHOMEHELP "<prefix>\\lib"
20 #else
21 #if defined(PYOS_OS2) && defined(PYCC_GCC)
22 #define PYTHONHOMEHELP "<prefix>/Lib"
23 #else
24 #define PYTHONHOMEHELP "<prefix>/pythonX.X"
25 #endif
26 #endif
28 #include "pygetopt.h"
30 #define COPYRIGHT \
31 "Type \"help\", \"copyright\", \"credits\" or \"license\" " \
32 "for more information."
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 /* For Py_GetArgcArgv(); set by main() */
39 static char **orig_argv;
40 static int orig_argc;
42 /* command line options */
43 #define BASE_OPTS "3bBc:dEhiJm:OQ:sStuUvVW:xX?"
45 #ifndef RISCOS
46 #define PROGRAM_OPTS BASE_OPTS
47 #else /*RISCOS*/
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;
53 #endif /*RISCOS*/
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 -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x\n\
63 -c cmd : program passed in as string (terminates option list)\n\
64 -d : debug output from parser; also PYTHONDEBUG=x\n\
65 -E : ignore PYTHON* environment variables (such as PYTHONPATH)\n\
66 -h : print this help message and exit (also --help)\n\
67 -i : inspect interactively after running script; forces a prompt even\n\
69 static char *usage_2 = "\
70 if stdin does not appear to be a terminal; also PYTHONINSPECT=x\n\
71 -m mod : run library module as a script (terminates option list)\n\
72 -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x\n\
73 -OO : remove doc-strings in addition to the -O optimizations\n\
74 -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew\n\
75 -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE\n\
76 -S : don't imply 'import site' on initialization\n\
77 -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
79 static char *usage_3 = "\
80 -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n\
81 see man page for details on internal buffering relating to '-u'\n\
82 -v : verbose (trace import statements); also PYTHONVERBOSE=x\n\
83 can be supplied multiple times to increase verbosity\n\
84 -V : print the Python version number and exit (also --version)\n\
85 -W arg : warning control; arg is action:message:category:module:lineno\n\
86 -x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
88 static char *usage_4 = "\
89 -3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix\n\
90 file : program read from script file\n\
91 - : program read from stdin (default; interactive mode if a tty)\n\
92 arg ...: arguments passed to program in sys.argv[1:]\n\n\
93 Other environment variables:\n\
94 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
95 PYTHONPATH : '%c'-separated list of directories prefixed to the\n\
96 default module search path. The result is sys.path.\n\
98 static char *usage_5 = "\
99 PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
100 The default module search path uses %s.\n\
101 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
102 PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
106 static int
107 usage(int exitcode, char* program)
109 FILE *f = exitcode ? stderr : stdout;
111 fprintf(f, usage_line, program);
112 if (exitcode)
113 fprintf(f, "Try `python -h' for more information.\n");
114 else {
115 fputs(usage_1, f);
116 fputs(usage_2, f);
117 fputs(usage_3, f);
118 fprintf(f, usage_4, DELIM);
119 fprintf(f, usage_5, DELIM, PYTHONHOMEHELP);
121 #if defined(__VMS)
122 if (exitcode == 0) {
123 /* suppress 'error' message */
124 return 1;
126 else {
127 /* STS$M_INHIB_MSG + SS$_ABORT */
128 return 0x1000002c;
130 #else
131 return exitcode;
132 #endif
133 /*NOTREACHED*/
136 static void RunStartupFile(PyCompilerFlags *cf)
138 char *startup = Py_GETENV("PYTHONSTARTUP");
139 if (startup != NULL && startup[0] != '\0') {
140 FILE *fp = fopen(startup, "r");
141 if (fp != NULL) {
142 (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
143 PyErr_Clear();
144 fclose(fp);
145 } else {
146 int save_errno;
147 save_errno = errno;
148 PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
149 errno = save_errno;
150 PyErr_SetFromErrnoWithFilename(PyExc_IOError,
151 startup);
152 PyErr_Print();
153 PyErr_Clear();
159 static int RunModule(char *module, int set_argv0)
161 PyObject *runpy, *runmodule, *runargs, *result;
162 runpy = PyImport_ImportModule("runpy");
163 if (runpy == NULL) {
164 fprintf(stderr, "Could not import runpy module\n");
165 return -1;
167 runmodule = PyObject_GetAttrString(runpy, "_run_module_as_main");
168 if (runmodule == NULL) {
169 fprintf(stderr, "Could not access runpy._run_module_as_main\n");
170 Py_DECREF(runpy);
171 return -1;
173 runargs = Py_BuildValue("(si)", module, set_argv0);
174 if (runargs == NULL) {
175 fprintf(stderr,
176 "Could not create arguments for runpy._run_module_as_main\n");
177 Py_DECREF(runpy);
178 Py_DECREF(runmodule);
179 return -1;
181 result = PyObject_Call(runmodule, runargs, NULL);
182 if (result == NULL) {
183 PyErr_Print();
185 Py_DECREF(runpy);
186 Py_DECREF(runmodule);
187 Py_DECREF(runargs);
188 if (result == NULL) {
189 return -1;
191 Py_DECREF(result);
192 return 0;
195 static int RunMainFromImporter(char *filename)
197 PyObject *argv0 = NULL, *importer = NULL;
199 if ((argv0 = PyString_FromString(filename)) &&
200 (importer = PyImport_GetImporter(argv0)) &&
201 (importer->ob_type != &PyNullImporter_Type))
203 /* argv0 is usable as an import source, so
204 put it in sys.path[0] and import __main__ */
205 PyObject *sys_path = NULL;
206 if ((sys_path = PySys_GetObject("path")) &&
207 !PyList_SetItem(sys_path, 0, argv0))
209 Py_INCREF(argv0);
210 Py_DECREF(importer);
211 sys_path = NULL;
212 return RunModule("__main__", 0) != 0;
215 Py_XDECREF(argv0);
216 Py_XDECREF(importer);
217 if (PyErr_Occurred()) {
218 PyErr_Print();
219 return 1;
221 return -1;
225 /* Wait until threading._shutdown completes, provided
226 the threading module was imported in the first place.
227 The shutdown routine will wait until all non-daemon
228 "threading" threads have completed. */
229 #include "abstract.h"
230 static void
231 WaitForThreadShutdown(void)
233 #ifdef WITH_THREAD
234 PyObject *result;
235 PyThreadState *tstate = PyThreadState_GET();
236 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
237 "threading");
238 if (threading == NULL) {
239 /* threading not imported */
240 PyErr_Clear();
241 return;
243 result = PyObject_CallMethod(threading, "_shutdown", "");
244 if (result == NULL)
245 PyErr_WriteUnraisable(threading);
246 else
247 Py_DECREF(result);
248 Py_DECREF(threading);
249 #endif
252 /* Main program */
255 Py_Main(int argc, char **argv)
257 int c;
258 int sts;
259 char *command = NULL;
260 char *filename = NULL;
261 char *module = NULL;
262 FILE *fp = stdin;
263 char *p;
264 int unbuffered = 0;
265 int skipfirstline = 0;
266 int stdin_is_interactive = 0;
267 int help = 0;
268 int version = 0;
269 int saw_unbuffered_flag = 0;
270 PyCompilerFlags cf;
272 cf.cf_flags = 0;
274 orig_argc = argc; /* For Py_GetArgcArgv() */
275 orig_argv = argv;
277 #ifdef RISCOS
278 Py_RISCOSWimpFlag = 0;
279 #endif
281 PySys_ResetWarnOptions();
283 while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
284 if (c == 'c') {
285 /* -c is the last option; following arguments
286 that look like options are left for the
287 command to interpret. */
288 command = (char *)malloc(strlen(_PyOS_optarg) + 2);
289 if (command == NULL)
290 Py_FatalError(
291 "not enough memory to copy -c argument");
292 strcpy(command, _PyOS_optarg);
293 strcat(command, "\n");
294 break;
297 if (c == 'm') {
298 /* -m is the last option; following arguments
299 that look like options are left for the
300 module to interpret. */
301 module = (char *)malloc(strlen(_PyOS_optarg) + 2);
302 if (module == NULL)
303 Py_FatalError(
304 "not enough memory to copy -m argument");
305 strcpy(module, _PyOS_optarg);
306 break;
309 switch (c) {
310 case 'b':
311 Py_BytesWarningFlag++;
312 break;
314 case 'd':
315 Py_DebugFlag++;
316 break;
318 case '3':
319 Py_Py3kWarningFlag++;
320 if (!Py_DivisionWarningFlag)
321 Py_DivisionWarningFlag = 1;
322 break;
324 case 'Q':
325 if (strcmp(_PyOS_optarg, "old") == 0) {
326 Py_DivisionWarningFlag = 0;
327 break;
329 if (strcmp(_PyOS_optarg, "warn") == 0) {
330 Py_DivisionWarningFlag = 1;
331 break;
333 if (strcmp(_PyOS_optarg, "warnall") == 0) {
334 Py_DivisionWarningFlag = 2;
335 break;
337 if (strcmp(_PyOS_optarg, "new") == 0) {
338 /* This only affects __main__ */
339 cf.cf_flags |= CO_FUTURE_DIVISION;
340 /* And this tells the eval loop to treat
341 BINARY_DIVIDE as BINARY_TRUE_DIVIDE */
342 _Py_QnewFlag = 1;
343 break;
345 fprintf(stderr,
346 "-Q option should be `-Qold', "
347 "`-Qwarn', `-Qwarnall', or `-Qnew' only\n");
348 return usage(2, argv[0]);
349 /* NOTREACHED */
351 case 'i':
352 Py_InspectFlag++;
353 Py_InteractiveFlag++;
354 break;
356 /* case 'J': reserved for Jython */
358 case 'O':
359 Py_OptimizeFlag++;
360 break;
362 case 'B':
363 Py_DontWriteBytecodeFlag++;
364 break;
366 case 's':
367 Py_NoUserSiteDirectory++;
368 break;
370 case 'S':
371 Py_NoSiteFlag++;
372 break;
374 case 'E':
375 Py_IgnoreEnvironmentFlag++;
376 break;
378 case 't':
379 Py_TabcheckFlag++;
380 break;
382 case 'u':
383 unbuffered++;
384 saw_unbuffered_flag = 1;
385 break;
387 case 'v':
388 Py_VerboseFlag++;
389 break;
391 #ifdef RISCOS
392 case 'w':
393 Py_RISCOSWimpFlag = 1;
394 break;
395 #endif
397 case 'x':
398 skipfirstline = 1;
399 break;
401 /* case 'X': reserved for implementation-specific arguments */
403 case 'U':
404 Py_UnicodeFlag++;
405 break;
406 case 'h':
407 case '?':
408 help++;
409 break;
410 case 'V':
411 version++;
412 break;
414 case 'W':
415 PySys_AddWarnOption(_PyOS_optarg);
416 break;
418 /* This space reserved for other options */
420 default:
421 return usage(2, argv[0]);
422 /*NOTREACHED*/
427 if (help)
428 return usage(0, argv[0]);
430 if (version) {
431 fprintf(stderr, "Python %s\n", PY_VERSION);
432 return 0;
435 if (Py_Py3kWarningFlag && !Py_TabcheckFlag)
436 /* -3 implies -t (but not -tt) */
437 Py_TabcheckFlag = 1;
439 if (!Py_InspectFlag &&
440 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
441 Py_InspectFlag = 1;
442 if (!saw_unbuffered_flag &&
443 (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
444 unbuffered = 1;
446 if (!Py_NoUserSiteDirectory &&
447 (p = Py_GETENV("PYTHONNOUSERSITE")) && *p != '\0')
448 Py_NoUserSiteDirectory = 1;
450 if (command == NULL && module == NULL && _PyOS_optind < argc &&
451 strcmp(argv[_PyOS_optind], "-") != 0)
453 #ifdef __VMS
454 filename = decc$translate_vms(argv[_PyOS_optind]);
455 if (filename == (char *)0 || filename == (char *)-1)
456 filename = argv[_PyOS_optind];
458 #else
459 filename = argv[_PyOS_optind];
460 #endif
463 stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
465 if (unbuffered) {
466 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
467 _setmode(fileno(stdin), O_BINARY);
468 _setmode(fileno(stdout), O_BINARY);
469 #endif
470 #ifdef HAVE_SETVBUF
471 setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
472 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
473 setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
474 #else /* !HAVE_SETVBUF */
475 setbuf(stdin, (char *)NULL);
476 setbuf(stdout, (char *)NULL);
477 setbuf(stderr, (char *)NULL);
478 #endif /* !HAVE_SETVBUF */
480 else if (Py_InteractiveFlag) {
481 #ifdef MS_WINDOWS
482 /* Doesn't have to have line-buffered -- use unbuffered */
483 /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
484 setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
485 #else /* !MS_WINDOWS */
486 #ifdef HAVE_SETVBUF
487 setvbuf(stdin, (char *)NULL, _IOLBF, BUFSIZ);
488 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
489 #endif /* HAVE_SETVBUF */
490 #endif /* !MS_WINDOWS */
491 /* Leave stderr alone - it should be unbuffered anyway. */
493 #ifdef __VMS
494 else {
495 setvbuf (stdout, (char *)NULL, _IOLBF, BUFSIZ);
497 #endif /* __VMS */
499 #ifdef __APPLE__
500 /* On MacOS X, when the Python interpreter is embedded in an
501 application bundle, it gets executed by a bootstrapping script
502 that does os.execve() with an argv[0] that's different from the
503 actual Python executable. This is needed to keep the Finder happy,
504 or rather, to work around Apple's overly strict requirements of
505 the process name. However, we still need a usable sys.executable,
506 so the actual executable path is passed in an environment variable.
507 See Lib/plat-mac/bundlebuiler.py for details about the bootstrap
508 script. */
509 if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0')
510 Py_SetProgramName(p);
511 else
512 Py_SetProgramName(argv[0]);
513 #else
514 Py_SetProgramName(argv[0]);
515 #endif
516 Py_Initialize();
518 if (Py_VerboseFlag ||
519 (command == NULL && filename == NULL && module == NULL && stdin_is_interactive)) {
520 fprintf(stderr, "Python %s on %s\n",
521 Py_GetVersion(), Py_GetPlatform());
522 if (!Py_NoSiteFlag)
523 fprintf(stderr, "%s\n", COPYRIGHT);
526 if (command != NULL) {
527 /* Backup _PyOS_optind and force sys.argv[0] = '-c' */
528 _PyOS_optind--;
529 argv[_PyOS_optind] = "-c";
532 if (module != NULL) {
533 /* Backup _PyOS_optind and force sys.argv[0] = '-c'
534 so that PySys_SetArgv correctly sets sys.path[0] to ''*/
535 _PyOS_optind--;
536 argv[_PyOS_optind] = "-c";
539 PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
541 if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
542 isatty(fileno(stdin))) {
543 PyObject *v;
544 v = PyImport_ImportModule("readline");
545 if (v == NULL)
546 PyErr_Clear();
547 else
548 Py_DECREF(v);
551 if (command) {
552 sts = PyRun_SimpleStringFlags(command, &cf) != 0;
553 free(command);
554 } else if (module) {
555 sts = RunModule(module, 1);
556 free(module);
558 else {
560 if (filename == NULL && stdin_is_interactive) {
561 Py_InspectFlag = 0; /* do exit on SystemExit */
562 RunStartupFile(&cf);
564 /* XXX */
566 sts = -1; /* keep track of whether we've already run __main__ */
568 if (filename != NULL) {
569 sts = RunMainFromImporter(filename);
572 if (sts==-1 && filename!=NULL) {
573 if ((fp = fopen(filename, "r")) == NULL) {
574 fprintf(stderr, "%s: can't open file '%s': [Errno %d] %s\n",
575 argv[0], filename, errno, strerror(errno));
577 return 2;
579 else if (skipfirstline) {
580 int ch;
581 /* Push back first newline so line numbers
582 remain the same */
583 while ((ch = getc(fp)) != EOF) {
584 if (ch == '\n') {
585 (void)ungetc(ch, fp);
586 break;
591 /* XXX: does this work on Win/Win64? (see posix_fstat) */
592 struct stat sb;
593 if (fstat(fileno(fp), &sb) == 0 &&
594 S_ISDIR(sb.st_mode)) {
595 fprintf(stderr, "%s: '%s' is a directory, cannot continue\n", argv[0], filename);
596 fclose(fp);
597 return 1;
602 if (sts==-1) {
603 sts = PyRun_AnyFileExFlags(
605 filename == NULL ? "<stdin>" : filename,
606 filename != NULL, &cf) != 0;
611 /* Check this environment variable at the end, to give programs the
612 * opportunity to set it from Python.
614 if (!Py_InspectFlag &&
615 (p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
617 Py_InspectFlag = 1;
620 if (Py_InspectFlag && stdin_is_interactive &&
621 (filename != NULL || command != NULL || module != NULL)) {
622 Py_InspectFlag = 0;
623 /* XXX */
624 sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
627 WaitForThreadShutdown();
629 Py_Finalize();
630 #ifdef RISCOS
631 if (Py_RISCOSWimpFlag)
632 fprintf(stderr, "\x0cq\x0c"); /* make frontend quit */
633 #endif
635 #ifdef __INSURE__
636 /* Insure++ is a memory analysis tool that aids in discovering
637 * memory leaks and other memory problems. On Python exit, the
638 * interned string dictionary is flagged as being in use at exit
639 * (which it is). Under normal circumstances, this is fine because
640 * the memory will be automatically reclaimed by the system. Under
641 * memory debugging, it's a huge source of useless noise, so we
642 * trade off slower shutdown for less distraction in the memory
643 * reports. -baw
645 _Py_ReleaseInternedStrings();
646 #endif /* __INSURE__ */
648 return sts;
651 /* this is gonna seem *real weird*, but if you put some other code between
652 Py_Main() and Py_GetArgcArgv() you will need to adjust the test in the
653 while statement in Misc/gdbinit:ppystack */
655 /* Make the *original* argc/argv available to other modules.
656 This is rare, but it is needed by the secureware extension. */
658 void
659 Py_GetArgcArgv(int *argc, char ***argv)
661 *argc = orig_argc;
662 *argv = orig_argv;
665 #ifdef __cplusplus
667 #endif