Skip to content

Commit d3fa7d7

Browse files
r41k0uclaude
andcommitted
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) <noreply@anthropic.com>
1 parent 49ed2cf commit d3fa7d7

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

‎pythonbpf/helper/printk_formatter.py‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ast
22
import logging
3+
from pythonbpf.type_deducer import signedness
34

45
from llvmlite import ir
56
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
155156
def _populate_fval(ftype, node, fmt_parts, exprs):
156157
"""Populate format parts and expressions based on field type."""
157158
if isinstance(ftype, ir.IntType):
158-
# TODO: We print as signed integers only for now
159-
if ftype.width == 64:
160-
fmt_parts.append("%lld")
161-
exprs.append(node)
162-
elif ftype.width == 32:
163-
fmt_parts.append("%d")
164-
exprs.append(node)
165-
else:
166-
raise NotImplementedError(
167-
f"Unsupported integer width in f-string: {ftype.width}"
168-
)
159+
# Every integer argument is widened to 64 bits (per its sign) before
160+
# the call, so the 64-bit format fits any width; the sign picks it.
161+
fmt_parts.append("%lld" if signedness(ftype) else "%llu")
162+
exprs.append(node)
169163
elif isinstance(ftype, ir.PointerType):
170164
target, depth = get_base_type_and_depth(ftype)
171165
if isinstance(target, ir.IntType):

0 commit comments

Comments
 (0)