Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
2058f81
[mypyc] feat: new primitive for `int.to_bytes`
BobTheBuidler Aug 16, 2025
f62bfd2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 16, 2025
813c510
add headers
BobTheBuidler Aug 16, 2025
4be77d6
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Aug 16, 2025
cb93329
cover all arg combos
BobTheBuidler Aug 16, 2025
6ff1c9b
CPyLong_ToBytes header
BobTheBuidler Aug 16, 2025
166b6f4
;
BobTheBuidler Aug 16, 2025
be2d2de
fix name
BobTheBuidler Aug 16, 2025
9ff3a7c
fix _PyLong_AsByteArray compile err
BobTheBuidler Aug 16, 2025
8399619
Update ir.py
BobTheBuidler Aug 17, 2025
db7b483
define header
BobTheBuidler Aug 17, 2025
ed05c00
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Aug 17, 2025
a0c147b
fix ir
BobTheBuidler Aug 17, 2025
5bab265
Update ir.py
BobTheBuidler Aug 17, 2025
b346bbf
fix ir
BobTheBuidler Aug 17, 2025
8d523a0
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Aug 17, 2025
85652a2
fix ir
BobTheBuidler Aug 17, 2025
8e97165
add ir test
BobTheBuidler Aug 17, 2025
3660a01
fix py 3.10
BobTheBuidler Aug 17, 2025
6a8a83c
use _PyLong_AsByteArray on all pythons
BobTheBuidler Aug 17, 2025
ec95008
fix: py3.13 and 3.14
BobTheBuidler Aug 17, 2025
95fa8b0
optimize if check
BobTheBuidler Aug 17, 2025
78f4ca1
Merge branch 'master' into to-bytes
BobTheBuidler Aug 20, 2025
326484b
Update CPy.h
BobTheBuidler Aug 21, 2025
245a122
Update int_ops.c
BobTheBuidler Aug 21, 2025
d2994e1
Update int_ops.c
BobTheBuidler Aug 21, 2025
1138448
Merge branch 'master' into to-bytes
BobTheBuidler Aug 23, 2025
0715ca2
test long
BobTheBuidler Aug 25, 2025
7cce1e7
add overflow error tests
BobTheBuidler Aug 25, 2025
c3aaefa
Merge branch 'master' into to-bytes
BobTheBuidler Aug 29, 2025
0d7cb57
fix: move helper func up in C file
BobTheBuidler Sep 8, 2025
db09bc0
Update ir.py
BobTheBuidler Sep 9, 2025
a7151dd
Update irbuild-int.test
BobTheBuidler Sep 9, 2025
bdd320e
feat: specialize
BobTheBuidler Sep 10, 2025
4518aea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 10, 2025
97873da
missing }
BobTheBuidler Sep 10, 2025
d7b0023
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Sep 10, 2025
26cd5c6
fix: specialize
BobTheBuidler Sep 10, 2025
30c4a30
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 10, 2025
84acab3
fix
BobTheBuidler Sep 10, 2025
b5a6c06
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 10, 2025
ef5b1d7
fix
BobTheBuidler Sep 10, 2025
9b629a5
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Sep 10, 2025
9b69d58
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 10, 2025
6a5de07
fix
BobTheBuidler Sep 10, 2025
251986e
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Sep 10, 2025
f5f9d30
fix
BobTheBuidler Sep 10, 2025
9a27e5d
Update irbuild-int.test
BobTheBuidler Sep 10, 2025
6c82357
Update run-integers.test
BobTheBuidler Sep 10, 2025
c4d9673
Update irbuild-int.test
BobTheBuidler Sep 10, 2025
3ad1e50
maybe fix
BobTheBuidler Sep 10, 2025
77535d3
Update int_ops.py
BobTheBuidler Sep 10, 2025
d291fb1
Update irbuild-int.test
BobTheBuidler Sep 10, 2025
66bb191
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into…
BobTheBuidler Sep 10, 2025
420b8e2
Merge branch 'master' into to-bytes
BobTheBuidler Sep 18, 2025
2fc6433
Merge branch 'master' into to-bytes
BobTheBuidler Sep 30, 2025
11a6aea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 30, 2025
2d69419
Merge branch 'master' into to-bytes
BobTheBuidler Oct 14, 2025
4efde1d
Merge branch 'master' into to-bytes
BobTheBuidler Jan 17, 2026
c63862d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2026
5b7cae9
Merge branch 'master' into to-bytes
BobTheBuidler Jan 18, 2026
c761f6b
Fix int.to_bytes specialization arg handling (#13)
BobTheBuidler Jan 18, 2026
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
Prev Previous commit
Next Next commit
Merge branch 'master' into to-bytes
  • Loading branch information
BobTheBuidler authored Jan 17, 2026
commit 4efde1d6170dd205b4e3b45cfd5539be8900ac2c
154 changes: 154 additions & 0 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
is_int64_rprimitive,
is_int_rprimitive,
is_list_rprimitive,
is_sequence_rprimitive,
is_str_rprimitive,
is_tagged,
is_uint8_rprimitive,
list_rprimitive,
object_rprimitive,
Expand Down Expand Up @@ -117,6 +119,14 @@
int_to_little_endian_op,
isinstance_int,
)
from mypyc.primitives.generic_ops import generic_setattr, setup_object
from mypyc.primitives.int_ops import isinstance_int
from mypyc.primitives.librt_strings_ops import (
bytes_writer_adjust_index_op,
bytes_writer_get_item_unsafe_op,
bytes_writer_range_check_op,
bytes_writer_set_item_unsafe_op,
)
from mypyc.primitives.list_ops import isinstance_list, new_list_set_item_op
from mypyc.primitives.misc_ops import isinstance_bool
from mypyc.primitives.set_ops import isinstance_frozenset, isinstance_set
Expand Down Expand Up @@ -1273,4 +1283,148 @@ def specialize_int_to_bytes(builder: IRBuilder, expr: CallExpr, callee: RefExpr)
byteorder_arg = builder.accept(byteorder_expr)
return builder.call_c(
int_to_bytes_op, [self_arg, length_arg, byteorder_arg, signed_arg], expr.line


def translate_getitem_with_bounds_check(
builder: IRBuilder,
base_expr: Expression,
args: list[Expression],
ctx_expr: Expression,
adjust_index_op: PrimitiveDescription,
range_check_op: PrimitiveDescription,
get_item_unsafe_op: PrimitiveDescription,
) -> Value | None:
"""Shared helper for optimized __getitem__ with bounds checking.

This implements the common pattern of:
1. Adjusting negative indices
2. Checking if index is in valid range
3. Raising IndexError if out of range
4. Getting the item if in range

Args:
builder: The IR builder
base_expr: The base object expression
args: The arguments to __getitem__ (should be length 1)
ctx_expr: The context expression for line numbers
adjust_index_op: Primitive op to adjust negative indices
range_check_op: Primitive op to check if index is in valid range
get_item_unsafe_op: Primitive op to get item (no bounds checking)

Returns:
The result value, or None if optimization doesn't apply
"""
# Check that we have exactly one argument
if len(args) != 1:
return None

# Get the object
obj = builder.accept(base_expr)

# Get the index argument
index = builder.accept(args[0])

# Adjust the index (handle negative indices)
adjusted_index = builder.primitive_op(adjust_index_op, [obj, index], ctx_expr.line)

# Check if the adjusted index is in valid range
range_check = builder.primitive_op(range_check_op, [obj, adjusted_index], ctx_expr.line)

# Create blocks for branching
valid_block = BasicBlock()
invalid_block = BasicBlock()

builder.add_bool_branch(range_check, valid_block, invalid_block)

# Handle invalid index - raise IndexError
builder.activate_block(invalid_block)
builder.add(
RaiseStandardError(RaiseStandardError.INDEX_ERROR, "index out of range", ctx_expr.line)
)
builder.add(Unreachable())

# Handle valid index - get the item
builder.activate_block(valid_block)
result = builder.primitive_op(get_item_unsafe_op, [obj, adjusted_index], ctx_expr.line)

return result


@specialize_dunder("__getitem__", bytes_writer_rprimitive)
def translate_bytes_writer_get_item(
builder: IRBuilder, base_expr: Expression, args: list[Expression], ctx_expr: Expression
) -> Value | None:
"""Optimized BytesWriter.__getitem__ implementation with bounds checking."""
return translate_getitem_with_bounds_check(
builder,
base_expr,
args,
ctx_expr,
bytes_writer_adjust_index_op,
bytes_writer_range_check_op,
bytes_writer_get_item_unsafe_op,
)


@specialize_dunder("__setitem__", bytes_writer_rprimitive)
def translate_bytes_writer_set_item(
builder: IRBuilder, base_expr: Expression, args: list[Expression], ctx_expr: Expression
) -> Value | None:
"""Optimized BytesWriter.__setitem__ implementation with bounds checking."""
# Check that we have exactly two arguments (index and value)
if len(args) != 2:
return None

# Get the BytesWriter object
obj = builder.accept(base_expr)

# Get the index and value arguments
index = builder.accept(args[0])
value = builder.accept(args[1])

# Adjust the index (handle negative indices)
adjusted_index = builder.primitive_op(
bytes_writer_adjust_index_op, [obj, index], ctx_expr.line
)

# Check if the adjusted index is in valid range
range_check = builder.primitive_op(
bytes_writer_range_check_op, [obj, adjusted_index], ctx_expr.line
)

# Create blocks for branching
valid_block = BasicBlock()
invalid_block = BasicBlock()

builder.add_bool_branch(range_check, valid_block, invalid_block)

# Handle invalid index - raise IndexError
builder.activate_block(invalid_block)
builder.add(
RaiseStandardError(RaiseStandardError.INDEX_ERROR, "index out of range", ctx_expr.line)
)
builder.add(Unreachable())

# Handle valid index - set the item
builder.activate_block(valid_block)
builder.primitive_op(
bytes_writer_set_item_unsafe_op, [obj, adjusted_index, value], ctx_expr.line
)

return builder.none()


@specialize_dunder("__getitem__", bytes_rprimitive)
def translate_bytes_get_item(
builder: IRBuilder, base_expr: Expression, args: list[Expression], ctx_expr: Expression
) -> Value | None:
"""Optimized bytes.__getitem__ implementation with bounds checking."""
return translate_getitem_with_bounds_check(
builder,
base_expr,
args,
ctx_expr,
bytes_adjust_index_op,
bytes_range_check_op,
bytes_get_item_unsafe_op,
)
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.