From d3fa7d7e42b9a810676ce20d38bef9edc4b37f3a Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 25 Sep 2026 22:20:00 +0530 Subject: [PATCH] Core: Print integers of any width in f-strings The printk formatter accepted 64- and 32-bit integers only and raised for anything else. That went unnoticed while c_uint16(0) and friends silently made 64-bit slots; with those slots now their declared width, printing any 8- or 16-bit local failed. Every integer argument is already widened to 64 bits per its sign before the call, so the 64-bit format fits every width, and the sign now chooses %lld or %llu (an unsigned value above INT_MAX printed as negative before). Co-Authored-By: Claude Opus 5.5 (1M context) --- pythonbpf/helper/printk_formatter.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/pythonbpf/helper/printk_formatter.py b/pythonbpf/helper/printk_formatter.py index 47262c9..1b96ca8 100644 --- a/pythonbpf/helper/printk_formatter.py +++ b/pythonbpf/helper/printk_formatter.py @@ -1,5 +1,6 @@ import ast import logging +from pythonbpf.type_deducer import signedness from llvmlite import ir from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth, convert @@ -155,17 +156,10 @@ def _process_attr_in_fval(attr_node, fmt_parts, exprs, local_sym_tab, struct_sym def _populate_fval(ftype, node, fmt_parts, exprs): """Populate format parts and expressions based on field type.""" if isinstance(ftype, ir.IntType): - # TODO: We print as signed integers only for now - if ftype.width == 64: - fmt_parts.append("%lld") - exprs.append(node) - elif ftype.width == 32: - fmt_parts.append("%d") - exprs.append(node) - else: - raise NotImplementedError( - f"Unsupported integer width in f-string: {ftype.width}" - ) + # Every integer argument is widened to 64 bits (per its sign) before + # the call, so the 64-bit format fits any width; the sign picks it. + fmt_parts.append("%lld" if signedness(ftype) else "%llu") + exprs.append(node) elif isinstance(ftype, ir.PointerType): target, depth = get_base_type_and_depth(ftype) if isinstance(target, ir.IntType):