~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_static_tuple_c.c

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-17 01:21:38 UTC
  • mfrom: (4739.4.2 2.1-st-from-sequence)
  • Revision ID: pqm@pqm.ubuntu.com-20091017012138-b962gzlvdccl73ut
(jam) Add StaticTuple.from_sequence()

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
}
175
175
 
176
176
 
 
177
static StaticTuple *
 
178
StaticTuple_FromSequence(PyObject *sequence)
 
179
{
 
180
    StaticTuple *new;
 
181
    PyObject *item;
 
182
    Py_ssize_t i, size;
 
183
 
 
184
    if (StaticTuple_CheckExact(sequence)) {
 
185
        Py_INCREF(sequence);
 
186
        return (StaticTuple *)sequence;
 
187
    }
 
188
    if (!PySequence_Check(sequence)) {
 
189
        PyErr_Format(PyExc_TypeError, "Type %s is not a sequence type",
 
190
                     Py_TYPE(sequence)->tp_name);
 
191
        return NULL;
 
192
    }
 
193
    size = PySequence_Size(sequence);
 
194
    if (size == -1)
 
195
        return NULL;
 
196
    new = StaticTuple_New(size);
 
197
    if (new == NULL) {
 
198
        return NULL;
 
199
    }
 
200
    for (i = 0; i < size; ++i) {
 
201
        // This returns a new reference, which we then 'steal' with 
 
202
        // StaticTuple_SET_ITEM
 
203
        item = PySequence_GetItem(sequence, i);
 
204
        if (item == NULL) {
 
205
            Py_DECREF(new);
 
206
            return NULL;
 
207
        }
 
208
        StaticTuple_SET_ITEM(new, i, item);
 
209
    }
 
210
    return (StaticTuple *)new;
 
211
}
 
212
 
 
213
static StaticTuple *
 
214
StaticTuple_from_sequence(PyObject *self, PyObject *args, PyObject *kwargs)
 
215
{
 
216
    PyObject *sequence;
 
217
    if (!PyArg_ParseTuple(args, "O", &sequence))
 
218
        return NULL;
 
219
    return StaticTuple_FromSequence(sequence);
 
220
}
 
221
 
 
222
 
177
223
static PyObject *
178
224
StaticTuple_new_constructor(PyTypeObject *type, PyObject *args, PyObject *kwds)
179
225
{
571
617
    {"intern", (PyCFunction)StaticTuple_Intern, METH_NOARGS, StaticTuple_Intern_doc},
572
618
    {"_is_interned", (PyCFunction)StaticTuple__is_interned, METH_NOARGS,
573
619
     StaticTuple__is_interned_doc},
 
620
    {"from_sequence", (PyCFunction)StaticTuple_from_sequence,
 
621
     METH_STATIC | METH_VARARGS,
 
622
     "Create a StaticTuple from a given sequence. This functions"
 
623
     " the same as the tuple() constructor."},
574
624
    {NULL, NULL} /* sentinel */
575
625
};
576
626
 
690
740
        "StaticTuple *(Py_ssize_t)");
691
741
    _export_function(m, "StaticTuple_Intern", StaticTuple_Intern,
692
742
        "StaticTuple *(StaticTuple *)");
 
743
    _export_function(m, "StaticTuple_FromSequence", StaticTuple_FromSequence,
 
744
        "StaticTuple *(PyObject *)");
693
745
    _export_function(m, "_StaticTuple_CheckExact", _StaticTuple_CheckExact,
694
746
        "int(PyObject *)");
695
747
}