From d36cf5f1d20ce9f111a8fc997104785086e8eee6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 10 Jun 2020 18:38:05 +0200 Subject: [PATCH] bpo-40943: Replace PY_FORMAT_SIZE_T with "z" (GH-20781) The PEP 353, written in 2005, introduced PY_FORMAT_SIZE_T. Python no longer supports macOS 10.4 and Visual Studio 2010, but requires more recent macOS and Visual Studio versions. In 2020 with Python 3.10, it is now safe to use directly "%zu" to format size_t and "%zi" to format Py_ssize_t. --- Include/pyport.h | 4 ++- Modules/_pickle.c | 4 +-- Modules/_sre.c | 2 +- Modules/gcmodule.c | 5 ++- Modules/sre_lib.h | 29 ++++++++---------- Objects/bytesobject.c | 30 +++++++++--------- Objects/object.c | 10 +++--- Objects/obmalloc.c | 19 +++++------- Objects/unicodeobject.c | 68 ++++++++++++++++++++--------------------- Python/getargs.c | 7 ++--- Python/pyhash.c | 12 +++----- 11 files changed, 89 insertions(+), 101 deletions(-) diff --git a/Include/pyport.h b/Include/pyport.h index bdbd0c942f6..3c71f30bce1 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -131,7 +131,9 @@ typedef int Py_ssize_clean_t; /* Smallest negative value of type Py_ssize_t. */ #define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1) -/* PY_FORMAT_SIZE_T is a platform-specific modifier for use in a printf +/* Macro kept for backward compatibility: use "z" in new code. + * + * PY_FORMAT_SIZE_T is a platform-specific modifier for use in a printf * format to convert an argument with the width of a size_t or Py_ssize_t. * C99 introduced "z" for this purpose, but old MSVCs had not supported it. * Since MSVC supports "z" since (at least) 2015, we can just use "z" diff --git a/Modules/_pickle.c b/Modules/_pickle.c index 5539e64025a..25e888db19c 100644 --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -1715,7 +1715,7 @@ memo_get(PicklerObject *self, PyObject *key) if (!self->bin) { pdata[0] = GET; PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, - "%" PY_FORMAT_SIZE_T "d\n", *value); + "%zd\n", *value); len = strlen(pdata); } else { @@ -1772,7 +1772,7 @@ memo_put(PicklerObject *self, PyObject *obj) else if (!self->bin) { pdata[0] = PUT; PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, - "%" PY_FORMAT_SIZE_T "d\n", idx); + "%zd\n", idx); len = strlen(pdata); } else { diff --git a/Modules/_sre.c b/Modules/_sre.c index bdc427822d7..70bd8baa01e 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -211,7 +211,7 @@ data_stack_grow(SRE_STATE* state, Py_ssize_t size) if (cursize < minsize) { void* stack; cursize = minsize+minsize/4+1024; - TRACE(("allocate/grow stack %" PY_FORMAT_SIZE_T "d\n", cursize)); + TRACE(("allocate/grow stack %zd\n", cursize)); stack = PyMem_REALLOC(state->data_stack, cursize); if (!stack) { data_stack_dealloc(state); diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index f68258d7a32..b3bcc8aa426 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -1043,7 +1043,7 @@ show_stats_each_generations(GCState *gcstate) for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) { pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos, - " %"PY_FORMAT_SIZE_T"d", + " %zd", gc_list_size(GEN_HEAD(gcstate, i))); } @@ -1290,8 +1290,7 @@ collect(PyThreadState *tstate, int generation, if (gcstate->debug & DEBUG_STATS) { double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1); PySys_WriteStderr( - "gc: done, %" PY_FORMAT_SIZE_T "d unreachable, " - "%" PY_FORMAT_SIZE_T "d uncollectable, %.4fs elapsed\n", + "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n", n+m, n, d); } diff --git a/Modules/sre_lib.h b/Modules/sre_lib.h index 2657d8d82c6..cfe0a4af2c4 100644 --- a/Modules/sre_lib.h +++ b/Modules/sre_lib.h @@ -323,12 +323,12 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount) if (!i) break; } - TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr, + TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr, (SRE_CHAR*) state->ptr - ptr)); return (SRE_CHAR*) state->ptr - ptr; } - TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr, + TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr)); return ptr - (SRE_CHAR*) state->ptr; } @@ -414,8 +414,7 @@ SRE(info)(SRE_STATE* state, const SRE_CODE* pattern) #define DATA_STACK_ALLOC(state, type, ptr) \ do { \ alloc_pos = state->data_stack_base; \ - TRACE(("allocating %s in %" PY_FORMAT_SIZE_T "d " \ - "(%" PY_FORMAT_SIZE_T "d)\n", \ + TRACE(("allocating %s in %zd (%zd)\n", \ Py_STRINGIFY(type), alloc_pos, sizeof(type))); \ if (sizeof(type) > state->data_stack_size - alloc_pos) { \ int j = data_stack_grow(state, sizeof(type)); \ @@ -429,14 +428,13 @@ do { \ #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \ do { \ - TRACE(("looking up %s at %" PY_FORMAT_SIZE_T "d\n", Py_STRINGIFY(type), pos)); \ + TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \ ptr = (type*)(state->data_stack+pos); \ } while (0) #define DATA_STACK_PUSH(state, data, size) \ do { \ - TRACE(("copy data in %p to %" PY_FORMAT_SIZE_T "d " \ - "(%" PY_FORMAT_SIZE_T "d)\n", \ + TRACE(("copy data in %p to %zd (%zd)\n", \ data, state->data_stack_base, size)); \ if (size > state->data_stack_size - state->data_stack_base) { \ int j = data_stack_grow(state, size); \ @@ -453,8 +451,7 @@ do { \ safely casted to `void*`, see bpo-39943 for details. */ #define DATA_STACK_POP(state, data, size, discard) \ do { \ - TRACE(("copy data to %p from %" PY_FORMAT_SIZE_T "d " \ - "(%" PY_FORMAT_SIZE_T "d)\n", \ + TRACE(("copy data to %p from %zd (%zd)\n", \ data, state->data_stack_base-size, size)); \ memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \ if (discard) \ @@ -463,8 +460,7 @@ do { \ #define DATA_STACK_POP_DISCARD(state, size) \ do { \ - TRACE(("discard data from %" PY_FORMAT_SIZE_T "d " \ - "(%" PY_FORMAT_SIZE_T "d)\n", \ + TRACE(("discard data from %zd (%zd)\n", \ state->data_stack_base-size, size)); \ state->data_stack_base -= size; \ } while(0) @@ -577,8 +573,7 @@ entrance: /* optimization info block */ /* <1=skip> <2=flags> <3=min> ... */ if (ctx->pattern[3] && (uintptr_t)(end - ctx->ptr) < ctx->pattern[3]) { - TRACE(("reject (got %" PY_FORMAT_SIZE_T "d chars, " - "need %" PY_FORMAT_SIZE_T "d)\n", + TRACE(("reject (got %zd chars, need %zd)\n", end - ctx->ptr, (Py_ssize_t) ctx->pattern[3])); RETURN_FAILURE; } @@ -1028,7 +1023,7 @@ entrance: ctx->count = ctx->u.rep->count+1; - TRACE(("|%p|%p|MAX_UNTIL %" PY_FORMAT_SIZE_T "d\n", ctx->pattern, + TRACE(("|%p|%p|MAX_UNTIL %zd\n", ctx->pattern, ctx->ptr, ctx->count)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { @@ -1091,7 +1086,7 @@ entrance: ctx->count = ctx->u.rep->count+1; - TRACE(("|%p|%p|MIN_UNTIL %" PY_FORMAT_SIZE_T "d %p\n", ctx->pattern, + TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", ctx->pattern, ctx->ptr, ctx->count, ctx->u.rep->pattern)); if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { @@ -1358,7 +1353,7 @@ exit: TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr)); goto jump_assert_not; case JUMP_NONE: - TRACE(("|%p|%p|RETURN %" PY_FORMAT_SIZE_T "d\n", ctx->pattern, + TRACE(("|%p|%p|RETURN %zd\n", ctx->pattern, ctx->ptr, ret)); break; } @@ -1420,7 +1415,7 @@ SRE(search)(SRE_STATE* state, SRE_CODE* pattern) pattern += 1 + pattern[1]; } - TRACE(("prefix = %p %" PY_FORMAT_SIZE_T "d %" PY_FORMAT_SIZE_T "d\n", + TRACE(("prefix = %p %zd %zd\n", prefix, prefix_len, prefix_skip)); TRACE(("charset = %p\n", charset)); diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 8d6454059ef..b79c2460409 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -256,27 +256,29 @@ PyBytes_FromFormatV(const char *format, va_list vargs) } case 'd': - if (longflag) + if (longflag) { sprintf(buffer, "%ld", va_arg(vargs, long)); - else if (size_tflag) - sprintf(buffer, "%" PY_FORMAT_SIZE_T "d", - va_arg(vargs, Py_ssize_t)); - else + } + else if (size_tflag) { + sprintf(buffer, "%zd", va_arg(vargs, Py_ssize_t)); + } + else { sprintf(buffer, "%d", va_arg(vargs, int)); + } assert(strlen(buffer) < sizeof(buffer)); WRITE_BYTES(buffer); break; case 'u': - if (longflag) - sprintf(buffer, "%lu", - va_arg(vargs, unsigned long)); - else if (size_tflag) - sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", - va_arg(vargs, size_t)); - else - sprintf(buffer, "%u", - va_arg(vargs, unsigned int)); + if (longflag) { + sprintf(buffer, "%lu", va_arg(vargs, unsigned long)); + } + else if (size_tflag) { + sprintf(buffer, "%zu", va_arg(vargs, size_t)); + } + else { + sprintf(buffer, "%u", va_arg(vargs, unsigned int)); + } assert(strlen(buffer) < sizeof(buffer)); WRITE_BYTES(buffer); break; diff --git a/Objects/object.c b/Objects/object.c index 623ee52eb1e..10cbd1b7c16 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -66,8 +66,7 @@ _Py_GetRefTotal(void) void _PyDebug_PrintTotalRefs(void) { fprintf(stderr, - "[%" PY_FORMAT_SIZE_T "d refs, " - "%" PY_FORMAT_SIZE_T "d blocks]\n", + "[%zd refs, %zd blocks]\n", _Py_GetRefTotal(), _Py_GetAllocatedBlocks()); } #endif /* Py_REF_DEBUG */ @@ -1876,9 +1875,10 @@ _Py_PrintReferences(FILE *fp) PyObject *op; fprintf(fp, "Remaining objects:\n"); for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { - fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op)); - if (PyObject_Print(op, fp, 0) != 0) + fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op)); + if (PyObject_Print(op, fp, 0) != 0) { PyErr_Clear(); + } putc('\n', fp); } } @@ -1892,7 +1892,7 @@ _Py_PrintReferenceAddresses(FILE *fp) PyObject *op; fprintf(fp, "Remaining object addresses:\n"); for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) - fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op, + fprintf(fp, "%p [%zd] %s\n", (void *)op, Py_REFCNT(op), Py_TYPE(op)->tp_name); } diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index eb34f10bddf..03d0e8e5126 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -2420,8 +2420,7 @@ _PyObject_DebugDumpAddress(const void *p) fprintf(stderr, " API '%c'\n", id); nbytes = read_size_t(q - 2*SST); - fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally " - "requested\n", nbytes); + fprintf(stderr, " %zu bytes originally requested\n", nbytes); /* In case this is nuts, check the leading pad bytes first. */ fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1); @@ -2477,8 +2476,9 @@ _PyObject_DebugDumpAddress(const void *p) #ifdef PYMEM_DEBUG_SERIALNO size_t serial = read_size_t(tail + SST); - fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T - "u to debug malloc/realloc.\n", serial); + fprintf(stderr, + " The block was made by call #%zu to debug malloc/realloc.\n", + serial); #endif if (nbytes > 0) { @@ -2553,7 +2553,7 @@ _PyDebugAllocatorStats(FILE *out, char buf1[128]; char buf2[128]; PyOS_snprintf(buf1, sizeof(buf1), - "%d %ss * %" PY_FORMAT_SIZE_T "d bytes each", + "%d %ss * %zd bytes each", num_blocks, block_name, sizeof_block); PyOS_snprintf(buf2, sizeof(buf2), "%48s ", buf1); @@ -2694,10 +2694,7 @@ _PyObject_DebugMallocStats(FILE *out) assert(b == 0 && f == 0); continue; } - fprintf(out, "%5u %6u " - "%11" PY_FORMAT_SIZE_T "u " - "%15" PY_FORMAT_SIZE_T "u " - "%13" PY_FORMAT_SIZE_T "u\n", + fprintf(out, "%5u %6u %11zu %15zu %13zu\n", i, size, p, b, f); allocated_bytes += b * size; available_bytes += f * size; @@ -2716,8 +2713,8 @@ _PyObject_DebugMallocStats(FILE *out) (void)printone(out, "# arenas allocated current", narenas); PyOS_snprintf(buf, sizeof(buf), - "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena", - narenas, ARENA_SIZE); + "%zu arenas * %d bytes/arena", + narenas, ARENA_SIZE); (void)printone(out, buf, narenas * ARENA_SIZE); fputc('\n', out); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index df10888949a..7ab0c882db0 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1356,19 +1356,18 @@ _PyUnicode_Dump(PyObject *op) } else data = unicode->data.any; - printf("%s: len=%" PY_FORMAT_SIZE_T "u, ", - unicode_kind_name(op), ascii->length); + printf("%s: len=%zu, ", unicode_kind_name(op), ascii->length); if (ascii->wstr == data) printf("shared "); printf("wstr=%p", (void *)ascii->wstr); if (!(ascii->state.ascii == 1 && ascii->state.compact == 1)) { - printf(" (%" PY_FORMAT_SIZE_T "u), ", compact->wstr_length); - if (!ascii->state.compact && compact->utf8 == unicode->data.any) + printf(" (%zu), ", compact->wstr_length); + if (!ascii->state.compact && compact->utf8 == unicode->data.any) { printf("shared "); - printf("utf8=%p (%" PY_FORMAT_SIZE_T "u)", - (void *)compact->utf8, compact->utf8_length); + } + printf("utf8=%p (%zu)", (void *)compact->utf8, compact->utf8_length); } printf(", data=%p\n", data); } @@ -2845,35 +2844,35 @@ unicode_fromformat_arg(_PyUnicodeWriter *writer, Py_ssize_t arglen; if (*f == 'u') { - if (longflag) - len = sprintf(buffer, "%lu", - va_arg(*vargs, unsigned long)); - else if (longlongflag) - len = sprintf(buffer, "%llu", - va_arg(*vargs, unsigned long long)); - else if (size_tflag) - len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "u", - va_arg(*vargs, size_t)); - else - len = sprintf(buffer, "%u", - va_arg(*vargs, unsigned int)); + if (longflag) { + len = sprintf(buffer, "%lu", va_arg(*vargs, unsigned long)); + } + else if (longlongflag) { + len = sprintf(buffer, "%llu", va_arg(*vargs, unsigned long long)); + } + else if (size_tflag) { + len = sprintf(buffer, "%zu", va_arg(*vargs, size_t)); + } + else { + len = sprintf(buffer, "%u", va_arg(*vargs, unsigned int)); + } } else if (*f == 'x') { len = sprintf(buffer, "%x", va_arg(*vargs, int)); } else { - if (longflag) - len = sprintf(buffer, "%li", - va_arg(*vargs, long)); - else if (longlongflag) - len = sprintf(buffer, "%lli", - va_arg(*vargs, long long)); - else if (size_tflag) - len = sprintf(buffer, "%" PY_FORMAT_SIZE_T "i", - va_arg(*vargs, Py_ssize_t)); - else - len = sprintf(buffer, "%i", - va_arg(*vargs, int)); + if (longflag) { + len = sprintf(buffer, "%li", va_arg(*vargs, long)); + } + else if (longlongflag) { + len = sprintf(buffer, "%lli", va_arg(*vargs, long long)); + } + else if (size_tflag) { + len = sprintf(buffer, "%zi", va_arg(*vargs, Py_ssize_t)); + } + else { + len = sprintf(buffer, "%i", va_arg(*vargs, int)); + } } assert(len >= 0); @@ -15657,8 +15656,7 @@ unicode_release_interned(void) Py_ssize_t n = PyList_GET_SIZE(keys); #ifdef INTERNED_STATS - fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", - n); + fprintf(stderr, "releasing %zd interned strings\n", n); Py_ssize_t immortal_size = 0, mortal_size = 0; #endif @@ -15688,9 +15686,9 @@ unicode_release_interned(void) _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; } #ifdef INTERNED_STATS - fprintf(stderr, "total size of all interned strings: " - "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " - "mortal/immortal\n", mortal_size, immortal_size); + fprintf(stderr, + "total size of all interned strings: %zd/%zd mortal/immortal\n", + mortal_size, immortal_size); #endif Py_DECREF(keys); PyDict_Clear(interned); diff --git a/Python/getargs.c b/Python/getargs.c index 524ad917cd0..d2dba49966d 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -452,7 +452,7 @@ seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, } if (iarg != 0) { PyOS_snprintf(p, sizeof(buf) - (p - buf), - "argument %" PY_FORMAT_SIZE_T "d", iarg); + "argument %zd", iarg); i = 0; p += strlen(p); while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) { @@ -540,15 +540,14 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, levels[0] = 0; if (toplevel) { PyOS_snprintf(msgbuf, bufsize, - "expected %d argument%s, not %" PY_FORMAT_SIZE_T "d", + "expected %d argument%s, not %zd", n, n == 1 ? "" : "s", len); } else { PyOS_snprintf(msgbuf, bufsize, - "must be sequence of length %d, " - "not %" PY_FORMAT_SIZE_T "d", + "must be sequence of length %d, not %zd", n, len); } return msgbuf; diff --git a/Python/pyhash.c b/Python/pyhash.c index 3843079fbbc..3b6c34eefd5 100644 --- a/Python/pyhash.c +++ b/Python/pyhash.c @@ -200,18 +200,14 @@ void _PyHash_Fini(void) { #ifdef Py_HASH_STATS - int i; - Py_ssize_t total = 0; - const char *fmt = "%2i %8" PY_FORMAT_SIZE_T "d %8" PY_FORMAT_SIZE_T "d\n"; - fprintf(stderr, "len calls total\n"); - for (i = 1; i <= Py_HASH_STATS_MAX; i++) { + Py_ssize_t total = 0; + for (int i = 1; i <= Py_HASH_STATS_MAX; i++) { total += hashstats[i]; - fprintf(stderr, fmt, i, hashstats[i], total); + fprintf(stderr, "%2i %8zd %8zd\n", i, hashstats[i], total); } total += hashstats[0]; - fprintf(stderr, "> %8" PY_FORMAT_SIZE_T "d %8" PY_FORMAT_SIZE_T "d\n", - hashstats[0], total); + fprintf(stderr, "> %8zd %8zd\n", hashstats[0], total); #endif }