~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_c.c

  • Committer: Matt Nordhoff
  • Date: 2009-10-27 09:25:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4774.
  • Revision ID: mnordhoff@mattnordhoff.com-20091027092531-q0lygzntui81p7qn
_static_tuple_c.StaticTuple.from_sequence() now supports arbitrary iterables (by converting them to tuples first).

(I basically copied and pasted an example from jam. Except for the ugly reference counting; that was all me.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
184
184
StaticTuple_FromSequence(PyObject *sequence)
185
185
{
186
186
    StaticTuple *new;
 
187
    PyObject *as_tuple = NULL;
187
188
    PyObject *item;
188
189
    Py_ssize_t i, size;
189
190
 
192
193
        return (StaticTuple *)sequence;
193
194
    }
194
195
    if (!PySequence_Check(sequence)) {
195
 
        PyErr_Format(PyExc_TypeError, "Type %s is not a sequence type",
196
 
                     Py_TYPE(sequence)->tp_name);
197
 
        return NULL;
 
196
        as_tuple = PySequence_Tuple(sequence);
 
197
        sequence = as_tuple;
198
198
    }
199
199
    size = PySequence_Size(sequence);
200
 
    if (size == -1)
 
200
    if (size == -1) {
 
201
        Py_XDECREF(as_tuple);
201
202
        return NULL;
 
203
    }
202
204
    new = StaticTuple_New(size);
203
205
    if (new == NULL) {
 
206
        Py_XDECREF(as_tuple);
204
207
        return NULL;
205
208
    }
206
209
    for (i = 0; i < size; ++i) {
209
212
        item = PySequence_GetItem(sequence, i);
210
213
        if (item == NULL) {
211
214
            Py_DECREF(new);
 
215
            Py_XDECREF(as_tuple);
212
216
            return NULL;
213
217
        }
214
218
        StaticTuple_SET_ITEM(new, i, item);
215
219
    }
 
220
    Py_XDECREF(as_tuple);
216
221
    return (StaticTuple *)new;
217
222
}
218
223