[Python-checkins] CVS: python/dist/src/Modules main.c,1.56,1.57

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 31 Aug 2001 10:40:17 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv8024/Modules

Modified Files:
	main.c 
Log Message:
Add warning mode for classic division, almost exactly as specified in
PEP 238.  Changes:

- add a new flag variable Py_DivisionWarningFlag, declared in
  pydebug.h, defined in object.c, set in main.c, and used in
  {int,long,float,complex}object.c.  When this flag is set, the
  classic division operator issues a DeprecationWarning message.

- add a new API PyRun_SimpleStringFlags() to match
  PyRun_SimpleString().  The main() function calls this so that
  commands run with -c can also benefit from -Dnew.

- While I was at it, I changed the usage message in main() somewhat:
  alphabetized the options, split it in *four* parts to fit in under
  512 bytes (not that I still believe this is necessary -- doc strings
  elsewhere are much longer), and perhaps most visibly, don't display
  the full list of options on each command line error.  Instead, the
  full list is only displayed when -h is used, and otherwise a brief
  reminder of -h is displayed.  When -h is used, write to stdout so
  that you can do `python -h | more'.

Notes:

- I don't want to use the -W option to control whether the classic
  division warning is issued or not, because the machinery to decide
  whether to display the warning or not is very expensive (it involves
  calling into the warnings.py module).  You can use -Werror to turn
  the warnings into exceptions though.

- The -Dnew option doesn't select future division for all of the
  program -- only for the __main__ module.  I don't know if I'll ever
  change this -- it would require changes to the .pyc file magic
  number to do it right, and a more global notion of compiler flags.

- You can usefully combine -Dwarn and -Dnew: this gives the __main__
  module new division, and warns about classic division everywhere
  else.



Index: main.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/main.c,v
retrieving revision 1.56
retrieving revision 1.57
diff -C2 -d -r1.56 -r1.57
*** main.c	2001/07/23 16:30:27	1.56
--- main.c	2001/08/31 17:40:15	1.57
***************
*** 3,6 ****
--- 3,7 ----
  #include "Python.h"
  #include "osdefs.h"
+ #include "compile.h" /* For CO_FUTURE_DIVISION */
  
  #ifdef HAVE_UNISTD_H
***************
*** 29,33 ****
  
  /* command line options */
! #define BASE_OPTS "c:diOSEtuUvxXhVW:"
  
  #ifndef RISCOS
--- 30,34 ----
  
  /* command line options */
! #define BASE_OPTS "c:dD:EhiOStuUvVWxX:"
  
  #ifndef RISCOS
***************
*** 46,52 ****
  
  /* Long usage message, split into parts < 512 bytes */
! static char *usage_top = "\
  Options and arguments (and corresponding environment variables):\n\
  -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  -i     : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
           and force prompts, even if stdin does not appear to be a terminal\n\
--- 47,59 ----
  
  /* Long usage message, split into parts < 512 bytes */
! 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\
***************
*** 54,73 ****
  -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\
  -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\
  file   : program read from script file\n\
  -      : program read from stdin (default; interactive mode if a tty)\n\
  ";
! static char *usage_bot = "\
  arg ...: arguments passed to program in sys.argv[1:]\n\
  Other environment variables:\n\
--- 61,77 ----
  -OO    : remove doc-strings in addition to the -O optimizations\n\
  -S     : don't imply 'import site' on initialization\n\
  -t     : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
  -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\
  -V     : print the Python version number and exit\n\
  -W arg : warning control (arg is action:message:category:module:lineno)\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_4 = "\
  arg ...: arguments passed to program in sys.argv[1:]\n\
  Other environment variables:\n\
***************
*** 84,91 ****
  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);
  	exit(exitcode);
  	/*NOTREACHED*/
--- 88,102 ----
  usage(int exitcode, char* program)
  {
! 	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*/
***************
*** 114,117 ****
--- 125,130 ----
  	PyCompilerFlags cf;
  
+ 	cf.cf_flags = 0;
+ 
  	orig_argc = argc;	/* For Py_GetArgcArgv() */
  	orig_argv = argv;
***************
*** 136,140 ****
  			break;
  		}
! 		
  		switch (c) {
  
--- 149,153 ----
  			break;
  		}
! 
  		switch (c) {
  
***************
*** 143,146 ****
--- 156,179 ----
  			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++;
***************
*** 291,296 ****
  		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' */
--- 324,328 ----
  		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' */
***************
*** 311,318 ****
  	}
  
- 	cf.cf_flags = 0;
- 
  	if (command) {
! 		sts = PyRun_SimpleString(command) != 0;
  		free(command);
  	}
--- 343,348 ----
  	}
  
  	if (command) {
! 		sts = PyRun_SimpleStringFlags(command, &cf) != 0;
  		free(command);
  	}