changeset 51673:ecc3a893979d stable

cffi: pass C type and attribute names as str instead of bytes
author Manuel Jacob <me@manueljacob.de>
date Mon, 05 Aug 2024 21:08:36 +0200
parents 99632adff795
children 6d7fdf90aa96
files mercurial/cffi/bdiff.py mercurial/cffi/mpatch.py mercurial/cffi/osutil.py
diffstat 3 files changed, 21 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/cffi/bdiff.py	Mon Aug 05 20:47:17 2024 +0200
+++ b/mercurial/cffi/bdiff.py	Mon Aug 05 21:08:36 2024 +0200
@@ -21,11 +21,11 @@
 
 
 def blocks(sa: bytes, sb: bytes) -> List[Tuple[int, int, int, int]]:
-    a = ffi.new(b"struct bdiff_line**")
-    b = ffi.new(b"struct bdiff_line**")
-    ac = ffi.new(b"char[]", str(sa))
-    bc = ffi.new(b"char[]", str(sb))
-    l = ffi.new(b"struct bdiff_hunk*")
+    a = ffi.new("struct bdiff_line**")
+    b = ffi.new("struct bdiff_line**")
+    ac = ffi.new("char[]", str(sa))
+    bc = ffi.new("char[]", str(sb))
+    l = ffi.new("struct bdiff_hunk*")
     try:
         an = lib.bdiff_splitlines(ac, len(sa), a)
         bn = lib.bdiff_splitlines(bc, len(sb), b)
@@ -49,11 +49,11 @@
 
 
 def bdiff(sa: bytes, sb: bytes) -> bytes:
-    a = ffi.new(b"struct bdiff_line**")
-    b = ffi.new(b"struct bdiff_line**")
-    ac = ffi.new(b"char[]", str(sa))
-    bc = ffi.new(b"char[]", str(sb))
-    l = ffi.new(b"struct bdiff_hunk*")
+    a = ffi.new("struct bdiff_line**")
+    b = ffi.new("struct bdiff_line**")
+    ac = ffi.new("char[]", str(sa))
+    bc = ffi.new("char[]", str(sb))
+    l = ffi.new("struct bdiff_hunk*")
     try:
         an = lib.bdiff_splitlines(ac, len(sa), a)
         bn = lib.bdiff_splitlines(bc, len(sb), b)
--- a/mercurial/cffi/mpatch.py	Mon Aug 05 20:47:17 2024 +0200
+++ b/mercurial/cffi/mpatch.py	Mon Aug 05 21:08:36 2024 +0200
@@ -19,8 +19,8 @@
 @ffi.def_extern()
 def cffi_get_next_item(arg, pos):
     all, bins = ffi.from_handle(arg)
-    container = ffi.new(b"struct mpatch_flist*[1]")
-    to_pass = ffi.new(b"char[]", str(bins[pos]))
+    container = ffi.new("struct mpatch_flist*[1]")
+    to_pass = ffi.new("char[]", str(bins[pos]))
     all.append(to_pass)
     r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container)
     if r < 0:
@@ -41,7 +41,7 @@
     if outlen < 0:
         lib.mpatch_lfree(patch)
         raise mpatchError(b"inconsistency detected")
-    buf = ffi.new(b"char[]", outlen)
+    buf = ffi.new("char[]", outlen)
     if lib.mpatch_apply(buf, text, len(text), patch) < 0:
         lib.mpatch_lfree(patch)
         raise mpatchError(b"error applying patches")
--- a/mercurial/cffi/osutil.py	Mon Aug 05 20:47:17 2024 +0200
+++ b/mercurial/cffi/osutil.py	Mon Aug 05 21:08:36 2024 +0200
@@ -39,8 +39,8 @@
             self.st_mtime = st_mtime
             self.st_size = st_size
 
-    tv_sec_ofs = ffi.offsetof(b"struct timespec", b"tv_sec")
-    buf = ffi.new(b"char[]", listdir_batch_size)
+    tv_sec_ofs = ffi.offsetof("struct timespec", "tv_sec")
+    buf = ffi.new("char[]", listdir_batch_size)
 
     def listdirinternal(dfd, req, stat, skip):
         ret = []
@@ -50,16 +50,16 @@
                 break
             if r == -1:
                 raise OSError(ffi.errno, os.strerror(ffi.errno))
-            cur = ffi.cast(b"val_attrs_t*", buf)
+            cur = ffi.cast("val_attrs_t*", buf)
             for i in range(r):
                 lgt = cur.length
-                assert lgt == ffi.cast(b'uint32_t*', cur)[0]
+                assert lgt == ffi.cast('uint32_t*', cur)[0]
                 ofs = cur.name_info.attr_dataoffset
                 str_lgt = cur.name_info.attr_length
-                base_ofs = ffi.offsetof(b'val_attrs_t', b'name_info')
+                base_ofs = ffi.offsetof('val_attrs_t', 'name_info')
                 name = bytes(
                     ffi.buffer(
-                        ffi.cast(b"char*", cur) + base_ofs + ofs, str_lgt - 1
+                        ffi.cast("char*", cur) + base_ofs + ofs, str_lgt - 1
                     )
                 )
                 tp = attrkinds[cur.obj_type]
@@ -84,12 +84,12 @@
                 else:
                     ret.append((name, tp))
                 cur = ffi.cast(
-                    b"val_attrs_t*", int(ffi.cast(b"intptr_t", cur)) + lgt
+                    "val_attrs_t*", int(ffi.cast("intptr_t", cur)) + lgt
                 )
         return ret
 
     def listdir(path, stat=False, skip=None):
-        req = ffi.new(b"struct attrlist*")
+        req = ffi.new("struct attrlist*")
         req.bitmapcount = lib.ATTR_BIT_MAP_COUNT
         req.commonattr = (
             lib.ATTR_CMN_RETURNED_ATTRS