/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_patiencediff_c.c

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 07:18:33 UTC
  • mfrom: (7141.3.3 fix1799482)
  • Revision ID: breezy.the.bot@gmail.com-20181116071833-e01b0833f3hkc3et
Report correct paths when running "brz add" in git repositories.

Merged from https://code.launchpad.net/~jelmer/brz/fix1799482/+merge/357734

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#define SENTINEL -1
47
47
 
48
48
 
49
 
/* malloc returns NULL on some platforms if you try to allocate nothing,
50
 
 * causing <https://bugs.edge.launchpad.net/bzr/+bug/511267> and
51
 
 * <https://bugs.edge.launchpad.net/bzr/+bug/331095>.  On glibc it passes, but
52
 
 * let's make it fail to aid testing. */
53
 
#define guarded_malloc(x) ( (x) ? malloc(x) : NULL )
54
 
 
55
49
enum {
56
50
    OP_EQUAL = 0,
57
51
    OP_INSERT,
151
145
compare_lines(struct line *a, struct line *b)
152
146
{
153
147
    return ((a->hash != b->hash)
154
 
            || PyObject_Compare(a->data, b->data));
 
148
            || PyObject_RichCompareBool(a->data, b->data, Py_EQ) == 0);
155
149
}
156
150
 
157
151
 
190
184
        hsize *= 2;
191
185
 
192
186
    /* can't be 0 */
193
 
    hashtable = (struct bucket *) guarded_malloc(sizeof(struct bucket) * hsize);
 
187
    hashtable = (struct bucket *)malloc(sizeof(struct bucket) * hsize);
194
188
    if (hashtable == NULL) {
195
189
        PyErr_NoMemory();
196
190
        return 0;
467
461
    last_a_pos = alo - 1;
468
462
    last_b_pos = blo - 1;
469
463
 
470
 
    lcs = (struct matching_line *)guarded_malloc(sizeof(struct matching_line) * (bhi - blo));
 
464
    lcs = (struct matching_line *)malloc(sizeof(struct matching_line) * (bhi - blo));
471
465
    if (lcs == NULL)
472
466
        return 0;
473
467
 
628
622
        goto error;
629
623
 
630
624
    if (bsize > 0) {
631
 
        matches = (struct matching_line *)guarded_malloc(sizeof(struct matching_line) * bsize);
 
625
        matches = (struct matching_line *)malloc(sizeof(struct matching_line) * bsize);
632
626
        if (matches == NULL)
633
627
            goto error;
634
628
 
635
 
        backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * bsize * 4);
 
629
        backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * bsize * 4);
636
630
        if (backpointers == NULL)
637
631
            goto error;
638
632
    }
641
635
 
642
636
    res = PyList_New(nmatches);
643
637
    for (i = 0; i < nmatches; i++) {
644
 
#if PY_VERSION_HEX < 0x02050000
645
 
        item = Py_BuildValue("ii", matches[nmatches - i - 1].a, matches[nmatches - i - 1].b);
646
 
#else
647
638
        item = Py_BuildValue("nn", matches[nmatches - i - 1].a, matches[nmatches - i - 1].b);
648
 
#endif
649
639
        if (item == NULL)
650
640
            goto error;
651
641
        if (PyList_SetItem(res, i, item) != 0)
680
670
    struct hashtable hashtable;
681
671
    struct matching_blocks matches;
682
672
 
683
 
#if PY_VERSION_HEX < 0x02050000
684
 
    if (!PyArg_ParseTuple(args, "OOiiiiOi", &aseq, &bseq, &alo, &blo,
685
 
                          &ahi, &bhi, &answer, &maxrecursion))
686
 
#else
687
673
    if (!PyArg_ParseTuple(args, "OOnnnnOi", &aseq, &bseq, &alo, &blo,
688
674
                          &ahi, &bhi, &answer, &maxrecursion))
689
 
#endif
690
675
        return NULL;
691
676
 
692
677
    hashtable.table = NULL;
703
688
    matches.count = 0;
704
689
 
705
690
    if (bsize > 0) {
706
 
        matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * bsize);
 
691
        matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * bsize);
707
692
        if (matches.matches == NULL)
708
693
            goto error;
709
694
 
710
 
        backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * bsize * 4);
 
695
        backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * bsize * 4);
711
696
        if (backpointers == NULL)
712
697
            goto error;
713
698
    } else {
722
707
 
723
708
    for (i = 0; i < matches.count; i++) {
724
709
        for (j = 0; j < matches.matches[i].len; j++) {
725
 
#if PY_VERSION_HEX < 0x02050000
726
 
            item = Py_BuildValue("ii", matches.matches[i].a + j,
727
 
                                 matches.matches[i].b + j);
728
 
#else
729
710
            item = Py_BuildValue("nn", matches.matches[i].a + j,
730
711
                                 matches.matches[i].b + j);
731
 
#endif
732
712
            if (item == NULL)
733
713
                goto error;
734
714
            if (PyList_Append(answer, item) != 0)
781
761
        }
782
762
 
783
763
        if (self->bsize > 0) {
784
 
            self->backpointers = (Py_ssize_t *)guarded_malloc(sizeof(Py_ssize_t) * self->bsize * 4);
 
764
            self->backpointers = (Py_ssize_t *)malloc(sizeof(Py_ssize_t) * self->bsize * 4);
785
765
            if (self->backpointers == NULL) {
786
766
                Py_DECREF(self);
787
767
                PyErr_NoMemory();
804
784
    free(self->hashtable.table);
805
785
    delete_lines(self->b, self->bsize);
806
786
    delete_lines(self->a, self->asize);
807
 
    self->ob_type->tp_free((PyObject *)self);
 
787
    ((PyObject *)self)->ob_type->tp_free((PyObject *)self);
808
788
}
809
789
 
810
790
 
833
813
    matches.count = 0;
834
814
    if (self->bsize > 0) {
835
815
        matches.matches = (struct matching_block *)
836
 
            guarded_malloc(sizeof(struct matching_block) * self->bsize);
 
816
            malloc(sizeof(struct matching_block) * self->bsize);
837
817
        if (matches.matches == NULL)
838
818
            return PyErr_NoMemory();
839
819
    } else
854
834
    }
855
835
 
856
836
    for (i = 0; i < matches.count; i++) {
857
 
#if PY_VERSION_HEX < 0x02050000
858
 
        item = Py_BuildValue("iii", matches.matches[i].a,
859
 
                             matches.matches[i].b, matches.matches[i].len);
860
 
#else
861
837
        item = Py_BuildValue("nnn", matches.matches[i].a,
862
838
                             matches.matches[i].b, matches.matches[i].len);
863
 
#endif
864
839
        if (item == NULL)
865
840
            goto error;
866
841
        if (PyList_SetItem(answer, i, item) != 0)
867
842
            goto error;
868
843
    }
869
 
#if PY_VERSION_HEX < 0x02050000
870
 
    item = Py_BuildValue("iii", self->asize, self->bsize, 0);
871
 
#else
872
844
    item = Py_BuildValue("nnn", self->asize, self->bsize, 0);
873
 
#endif
874
845
    if (item == NULL)
875
846
        goto error;
876
847
    if (PyList_SetItem(answer, i, item) != 0)
923
894
    struct matching_blocks matches;
924
895
 
925
896
    matches.count = 0;
926
 
    matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * (self->bsize + 1));
 
897
    matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * (self->bsize + 1));
927
898
    if (matches.matches == NULL)
928
899
        return PyErr_NoMemory();
929
900
 
960
931
            tag = OP_INSERT;
961
932
 
962
933
        if (tag != -1) {
963
 
#if PY_VERSION_HEX < 0x02050000
964
 
            item = Py_BuildValue("siiii", opcode_names[tag], i, ai, j, bj);
965
 
#else
966
934
            item = Py_BuildValue("snnnn", opcode_names[tag], i, ai, j, bj);
967
 
#endif
968
935
            if (item == NULL)
969
936
                goto error;
970
937
            if (PyList_Append(answer, item) != 0)
975
942
        j = bj + matches.matches[k].len;
976
943
 
977
944
        if (matches.matches[k].len > 0) {
978
 
#if PY_VERSION_HEX < 0x02050000
979
 
            item = Py_BuildValue("siiii", opcode_names[OP_EQUAL], ai, i, bj, j);
980
 
#else
981
945
            item = Py_BuildValue("snnnn", opcode_names[OP_EQUAL], ai, i, bj, j);
982
 
#endif
983
946
            if (item == NULL)
984
947
                goto error;
985
948
            if (PyList_Append(answer, item) != 0)
1036
999
        return NULL;
1037
1000
 
1038
1001
    matches.count = 0;
1039
 
    matches.matches = (struct matching_block *)guarded_malloc(sizeof(struct matching_block) * (self->bsize + 1));
 
1002
    matches.matches = (struct matching_block *)malloc(sizeof(struct matching_block) * (self->bsize + 1));
1040
1003
    if (matches.matches == NULL)
1041
1004
        return PyErr_NoMemory();
1042
1005
 
1054
1017
    matches.count++;
1055
1018
 
1056
1019
    ncodes = 0;
1057
 
    codes = (struct opcode *)guarded_malloc(sizeof(struct opcode) * matches.count * 2);
 
1020
    codes = (struct opcode *)malloc(sizeof(struct opcode) * matches.count * 2);
1058
1021
    if (codes == NULL) {
1059
1022
        free(matches.matches);
1060
1023
        return PyErr_NoMemory();
1137
1100
        /* end the current group and start a new one whenever
1138
1101
           there is a large range with no changes. */
1139
1102
        if (tag == OP_EQUAL && i2 - i1 > nn) {
1140
 
#if PY_VERSION_HEX < 0x02050000
1141
 
            item = Py_BuildValue("siiii", opcode_names[tag],
1142
 
                                  i1, MIN(i2, i1 + n), j1, MIN(j2, j1 + n));
1143
 
#else
1144
1103
            item = Py_BuildValue("snnnn", opcode_names[tag],
1145
1104
                                  i1, MIN(i2, i1 + n), j1, MIN(j2, j1 + n));
1146
 
#endif
1147
1105
            if (item == NULL)
1148
1106
                goto error;
1149
1107
            if (PyList_Append(group, item) != 0)
1156
1114
            i1 = MAX(i1, i2 - n);
1157
1115
            j1 = MAX(j1, j2 - n);
1158
1116
        }
1159
 
#if PY_VERSION_HEX < 0x02050000
1160
 
        item = Py_BuildValue("siiii", opcode_names[tag], i1, i2, j1 ,j2);
1161
 
#else
1162
1117
        item = Py_BuildValue("snnnn", opcode_names[tag], i1, i2, j1 ,j2);
1163
 
#endif
1164
1118
        if (item == NULL)
1165
1119
            goto error;
1166
1120
        if (PyList_Append(group, item) != 0)
1210
1164
 
1211
1165
static PyTypeObject PatienceSequenceMatcherType = {
1212
1166
    PyObject_HEAD_INIT(NULL)
1213
 
    0,                                           /* ob_size */
1214
 
    "PatienceSequenceMatcher",                   /* tp_name */
1215
 
    sizeof(PatienceSequenceMatcher),             /* tp_basicsize */
1216
 
    0,                                           /* tp_itemsize */
1217
 
    (destructor)PatienceSequenceMatcher_dealloc, /* tp_dealloc */
1218
 
    0,                                           /* tp_print */
1219
 
    0,                                           /* tp_getattr */
1220
 
    0,                                           /* tp_setattr */
1221
 
    0,                                           /* tp_compare */
1222
 
    0,                                           /* tp_repr */
1223
 
    0,                                           /* tp_as_number */
1224
 
    0,                                           /* tp_as_sequence */
1225
 
    0,                                           /* tp_as_mapping */
1226
 
    0,                                           /* tp_hash */
1227
 
    0,                                           /* tp_call */
1228
 
    0,                                           /* tp_str */
1229
 
    0,                                           /* tp_getattro */
1230
 
    0,                                           /* tp_setattro */
1231
 
    0,                                           /* tp_as_buffer */
1232
 
    Py_TPFLAGS_DEFAULT,                          /* tp_flags*/
1233
 
    PatienceSequenceMatcher_doc,                 /* tp_doc */
1234
 
    0,                                           /* tp_traverse */
1235
 
    0,                                           /* tp_clear */
1236
 
    0,                                           /* tp_richcompare */
1237
 
    0,                                           /* tp_weaklistoffset */
1238
 
    0,                                           /* tp_iter */
1239
 
    0,                                           /* tp_iternext */
1240
 
    PatienceSequenceMatcher_methods,             /* tp_methods */
1241
 
    0,                                           /* tp_members */
1242
 
    0,                                           /* tp_getset */
1243
 
    0,                                           /* tp_base */
1244
 
    0,                                           /* tp_dict */
1245
 
    0,                                           /* tp_descr_get */
1246
 
    0,                                           /* tp_descr_set */
1247
 
    0,                                           /* tp_dictoffset */
1248
 
    0,                                           /* tp_init */
1249
 
    0,                                           /* tp_alloc */
1250
 
    PatienceSequenceMatcher_new,                 /* tp_new */
 
1167
    .tp_name = "PatienceSequenceMatcher",
 
1168
    .tp_basicsize = sizeof(PatienceSequenceMatcher),
 
1169
    .tp_dealloc = (destructor)PatienceSequenceMatcher_dealloc,
 
1170
    .tp_flags = Py_TPFLAGS_DEFAULT,
 
1171
    .tp_doc = PatienceSequenceMatcher_doc,
 
1172
    .tp_methods = PatienceSequenceMatcher_methods,
 
1173
    .tp_new = PatienceSequenceMatcher_new,
1251
1174
};
1252
1175
 
1253
1176
 
1257
1180
    {NULL, NULL}
1258
1181
};
1259
1182
 
1260
 
 
1261
 
PyMODINIT_FUNC
1262
 
init_patiencediff_c(void)
 
1183
PYMOD_INIT_FUNC(_patiencediff_c)
1263
1184
{
1264
1185
    PyObject* m;
1265
1186
 
1266
1187
    if (PyType_Ready(&PatienceSequenceMatcherType) < 0)
1267
 
        return;
1268
 
 
1269
 
    m = Py_InitModule3("_patiencediff_c", cpatiencediff_methods,
1270
 
                       "C implementation of PatienceSequenceMatcher");
1271
 
    if (m == NULL)
1272
 
      return;
1273
 
 
 
1188
        return PYMOD_ERROR;
 
1189
 
 
1190
    PYMOD_CREATE(m, "_patiencediff_c",
 
1191
                 "C implementation of PatienceSequenceMatcher",
 
1192
                 cpatiencediff_methods);
 
1193
    if (m == NULL) {
 
1194
        return PYMOD_ERROR;
 
1195
    }
1274
1196
    Py_INCREF(&PatienceSequenceMatcherType);
1275
1197
    PyModule_AddObject(m, "PatienceSequenceMatcher_c",
1276
1198
                       (PyObject *)&PatienceSequenceMatcherType);
 
1199
    return PYMOD_SUCCESS(m);
1277
1200
}
1278
1201
 
1279
1202