Skip to content

bpo-40943: Replace PY_FORMAT_SIZE_T with "z" #20781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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);
}

Expand Down
29 changes: 12 additions & 17 deletions Modules/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)); \
Expand All @@ -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); \
Expand All @@ -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) \
Expand All @@ -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)
Expand Down Expand Up @@ -577,8 +573,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
/* optimization info block */
/* <INFO> <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;
}
Expand Down Expand Up @@ -1028,7 +1023,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

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]) {
Expand Down Expand Up @@ -1091,7 +1086,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

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]) {
Expand Down Expand Up @@ -1358,7 +1353,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
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;
}
Expand Down Expand Up @@ -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));

Expand Down
30 changes: 16 additions & 14 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}

Expand Down
19 changes: 8 additions & 11 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading