Mercurial > hg-stable
changeset 32546:25b37900d6e0
cffi: remove superfluous "if True" blocks
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Tue, 02 May 2017 21:45:10 +0900 |
parents | 0e8b0b9a7acc |
children | 3f5d675fddf4 |
files | mercurial/cffi/bdiff.py mercurial/cffi/mpatch.py |
diffstat | 2 files changed, 88 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/cffi/bdiff.py Tue May 02 21:15:31 2017 +0900 +++ b/mercurial/cffi/bdiff.py Tue May 02 21:45:10 2017 +0900 @@ -15,64 +15,62 @@ ffi = _bdiff.ffi lib = _bdiff.lib -if True: - if True: - def blocks(sa, sb): - 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) - if not a[0] or not b[0]: - raise MemoryError - count = lib.bdiff_diff(a[0], an, b[0], bn, l) - if count < 0: - raise MemoryError - rl = [None] * count - h = l.next - i = 0 - while h: - rl[i] = (h.a1, h.a2, h.b1, h.b2) - h = h.next - i += 1 - finally: - lib.free(a[0]) - lib.free(b[0]) - lib.bdiff_freehunks(l.next) - return rl +def blocks(sa, sb): + 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) + if not a[0] or not b[0]: + raise MemoryError + count = lib.bdiff_diff(a[0], an, b[0], bn, l) + if count < 0: + raise MemoryError + rl = [None] * count + h = l.next + i = 0 + while h: + rl[i] = (h.a1, h.a2, h.b1, h.b2) + h = h.next + i += 1 + finally: + lib.free(a[0]) + lib.free(b[0]) + lib.bdiff_freehunks(l.next) + return rl - def bdiff(sa, sb): - 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) - if not a[0] or not b[0]: - raise MemoryError - count = lib.bdiff_diff(a[0], an, b[0], bn, l) - if count < 0: - raise MemoryError - rl = [] - h = l.next - la = lb = 0 - while h: - if h.a1 != la or h.b1 != lb: - lgt = (b[0] + h.b1).l - (b[0] + lb).l - rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, - (a[0] + h.a1).l - a[0].l, lgt)) - rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) - la = h.a2 - lb = h.b2 - h = h.next +def bdiff(sa, sb): + 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) + if not a[0] or not b[0]: + raise MemoryError + count = lib.bdiff_diff(a[0], an, b[0], bn, l) + if count < 0: + raise MemoryError + rl = [] + h = l.next + la = lb = 0 + while h: + if h.a1 != la or h.b1 != lb: + lgt = (b[0] + h.b1).l - (b[0] + lb).l + rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, + (a[0] + h.a1).l - a[0].l, lgt)) + rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) + la = h.a2 + lb = h.b2 + h = h.next - finally: - lib.free(a[0]) - lib.free(b[0]) - lib.bdiff_freehunks(l.next) - return "".join(rl) + finally: + lib.free(a[0]) + lib.free(b[0]) + lib.bdiff_freehunks(l.next) + return "".join(rl)
--- a/mercurial/cffi/mpatch.py Tue May 02 21:15:31 2017 +0900 +++ b/mercurial/cffi/mpatch.py Tue May 02 21:45:10 2017 +0900 @@ -14,37 +14,35 @@ ffi = _mpatch.ffi lib = _mpatch.lib -if True: - if True: - @ffi.def_extern() - 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])) - all.append(to_pass) - r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) - if r < 0: - return ffi.NULL - return container[0] +@ffi.def_extern() +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])) + all.append(to_pass) + r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container) + if r < 0: + return ffi.NULL + return container[0] - def patches(text, bins): - lgt = len(bins) - all = [] - if not lgt: - return text - arg = (all, bins) - patch = lib.mpatch_fold(ffi.new_handle(arg), - lib.cffi_get_next_item, 0, lgt) - if not patch: - raise mpatchError("cannot decode chunk") - outlen = lib.mpatch_calcsize(len(text), patch) - if outlen < 0: - lib.mpatch_lfree(patch) - raise mpatchError("inconsistency detected") - buf = ffi.new("char[]", outlen) - if lib.mpatch_apply(buf, text, len(text), patch) < 0: - lib.mpatch_lfree(patch) - raise mpatchError("error applying patches") - res = ffi.buffer(buf, outlen)[:] - lib.mpatch_lfree(patch) - return res +def patches(text, bins): + lgt = len(bins) + all = [] + if not lgt: + return text + arg = (all, bins) + patch = lib.mpatch_fold(ffi.new_handle(arg), + lib.cffi_get_next_item, 0, lgt) + if not patch: + raise mpatchError("cannot decode chunk") + outlen = lib.mpatch_calcsize(len(text), patch) + if outlen < 0: + lib.mpatch_lfree(patch) + raise mpatchError("inconsistency detected") + buf = ffi.new("char[]", outlen) + if lib.mpatch_apply(buf, text, len(text), patch) < 0: + lib.mpatch_lfree(patch) + raise mpatchError("error applying patches") + res = ffi.buffer(buf, outlen)[:] + lib.mpatch_lfree(patch) + return res