##// END OF EJS Templates
cext: accept arguments as Py_buffer...
Gregory Szorc -
r36673:68026dd7 default
parent child Browse files
Show More
@@ -60,7 +60,8 b' nomem:'
60
60
61 static PyObject *bdiff(PyObject *self, PyObject *args)
61 static PyObject *bdiff(PyObject *self, PyObject *args)
62 {
62 {
63 char *sa, *sb, *rb, *ia, *ib;
63 Py_buffer ba, bb;
64 char *rb, *ia, *ib;
64 PyObject *result = NULL;
65 PyObject *result = NULL;
65 struct bdiff_line *al = NULL, *bl = NULL;
66 struct bdiff_line *al = NULL, *bl = NULL;
66 struct bdiff_hunk l, *h;
67 struct bdiff_hunk l, *h;
@@ -70,25 +71,39 b' static PyObject *bdiff(PyObject *self, P'
70
71
71 l.next = NULL;
72 l.next = NULL;
72
73
73 if (!PyArg_ParseTuple(args, PY23("s#s#:bdiff", "y#y#:bdiff"), &sa, &la,
74 if (!PyArg_ParseTuple(args, PY23("s*s*:bdiff", "y*y*:bdiff"), &ba, &bb))
74 &sb, &lb))
75 return NULL;
75 return NULL;
76
76
77 if (!PyBuffer_IsContiguous(&ba, 'C') || ba.ndim > 1) {
78 PyErr_SetString(PyExc_ValueError, "bdiff input not contiguous");
79 goto cleanup;
80 }
81
82 if (!PyBuffer_IsContiguous(&bb, 'C') || bb.ndim > 1) {
83 PyErr_SetString(PyExc_ValueError, "bdiff input not contiguous");
84 goto cleanup;
85 }
86
87 la = ba.len;
88 lb = bb.len;
89
77 if (la > UINT_MAX || lb > UINT_MAX) {
90 if (la > UINT_MAX || lb > UINT_MAX) {
78 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
91 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
79 return NULL;
92 goto cleanup;
80 }
93 }
81
94
82 _save = PyEval_SaveThread();
95 _save = PyEval_SaveThread();
83
96
84 lmax = la > lb ? lb : la;
97 lmax = la > lb ? lb : la;
85 for (ia = sa, ib = sb; li < lmax && *ia == *ib; ++li, ++ia, ++ib)
98 for (ia = ba.buf, ib = bb.buf; li < lmax && *ia == *ib;
99 ++li, ++ia, ++ib) {
86 if (*ia == '\n')
100 if (*ia == '\n')
87 lcommon = li + 1;
101 lcommon = li + 1;
102 }
88 /* we can almost add: if (li == lmax) lcommon = li; */
103 /* we can almost add: if (li == lmax) lcommon = li; */
89
104
90 an = bdiff_splitlines(sa + lcommon, la - lcommon, &al);
105 an = bdiff_splitlines(ba.buf + lcommon, la - lcommon, &al);
91 bn = bdiff_splitlines(sb + lcommon, lb - lcommon, &bl);
106 bn = bdiff_splitlines(bb.buf + lcommon, lb - lcommon, &bl);
92 if (!al || !bl) {
107 if (!al || !bl) {
93 PyErr_NoMemory();
108 PyErr_NoMemory();
94 goto cleanup;
109 goto cleanup;
@@ -137,6 +152,8 b' static PyObject *bdiff(PyObject *self, P'
137 cleanup:
152 cleanup:
138 if (_save)
153 if (_save)
139 PyEval_RestoreThread(_save);
154 PyEval_RestoreThread(_save);
155 PyBuffer_Release(&ba);
156 PyBuffer_Release(&bb);
140 if (al) {
157 if (al) {
141 free(al);
158 free(al);
142 }
159 }
@@ -30,17 +30,9 b' blocks = bdiff.blocks'
30 fixws = bdiff.fixws
30 fixws = bdiff.fixws
31 patches = mpatch.patches
31 patches = mpatch.patches
32 patchedsize = mpatch.patchedsize
32 patchedsize = mpatch.patchedsize
33 _textdiff = bdiff.bdiff
33 textdiff = bdiff.bdiff
34 splitnewlines = bdiff.splitnewlines
34 splitnewlines = bdiff.splitnewlines
35
35
36 # On Python 3, util.buffer() creates a memoryview, which appears not
37 # supporting the buffer protocol
38 if pycompat.ispy3:
39 def textdiff(a, b):
40 return _textdiff(bytes(a), bytes(b))
41 else:
42 textdiff = _textdiff
43
44 class diffopts(object):
36 class diffopts(object):
45 '''context is the number of context lines
37 '''context is the number of context lines
46 text treats all files as text
38 text treats all files as text
General Comments 0
You need to be logged in to leave comments. Login now