cffi: pass bytes instead of str to ffi.new("char[]", …) stable
authorManuel Jacob <me@manueljacob.de>
Tue, 06 Aug 2024 17:53:59 +0200
branchstable
changeset 51792 472699b5ddb3
parent 51791 6d7fdf90aa96
child 51793 0f62ea8a9be8
cffi: pass bytes instead of str to ffi.new("char[]", …) The type annotations seem to imply that the passed values are always already bytes, but they aren’t necessarily. Before Python 3.11, the documentation stated that bytes can be used to annotate arguments whose type is actually any of bytes, bytearray, or memoryview.
mercurial/cffi/bdiff.py
mercurial/cffi/mpatch.py
--- a/mercurial/cffi/bdiff.py	Mon Aug 05 21:21:32 2024 +0200
+++ b/mercurial/cffi/bdiff.py	Tue Aug 06 17:53:59 2024 +0200
@@ -23,8 +23,8 @@
 def blocks(sa: bytes, sb: bytes) -> List[Tuple[int, int, int, int]]:
     a = ffi.new("struct bdiff_line**")
     b = ffi.new("struct bdiff_line**")
-    ac = ffi.new("char[]", str(sa))
-    bc = ffi.new("char[]", str(sb))
+    ac = ffi.new("char[]", bytes(sa))
+    bc = ffi.new("char[]", bytes(sb))
     l = ffi.new("struct bdiff_hunk*")
     try:
         an = lib.bdiff_splitlines(ac, len(sa), a)
@@ -51,8 +51,8 @@
 def bdiff(sa: bytes, sb: bytes) -> bytes:
     a = ffi.new("struct bdiff_line**")
     b = ffi.new("struct bdiff_line**")
-    ac = ffi.new("char[]", str(sa))
-    bc = ffi.new("char[]", str(sb))
+    ac = ffi.new("char[]", bytes(sa))
+    bc = ffi.new("char[]", bytes(sb))
     l = ffi.new("struct bdiff_hunk*")
     try:
         an = lib.bdiff_splitlines(ac, len(sa), a)
--- a/mercurial/cffi/mpatch.py	Mon Aug 05 21:21:32 2024 +0200
+++ b/mercurial/cffi/mpatch.py	Tue Aug 06 17:53:59 2024 +0200
@@ -20,7 +20,7 @@
 def cffi_get_next_item(arg, pos):
     all, bins = ffi.from_handle(arg)
     container = ffi.new("struct mpatch_flist*[1]")
-    to_pass = ffi.new("char[]", str(bins[pos]))
+    to_pass = ffi.new("char[]", bytes(bins[pos]))
     all.append(to_pass)
     r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container)
     if r < 0: