[Python-checkins] python/dist/src/Objects intobject.c, 2.106, 2.107 stringobject.c, 2.213, 2.214 unicodeobject.c, 2.202, 2.203

gvanrossum at users.sourceforge.net gvanrossum at users.sourceforge.net
Sat Nov 29 18:52:15 EST 2003


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

Modified Files:
	intobject.c stringobject.c unicodeobject.c 
Log Message:
- Removed FutureWarnings related to hex/oct literals and conversions
  and left shifts.  (Thanks to Kalle Svensson for SF patch 849227.)
  This addresses most of the remaining semantic changes promised by
  PEP 237, except for repr() of a long, which still shows the trailing
  'L'.  The PEP appears to promise warnings for operations that
  changed semantics compared to Python 2.3, but this is not
  implemented; we've suffered through enough warnings related to
  hex/oct literals and I think it's best to be silent now.


Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.106
retrieving revision 2.107
diff -C2 -d -r2.106 -r2.107
*** intobject.c	11 Aug 2003 17:32:02 -0000	2.106
--- intobject.c	29 Nov 2003 23:52:12 -0000	2.107
***************
*** 279,283 ****
  	long x;
  	char buffer[256]; /* For errors */
- 	int warn = 0;
  
  	if ((base != 0 && base < 2) || base > 36) {
--- 279,282 ----
***************
*** 293,297 ****
  		x = (long) PyOS_strtoul(s, &end, base);
  		if (x < 0)
! 			warn = 1;
  	}
  	else
--- 292,296 ----
  		x = (long) PyOS_strtoul(s, &end, base);
  		if (x < 0)
! 			return PyLong_FromString(s, pend, base);
  	}
  	else
***************
*** 313,321 ****
  		return PyLong_FromString(s, pend, base);
  	}
- 	if (warn) {
- 		if (PyErr_Warn(PyExc_FutureWarning,
- 			"int('0...', 0): sign will change in Python 2.4") < 0)
- 			return NULL;
- 	}
  	if (pend)
  		*pend = end;
--- 312,315 ----
***************
*** 767,782 ****
  		return int_pos(v);
  	if (b >= LONG_BIT) {
! 		if (PyErr_Warn(PyExc_FutureWarning,
! 			       "x<<y losing bits or changing sign "
! 			       "will return a long in Python 2.4 and up") < 0)
! 			return NULL;
! 		return PyInt_FromLong(0L);
  	}
  	c = a << b;
  	if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
! 		if (PyErr_Warn(PyExc_FutureWarning,
! 			       "x<<y losing bits or changing sign "
! 			       "will return a long in Python 2.4 and up") < 0)
! 			return NULL;
  	}
  	return PyInt_FromLong(c);
--- 761,771 ----
  		return int_pos(v);
  	if (b >= LONG_BIT) {
! 		return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
! 				       PyLong_FromLong(PyInt_AS_LONG(w)));
  	}
  	c = a << b;
  	if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
! 		return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
! 				       PyLong_FromLong(PyInt_AS_LONG(w)));
  	}
  	return PyInt_FromLong(c);
***************
*** 869,879 ****
  	char buf[100];
  	long x = v -> ob_ival;
! 	if (x < 0) {
! 		if (PyErr_Warn(PyExc_FutureWarning,
! 			       "hex()/oct() of negative int will return "
! 			       "a signed string in Python 2.4 and up") < 0)
! 			return NULL;
! 	}
! 	if (x == 0)
  		strcpy(buf, "0");
  	else
--- 858,864 ----
  	char buf[100];
  	long x = v -> ob_ival;
! 	if (x < 0)
! 		PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
! 	else if (x == 0)
  		strcpy(buf, "0");
  	else
***************
*** 887,897 ****
  	char buf[100];
  	long x = v -> ob_ival;
! 	if (x < 0) {
! 		if (PyErr_Warn(PyExc_FutureWarning,
! 			       "hex()/oct() of negative int will return "
! 			       "a signed string in Python 2.4 and up") < 0)
! 			return NULL;
! 	}
! 	PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
  	return PyString_FromString(buf);
  }
--- 872,879 ----
  	char buf[100];
  	long x = v -> ob_ival;
! 	if (x < 0)
! 		PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
! 	else
! 		PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
  	return PyString_FromString(buf);
  }

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.213
retrieving revision 2.214
diff -C2 -d -r2.213 -r2.214
*** stringobject.c	26 Nov 2003 08:21:35 -0000	2.213
--- stringobject.c	29 Nov 2003 23:52:13 -0000	2.214
***************
*** 3566,3569 ****
--- 3566,3570 ----
  	   + 1 + 1 = 24 */
  	char fmt[64];	/* plenty big enough! */
+ 	char *sign;
  	long x;
  
***************
*** 3573,3582 ****
  		return -1;
  	}
! 	if (x < 0 && type != 'd' && type != 'i') {
! 		if (PyErr_Warn(PyExc_FutureWarning,
! 			       "%u/%o/%x/%X of negative int will return "
! 			       "a signed string in Python 2.4 and up") < 0)
! 			return -1;
  	}
  	if (prec < 0)
  		prec = 1;
--- 3574,3584 ----
  		return -1;
  	}
! 	if (x < 0 && type == 'u') {
! 		type = 'd';
  	}
+ 	if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
+ 		sign = "-";
+ 	else
+ 		sign = "";
  	if (prec < 0)
  		prec = 1;
***************
*** 3604,3625 ****
  		 * formatint() in unicodeobject.c
  		 */
! 		PyOS_snprintf(fmt, sizeof(fmt), "0%c%%.%dl%c",
! 			      type, prec, type);
  	}
  	else {
! 		PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c",
! 			      (flags&F_ALT) ? "#" : "",
  			      prec, type);
  	}
  
! 	/* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal))
! 	 * worst case buf = '0x' + [0-9]*prec, where prec >= 11
  	 */
! 	if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) {
  		PyErr_SetString(PyExc_OverflowError,
  		    "formatted integer is too long (precision too large?)");
  		return -1;
  	}
! 	PyOS_snprintf(buf, buflen, fmt, x);
  	return strlen(buf);
  }
--- 3606,3630 ----
  		 * formatint() in unicodeobject.c
  		 */
! 		PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
! 			      sign, type, prec, type);
  	}
  	else {
! 		PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
! 			      sign, (flags&F_ALT) ? "#" : "",
  			      prec, type);
  	}
  
! 	/* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
! 	 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
  	 */
! 	if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
  		PyErr_SetString(PyExc_OverflowError,
  		    "formatted integer is too long (precision too large?)");
  		return -1;
  	}
! 	if (sign[0])
! 		PyOS_snprintf(buf, buflen, fmt, -x);
! 	else
! 		PyOS_snprintf(buf, buflen, fmt, x);
  	return strlen(buf);
  }
***************
*** 3908,3913 ****
  					if (!temp)
  						goto error;
- 					/* unbounded ints can always produce
- 					   a sign character! */
  					sign = 1;
  				}
--- 3913,3916 ----
***************
*** 3919,3924 ****
  					if (len < 0)
  						goto error;
! 					/* only d conversion is signed */
! 					sign = c == 'd';
  				}
  				if (flags & F_ZERO)
--- 3922,3926 ----
  					if (len < 0)
  						goto error;
! 					sign = 1;
  				}
  				if (flags & F_ZERO)

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.202
retrieving revision 2.203
diff -C2 -d -r2.202 -r2.203
*** unicodeobject.c	26 Nov 2003 08:21:35 -0000	2.202
--- unicodeobject.c	29 Nov 2003 23:52:13 -0000	2.203
***************
*** 6178,6181 ****
--- 6178,6182 ----
       */
      char fmt[64]; /* plenty big enough! */
+     char *sign;
      long x;
  
***************
*** 6183,6199 ****
      if (x == -1 && PyErr_Occurred())
          return -1;
!     if (x < 0 && type != 'd' && type != 'i') {
! 	if (PyErr_Warn(PyExc_FutureWarning,
! 		       "%u/%o/%x/%X of negative int will return "
! 		       "a signed string in Python 2.4 and up") < 0)
! 	    return -1;
      }
      if (prec < 0)
          prec = 1;
  
!     /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec,len(x in octal))
!      * worst case buf = '0x' + [0-9]*prec, where prec >= 11
       */
!     if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) {
          PyErr_SetString(PyExc_OverflowError,
      	        "formatted integer is too long (precision too large?)");
--- 6184,6201 ----
      if (x == -1 && PyErr_Occurred())
          return -1;
!     if (x < 0 && type == 'u') {
!         type = 'd';
      }
+     if (x < 0 && (type == 'x' || type == 'X' || type == 'o'))
+         sign = "-";
+     else
+         sign = "";
      if (prec < 0)
          prec = 1;
  
!     /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal))
!      * worst case buf = '-0x' + [0-9]*prec, where prec >= 11
       */
!     if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) {
          PyErr_SetString(PyExc_OverflowError,
      	        "formatted integer is too long (precision too large?)");
***************
*** 6223,6235 ****
           * formatint() in stringobject.c
           */
!         PyOS_snprintf(fmt, sizeof(fmt), "0%c%%.%dl%c",
!                       type, prec, type);
      }
      else {
!         PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c",
!                       (flags&F_ALT) ? "#" : "",
                        prec, type);
      }
!     return usprintf(buf, fmt, x);
  }
  
--- 6225,6240 ----
           * formatint() in stringobject.c
           */
!         PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c",
!                       sign, type, prec, type);
      }
      else {
!         PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c",
!                       sign, (flags&F_ALT) ? "#" : "",
                        prec, type);
      }
!     if (sign[0])
!         return usprintf(buf, fmt, -x);
!     else
!         return usprintf(buf, fmt, x);
  }
  
***************
*** 6567,6572 ****
  		    pbuf = PyUnicode_AS_UNICODE(temp);
  		    len = PyUnicode_GET_SIZE(temp);
- 		    /* unbounded ints can always produce
- 		       a sign character! */
  		    sign = 1;
  		}
--- 6572,6575 ----
***************
*** 6577,6582 ****
  		    if (len < 0)
  			goto onError;
! 		    /* only d conversion is signed */
! 		    sign = c == 'd';
  		}
  		if (flags & F_ZERO)
--- 6580,6584 ----
  		    if (len < 0)
  			goto onError;
! 		    sign = 1;
  		}
  		if (flags & F_ZERO)





More information about the Python-checkins mailing list