mercurial/diffhelpers.c
author Gregory Szorc <gregory.szorc@gmail.com>
Tue, 23 Jun 2015 22:20:08 -0700
changeset 25660 328739ea70c3
parent 23964 f1c127df7c4f
child 32357 2f51f4c535d2
permissions -rw-r--r--
global: mass rewrite to use modern exception syntax Python 2.6 introduced the "except type as instance" syntax, replacing the "except type, instance" syntax that came before. Python 3 dropped support for the latter syntax. Since we no longer support Python 2.4 or 2.5, we have no need to continue supporting the "except type, instance". This patch mass rewrites the exception syntax to be Python 2.6+ and Python 3 compatible. This patch was produced by running `2to3 -f except -w -n .`.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
/*
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
 * diffhelpers.c - helper routines for mpatch
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
 *
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
 * Copyright 2007 Chris Mason <chris.mason@oracle.com>
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
 *
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
 * This software may be used and distributed according to the terms
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
 * of the GNU General Public License v2, incorporated herein by reference.
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
 */
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
#include <Python.h>
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
#include <stdlib.h>
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
#include <string.h>
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    14
#include "util.h"
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    15
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
static char diffhelpers_doc[] = "Efficient diff parsing";
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
static PyObject *diffhelpers_Error;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    20
/* fixup the last lines of a and b when the patch has no newline at eof */
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    21
static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    22
{
16693
f1aa3010642f diffhelpers: use Py_ssize_t in _fix_newline()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16651
diff changeset
    23
	Py_ssize_t hunksz = PyList_Size(hunk);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
	PyObject *s = PyList_GET_ITEM(hunk, hunksz-1);
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    25
	char *l = PyBytes_AsString(s);
16693
f1aa3010642f diffhelpers: use Py_ssize_t in _fix_newline()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16651
diff changeset
    26
	Py_ssize_t alen = PyList_Size(a);
f1aa3010642f diffhelpers: use Py_ssize_t in _fix_newline()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16651
diff changeset
    27
	Py_ssize_t blen = PyList_Size(b);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
	char c = l[0];
10146
9c59cdafcc24 diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents: 10129
diff changeset
    29
	PyObject *hline;
16693
f1aa3010642f diffhelpers: use Py_ssize_t in _fix_newline()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16651
diff changeset
    30
	Py_ssize_t sz = PyBytes_GET_SIZE(s);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    31
10146
9c59cdafcc24 diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents: 10129
diff changeset
    32
	if (sz > 1 && l[sz-2] == '\r')
9c59cdafcc24 diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents: 10129
diff changeset
    33
		/* tolerate CRLF in last line */
9c59cdafcc24 diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents: 10129
diff changeset
    34
		sz -= 1;
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    35
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    36
	hline = PyBytes_FromStringAndSize(l, sz-1);
23959
c8e7fa41bfc5 diffhelpers: verify hline was created before using it
Augie Fackler <augie@google.com>
parents: 16695
diff changeset
    37
	if (!hline) {
23964
f1c127df7c4f diffhelpers: fix botched return statement from c8e7fa41bfc5
Augie Fackler <raf@durin42.com>
parents: 23959
diff changeset
    38
		return;
23959
c8e7fa41bfc5 diffhelpers: verify hline was created before using it
Augie Fackler <augie@google.com>
parents: 16695
diff changeset
    39
	}
10146
9c59cdafcc24 diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents: 10129
diff changeset
    40
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    41
	if (c == ' ' || c == '+') {
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    42
		PyObject *rline = PyBytes_FromStringAndSize(l + 1, sz - 2);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    43
		PyList_SetItem(b, blen-1, rline);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    44
	}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    45
	if (c == ' ' || c == '-') {
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    46
		Py_INCREF(hline);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
		PyList_SetItem(a, alen-1, hline);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    48
	}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    49
	PyList_SetItem(hunk, hunksz-1, hline);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    51
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    52
/* python callable form of _fix_newline */
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
static PyObject *
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    54
fix_newline(PyObject *self, PyObject *args)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
{
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
	PyObject *hunk, *a, *b;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
	if (!PyArg_ParseTuple(args, "OOO", &hunk, &a, &b))
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
		return NULL;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
	_fix_newline(hunk, a, b);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
	return Py_BuildValue("l", 0);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    61
}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    62
16694
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    63
#if (PY_VERSION_HEX < 0x02050000)
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    64
static const char *addlines_format = "OOiiOO";
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    65
#else
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    66
static const char *addlines_format = "OOnnOO";
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    67
#endif
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    68
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    69
/*
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    70
 * read lines from fp into the hunk.  The hunk is parsed into two arrays
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    71
 * a and b.  a gets the old state of the text, b gets the new state
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    72
 * The control char from the hunk is saved when inserting into a, but not b
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    73
 * (for performance while deleting files)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    74
 */
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    75
static PyObject *
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    76
addlines(PyObject *self, PyObject *args)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    77
{
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    78
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    79
	PyObject *fp, *hunk, *a, *b, *x;
16694
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    80
	Py_ssize_t i;
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    81
	Py_ssize_t lena, lenb;
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    82
	Py_ssize_t num;
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    83
	Py_ssize_t todoa, todob;
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    84
	char *s, c;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    85
	PyObject *l;
16694
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    86
	if (!PyArg_ParseTuple(args, addlines_format,
797b76205408 diffhelpers: use Py_ssize_t in addlines()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16693
diff changeset
    87
			      &fp, &hunk, &lena, &lenb, &a, &b))
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    88
		return NULL;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    89
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10146
diff changeset
    90
	while (1) {
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    91
		todoa = lena - PyList_Size(a);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    92
		todob = lenb - PyList_Size(b);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    93
		num = todoa > todob ? todoa : todob;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    94
		if (num == 0)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    95
		    break;
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10146
diff changeset
    96
		for (i = 0; i < num; i++) {
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    97
			x = PyFile_GetLine(fp, 0);
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    98
			s = PyBytes_AsString(x);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    99
			c = *s;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   100
			if (strcmp(s, "\\ No newline at end of file\n") == 0) {
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   101
				_fix_newline(hunk, a, b);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   102
				continue;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
			}
5483
ec2cc1dadbf7 Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents: 4897
diff changeset
   104
			if (c == '\n') {
ec2cc1dadbf7 Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents: 4897
diff changeset
   105
				/* Some patches may be missing the control char
ec2cc1dadbf7 Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents: 4897
diff changeset
   106
				 * on empty lines. Supply a leading space. */
ec2cc1dadbf7 Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents: 4897
diff changeset
   107
				Py_DECREF(x);
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   108
				x = PyBytes_FromString(" \n");
5483
ec2cc1dadbf7 Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents: 4897
diff changeset
   109
			}
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   110
			PyList_Append(hunk, x);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   111
			if (c == '+') {
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   112
				l = PyBytes_FromString(s + 1);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   113
				PyList_Append(b, l);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   114
				Py_DECREF(l);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   115
			} else if (c == '-') {
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   116
				PyList_Append(a, x);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   117
			} else {
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   118
				l = PyBytes_FromString(s + 1);
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   119
				PyList_Append(b, l);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   120
				Py_DECREF(l);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   121
				PyList_Append(a, x);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   122
			}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   123
			Py_DECREF(x);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   124
		}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   125
	}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   126
	return Py_BuildValue("l", 0);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   127
}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   128
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   129
/*
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   130
 * compare the lines in a with the lines in b.  a is assumed to have
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   131
 * a control char at the start of each line, this char is ignored in the
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   132
 * compare
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   133
 */
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   134
static PyObject *
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   135
testhunk(PyObject *self, PyObject *args)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   136
{
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   137
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   138
	PyObject *a, *b;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   139
	long bstart;
16695
0a0933d3d59c diffhelpers: use Py_ssize_t in testhunk()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16694
diff changeset
   140
	Py_ssize_t alen, blen;
0a0933d3d59c diffhelpers: use Py_ssize_t in testhunk()
Adrian Buehlmann <adrian@cadifra.com>
parents: 16694
diff changeset
   141
	Py_ssize_t i;
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   142
	char *sa, *sb;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   143
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   144
	if (!PyArg_ParseTuple(args, "OOl", &a, &b, &bstart))
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   145
		return NULL;
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   146
	alen = PyList_Size(a);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   147
	blen = PyList_Size(b);
16651
9e40bc4c1bde diffhelpers: harden testhunk
Matt Mackall <mpm@selenic.com>
parents: 13302
diff changeset
   148
	if (alen > blen - bstart || bstart < 0) {
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   149
		return Py_BuildValue("l", -1);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   150
	}
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10146
diff changeset
   151
	for (i = 0; i < alen; i++) {
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   152
		sa = PyBytes_AsString(PyList_GET_ITEM(a, i));
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   153
		sb = PyBytes_AsString(PyList_GET_ITEM(b, i + bstart));
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10146
diff changeset
   154
		if (strcmp(sa + 1, sb) != 0)
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   155
			return Py_BuildValue("l", -1);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   156
	}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   157
	return Py_BuildValue("l", 0);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   158
}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   159
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   160
static PyMethodDef methods[] = {
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   161
	{"addlines", addlines, METH_VARARGS, "add lines to a hunk\n"},
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   162
	{"fix_newline", fix_newline, METH_VARARGS, "fixup newline counters\n"},
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   163
	{"testhunk", testhunk, METH_VARARGS, "test lines in a hunk\n"},
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   164
	{NULL, NULL}
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   165
};
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   166
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   167
#ifdef IS_PY3K
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   168
static struct PyModuleDef diffhelpers_module = {
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   169
	PyModuleDef_HEAD_INIT,
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   170
	"diffhelpers",
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   171
	diffhelpers_doc,
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   172
	-1,
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   173
	methods
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   174
};
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   175
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   176
PyMODINIT_FUNC PyInit_diffhelpers(void)
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   177
{
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   178
	PyObject *m;
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   179
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   180
	m = PyModule_Create(&diffhelpers_module);
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   181
	if (m == NULL)
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   182
		return NULL;
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   183
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   184
	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   185
											NULL, NULL);
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   186
	Py_INCREF(diffhelpers_Error);
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   187
	PyModule_AddObject(m, "diffhelpersError", diffhelpers_Error);
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   188
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   189
	return m;
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   190
}
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   191
#else
4897
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   192
PyMODINIT_FUNC
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   193
initdiffhelpers(void)
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   194
{
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   195
	Py_InitModule3("diffhelpers", methods, diffhelpers_doc);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   196
	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   197
	                                        NULL, NULL);
4574925db5c0 Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   198
}
11363
f50103035c38 diffhelpers.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   199
#endif