Skip to content

Commit 66bb191

Browse files
committed
Merge branch 'to-bytes' of https://github.com/BobTheBuidler/mypy into to-bytes
2 parents f5f9d30 + d291fb1 commit 66bb191

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

mypyc/primitives/int_ops.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,12 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription:
307307
error_kind=ERR_NEVER,
308308
)
309309

310-
311-
# int.to_bytes(length, byteorder, signed=...)
312-
int_to_bytes_op = custom_op(
313-
arg_types=[int_rprimitive, int_rprimitive, str_rprimitive, bool_rprimitive],
314-
return_type=bytes_rprimitive,
315-
c_function_name="CPyTagged_ToBytes",
316-
error_kind=ERR_MAGIC,
317-
)
318-
319-
# specialized custom_op cases for int.to_bytes:
310+
# specialized custom_op cases for int.to_bytes
320311

321312
# int.to_bytes(length, "big")
322313
# int.to_bytes(length, "big", signed=...)
323314
int_to_big_endian_op = custom_op(
324-
arg_types=[int_rprimitive, int_rprimitive, bool_rprimitive],
315+
arg_types=[int_rprimitive, c_pyssize_t_rprimitive, bool_rprimitive],
325316
return_type=bytes_rprimitive,
326317
c_function_name="CPyTagged_ToBigEndianBytes",
327318
error_kind=ERR_MAGIC,
@@ -330,8 +321,16 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription:
330321
# int.to_bytes(length, "little")
331322
# int.to_bytes(length, "little", signed=...)
332323
int_to_little_endian_op = custom_op(
333-
arg_types=[int_rprimitive, int_rprimitive, bool_rprimitive],
324+
arg_types=[int_rprimitive, c_pyssize_t_rprimitive, bool_rprimitive],
334325
return_type=bytes_rprimitive,
335326
c_function_name="CPyTagged_ToLittleEndianBytes",
336327
error_kind=ERR_MAGIC,
337328
)
329+
330+
# int.to_bytes(length, byteorder, signed=...)
331+
int_to_bytes_op = custom_op(
332+
arg_types=[int_rprimitive, c_pyssize_t_rprimitive, str_rprimitive, bool_rprimitive],
333+
return_type=bytes_rprimitive,
334+
c_function_name="CPyTagged_ToBytes",
335+
error_kind=ERR_MAGIC,
336+
)

mypyc/test-data/irbuild-int.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ def f(x):
224224
x :: int
225225
r0 :: bytes
226226
L0:
227-
r0 = CPyTagged_ToBigEndianBytes(x, 4, 0)
227+
r0 = CPyTagged_ToBigEndianBytes(x, 2, 0)
228228
return r0
229229
def g(x):
230230
x :: int
231231
r0 :: bytes
232232
L0:
233-
r0 = CPyTagged_ToLittleEndianBytes(x, 8, 1)
233+
r0 = CPyTagged_ToLittleEndianBytes(x, 4, 1)
234234
return r0
235-
def h(x):
235+
def h(x, byteorder):
236236
x :: int
237237
byteorder :: str
238238
r0 :: bytes

mypyc/test-data/run-integers.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ from testutil import assertRaises
578578
def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes:
579579
return n.to_bytes(length, byteorder, signed=signed)
580580
def test_to_bytes() -> None:
581-
assert to_bytes(255, 2, "big") == b'\x00\xff'
582-
assert to_bytes(255, 2, "little") == b'\xff\x00'
583-
assert to_bytes(-1, 2, "big", True) == b'\xff\xff'
584-
assert to_bytes(0, 1, "big") == b'\x00'
581+
assert to_bytes(255, 2, "big") == b'\x00\xff', to_bytes(255, 2, "big")
582+
assert to_bytes(255, 2, "little") == b'\xff\x00', to_bytes(255, 2, "little")
583+
assert to_bytes(-1, 2, "big", True) == b'\xff\xff', to_bytes(-1, 2, "big", True)
584+
assert to_bytes(0, 1, "big") == b'\x00', to_bytes(0, 1, "big")
585585
# test with a value that does not fit in 64 bits
586-
assert to_bytes(10**30, 16, "big") == b'\x00\x00\x00\x0c\x9f,\x9c\xd0Ft\xed\xea@\x00\x00\x00'
586+
assert to_bytes(10**30, 16, "big") == b'\x00\x00\x00\x0c\x9f,\x9c\xd0Ft\xed\xea@\x00\x00\x00', to_bytes(10**30, 16, "big")
587587
# unsigned, too large for 1 byte
588588
with assertRaises(OverflowError):
589589
to_bytes(256, 1, "big")

0 commit comments

Comments
 (0)