[PATCH 4 of 4 v2] mpatch: implement cffi version of mpatch

Yuya Nishihara yuya at tcha.org
Mon Jun 27 10:05:45 EDT 2016


On Thu, 23 Jun 2016 08:57:11 +0200, Maciej Fijalkowski wrote:
> # HG changeset patch
> # User Maciej Fijalkowski <fijall at gmail.com>
> # Date 1466664974 -7200
> #      Thu Jun 23 08:56:14 2016 +0200
> # Node ID ec1da6e456500f9ccc6707eaf4ca4b0b9210a3de
> # Parent  42e8ffc4adb694a2a0a4787e129aa4e7d0296631
> mpatch: implement cffi version of mpatch

> +ffi.set_source("_mpatch_cffi", open(mpatch_c).read(),
> +               include_dirs=["mercurial"])

Maybe we'd better put the _mpatch_cffi module under mercurial package?

> -struct flist* cpy_get_next_item(void* bins, ssize_t pos)
> +struct flist *cpy_get_item(void *bins, ssize_t pos)

Nit: this can be a local function. And this hunk should belong to the patch 2.

> +        def patches(text, bins):
> +            lgt = len(bins)
> +            all = []
> +            if not lgt:
> +                return text
> +            arg = (all, bins)
> +            patch = lib.fold(ffi.new_handle(arg), lib.cffi_get_next_item, 0,
> +                             lgt)
> +            if not patch:
> +                raise mpatchError("cannot decode chunk")
> +            outlen = lib.calcsize(len(text), patch)
> +            if outlen < 0:
> +                lib.lfree(patch)
> +                raise mpatchError("inconsistency detected")
> +            buf = ffi.new("char[]", outlen)
> +            if lib.apply(buf, text, len(text), patch) < 0:
> +                lib.lfree(patch)
> +                raise mpatchError("error applying patches")
> +            res = ffi.buffer(buf, outlen)[:]
> +            lib.lfree(patch)

I don't know how CFFI works, but if we can use try-finally, I'll do

    if not patch:
        raise mpatchError("cannot decode chunk")
    try:
        ...
    finally:
        lib.lfree(patch)


More information about the Mercurial-devel mailing list