[Patches] make 'b' formatter an *unsigned* char

Trent Mick trentm@activestate.com
Tue, 9 May 2000 14:22:19 -0700


Discussion:

Limit the 'b' formatter of PyArg_ParseTuple to valid values of an unsigned
char, i.e. [0,UCHAR_MAX]. It is expected that this is the common usage of 'b'.
An OverflowError is raised if the parsed value is outside this range.


Legal:

I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.

Patch:


diff -c3  /home/trentm/main/contrib/python/dist/src/Python/getargs.c ./Python/getargs.c
*** /home/trentm/main/contrib/python/dist/src/Python/getargs.c	Mon May  8 11:49:05 2000
--- ./Python/getargs.c	Tue May  9 14:15:53 2000
***************
*** 465,492 ****
  	
  	switch (c) {
  	
! 	case 'b': /* byte -- very short int */
  		{
  			char *p = va_arg(*p_va, char *);
  			long ival = PyInt_AsLong(arg);
  			if (ival == -1 && PyErr_Occurred())
  				return "integer<b>";
! 			else if (ival < CHAR_MIN) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"byte integer is less than minimum");
  				return "integer<b>";
  			}
! 			else if (ival > CHAR_MAX && ival >= 256) {
  				PyErr_SetString(PyExc_OverflowError,
! 				    "byte integer is greater than maximum");
  				return "integer<b>";
  			}
  			else
! 				*p = (char) ival;
  			break;
  		}
  	
! 	case 'h': /* short int */
  		{
  			short *p = va_arg(*p_va, short *);
  			long ival = PyInt_AsLong(arg);
--- 465,492 ----
  	
  	switch (c) {
  	
! 	case 'b': /* unsigned byte -- very short int */
  		{
  			char *p = va_arg(*p_va, char *);
  			long ival = PyInt_AsLong(arg);
  			if (ival == -1 && PyErr_Occurred())
  				return "integer<b>";
! 			else if (ival < 0) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"unsigned byte integer is less than minimum");
  				return "integer<b>";
  			}
! 			else if (ival > UCHAR_MAX) {
  				PyErr_SetString(PyExc_OverflowError,
! 				    "unsigned byte integer is greater than maximum");
  				return "integer<b>";
  			}
  			else
! 				*p = (unsigned char) ival;
  			break;
  		}
  	
! 	case 'h': /* signed short int */
  		{
  			short *p = va_arg(*p_va, short *);
  			long ival = PyInt_AsLong(arg);
***************
*** 494,505 ****
  				return "integer<h>";
  			else if (ival < SHRT_MIN) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"short integer is less than minimum");
  				return "integer<h>";
  			}
  			else if (ival > SHRT_MAX) {
  				PyErr_SetString(PyExc_OverflowError,
! 				  "short integer is greater than maximum");
  				return "integer<h>";
  			}
  			else
--- 494,505 ----
  				return "integer<h>";
  			else if (ival < SHRT_MIN) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"signed short integer is less than minimum");
  				return "integer<h>";
  			}
  			else if (ival > SHRT_MAX) {
  				PyErr_SetString(PyExc_OverflowError,
! 				  "signed short integer is greater than maximum");
  				return "integer<h>";
  			}
  			else
***************
*** 507,513 ****
  			break;
  		}
  	
! 	case 'i': /* int */
  		{
  			int *p = va_arg(*p_va, int *);
  			long ival = PyInt_AsLong(arg);
--- 507,513 ----
  			break;
  		}
  	
! 	case 'i': /* signed int */
  		{
  			int *p = va_arg(*p_va, int *);
  			long ival = PyInt_AsLong(arg);
***************
*** 515,526 ****
  				return "integer<i>";
  			else if (ival < INT_MIN) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"integer is less than minimum");
  				return "integer<i>";
  			}
  			else if (ival > INT_MAX) {
  				PyErr_SetString(PyExc_OverflowError,
! 				  "integer is greater than maximum");
  				return "integer<i>";
  			}
  			else
--- 515,526 ----
  				return "integer<i>";
  			else if (ival < INT_MIN) {
  				PyErr_SetString(PyExc_OverflowError,
! 					"signed integer is less than minimum");
  				return "integer<i>";
  			}
  			else if (ival > INT_MAX) {
  				PyErr_SetString(PyExc_OverflowError,
! 				  "signed integer is greater than maximum");
  				return "integer<i>";
  			}
  			else