Support %zd in PyErr_Format and PyString_FromFormat.
diff --git a/Objects/bufferobject.c b/Objects/bufferobject.c
index a21d0b1..73f361e 100644
--- a/Objects/bufferobject.c
+++ b/Objects/bufferobject.c
@@ -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,
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index abc9c8c..7910cb6 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -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;
 		}
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index d644001..ff70baf 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -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) {
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 460f63a..a970c14 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -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);
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index a8e1cb6..8821dcec 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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);
diff --git a/Objects/structseq.c b/Objects/structseq.c
index a95f3a9..7a01fc4 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -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;
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index f52a87f..ac1e064 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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;
 }
 
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 1652b2f..684706d 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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;
     }