Support %zd in PyErr_Format and PyString_FromFormat.

This commit is contained in:
Martin v. Löwis 2006-02-16 06:54:25 +00:00
parent 26efe402c2
commit 2c95cc6d72
8 changed files with 35 additions and 19 deletions

View File

@ -243,14 +243,14 @@ buffer_repr(PyBufferObject *self)
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,

View File

@ -1148,7 +1148,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
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;
}

View File

@ -248,8 +248,8 @@ func_set_code(PyFunctionObject *op, PyObject *value)
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 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
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) {

View File

@ -2598,9 +2598,8 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
}
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);

View File

@ -181,6 +181,9 @@ PyString_FromFormatV(const char *format, va_list vargs)
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 @@ PyString_FromFormatV(const char *format, va_list vargs)
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 @@ PyString_FromFormatV(const char *format, va_list vargs)
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 @@ PyString_FromFormatV(const char *format, va_list vargs)
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);

View File

@ -125,7 +125,7 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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 @@ structseq_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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;

View File

@ -3372,10 +3372,9 @@ check_num_args(PyObject *ob, int n)
}
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;
}

View File

@ -791,8 +791,7 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler
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 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors,
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 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors,
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;
}