From 407e30ca3ff84a0e8655c4e70ec244fdd75e42b6 Mon Sep 17 00:00:00 2001 From: "mark.dickinson" Date: Tue, 24 Nov 2009 20:51:48 +0000 Subject: [PATCH] Issue #7228: Fix format mismatch when printing something of type off_t. (Should silence some compiler warnings.) git-svn-id: http://svn.python.org/projects/python/trunk@76502 6015fed2-1504-0410-9fe1-9d1591cc4771 --- Modules/_io/_iomodule.h | 18 +++++++++++++++++- Modules/_io/bufferedio.c | 6 ++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h index e220ec1fc3..790d9193c5 100644 --- a/Modules/_io/_iomodule.h +++ b/Modules/_io/_iomodule.h @@ -75,6 +75,14 @@ PyAPI_DATA(PyObject *) PyExc_BlockingIOError; * Offset type for positioning. */ +/* Printing a variable of type off_t (with e.g., PyString_FromFormat) + correctly and without producing compiler warnings is surprisingly painful. + We identify an integer type whose size matches off_t and then: (1) cast the + off_t to that integer type and (2) use the appropriate conversion + specification. The cast is necessary: gcc complains about formatting a + long with "%lld" even when both long and long long have the same + precision. */ + #if defined(MS_WIN64) || defined(MS_WINDOWS) /* Windows uses long long for offsets */ @@ -83,6 +91,8 @@ typedef PY_LONG_LONG Py_off_t; # define PyLong_FromOff_t PyLong_FromLongLong # define PY_OFF_T_MAX PY_LLONG_MAX # define PY_OFF_T_MIN PY_LLONG_MIN +# define PY_OFF_T_COMPAT PY_LONG_LONG /* type compatible with off_t */ +# define PY_PRIdOFF "lld" /* format to use for that type */ #else @@ -93,16 +103,22 @@ typedef off_t Py_off_t; # define PyLong_FromOff_t PyLong_FromSsize_t # define PY_OFF_T_MAX PY_SSIZE_T_MAX # define PY_OFF_T_MIN PY_SSIZE_T_MIN -#elif (SIZEOF_OFF_T == SIZEOF_LONG_LONG) +# define PY_OFF_T_COMPAT Py_ssize_t +# define PY_PRIdOFF "zd" +#elif (HAVE_LONG_LONG && SIZEOF_OFF_T == SIZEOF_LONG_LONG) # define PyLong_AsOff_t PyLong_AsLongLong # define PyLong_FromOff_t PyLong_FromLongLong # define PY_OFF_T_MAX PY_LLONG_MAX # define PY_OFF_T_MIN PY_LLONG_MIN +# define PY_OFF_T_COMPAT PY_LONG_LONG +# define PY_PRIdOFF "lld" #elif (SIZEOF_OFF_T == SIZEOF_LONG) # define PyLong_AsOff_t PyLong_AsLong # define PyLong_FromOff_t PyLong_FromLong # define PY_OFF_T_MAX LONG_MAX # define PY_OFF_T_MIN LONG_MIN +# define PY_OFF_T_COMPAT long +# define PY_PRIdOFF "ld" #else # error off_t does not match either size_t, long, or long long! #endif diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 9aa7d4b8b8..3ab7906a93 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -582,7 +582,8 @@ _buffered_raw_tell(buffered *self) if (n < 0) { if (!PyErr_Occurred()) PyErr_Format(PyExc_IOError, - "Raw stream returned invalid position %zd", n); + "Raw stream returned invalid position %" PY_PRIdOFF, + (PY_OFF_T_COMPAT)n); return -1; } self->abs_pos = n; @@ -614,7 +615,8 @@ _buffered_raw_seek(buffered *self, Py_off_t target, int whence) if (n < 0) { if (!PyErr_Occurred()) PyErr_Format(PyExc_IOError, - "Raw stream returned invalid position %zd", n); + "Raw stream returned invalid position %" PY_PRIdOFF, + (PY_OFF_T_COMPAT)n); return -1; } self->abs_pos = n; -- 2.11.4.GIT