~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_patiencediff_c.c

  • Committer: John Arbash Meinel
  • Date: 2007-12-04 16:28:28 UTC
  • mto: This revision was merged to the branch mainline in revision 3131.
  • Revision ID: john@arbash-meinel.com-20071204162828-chfnl5ylzkh0y2ll
Enable some error checking, and small amount of code cleanup.
If PyObject_Hash() or PyObject_Length() fails, we should raise an exception, not
silently swallow it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
546
546
static Py_ssize_t
547
547
load_lines(PyObject *orig, struct line **lines)
548
548
{
549
 
    Py_ssize_t size, i, j, tuple_len;
550
 
    Py_ssize_t total_len;
551
 
    // int h;
552
 
    // char *p;
 
549
    Py_ssize_t size, i, j, tuple_len, cur_len, total_len;
553
550
    struct line *line;
554
 
    PyObject *seq, *item;
 
551
    PyObject *seq, *item, *subitem;
555
552
 
556
553
    seq = PySequence_Fast(orig, "sequence expected");
557
554
    if (seq == NULL) {
574
571
    for (i = 0; i < size; i++) {
575
572
        item = PySequence_Fast_GET_ITEM(seq, i);
576
573
        if (PyString_Check(item)) {
577
 
            // we could use the 'djb2' hash here if we find it is better than
578
 
            // PyObject_Hash, however it seems a lot slower to compute, and
579
 
            // doesn't give particularly better results
 
574
            /* we could use the 'djb2' hash here if we find it is better than
 
575
               PyObject_Hash, however it seems measurably slower to compute,
 
576
               and doesn't give particularly better results
 
577
             */
580
578
            line->len = PyString_GET_SIZE(item);
581
579
        } else if (PyTuple_Check(item)) {
582
580
            total_len = 0;
583
581
            tuple_len = PyObject_Length(item);
584
582
            for (j = 0; j < tuple_len; ++j) {
585
 
                total_len += PyObject_Length(PySequence_Fast_GET_ITEM(item, j));
 
583
                subitem = PySequence_Fast_GET_ITEM(item, j);
 
584
                cur_len = PyObject_Length(subitem);
 
585
                if (cur_len == -1) {
 
586
                    /* Error */
 
587
                    return -1;
 
588
                }
 
589
                total_len += cur_len;
586
590
            }
587
591
            line->len = total_len;
588
592
        } else {
589
 
            // Generic length
590
 
            line->len = PyObject_Length(item);
 
593
            /* Generic length */
 
594
            cur_len = PyObject_Length(item);
 
595
            if (cur_len == -1) {
 
596
                /* Error */
 
597
                return -1;
 
598
            }
 
599
            line->len = cur_len;
591
600
        }
592
601
        line->data = item;
593
602
        line->hash = PyObject_Hash(item);
 
603
        if (line->hash == (-1)) {
 
604
            /* Propogate the hash exception */
 
605
            return -1;
 
606
        }
594
607
        line->next = SENTINEL;
595
608
        line++;
596
609
    }