[PATCH] base85: fix format char for Py_ssize_t vars (issue3481)

Adrian Buehlmann adrian at cadifra.com
Mon Jun 4 07:21:03 CDT 2012


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1338811441 -7200
# Node ID 44cde0815819d36f3a03aefc1950e9b82a2892a1
# Parent  0a0cf3f26938ff7a084f2dcc9e59152ac6060e1e
base85: fix format char for Py_ssize_t vars (issue3481)

The format char(s) for the format string of functions like PyString_FromFormat()
has to be "%zd" for arguments of type Py_ssize_t.

This format was introduced with Python 2.5. For Python 2.4, we've typedef'ed
Py_ssize_t to int (in util.h), which requires format char "%d" instead.

To distinguish these cases, a new define FORMATCHAR_PY_SSIZE_T is added to
util.h.

diff --git a/mercurial/base85.c b/mercurial/base85.c
--- a/mercurial/base85.c
+++ b/mercurial/base85.c
@@ -111,7 +111,8 @@
 			if (c < 0)
 				return PyErr_Format(
 					PyExc_ValueError,
-					"bad base85 character at position %d", i);
+					"bad base85 character at position "
+					FORMATCHAR_PY_SSIZE_T, i);
 			acc = acc * 85 + c;
 		}
 		if (i++ < len)
@@ -120,13 +121,15 @@
 			if (c < 0)
 				return PyErr_Format(
 					PyExc_ValueError,
-					"bad base85 character at position %d", i);
+					"bad base85 character at position "
+					FORMATCHAR_PY_SSIZE_T, i);
 			/* overflow detection: 0xffffffff == "|NsC0",
 			 * "|NsC" == 0x03030303 */
 			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
 				return PyErr_Format(
 					PyExc_ValueError,
-					"bad base85 sequence at position %d", i);
+					"bad base85 sequence at position "
+					FORMATCHAR_PY_SSIZE_T, i);
 			acc += c;
 		}
 
diff --git a/mercurial/util.h b/mercurial/util.h
--- a/mercurial/util.h
+++ b/mercurial/util.h
@@ -101,7 +101,10 @@
 
 #endif /* PY_VERSION_HEX */
 
-#if (PY_VERSION_HEX < 0x02050000)
+#if (PY_VERSION_HEX >= 0x02050000)
+/* Format characters for PyString_FromFormat */
+#define FORMATCHAR_PY_SSIZE_T "%zd"
+#else
 /* Definitions to get compatibility with python 2.4 and earlier which
    does not have Py_ssize_t. See also PEP 353.
    Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t.
@@ -111,11 +114,13 @@
 typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
 #define PyInt_FromSsize_t PyInt_FromLong
 
+#define FORMATCHAR_PY_SSIZE_T "%d"
+
 #if !defined(PY_SSIZE_T_MIN)
 #define PY_SSIZE_T_MAX INT_MAX
 #define PY_SSIZE_T_MIN INT_MIN
 #endif
-#endif
+#endif /* PY_VERSION_HEX */
 
 #ifdef _WIN32
 #ifdef _MSC_VER


More information about the Mercurial-devel mailing list