MCPcopy Create free account
hub / github.com/python/mypy / convert_format_expr_to_bytes

Function convert_format_expr_to_bytes

mypyc/irbuild/format_str_tokenizer.py:211–236  ·  view source on GitHub ↗

Convert expressions into bytes literal objects with the guidance of FormatOps. Return None when fails.

(
    builder: IRBuilder, format_ops: list[FormatOp], exprs: list[Expression], line: int
)

Source from the content-addressed store, hash-verified

209
210
211def convert_format_expr_to_bytes(
212 builder: IRBuilder, format_ops: list[FormatOp], exprs: list[Expression], line: int
213) -> list[Value] | None:
214 """Convert expressions into bytes literal objects with the guidance
215 of FormatOps. Return None when fails."""
216 if len(format_ops) != len(exprs):
217 return None
218
219 converted = []
220 for x, format_op in zip(exprs, format_ops):
221 node_type = builder.node_type(x)
222 # conversion type 's' is an alias of 'b' in bytes formatting
223 if format_op == FormatOp.BYTES or format_op == FormatOp.STR:
224 if is_bytes_rprimitive(node_type):
225 var_bytes = builder.accept(x)
226 else:
227 return None
228 elif format_op == FormatOp.INT:
229 if isinstance(folded := constant_fold_expr(builder, x), int):
230 var_bytes = builder.load_literal_value(str(folded).encode("ascii"))
231 elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
232 var_bytes = builder.call_c(int_to_ascii_op, [builder.accept(x)], line)
233 else:
234 return None
235 converted.append(var_bytes)
236 return converted
237
238
239def join_formatted_bytes(

Callers 1

Calls 14

is_bytes_rprimitiveFunction · 0.90
constant_fold_exprFunction · 0.90
is_int_rprimitiveFunction · 0.90
is_short_int_rprimitiveFunction · 0.90
lenFunction · 0.85
zipFunction · 0.85
isinstanceFunction · 0.85
strClass · 0.85
node_typeMethod · 0.80
load_literal_valueMethod · 0.80
encodeMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…