~ubuntu-branches/ubuntu/natty/dulwich/natty

« back to all changes in this revision

Viewing changes to dulwich/_objects.c

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2010-11-06 22:56:35 UTC
  • mfrom: (1.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20101106225635-89ht682ald4xvsi8
Tags: 0.6.2+bzr702-1
* Recommend python-fastimport. Closes: #600392
* New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
#define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
37
37
 
 
38
static PyObject *tree_entry_cls;
 
39
 
38
40
static PyObject *sha_to_pyhex(const unsigned char *sha)
39
41
{
40
42
        char hexsha[41];
144
146
        return strcmp(remain_a, remain_b);
145
147
}
146
148
 
147
 
static void free_tree_items(struct tree_item *items, int num) {
148
 
        int i;
149
 
        for (i = 0; i < num; i++) {
150
 
                Py_DECREF(items[i].tuple);
151
 
        }
152
 
        free(items);
153
 
}
154
 
 
155
149
static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
156
150
{
157
 
        struct tree_item *qsort_entries;
158
 
        int num, i;
159
 
        PyObject *ret;
 
151
        struct tree_item *qsort_entries = NULL;
 
152
        int num_entries, n = 0, i;
 
153
        PyObject *ret, *key, *value, *py_mode, *py_sha;
160
154
        Py_ssize_t pos = 0;
161
 
        PyObject *key, *value;
162
155
 
163
156
        if (!PyDict_Check(entries)) {
164
157
                PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
165
 
                return NULL;
 
158
                goto error;
166
159
        }
167
160
 
168
 
        num = PyDict_Size(entries);
169
 
        qsort_entries = malloc(num * sizeof(struct tree_item));
170
 
        if (qsort_entries == NULL) {
 
161
        num_entries = PyDict_Size(entries);
 
162
        if (PyErr_Occurred())
 
163
                goto error;
 
164
        qsort_entries = PyMem_New(struct tree_item, num_entries);
 
165
        if (!qsort_entries) {
171
166
                PyErr_NoMemory();
172
 
                return NULL;
 
167
                goto error;
173
168
        }
174
169
 
175
 
        i = 0;
176
170
        while (PyDict_Next(entries, &pos, &key, &value)) {
177
 
                PyObject *py_mode, *py_int_mode, *py_sha;
178
 
 
179
171
                if (!PyString_Check(key)) {
180
172
                        PyErr_SetString(PyExc_TypeError, "Name is not a string");
181
 
                        free_tree_items(qsort_entries, i);
182
 
                        return NULL;
 
173
                        goto error;
183
174
                }
184
175
 
185
176
                if (PyTuple_Size(value) != 2) {
186
177
                        PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
187
 
                        free_tree_items(qsort_entries, i);
188
 
                        return NULL;
 
178
                        goto error;
189
179
                }
190
180
 
191
181
                py_mode = PyTuple_GET_ITEM(value, 0);
192
 
                py_int_mode = PyNumber_Int(py_mode);
193
 
                if (!py_int_mode) {
 
182
                if (!PyInt_Check(py_mode)) {
194
183
                        PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
195
 
                        free_tree_items(qsort_entries, i);
196
 
                        return NULL;
 
184
                        goto error;
197
185
                }
198
186
 
199
187
                py_sha = PyTuple_GET_ITEM(value, 1);
200
188
                if (!PyString_Check(py_sha)) {
201
189
                        PyErr_SetString(PyExc_TypeError, "SHA is not a string");
202
 
                        Py_DECREF(py_int_mode);
203
 
                        free_tree_items(qsort_entries, i);
204
 
                        return NULL;
 
190
                        goto error;
205
191
                }
206
 
                qsort_entries[i].name = PyString_AS_STRING(key);
207
 
                qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
208
 
                qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
209
 
                Py_DECREF(py_int_mode);
210
 
                i++;
 
192
                qsort_entries[n].name = PyString_AS_STRING(key);
 
193
                qsort_entries[n].mode = PyInt_AS_LONG(py_mode);
 
194
 
 
195
                qsort_entries[n].tuple = PyObject_CallFunctionObjArgs(
 
196
                                tree_entry_cls, key, py_mode, py_sha, NULL);
 
197
                if (qsort_entries[n].tuple == NULL)
 
198
                        goto error;
 
199
                n++;
211
200
        }
212
201
 
213
 
        qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
 
202
        qsort(qsort_entries, num_entries, sizeof(struct tree_item), cmp_tree_item);
214
203
 
215
 
        ret = PyList_New(num);
 
204
        ret = PyList_New(num_entries);
216
205
        if (ret == NULL) {
217
 
                free_tree_items(qsort_entries, i);
218
206
                PyErr_NoMemory();
219
 
                return NULL;
 
207
                goto error;
220
208
        }
221
209
 
222
 
        for (i = 0; i < num; i++) {
 
210
        for (i = 0; i < num_entries; i++) {
223
211
                PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
224
212
        }
225
 
 
226
 
        free(qsort_entries);
227
 
 
 
213
        PyMem_Free(qsort_entries);
228
214
        return ret;
 
215
 
 
216
error:
 
217
        for (i = 0; i < n; i++) {
 
218
                Py_XDECREF(qsort_entries[i].tuple);
 
219
        }
 
220
        PyMem_Free(qsort_entries);
 
221
        return NULL;
229
222
}
230
223
 
231
224
static PyMethodDef py_objects_methods[] = {
234
227
        { NULL, NULL, 0, NULL }
235
228
};
236
229
 
237
 
void init_objects(void)
 
230
PyMODINIT_FUNC
 
231
init_objects(void)
238
232
{
239
 
        PyObject *m;
 
233
        PyObject *m, *objects_mod;
240
234
 
241
235
        m = Py_InitModule3("_objects", py_objects_methods, NULL);
242
236
        if (m == NULL)
243
237
                return;
 
238
 
 
239
        /* This is a circular import but should be safe since this module is
 
240
         * imported at at the very bottom of objects.py. */
 
241
        objects_mod = PyImport_ImportModule("dulwich.objects");
 
242
        if (objects_mod == NULL)
 
243
                return;
 
244
 
 
245
        tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
 
246
        Py_DECREF(objects_mod);
 
247
        if (tree_entry_cls == NULL)
 
248
                return;
244
249
}