Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
number of tests, all because of the codecs/_multibytecodecs issue described
here (it's not a Py3K issue, just something Py3K discovers):
http://mail.python.org/pipermail/python-dev/2006-April/064051.html

Hye-Shik Chang promised to look for a fix, so no need to fix it here. The
tests that are expected to break are:

test_codecencodings_cn
test_codecencodings_hk
test_codecencodings_jp
test_codecencodings_kr
test_codecencodings_tw
test_codecs
test_multibytecodec

This merge fixes an actual test failure (test_weakref) in this branch,
though, so I believe merging is the right thing to do anyway.
diff --git a/Python/modsupport.c b/Python/modsupport.c
index cb6bdfd..e291014 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -3,8 +3,11 @@
 
 #include "Python.h"
 
+#define FLAG_SIZE_T 1
 typedef double va_double;
 
+static PyObject *va_build_value(const char *, va_list, int);
+
 /* Package context -- the full module name for package imports */
 char *_Py_PackageContext = NULL;
 
@@ -146,14 +149,14 @@
 /* Generic function to create a value -- the inverse of getargs() */
 /* After an original idea and first implementation by Steven Miale */
 
-static PyObject *do_mktuple(const char**, va_list *, int, int);
-static PyObject *do_mklist(const char**, va_list *, int, int);
-static PyObject *do_mkdict(const char**, va_list *, int, int);
-static PyObject *do_mkvalue(const char**, va_list *);
+static PyObject *do_mktuple(const char**, va_list *, int, int, int);
+static PyObject *do_mklist(const char**, va_list *, int, int, int);
+static PyObject *do_mkdict(const char**, va_list *, int, int, int);
+static PyObject *do_mkvalue(const char**, va_list *, int);
 
 
 static PyObject *
-do_mkdict(const char **p_format, va_list *p_va, int endchar, int n)
+do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *d;
 	int i;
@@ -167,13 +170,13 @@
 	for (i = 0; i < n; i+= 2) {
 		PyObject *k, *v;
 		int err;
-		k = do_mkvalue(p_format, p_va);
+		k = do_mkvalue(p_format, p_va, flags);
 		if (k == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			k = Py_None;
 		}
-		v = do_mkvalue(p_format, p_va);
+		v = do_mkvalue(p_format, p_va, flags);
 		if (v == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
@@ -199,7 +202,7 @@
 }
 
 static PyObject *
-do_mklist(const char **p_format, va_list *p_va, int endchar, int n)
+do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *v;
 	int i;
@@ -212,13 +215,13 @@
 	/* Note that we can't bail immediately on error as this will leak
 	   refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
-		PyObject *w = do_mkvalue(p_format, p_va);
+		PyObject *w = do_mkvalue(p_format, p_va, flags);
 		if (w == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			w = Py_None;
 		}
-		PyList_SetItem(v, i, w);
+		PyList_SET_ITEM(v, i, w);
 	}
 
 	if (itemfailed) {
@@ -232,7 +235,6 @@
 				"Unmatched paren in format");
 		return NULL;
 	}
-
 	if (endchar)
 		++*p_format;
 	return v;
@@ -250,7 +252,7 @@
 #endif
 
 static PyObject *
-do_mktuple(const char **p_format, va_list *p_va, int endchar, int n)
+do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *v;
 	int i;
@@ -262,45 +264,46 @@
 	/* Note that we can't bail immediately on error as this will leak
 	   refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
-		PyObject *w = do_mkvalue(p_format, p_va);
+		PyObject *w = do_mkvalue(p_format, p_va, flags);
 		if (w == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			w = Py_None;
 		}
-		PyTuple_SetItem(v, i, w);
+		PyTuple_SET_ITEM(v, i, w);
 	}
-	if (v != NULL && **p_format != endchar) {
+	if (itemfailed) {
+		/* do_mkvalue() should have already set an error */
 		Py_DECREF(v);
-		v = NULL;
+		return NULL;
+	}
+	if (**p_format != endchar) {
+		Py_DECREF(v);
 		PyErr_SetString(PyExc_SystemError,
 				"Unmatched paren in format");
+		return NULL;
 	}
-	else if (endchar)
+	if (endchar)
 		++*p_format;
-	if (itemfailed) {
-		Py_DECREF(v);
-		v = NULL;
-	}
 	return v;
 }
 
 static PyObject *
-do_mkvalue(const char **p_format, va_list *p_va)
+do_mkvalue(const char **p_format, va_list *p_va, int flags)
 {
 	for (;;) {
 		switch (*(*p_format)++) {
 		case '(':
 			return do_mktuple(p_format, p_va, ')',
-					  countformat(*p_format, ')'));
+					  countformat(*p_format, ')'), flags);
 
 		case '[':
 			return do_mklist(p_format, p_va, ']',
-					 countformat(*p_format, ']'));
+					 countformat(*p_format, ']'), flags);
 
 		case '{':
 			return do_mkdict(p_format, p_va, '}',
-					 countformat(*p_format, '}'));
+					 countformat(*p_format, '}'), flags);
 
 		case 'b':
 		case 'B':
@@ -351,10 +354,13 @@
 		{
 			PyObject *v;
 			Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
-			int n;
+			Py_ssize_t n;	
 			if (**p_format == '#') {
 				++*p_format;
-				n = va_arg(*p_va, int);
+				if (flags & FLAG_SIZE_T)
+					n = va_arg(*p_va, Py_ssize_t);
+				else
+					n = va_arg(*p_va, int);
 			}
 			else
 				n = -1;
@@ -393,10 +399,13 @@
 		{
 			PyObject *v;
 			char *str = va_arg(*p_va, char *);
-			int n;
+			Py_ssize_t n;
 			if (**p_format == '#') {
 				++*p_format;
-				n = va_arg(*p_va, int);
+				if (flags & FLAG_SIZE_T)
+					n = va_arg(*p_va, Py_ssize_t);
+				else
+					n = va_arg(*p_va, int);
 			}
 			else
 				n = -1;
@@ -407,7 +416,7 @@
 			else {
 				if (n < 0) {
 					size_t m = strlen(str);
-					if (m > INT_MAX) {
+					if (m > PY_SSIZE_T_MAX) {
 						PyErr_SetString(PyExc_OverflowError,
 							"string too long for Python string");
 						return NULL;
@@ -472,7 +481,18 @@
 	va_list va;
 	PyObject* retval;
 	va_start(va, format);
-	retval = Py_VaBuildValue(format, va);
+	retval = va_build_value(format, va, 0);
+	va_end(va);
+	return retval;
+}
+
+PyObject *
+_Py_BuildValue_SizeT(const char *format, ...)
+{
+	va_list va;
+	PyObject* retval;
+	va_start(va, format);
+	retval = va_build_value(format, va, FLAG_SIZE_T);
 	va_end(va);
 	return retval;
 }
@@ -480,6 +500,18 @@
 PyObject *
 Py_VaBuildValue(const char *format, va_list va)
 {
+	return va_build_value(format, va, 0);
+}
+
+PyObject *
+_Py_VaBuildValue_SizeT(const char *format, va_list va)
+{
+	return va_build_value(format, va, FLAG_SIZE_T);
+}
+
+static PyObject *
+va_build_value(const char *format, va_list va, int flags)
+{
 	const char *f = format;
 	int n = countformat(f, '\0');
 	va_list lva;
@@ -501,8 +533,8 @@
 		return Py_None;
 	}
 	if (n == 1)
-		return do_mkvalue(&f, &lva);
-	return do_mktuple(&f, &lva, '\0', n);
+		return do_mkvalue(&f, &lva, flags);
+	return do_mktuple(&f, &lva, '\0', n, flags);
 }