[Python-checkins] CVS: python/dist/src/Objects stringobject.c,2.100,2.101 unicodeobject.c,2.80,2.81

Tim Peters tim_one@users.sourceforge.net
Wed, 11 Apr 2001 17:35:53 -0700


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

Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
Fix for SF bug #415514: "%#x" % 0 caused assertion failure/abort.
http://sourceforge.net/tracker/index.php?func=detail&aid=415514&group_id=5470&atid=105470
For short ints, Python defers to the platform C library to figure out what
%#x should do.  The code asserted that the platform C returned a string
beginning with "0x".  However, that's not true when-- and only when --the
*value* being formatted is 0.  Changed the code to live with C's inconsistency
here.  In the meantime, the problem does not arise if you format a long 0 (0L)
instead.  However, that's because the code *we* wrote to do %#x conversions on
longs produces a leading "0x" regardless of value.  That's probably wrong too:
we should drop leading "0x", for consistency with C, when (& only when) formatting
0L.  So I changed the long formatting code to do that too.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.100
retrieving revision 2.101
diff -C2 -r2.100 -r2.101
*** stringobject.c	2001/02/23 16:40:26	2.100
--- stringobject.c	2001/04/12 00:35:51	2.101
***************
*** 2576,2581 ****
  	assert(numdigits > 0);
  
! 	/* Get rid of base marker unless F_ALT */
! 	if ((flags & F_ALT) == 0) {
  		/* Need to skip 0x, 0X or 0. */
  		int skipped = 0;
--- 2576,2589 ----
  	assert(numdigits > 0);
  
! 	/* Get rid of base marker unless F_ALT.  Even if F_ALT, leading 0x
! 	 * must be stripped if the *value* is 0.
! 	 */
! 	if ((flags & F_ALT) == 0 ||
! 	    ((flags & F_ALT) &&
! 	     (type == 'x' || type == 'X') &&
! 	     numdigits == 1 &&
! 	     !sign &&
! 	     buf[2] == '0'
! 	    )) {
  		/* Need to skip 0x, 0X or 0. */
  		int skipped = 0;
***************
*** 3016,3030 ****
  			}
  			if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
  				assert(pbuf[0] == '0');
! 				assert(pbuf[1] == c);
! 				if (fill != ' ') {
! 					*res++ = *pbuf++;
! 					*res++ = *pbuf++;
! 				}
! 				rescnt -= 2;
! 				width -= 2;
! 				if (width < 0)
! 					width = 0;
! 				len -= 2;
  			}
  			if (width > len && !(flags & F_LJUST)) {
--- 3024,3042 ----
  			}
  			if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
+ 				/* There's a base marker ("0x" or "0X") if and
+ 				 * only if the value is non-zero.
+ 				 */
  				assert(pbuf[0] == '0');
! 				if (pbuf[1] == c) {
! 					if (fill != ' ') {
! 						*res++ = *pbuf++;
! 						*res++ = *pbuf++;
! 					}
! 					rescnt -= 2;
! 					width -= 2;
! 					if (width < 0)
! 						width = 0;
! 					len -= 2;
! 				}
  			}
  			if (width > len && !(flags & F_LJUST)) {
***************
*** 3038,3044 ****
  					*res++ = sign;
  				if ((flags & F_ALT) &&
! 				    (c == 'x' || c == 'X')) {
! 					assert(pbuf[0] == '0');
! 					assert(pbuf[1] == c);
  					*res++ = *pbuf++;
  					*res++ = *pbuf++;
--- 3050,3055 ----
  					*res++ = sign;
  				if ((flags & F_ALT) &&
! 				    (c == 'x' || c == 'X') &&
! 				    pbuf[1] == c) {
  					*res++ = *pbuf++;
  					*res++ = *pbuf++;

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.80
retrieving revision 2.81
diff -C2 -r2.80 -r2.81
*** unicodeobject.c	2001/02/18 22:13:49	2.80
--- unicodeobject.c	2001/04/12 00:35:51	2.81
***************
*** 5082,5095 ****
  	    if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
  		assert(pbuf[0] == '0');
! 		assert(pbuf[1] == c);
! 		if (fill != ' ') {
! 		    *res++ = *pbuf++;
! 		    *res++ = *pbuf++;
  		}
- 		rescnt -= 2;
- 		width -= 2;
- 		if (width < 0)
- 		    width = 0;
- 		len -= 2;
  	    }
  	    if (width > len && !(flags & F_LJUST)) {
--- 5082,5096 ----
  	    if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
  		assert(pbuf[0] == '0');
! 		if (pbuf[1] == c) {
! 			if (fill != ' ') {
! 			    *res++ = *pbuf++;
! 			    *res++ = *pbuf++;
! 			}
! 			rescnt -= 2;
! 			width -= 2;
! 			if (width < 0)
! 			    width = 0;
! 			len -= 2;
  		}
  	    }
  	    if (width > len && !(flags & F_LJUST)) {
***************
*** 5102,5108 ****
  		if (sign)
  		    *res++ = sign;
! 		if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
  		    assert(pbuf[0] == '0');
- 		    assert(pbuf[1] == c);
  		    *res++ = *pbuf++;
  		    *res++ = *pbuf++;
--- 5103,5109 ----
  		if (sign)
  		    *res++ = sign;
! 		if ((flags & F_ALT) && (c == 'x' || c == 'X') &&
! 		    pbuf[1] == c) {
  		    assert(pbuf[0] == '0');
  		    *res++ = *pbuf++;
  		    *res++ = *pbuf++;