[Python-checkins] r42401 - python/trunk/Objects/bufferobject.c python/trunk/Objects/dictobject.c python/trunk/Objects/funcobject.c python/trunk/Objects/listobject.c python/trunk/Objects/stringobject.c python/trunk/Objects/structseq.c python/trunk/Objects/typeobject.c python/trunk/Objects/unicodeobject.c

martin.v.loewis python-checkins at python.org
Thu Feb 16 07:54:27 CET 2006


Author: martin.v.loewis
Date: Thu Feb 16 07:54:25 2006
New Revision: 42401

Modified:
   python/trunk/Objects/bufferobject.c
   python/trunk/Objects/dictobject.c
   python/trunk/Objects/funcobject.c
   python/trunk/Objects/listobject.c
   python/trunk/Objects/stringobject.c
   python/trunk/Objects/structseq.c
   python/trunk/Objects/typeobject.c
   python/trunk/Objects/unicodeobject.c
Log:
Support %zd in PyErr_Format and PyString_FromFormat.


Modified: python/trunk/Objects/bufferobject.c
==============================================================================
--- python/trunk/Objects/bufferobject.c	(original)
+++ python/trunk/Objects/bufferobject.c	Thu Feb 16 07:54:25 2006
@@ -243,14 +243,14 @@
 	const char *status = self->b_readonly ? "read-only" : "read-write";
 
 	if ( self->b_base == NULL )
-		return PyString_FromFormat("<%s buffer ptr %p, size %ld at %p>",
+		return PyString_FromFormat("<%s buffer ptr %p, size %zd at %p>",
 					   status,
 					   self->b_ptr,
 					   (long)self->b_size,
 					   self);
 	else
 		return PyString_FromFormat(
-			"<%s buffer for %p, size %ld, offset %ld at %p>",
+			"<%s buffer for %p, size %zd, offset %zd at %p>",
 			status,
 			self->b_base,
 			(long)self->b_size,

Modified: python/trunk/Objects/dictobject.c
==============================================================================
--- python/trunk/Objects/dictobject.c	(original)
+++ python/trunk/Objects/dictobject.c	Thu Feb 16 07:54:25 2006
@@ -1148,7 +1148,7 @@
 		if (n != 2) {
 			PyErr_Format(PyExc_ValueError,
 				     "dictionary update sequence element #%d "
-				     "has length %ld; 2 is required",
+				     "has length %zd; 2 is required",
 				     i, (long)n);
 			goto Fail;
 		}

Modified: python/trunk/Objects/funcobject.c
==============================================================================
--- python/trunk/Objects/funcobject.c	(original)
+++ python/trunk/Objects/funcobject.c	Thu Feb 16 07:54:25 2006
@@ -248,8 +248,8 @@
 		    PyTuple_GET_SIZE(op->func_closure));
 	if (nclosure != nfree) {
 		PyErr_Format(PyExc_ValueError,
-			     "%s() requires a code object with %ld free vars,"
-			     " not %ld",
+			     "%s() requires a code object with %zd free vars,"
+			     " not %zd",
 			     PyString_AsString(op->func_name),
 			     (long)nclosure, (long)nfree);
 		return -1;
@@ -401,7 +401,7 @@
 	nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);
 	if (nfree != nclosure)
 		return PyErr_Format(PyExc_ValueError,
-				    "%s requires closure of length %ld, not %ld",
+				    "%s requires closure of length %zd, not %zd",
 				    PyString_AS_STRING(code->co_name),
 				    (long)nfree, (long)nclosure);
 	if (nclosure) {

Modified: python/trunk/Objects/listobject.c
==============================================================================
--- python/trunk/Objects/listobject.c	(original)
+++ python/trunk/Objects/listobject.c	Thu Feb 16 07:54:25 2006
@@ -2598,9 +2598,8 @@
 			}
 
 			if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
-				/* XXX can we use %zd here? */
 				PyErr_Format(PyExc_ValueError,
-            "attempt to assign sequence of size %ld to extended slice of size %ld",
+            "attempt to assign sequence of size %zd to extended slice of size %zd",
 					     (long)PySequence_Fast_GET_SIZE(seq),
 					     (long)slicelength);
 				Py_DECREF(seq);

Modified: python/trunk/Objects/stringobject.c
==============================================================================
--- python/trunk/Objects/stringobject.c	(original)
+++ python/trunk/Objects/stringobject.c	Thu Feb 16 07:54:25 2006
@@ -181,6 +181,9 @@
 			   added */
 			if (*f == 'l' && *(f+1) == 'd')
 				++f;
+			/* likewise for %zd */
+			if (*f == 'z' && *(f+1) == 'd')
+				++f;			
 
 			switch (*f) {
 			case 'c':
@@ -237,6 +240,7 @@
 			const char* p = f++;
 			Py_ssize_t i;
 			int longflag = 0;
+			int size_tflag = 0;
 			/* parse the width.precision part (we're only
 			   interested in the precision value, if any) */
 			n = 0;
@@ -256,6 +260,11 @@
 				longflag = 1;
 				++f;
 			}
+			/* handle the size_t flag. */
+			if (*f == 'z' && *(f+1) == 'd') {
+				size_tflag = 1;
+				++f;
+			}
 
 			switch (*f) {
 			case 'c':
@@ -264,6 +273,18 @@
 			case 'd':
 				if (longflag)
 					sprintf(s, "%ld", va_arg(vargs, long));
+				else if (size_tflag) {
+					/* Instead of checking whether the C
+					   library supports %zd, handle the
+					   common cases. */
+				        #if SIZEOF_SIZE_T == SIZEOF_LONG
+					sprintf(s, "%ld", va_arg(vargs, long));
+					#elif defined(MS_WINDOWS)
+					sprintf(s, "%Id", va_arg(vargs, size_t));
+					#else
+					#error Cannot print size_t values
+					#endif
+				}
 				else
 					sprintf(s, "%d", va_arg(vargs, int));
 				s += strlen(s);

Modified: python/trunk/Objects/structseq.c
==============================================================================
--- python/trunk/Objects/structseq.c	(original)
+++ python/trunk/Objects/structseq.c	Thu Feb 16 07:54:25 2006
@@ -125,7 +125,7 @@
 	if (min_len != max_len) {
 		if (len < min_len) {
 			PyErr_Format(PyExc_TypeError, 
-	       "%.500s() takes an at least %ld-sequence (%ld-sequence given)",
+	       "%.500s() takes an at least %zd-sequence (%zd-sequence given)",
 				     type->tp_name, (long)min_len, (long)len);
 			Py_DECREF(arg);
 			return NULL;
@@ -133,7 +133,7 @@
 
 		if (len > max_len) {
 			PyErr_Format(PyExc_TypeError, 
-	       "%.500s() takes an at most %ld-sequence (%ld-sequence given)",
+	       "%.500s() takes an at most %zd-sequence (%zd-sequence given)",
 				     type->tp_name, (long)max_len, (long)len);
 			Py_DECREF(arg);
 			return NULL;
@@ -142,7 +142,7 @@
 	else {
 		if (len != min_len) {
 			PyErr_Format(PyExc_TypeError, 
-	       "%.500s() takes a %ld-sequence (%ld-sequence given)",
+	       "%.500s() takes a %zd-sequence (%zd-sequence given)",
 				     type->tp_name, (long)min_len, (long)len);
 			Py_DECREF(arg);
 			return NULL;

Modified: python/trunk/Objects/typeobject.c
==============================================================================
--- python/trunk/Objects/typeobject.c	(original)
+++ python/trunk/Objects/typeobject.c	Thu Feb 16 07:54:25 2006
@@ -3372,10 +3372,9 @@
 	}
 	if (n == PyTuple_GET_SIZE(ob))
 		return 1;
-	/* XXX %zd? */
 	PyErr_Format(
 	    PyExc_TypeError, 
-	    "expected %d arguments, got %d", n, (int)PyTuple_GET_SIZE(ob));
+	    "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
 	return 0;
 }
 

Modified: python/trunk/Objects/unicodeobject.c
==============================================================================
--- python/trunk/Objects/unicodeobject.c	(original)
+++ python/trunk/Objects/unicodeobject.c	Thu Feb 16 07:54:25 2006
@@ -791,8 +791,7 @@
     if (newpos<0)
 	newpos = insize+newpos;
     if (newpos<0 || newpos>insize) {
-	/* XXX %zd? */
-	PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)newpos);
+	PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", newpos);
 	goto onError;
     }
 
@@ -2473,8 +2472,7 @@
     if (*newpos<0)
 	*newpos = size+*newpos;
     if (*newpos<0 || *newpos>size) {
-	/* XXX %zd? */
-	PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)*newpos);
+	PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
 	Py_DECREF(restuple);
 	return NULL;
     }
@@ -3373,8 +3371,7 @@
     else
         *newpos = i_newpos;
     if (*newpos<0 || *newpos>size) {
-	/* XXX %zd? */
-	PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", (int)*newpos);
+	PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
 	Py_DECREF(restuple);
 	return NULL;
     }


More information about the Python-checkins mailing list