diff options
Diffstat (limited to 'Modules/main.c')
-rw-r--r-- | Modules/main.c | 68 |
1 files changed, 49 insertions, 19 deletions
diff --git a/Modules/main.c b/Modules/main.c index ad2616d..7c368c7 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -2,6 +2,7 @@ #include "Python.h" #include "osdefs.h" +#include "compile.h" /* For CO_FUTURE_DIVISION */ #ifdef HAVE_UNISTD_H #include <unistd.h> @@ -28,7 +29,7 @@ static char **orig_argv; static int orig_argc; /* command line options */ -#define BASE_OPTS "c:diOSEtuUvxXhVW:" +#define BASE_OPTS "c:dD:EhiOStuUvVWxX:" #ifndef RISCOS #define PROGRAM_OPTS BASE_OPTS @@ -45,30 +46,33 @@ static char *usage_line = "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n"; /* Long usage message, split into parts < 512 bytes */ -static char *usage_top = "\ +static char *usage_1 = "\ Options and arguments (and corresponding environment variables):\n\ +-c cmd : program passed in as string (terminates option list)\n\ -d : debug output from parser (also PYTHONDEBUG=x)\n\ +-D arg : division options: -Dold (default), -Dwarn, -Dnew\n\ +-E : ignore environment variables (such as PYTHONPATH)\n\ +-h : print this help message and exit\n\ +"; +static char *usage_2 = "\ -i : inspect interactively after running script, (also PYTHONINSPECT=x)\n\ and force prompts, even if stdin does not appear to be a terminal\n\ -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\ -OO : remove doc-strings in addition to the -O optimizations\n\ -S : don't imply 'import site' on initialization\n\ --E : ignore environment variables (such as PYTHONPATH)\n\ -t : issue warnings about inconsistent tab usage (-tt: issue errors)\n\ -"; -static char *usage_mid = "\ -u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\ +"; +static char *usage_3 = "\ -U : Unicode literals: treats '...' literals like u'...'\n\ -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ --x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ --h : print this help message and exit\n\ -V : print the Python version number and exit\n\ -W arg : warning control (arg is action:message:category:module:lineno)\n\ --c cmd : program passed in as string (terminates option list)\n\ +-x : skip first line of source, allowing use of non-Unix forms of #!cmd\n\ file : program read from script file\n\ - : program read from stdin (default; interactive mode if a tty)\n\ "; -static char *usage_bot = "\ +static char *usage_4 = "\ arg ...: arguments passed to program in sys.argv[1:]\n\ Other environment variables:\n\ PYTHONSTARTUP: file executed on interactive startup (no default)\n\ @@ -83,10 +87,17 @@ PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ static void usage(int exitcode, char* program) { - fprintf(stderr, usage_line, program); - fprintf(stderr, usage_top); - fprintf(stderr, usage_mid); - fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP); + FILE *f = exitcode ? stderr : stdout; + + fprintf(f, usage_line, program); + if (exitcode) + fprintf(f, "Try `python -h' for more information.\n"); + else { + fprintf(f, usage_1); + fprintf(f, usage_2); + fprintf(f, usage_3); + fprintf(f, usage_4, DELIM, DELIM, PYTHONHOMEHELP); + } exit(exitcode); /*NOTREACHED*/ } @@ -113,6 +124,8 @@ Py_Main(int argc, char **argv) int saw_unbuffered_flag = 0; PyCompilerFlags cf; + cf.cf_flags = 0; + orig_argc = argc; /* For Py_GetArgcArgv() */ orig_argv = argv; @@ -135,13 +148,33 @@ Py_Main(int argc, char **argv) strcat(command, "\n"); break; } - + switch (c) { case 'd': Py_DebugFlag++; break; + case 'D': + if (strcmp(_PyOS_optarg, "old") == 0) { + Py_DivisionWarningFlag = 0; + break; + } + if (strcmp(_PyOS_optarg, "warn") == 0) { + Py_DivisionWarningFlag++; + break; + } + if (strcmp(_PyOS_optarg, "new") == 0) { + /* XXX This only affects __main__ */ + cf.cf_flags |= CO_FUTURE_DIVISION; + break; + } + fprintf(stderr, + "-D option should be " + "`-Dold', `-Dwarn' or `-Dnew' only\n"); + usage(2, argv[0]); + /* NOTREACHED */ + case 'i': inspect++; saw_inspect_flag = 1; @@ -290,8 +323,7 @@ Py_Main(int argc, char **argv) (command == NULL && filename == NULL && stdin_is_interactive)) fprintf(stderr, "Python %s on %s\n%s\n", Py_GetVersion(), Py_GetPlatform(), COPYRIGHT); - - + if (command != NULL) { /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ _PyOS_optind--; @@ -310,10 +342,8 @@ Py_Main(int argc, char **argv) Py_DECREF(v); } - cf.cf_flags = 0; - if (command) { - sts = PyRun_SimpleString(command) != 0; + sts = PyRun_SimpleStringFlags(command, &cf) != 0; free(command); } else { |