mercurial/cffi/mpatch.py
author Martin von Zweigbergk <martinvonz@google.com>
Fri, 09 Sep 2022 12:45:26 -0700
changeset 49495 59a72267f5ce
parent 48875 6000f5b25c9b
child 49599 94a797032fc4
permissions -rw-r--r--
fsmonitor: migrate Python ABCs from collections to collections.abc The Collections Abstract Base Classes in the collections module are deprecated since Python 3.3 in favor of collections.abc, and removed in Python 3.10.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32512
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
     1
# mpatch.py - CFFI implementation of mpatch.c
7699
fac054f84600 pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
     2
#
32512
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
     3
# Copyright 2016 Maciej Fijalkowski <fijall@gmail.com>
7699
fac054f84600 pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7775
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 8225
diff changeset
     6
# GNU General Public License version 2 or any later version.
7699
fac054f84600 pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
     7
27337
9a17576103a4 mpatch: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 16683
diff changeset
     8
32512
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
     9
from ..pure.mpatch import *
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
    10
from ..pure.mpatch import mpatchError  # silence pyflakes
46785
521ac0d7047f typing: disable import error warnings that are already handled
Matt Harbison <matt_harbison@yahoo.com>
parents: 43077
diff changeset
    11
from . import _mpatch  # pytype: disable=import-error
7699
fac054f84600 pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    12
32512
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
    13
ffi = _mpatch.ffi
0e8b0b9a7acc cffi: split modules from pure
Yuya Nishihara <yuya@tcha.org>
parents: 32506
diff changeset
    14
lib = _mpatch.lib
7699
fac054f84600 pure Python implementation of mpatch.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff changeset
    15
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 33572
diff changeset
    16
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    17
@ffi.def_extern()
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    18
def cffi_get_next_item(arg, pos):
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    19
    all, bins = ffi.from_handle(arg)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    20
    container = ffi.new(b"struct mpatch_flist*[1]")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    21
    to_pass = ffi.new(b"char[]", str(bins[pos]))
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    22
    all.append(to_pass)
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    23
    r = lib.mpatch_decode(to_pass, len(to_pass) - 1, container)
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    24
    if r < 0:
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    25
        return ffi.NULL
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    26
    return container[0]
29695
f2846d546645 mpatch: write a cffi version of mpatch.patches
Maciej Fijalkowski <fijall@gmail.com>
parents: 28861
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 33572
diff changeset
    28
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    29
def patches(text, bins):
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    30
    lgt = len(bins)
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    31
    all = []
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    32
    if not lgt:
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    33
        return text
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    34
    arg = (all, bins)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 33572
diff changeset
    35
    patch = lib.mpatch_fold(ffi.new_handle(arg), lib.cffi_get_next_item, 0, lgt)
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    36
    if not patch:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    37
        raise mpatchError(b"cannot decode chunk")
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    38
    outlen = lib.mpatch_calcsize(len(text), patch)
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    39
    if outlen < 0:
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    40
        lib.mpatch_lfree(patch)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
        raise mpatchError(b"inconsistency detected")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    42
    buf = ffi.new(b"char[]", outlen)
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    43
    if lib.mpatch_apply(buf, text, len(text), patch) < 0:
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    44
        lib.mpatch_lfree(patch)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
        raise mpatchError(b"error applying patches")
32513
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    46
    res = ffi.buffer(buf, outlen)[:]
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    47
    lib.mpatch_lfree(patch)
25b37900d6e0 cffi: remove superfluous "if True" blocks
Yuya Nishihara <yuya@tcha.org>
parents: 32512
diff changeset
    48
    return res