##// END OF EJS Templates
lazymanifest: fail if path or hash strings cannot be created...
Martin von Zweigbergk -
r24293:30e9ee20 default
parent child Browse files
Show More
@@ -1,852 +1,852
1 /*
1 /*
2 * manifest.c - manifest type that does on-demand parsing.
2 * manifest.c - manifest type that does on-demand parsing.
3 *
3 *
4 * Copyright 2015, Google Inc.
4 * Copyright 2015, Google Inc.
5 *
5 *
6 * This software may be used and distributed according to the terms of
6 * This software may be used and distributed according to the terms of
7 * the GNU General Public License, incorporated herein by reference.
7 * the GNU General Public License, incorporated herein by reference.
8 */
8 */
9 #include <assert.h>
9 #include <assert.h>
10 #include <string.h>
10 #include <string.h>
11 #include <stdlib.h>
11 #include <stdlib.h>
12
12
13 #include <Python.h>
13 #include <Python.h>
14
14
15 /* VC9 doesn't include bool and lacks stdbool.h based on my searching */
15 /* VC9 doesn't include bool and lacks stdbool.h based on my searching */
16 #ifdef _MSC_VER
16 #ifdef _MSC_VER
17 #define true 1
17 #define true 1
18 #define false 0
18 #define false 0
19 typedef unsigned char bool;
19 typedef unsigned char bool;
20 #else
20 #else
21 #include <stdbool.h>
21 #include <stdbool.h>
22 #endif
22 #endif
23
23
24 #define DEFAULT_LINES 100000
24 #define DEFAULT_LINES 100000
25
25
26 typedef struct {
26 typedef struct {
27 char *start;
27 char *start;
28 Py_ssize_t len; /* length of line including terminal newline */
28 Py_ssize_t len; /* length of line including terminal newline */
29 char hash_suffix;
29 char hash_suffix;
30 bool from_malloc;
30 bool from_malloc;
31 bool deleted;
31 bool deleted;
32 } line;
32 } line;
33
33
34 typedef struct {
34 typedef struct {
35 PyObject_HEAD
35 PyObject_HEAD
36 PyObject *pydata;
36 PyObject *pydata;
37 line *lines;
37 line *lines;
38 int numlines; /* number of line entries */
38 int numlines; /* number of line entries */
39 int livelines; /* number of non-deleted lines */
39 int livelines; /* number of non-deleted lines */
40 int maxlines; /* allocated number of lines */
40 int maxlines; /* allocated number of lines */
41 bool dirty;
41 bool dirty;
42 } lazymanifest;
42 } lazymanifest;
43
43
44 #define MANIFEST_OOM -1
44 #define MANIFEST_OOM -1
45 #define MANIFEST_NOT_SORTED -2
45 #define MANIFEST_NOT_SORTED -2
46 #define MANIFEST_MALFORMED -3
46 #define MANIFEST_MALFORMED -3
47
47
48 /* defined in parsers.c */
48 /* defined in parsers.c */
49 PyObject *unhexlify(const char *str, int len);
49 PyObject *unhexlify(const char *str, int len);
50
50
51 /* get the length of the path for a line */
51 /* get the length of the path for a line */
52 static size_t pathlen(line *l) {
52 static size_t pathlen(line *l) {
53 return strlen(l->start);
53 return strlen(l->start);
54 }
54 }
55
55
56 /* get the node value of a single line */
56 /* get the node value of a single line */
57 static PyObject *nodeof(line *l) {
57 static PyObject *nodeof(line *l) {
58 char *s = l->start;
58 char *s = l->start;
59 ssize_t llen = pathlen(l);
59 ssize_t llen = pathlen(l);
60 PyObject *hash = unhexlify(s + llen + 1, 40);
60 PyObject *hash = unhexlify(s + llen + 1, 40);
61 if (!hash) {
61 if (!hash) {
62 return NULL;
62 return NULL;
63 }
63 }
64 if (l->hash_suffix != '\0') {
64 if (l->hash_suffix != '\0') {
65 char newhash[21];
65 char newhash[21];
66 memcpy(newhash, PyString_AsString(hash), 20);
66 memcpy(newhash, PyString_AsString(hash), 20);
67 Py_DECREF(hash);
67 Py_DECREF(hash);
68 newhash[20] = l->hash_suffix;
68 newhash[20] = l->hash_suffix;
69 hash = PyString_FromStringAndSize(newhash, 21);
69 hash = PyString_FromStringAndSize(newhash, 21);
70 }
70 }
71 return hash;
71 return hash;
72 }
72 }
73
73
74 /* get the node hash and flags of a line as a tuple */
74 /* get the node hash and flags of a line as a tuple */
75 static PyObject *hashflags(line *l)
75 static PyObject *hashflags(line *l)
76 {
76 {
77 char *s = l->start;
77 char *s = l->start;
78 size_t plen = pathlen(l);
78 size_t plen = pathlen(l);
79 PyObject *hash = nodeof(l);
79 PyObject *hash = nodeof(l);
80
80
81 /* 40 for hash, 1 for null byte, 1 for newline */
81 /* 40 for hash, 1 for null byte, 1 for newline */
82 size_t hplen = plen + 42;
82 size_t hplen = plen + 42;
83 Py_ssize_t flen = l->len - hplen;
83 Py_ssize_t flen = l->len - hplen;
84 PyObject *flags;
84 PyObject *flags;
85 PyObject *tup;
85 PyObject *tup;
86
86
87 if (!hash)
87 if (!hash)
88 return NULL;
88 return NULL;
89 flags = PyString_FromStringAndSize(s + hplen - 1, flen);
89 flags = PyString_FromStringAndSize(s + hplen - 1, flen);
90 if (!flags) {
90 if (!flags) {
91 Py_DECREF(hash);
91 Py_DECREF(hash);
92 return NULL;
92 return NULL;
93 }
93 }
94 tup = PyTuple_Pack(2, hash, flags);
94 tup = PyTuple_Pack(2, hash, flags);
95 Py_DECREF(flags);
95 Py_DECREF(flags);
96 Py_DECREF(hash);
96 Py_DECREF(hash);
97 return tup;
97 return tup;
98 }
98 }
99
99
100 /* if we're about to run out of space in the line index, add more */
100 /* if we're about to run out of space in the line index, add more */
101 static bool realloc_if_full(lazymanifest *self)
101 static bool realloc_if_full(lazymanifest *self)
102 {
102 {
103 if (self->numlines == self->maxlines) {
103 if (self->numlines == self->maxlines) {
104 self->maxlines *= 2;
104 self->maxlines *= 2;
105 self->lines = realloc(self->lines, self->maxlines * sizeof(line));
105 self->lines = realloc(self->lines, self->maxlines * sizeof(line));
106 }
106 }
107 return self->lines;
107 return self->lines;
108 }
108 }
109
109
110 /*
110 /*
111 * Find the line boundaries in the manifest that 'data' points to and store
111 * Find the line boundaries in the manifest that 'data' points to and store
112 * information about each line in 'self'.
112 * information about each line in 'self'.
113 */
113 */
114 static int find_lines(lazymanifest *self, char *data, Py_ssize_t len)
114 static int find_lines(lazymanifest *self, char *data, Py_ssize_t len)
115 {
115 {
116 char *prev = NULL;
116 char *prev = NULL;
117 while (len > 0) {
117 while (len > 0) {
118 line *l;
118 line *l;
119 char *next = memchr(data, '\n', len);
119 char *next = memchr(data, '\n', len);
120 if (!next) {
120 if (!next) {
121 return MANIFEST_MALFORMED;
121 return MANIFEST_MALFORMED;
122 }
122 }
123 next++; /* advance past newline */
123 next++; /* advance past newline */
124 if (!realloc_if_full(self)) {
124 if (!realloc_if_full(self)) {
125 return MANIFEST_OOM; /* no memory */
125 return MANIFEST_OOM; /* no memory */
126 }
126 }
127 if (prev && strcmp(prev, data) > -1) {
127 if (prev && strcmp(prev, data) > -1) {
128 /* This data isn't sorted, so we have to abort. */
128 /* This data isn't sorted, so we have to abort. */
129 return MANIFEST_NOT_SORTED;
129 return MANIFEST_NOT_SORTED;
130 }
130 }
131 l = self->lines + ((self->numlines)++);
131 l = self->lines + ((self->numlines)++);
132 l->start = data;
132 l->start = data;
133 l->len = next - data;
133 l->len = next - data;
134 l->hash_suffix = '\0';
134 l->hash_suffix = '\0';
135 l->from_malloc = false;
135 l->from_malloc = false;
136 l->deleted = false;
136 l->deleted = false;
137 len = len - l->len;
137 len = len - l->len;
138 prev = data;
138 prev = data;
139 data = next;
139 data = next;
140 }
140 }
141 self->livelines = self->numlines;
141 self->livelines = self->numlines;
142 return 0;
142 return 0;
143 }
143 }
144
144
145 static int lazymanifest_init(lazymanifest *self, PyObject *args)
145 static int lazymanifest_init(lazymanifest *self, PyObject *args)
146 {
146 {
147 char *data;
147 char *data;
148 Py_ssize_t len;
148 Py_ssize_t len;
149 int err, ret;
149 int err, ret;
150 PyObject *pydata;
150 PyObject *pydata;
151 if (!PyArg_ParseTuple(args, "S", &pydata)) {
151 if (!PyArg_ParseTuple(args, "S", &pydata)) {
152 return -1;
152 return -1;
153 }
153 }
154 err = PyString_AsStringAndSize(pydata, &data, &len);
154 err = PyString_AsStringAndSize(pydata, &data, &len);
155
155
156 self->dirty = false;
156 self->dirty = false;
157 if (err == -1)
157 if (err == -1)
158 return -1;
158 return -1;
159 self->pydata = pydata;
159 self->pydata = pydata;
160 Py_INCREF(self->pydata);
160 Py_INCREF(self->pydata);
161 Py_BEGIN_ALLOW_THREADS
161 Py_BEGIN_ALLOW_THREADS
162 self->lines = malloc(DEFAULT_LINES * sizeof(line));
162 self->lines = malloc(DEFAULT_LINES * sizeof(line));
163 self->maxlines = DEFAULT_LINES;
163 self->maxlines = DEFAULT_LINES;
164 self->numlines = 0;
164 self->numlines = 0;
165 if (!self->lines)
165 if (!self->lines)
166 ret = MANIFEST_OOM;
166 ret = MANIFEST_OOM;
167 else
167 else
168 ret = find_lines(self, data, len);
168 ret = find_lines(self, data, len);
169 Py_END_ALLOW_THREADS
169 Py_END_ALLOW_THREADS
170 switch (ret) {
170 switch (ret) {
171 case 0:
171 case 0:
172 break;
172 break;
173 case MANIFEST_OOM:
173 case MANIFEST_OOM:
174 PyErr_NoMemory();
174 PyErr_NoMemory();
175 break;
175 break;
176 case MANIFEST_NOT_SORTED:
176 case MANIFEST_NOT_SORTED:
177 PyErr_Format(PyExc_ValueError,
177 PyErr_Format(PyExc_ValueError,
178 "Manifest lines not in sorted order.");
178 "Manifest lines not in sorted order.");
179 break;
179 break;
180 case MANIFEST_MALFORMED:
180 case MANIFEST_MALFORMED:
181 PyErr_Format(PyExc_ValueError,
181 PyErr_Format(PyExc_ValueError,
182 "Manifest did not end in a newline.");
182 "Manifest did not end in a newline.");
183 break;
183 break;
184 default:
184 default:
185 PyErr_Format(PyExc_ValueError,
185 PyErr_Format(PyExc_ValueError,
186 "Unknown problem parsing manifest.");
186 "Unknown problem parsing manifest.");
187 }
187 }
188 return ret == 0 ? 0 : -1;
188 return ret == 0 ? 0 : -1;
189 }
189 }
190
190
191 static void lazymanifest_dealloc(lazymanifest *self)
191 static void lazymanifest_dealloc(lazymanifest *self)
192 {
192 {
193 /* free any extra lines we had to allocate */
193 /* free any extra lines we had to allocate */
194 int i;
194 int i;
195 for (i = 0; i < self->numlines; i++) {
195 for (i = 0; i < self->numlines; i++) {
196 if (self->lines[i].from_malloc) {
196 if (self->lines[i].from_malloc) {
197 free(self->lines[i].start);
197 free(self->lines[i].start);
198 }
198 }
199 }
199 }
200 if (self->lines) {
200 if (self->lines) {
201 free(self->lines);
201 free(self->lines);
202 self->lines = NULL;
202 self->lines = NULL;
203 }
203 }
204 if (self->pydata) {
204 if (self->pydata) {
205 Py_DECREF(self->pydata);
205 Py_DECREF(self->pydata);
206 self->pydata = NULL;
206 self->pydata = NULL;
207 }
207 }
208 PyObject_Del(self);
208 PyObject_Del(self);
209 }
209 }
210
210
211 /* iteration support */
211 /* iteration support */
212
212
213 typedef struct {
213 typedef struct {
214 PyObject_HEAD lazymanifest *m;
214 PyObject_HEAD lazymanifest *m;
215 Py_ssize_t pos;
215 Py_ssize_t pos;
216 } lmIter;
216 } lmIter;
217
217
218 static void lmiter_dealloc(PyObject *o)
218 static void lmiter_dealloc(PyObject *o)
219 {
219 {
220 lmIter *self = (lmIter *)o;
220 lmIter *self = (lmIter *)o;
221 Py_DECREF(self->m);
221 Py_DECREF(self->m);
222 PyObject_Del(self);
222 PyObject_Del(self);
223 }
223 }
224
224
225 static PyObject *lmiter_iternext(PyObject *o)
225 static PyObject *lmiter_iternext(PyObject *o)
226 {
226 {
227 size_t pl;
227 size_t pl;
228 line *l;
228 line *l;
229 Py_ssize_t consumed;
229 Py_ssize_t consumed;
230 PyObject *ret = NULL, *path = NULL, *hash = NULL, *flags = NULL;
230 PyObject *ret = NULL, *path = NULL, *hash = NULL, *flags = NULL;
231 lmIter *self = (lmIter *)o;
231 lmIter *self = (lmIter *)o;
232 do {
232 do {
233 self->pos++;
233 self->pos++;
234 if (self->pos >= self->m->numlines) {
234 if (self->pos >= self->m->numlines) {
235 goto bail;
235 goto bail;
236 }
236 }
237 /* skip over deleted manifest entries */
237 /* skip over deleted manifest entries */
238 } while (self->m->lines[self->pos].deleted);
238 } while (self->m->lines[self->pos].deleted);
239 l = self->m->lines + self->pos;
239 l = self->m->lines + self->pos;
240 pl = pathlen(l);
240 pl = pathlen(l);
241 path = PyString_FromStringAndSize(l->start, pl);
241 path = PyString_FromStringAndSize(l->start, pl);
242 hash = nodeof(l);
242 hash = nodeof(l);
243 consumed = pl + 41;
243 consumed = pl + 41;
244 flags = PyString_FromStringAndSize(l->start + consumed,
244 flags = PyString_FromStringAndSize(l->start + consumed,
245 l->len - consumed - 1);
245 l->len - consumed - 1);
246 if (!flags) {
246 if (!path || !hash || !flags) {
247 goto bail;
247 goto bail;
248 }
248 }
249 ret = PyTuple_Pack(3, path, hash, flags);
249 ret = PyTuple_Pack(3, path, hash, flags);
250 bail:
250 bail:
251 Py_XDECREF(path);
251 Py_XDECREF(path);
252 Py_XDECREF(hash);
252 Py_XDECREF(hash);
253 Py_XDECREF(flags);
253 Py_XDECREF(flags);
254 return ret;
254 return ret;
255 }
255 }
256
256
257 static PyTypeObject lazymanifestIterator = {
257 static PyTypeObject lazymanifestIterator = {
258 PyObject_HEAD_INIT(NULL)
258 PyObject_HEAD_INIT(NULL)
259 0, /*ob_size */
259 0, /*ob_size */
260 "parsers.lazymanifest.iterator", /*tp_name */
260 "parsers.lazymanifest.iterator", /*tp_name */
261 sizeof(lmIter), /*tp_basicsize */
261 sizeof(lmIter), /*tp_basicsize */
262 0, /*tp_itemsize */
262 0, /*tp_itemsize */
263 lmiter_dealloc, /*tp_dealloc */
263 lmiter_dealloc, /*tp_dealloc */
264 0, /*tp_print */
264 0, /*tp_print */
265 0, /*tp_getattr */
265 0, /*tp_getattr */
266 0, /*tp_setattr */
266 0, /*tp_setattr */
267 0, /*tp_compare */
267 0, /*tp_compare */
268 0, /*tp_repr */
268 0, /*tp_repr */
269 0, /*tp_as_number */
269 0, /*tp_as_number */
270 0, /*tp_as_sequence */
270 0, /*tp_as_sequence */
271 0, /*tp_as_mapping */
271 0, /*tp_as_mapping */
272 0, /*tp_hash */
272 0, /*tp_hash */
273 0, /*tp_call */
273 0, /*tp_call */
274 0, /*tp_str */
274 0, /*tp_str */
275 0, /*tp_getattro */
275 0, /*tp_getattro */
276 0, /*tp_setattro */
276 0, /*tp_setattro */
277 0, /*tp_as_buffer */
277 0, /*tp_as_buffer */
278 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
278 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
279 use tp_iter and tp_iternext fields. */
279 use tp_iter and tp_iternext fields. */
280 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
280 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
281 "Iterator for a lazymanifest.", /* tp_doc */
281 "Iterator for a lazymanifest.", /* tp_doc */
282 0, /* tp_traverse */
282 0, /* tp_traverse */
283 0, /* tp_clear */
283 0, /* tp_clear */
284 0, /* tp_richcompare */
284 0, /* tp_richcompare */
285 0, /* tp_weaklistoffset */
285 0, /* tp_weaklistoffset */
286 PyObject_SelfIter, /* tp_iter: __iter__() method */
286 PyObject_SelfIter, /* tp_iter: __iter__() method */
287 lmiter_iternext, /* tp_iternext: next() method */
287 lmiter_iternext, /* tp_iternext: next() method */
288 };
288 };
289
289
290 static lazymanifest *lazymanifest_copy(lazymanifest *self);
290 static lazymanifest *lazymanifest_copy(lazymanifest *self);
291
291
292 static PyObject *lazymanifest_getiter(lazymanifest *self)
292 static PyObject *lazymanifest_getiter(lazymanifest *self)
293 {
293 {
294 lmIter *i = NULL;
294 lmIter *i = NULL;
295 lazymanifest *t = lazymanifest_copy(self);
295 lazymanifest *t = lazymanifest_copy(self);
296 if (!t) {
296 if (!t) {
297 PyErr_NoMemory();
297 PyErr_NoMemory();
298 return NULL;
298 return NULL;
299 }
299 }
300 i = PyObject_New(lmIter, &lazymanifestIterator);
300 i = PyObject_New(lmIter, &lazymanifestIterator);
301 if (i) {
301 if (i) {
302 i->m = t;
302 i->m = t;
303 i->pos = -1;
303 i->pos = -1;
304 } else {
304 } else {
305 Py_DECREF(t);
305 Py_DECREF(t);
306 PyErr_NoMemory();
306 PyErr_NoMemory();
307 }
307 }
308 return (PyObject *)i;
308 return (PyObject *)i;
309 }
309 }
310
310
311 /* __getitem__ and __setitem__ support */
311 /* __getitem__ and __setitem__ support */
312
312
313 static Py_ssize_t lazymanifest_size(lazymanifest *self)
313 static Py_ssize_t lazymanifest_size(lazymanifest *self)
314 {
314 {
315 return self->livelines;
315 return self->livelines;
316 }
316 }
317
317
318 static int linecmp(const void *left, const void *right)
318 static int linecmp(const void *left, const void *right)
319 {
319 {
320 return strcmp(((const line *)left)->start,
320 return strcmp(((const line *)left)->start,
321 ((const line *)right)->start);
321 ((const line *)right)->start);
322 }
322 }
323
323
324 static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key)
324 static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key)
325 {
325 {
326 line needle;
326 line needle;
327 line *hit;
327 line *hit;
328 if (!PyString_Check(key)) {
328 if (!PyString_Check(key)) {
329 PyErr_Format(PyExc_TypeError,
329 PyErr_Format(PyExc_TypeError,
330 "getitem: manifest keys must be a string.");
330 "getitem: manifest keys must be a string.");
331 return NULL;
331 return NULL;
332 }
332 }
333 needle.start = PyString_AsString(key);
333 needle.start = PyString_AsString(key);
334 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
334 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
335 &linecmp);
335 &linecmp);
336 if (!hit || hit->deleted) {
336 if (!hit || hit->deleted) {
337 PyErr_Format(PyExc_KeyError, "No such manifest entry.");
337 PyErr_Format(PyExc_KeyError, "No such manifest entry.");
338 return NULL;
338 return NULL;
339 }
339 }
340 return hashflags(hit);
340 return hashflags(hit);
341 }
341 }
342
342
343 static int lazymanifest_delitem(lazymanifest *self, PyObject *key)
343 static int lazymanifest_delitem(lazymanifest *self, PyObject *key)
344 {
344 {
345 line needle;
345 line needle;
346 line *hit;
346 line *hit;
347 if (!PyString_Check(key)) {
347 if (!PyString_Check(key)) {
348 PyErr_Format(PyExc_TypeError,
348 PyErr_Format(PyExc_TypeError,
349 "delitem: manifest keys must be a string.");
349 "delitem: manifest keys must be a string.");
350 return -1;
350 return -1;
351 }
351 }
352 needle.start = PyString_AsString(key);
352 needle.start = PyString_AsString(key);
353 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
353 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
354 &linecmp);
354 &linecmp);
355 if (!hit || hit->deleted) {
355 if (!hit || hit->deleted) {
356 PyErr_Format(PyExc_KeyError,
356 PyErr_Format(PyExc_KeyError,
357 "Tried to delete nonexistent manifest entry.");
357 "Tried to delete nonexistent manifest entry.");
358 return -1;
358 return -1;
359 }
359 }
360 self->dirty = true;
360 self->dirty = true;
361 hit->deleted = true;
361 hit->deleted = true;
362 self->livelines--;
362 self->livelines--;
363 return 0;
363 return 0;
364 }
364 }
365
365
366 /* Do a binary search for the insertion point for new, creating the
366 /* Do a binary search for the insertion point for new, creating the
367 * new entry if needed. */
367 * new entry if needed. */
368 static int internalsetitem(lazymanifest *self, line *new) {
368 static int internalsetitem(lazymanifest *self, line *new) {
369 int start = 0, end = self->numlines;
369 int start = 0, end = self->numlines;
370 while (start < end) {
370 while (start < end) {
371 int pos = start + (end - start) / 2;
371 int pos = start + (end - start) / 2;
372 int c = linecmp(new, self->lines + pos);
372 int c = linecmp(new, self->lines + pos);
373 if (c < 0)
373 if (c < 0)
374 end = pos;
374 end = pos;
375 else if (c > 0)
375 else if (c > 0)
376 start = pos + 1;
376 start = pos + 1;
377 else {
377 else {
378 if (self->lines[pos].deleted)
378 if (self->lines[pos].deleted)
379 self->livelines++;
379 self->livelines++;
380 start = pos;
380 start = pos;
381 goto finish;
381 goto finish;
382 }
382 }
383 }
383 }
384 /* being here means we need to do an insert */
384 /* being here means we need to do an insert */
385 if (!realloc_if_full(self)) {
385 if (!realloc_if_full(self)) {
386 PyErr_NoMemory();
386 PyErr_NoMemory();
387 return -1;
387 return -1;
388 }
388 }
389 memmove(self->lines + start + 1, self->lines + start,
389 memmove(self->lines + start + 1, self->lines + start,
390 (self->numlines - start) * sizeof(line));
390 (self->numlines - start) * sizeof(line));
391 self->numlines++;
391 self->numlines++;
392 self->livelines++;
392 self->livelines++;
393 finish:
393 finish:
394 self->lines[start] = *new;
394 self->lines[start] = *new;
395 self->dirty = true;
395 self->dirty = true;
396 return 0;
396 return 0;
397 }
397 }
398
398
399 static int lazymanifest_setitem(
399 static int lazymanifest_setitem(
400 lazymanifest *self, PyObject *key, PyObject *value)
400 lazymanifest *self, PyObject *key, PyObject *value)
401 {
401 {
402 char *path;
402 char *path;
403 Py_ssize_t plen;
403 Py_ssize_t plen;
404 PyObject *pyhash;
404 PyObject *pyhash;
405 Py_ssize_t hlen;
405 Py_ssize_t hlen;
406 char *hash;
406 char *hash;
407 PyObject *pyflags;
407 PyObject *pyflags;
408 char *flags;
408 char *flags;
409 Py_ssize_t flen;
409 Py_ssize_t flen;
410 size_t dlen;
410 size_t dlen;
411 char *dest;
411 char *dest;
412 int i;
412 int i;
413 line new;
413 line new;
414 if (!PyString_Check(key)) {
414 if (!PyString_Check(key)) {
415 PyErr_Format(PyExc_TypeError,
415 PyErr_Format(PyExc_TypeError,
416 "setitem: manifest keys must be a string.");
416 "setitem: manifest keys must be a string.");
417 return -1;
417 return -1;
418 }
418 }
419 if (!value) {
419 if (!value) {
420 return lazymanifest_delitem(self, key);
420 return lazymanifest_delitem(self, key);
421 }
421 }
422 if (!PyTuple_Check(value) || PyTuple_Size(value) != 2) {
422 if (!PyTuple_Check(value) || PyTuple_Size(value) != 2) {
423 PyErr_Format(PyExc_TypeError,
423 PyErr_Format(PyExc_TypeError,
424 "Manifest values must be a tuple of (node, flags).");
424 "Manifest values must be a tuple of (node, flags).");
425 return -1;
425 return -1;
426 }
426 }
427 if (PyString_AsStringAndSize(key, &path, &plen) == -1) {
427 if (PyString_AsStringAndSize(key, &path, &plen) == -1) {
428 return -1;
428 return -1;
429 }
429 }
430
430
431 pyhash = PyTuple_GetItem(value, 0);
431 pyhash = PyTuple_GetItem(value, 0);
432 if (!PyString_Check(pyhash)) {
432 if (!PyString_Check(pyhash)) {
433 PyErr_Format(PyExc_TypeError,
433 PyErr_Format(PyExc_TypeError,
434 "node must be a 20-byte string");
434 "node must be a 20-byte string");
435 return -1;
435 return -1;
436 }
436 }
437 hlen = PyString_Size(pyhash);
437 hlen = PyString_Size(pyhash);
438 /* Some parts of the codebase try and set 21 or 22
438 /* Some parts of the codebase try and set 21 or 22
439 * byte "hash" values in order to perturb things for
439 * byte "hash" values in order to perturb things for
440 * status. We have to preserve at least the 21st
440 * status. We have to preserve at least the 21st
441 * byte. Sigh. If there's a 22nd byte, we drop it on
441 * byte. Sigh. If there's a 22nd byte, we drop it on
442 * the floor, which works fine.
442 * the floor, which works fine.
443 */
443 */
444 if (hlen != 20 && hlen != 21 && hlen != 22) {
444 if (hlen != 20 && hlen != 21 && hlen != 22) {
445 PyErr_Format(PyExc_TypeError,
445 PyErr_Format(PyExc_TypeError,
446 "node must be a 20-byte string");
446 "node must be a 20-byte string");
447 return -1;
447 return -1;
448 }
448 }
449 hash = PyString_AsString(pyhash);
449 hash = PyString_AsString(pyhash);
450
450
451 pyflags = PyTuple_GetItem(value, 1);
451 pyflags = PyTuple_GetItem(value, 1);
452 if (!PyString_Check(pyflags) || PyString_Size(pyflags) > 1) {
452 if (!PyString_Check(pyflags) || PyString_Size(pyflags) > 1) {
453 PyErr_Format(PyExc_TypeError,
453 PyErr_Format(PyExc_TypeError,
454 "flags must a 0 or 1 byte string");
454 "flags must a 0 or 1 byte string");
455 return -1;
455 return -1;
456 }
456 }
457 if (PyString_AsStringAndSize(pyflags, &flags, &flen) == -1) {
457 if (PyString_AsStringAndSize(pyflags, &flags, &flen) == -1) {
458 return -1;
458 return -1;
459 }
459 }
460 /* one null byte and one newline */
460 /* one null byte and one newline */
461 dlen = plen + 41 + flen + 1;
461 dlen = plen + 41 + flen + 1;
462 dest = malloc(dlen);
462 dest = malloc(dlen);
463 if (!dest) {
463 if (!dest) {
464 PyErr_NoMemory();
464 PyErr_NoMemory();
465 return -1;
465 return -1;
466 }
466 }
467 memcpy(dest, path, plen + 1);
467 memcpy(dest, path, plen + 1);
468 for (i = 0; i < 20; i++) {
468 for (i = 0; i < 20; i++) {
469 /* Cast to unsigned, so it will not get sign-extended when promoted
469 /* Cast to unsigned, so it will not get sign-extended when promoted
470 * to int (as is done when passing to a variadic function)
470 * to int (as is done when passing to a variadic function)
471 */
471 */
472 sprintf(dest + plen + 1 + (i * 2), "%02x", (unsigned char)hash[i]);
472 sprintf(dest + plen + 1 + (i * 2), "%02x", (unsigned char)hash[i]);
473 }
473 }
474 memcpy(dest + plen + 41, flags, flen);
474 memcpy(dest + plen + 41, flags, flen);
475 dest[plen + 41 + flen] = '\n';
475 dest[plen + 41 + flen] = '\n';
476 new.start = dest;
476 new.start = dest;
477 new.len = dlen;
477 new.len = dlen;
478 new.hash_suffix = '\0';
478 new.hash_suffix = '\0';
479 if (hlen > 20) {
479 if (hlen > 20) {
480 new.hash_suffix = hash[20];
480 new.hash_suffix = hash[20];
481 }
481 }
482 new.from_malloc = true; /* is `start` a pointer we allocated? */
482 new.from_malloc = true; /* is `start` a pointer we allocated? */
483 new.deleted = false; /* is this entry deleted? */
483 new.deleted = false; /* is this entry deleted? */
484 if (internalsetitem(self, &new)) {
484 if (internalsetitem(self, &new)) {
485 return -1;
485 return -1;
486 }
486 }
487 return 0;
487 return 0;
488 }
488 }
489
489
490 static PyMappingMethods lazymanifest_mapping_methods = {
490 static PyMappingMethods lazymanifest_mapping_methods = {
491 (lenfunc)lazymanifest_size, /* mp_length */
491 (lenfunc)lazymanifest_size, /* mp_length */
492 (binaryfunc)lazymanifest_getitem, /* mp_subscript */
492 (binaryfunc)lazymanifest_getitem, /* mp_subscript */
493 (objobjargproc)lazymanifest_setitem, /* mp_ass_subscript */
493 (objobjargproc)lazymanifest_setitem, /* mp_ass_subscript */
494 };
494 };
495
495
496 /* sequence methods (important or __contains__ builds an iterator */
496 /* sequence methods (important or __contains__ builds an iterator */
497
497
498 static int lazymanifest_contains(lazymanifest *self, PyObject *key)
498 static int lazymanifest_contains(lazymanifest *self, PyObject *key)
499 {
499 {
500 line needle;
500 line needle;
501 line *hit;
501 line *hit;
502 if (!PyString_Check(key)) {
502 if (!PyString_Check(key)) {
503 /* Our keys are always strings, so if the contains
503 /* Our keys are always strings, so if the contains
504 * check is for a non-string, just return false. */
504 * check is for a non-string, just return false. */
505 return 0;
505 return 0;
506 }
506 }
507 needle.start = PyString_AsString(key);
507 needle.start = PyString_AsString(key);
508 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
508 hit = bsearch(&needle, self->lines, self->numlines, sizeof(line),
509 &linecmp);
509 &linecmp);
510 if (!hit || hit->deleted) {
510 if (!hit || hit->deleted) {
511 return 0;
511 return 0;
512 }
512 }
513 return 1;
513 return 1;
514 }
514 }
515
515
516 static PySequenceMethods lazymanifest_seq_meths = {
516 static PySequenceMethods lazymanifest_seq_meths = {
517 (lenfunc)lazymanifest_size, /* sq_length */
517 (lenfunc)lazymanifest_size, /* sq_length */
518 0, /* sq_concat */
518 0, /* sq_concat */
519 0, /* sq_repeat */
519 0, /* sq_repeat */
520 0, /* sq_item */
520 0, /* sq_item */
521 0, /* sq_slice */
521 0, /* sq_slice */
522 0, /* sq_ass_item */
522 0, /* sq_ass_item */
523 0, /* sq_ass_slice */
523 0, /* sq_ass_slice */
524 (objobjproc)lazymanifest_contains, /* sq_contains */
524 (objobjproc)lazymanifest_contains, /* sq_contains */
525 0, /* sq_inplace_concat */
525 0, /* sq_inplace_concat */
526 0, /* sq_inplace_repeat */
526 0, /* sq_inplace_repeat */
527 };
527 };
528
528
529
529
530 /* Other methods (copy, diff, etc) */
530 /* Other methods (copy, diff, etc) */
531 static PyTypeObject lazymanifestType;
531 static PyTypeObject lazymanifestType;
532
532
533 /* If the manifest has changes, build the new manifest text and reindex it. */
533 /* If the manifest has changes, build the new manifest text and reindex it. */
534 static int compact(lazymanifest *self) {
534 static int compact(lazymanifest *self) {
535 int i;
535 int i;
536 ssize_t need = 0;
536 ssize_t need = 0;
537 char *data;
537 char *data;
538 line *src, *dst;
538 line *src, *dst;
539 PyObject *pydata;
539 PyObject *pydata;
540 if (!self->dirty)
540 if (!self->dirty)
541 return 0;
541 return 0;
542 for (i = 0; i < self->numlines; i++) {
542 for (i = 0; i < self->numlines; i++) {
543 if (!self->lines[i].deleted) {
543 if (!self->lines[i].deleted) {
544 need += self->lines[i].len;
544 need += self->lines[i].len;
545 }
545 }
546 }
546 }
547 pydata = PyString_FromStringAndSize(NULL, need);
547 pydata = PyString_FromStringAndSize(NULL, need);
548 if (!pydata)
548 if (!pydata)
549 return -1;
549 return -1;
550 data = PyString_AsString(pydata);
550 data = PyString_AsString(pydata);
551 if (!data) {
551 if (!data) {
552 return -1;
552 return -1;
553 }
553 }
554 src = self->lines;
554 src = self->lines;
555 dst = self->lines;
555 dst = self->lines;
556 for (i = 0; i < self->numlines; i++, src++) {
556 for (i = 0; i < self->numlines; i++, src++) {
557 char *tofree = NULL;
557 char *tofree = NULL;
558 if (src->from_malloc) {
558 if (src->from_malloc) {
559 tofree = src->start;
559 tofree = src->start;
560 }
560 }
561 if (!src->deleted) {
561 if (!src->deleted) {
562 memcpy(data, src->start, src->len);
562 memcpy(data, src->start, src->len);
563 *dst = *src;
563 *dst = *src;
564 dst->start = data;
564 dst->start = data;
565 dst->from_malloc = false;
565 dst->from_malloc = false;
566 data += dst->len;
566 data += dst->len;
567 dst++;
567 dst++;
568 }
568 }
569 free(tofree);
569 free(tofree);
570 }
570 }
571 Py_DECREF(self->pydata);
571 Py_DECREF(self->pydata);
572 self->pydata = pydata;
572 self->pydata = pydata;
573 self->numlines = self->livelines;
573 self->numlines = self->livelines;
574 self->dirty = false;
574 self->dirty = false;
575 return 0;
575 return 0;
576 }
576 }
577
577
578 static PyObject *lazymanifest_text(lazymanifest *self)
578 static PyObject *lazymanifest_text(lazymanifest *self)
579 {
579 {
580 if (compact(self) != 0) {
580 if (compact(self) != 0) {
581 PyErr_NoMemory();
581 PyErr_NoMemory();
582 return NULL;
582 return NULL;
583 }
583 }
584 Py_INCREF(self->pydata);
584 Py_INCREF(self->pydata);
585 return self->pydata;
585 return self->pydata;
586 }
586 }
587
587
588 static lazymanifest *lazymanifest_copy(lazymanifest *self)
588 static lazymanifest *lazymanifest_copy(lazymanifest *self)
589 {
589 {
590 lazymanifest *copy = NULL;
590 lazymanifest *copy = NULL;
591 if (compact(self) != 0) {
591 if (compact(self) != 0) {
592 goto nomem;
592 goto nomem;
593 }
593 }
594 copy = PyObject_New(lazymanifest, &lazymanifestType);
594 copy = PyObject_New(lazymanifest, &lazymanifestType);
595 if (!copy) {
595 if (!copy) {
596 goto nomem;
596 goto nomem;
597 }
597 }
598 copy->numlines = self->numlines;
598 copy->numlines = self->numlines;
599 copy->livelines = self->livelines;
599 copy->livelines = self->livelines;
600 copy->dirty = false;
600 copy->dirty = false;
601 copy->lines = malloc(self->maxlines *sizeof(line));
601 copy->lines = malloc(self->maxlines *sizeof(line));
602 if (!copy->lines) {
602 if (!copy->lines) {
603 goto nomem;
603 goto nomem;
604 }
604 }
605 memcpy(copy->lines, self->lines, self->numlines * sizeof(line));
605 memcpy(copy->lines, self->lines, self->numlines * sizeof(line));
606 copy->maxlines = self->maxlines;
606 copy->maxlines = self->maxlines;
607 copy->pydata = self->pydata;
607 copy->pydata = self->pydata;
608 Py_INCREF(copy->pydata);
608 Py_INCREF(copy->pydata);
609 return copy;
609 return copy;
610 nomem:
610 nomem:
611 PyErr_NoMemory();
611 PyErr_NoMemory();
612 Py_XDECREF(copy);
612 Py_XDECREF(copy);
613 return NULL;
613 return NULL;
614 }
614 }
615
615
616 static lazymanifest *lazymanifest_filtercopy(
616 static lazymanifest *lazymanifest_filtercopy(
617 lazymanifest *self, PyObject *matchfn)
617 lazymanifest *self, PyObject *matchfn)
618 {
618 {
619 lazymanifest *copy = NULL;
619 lazymanifest *copy = NULL;
620 int i;
620 int i;
621 if (!PyCallable_Check(matchfn)) {
621 if (!PyCallable_Check(matchfn)) {
622 PyErr_SetString(PyExc_TypeError, "matchfn must be callable");
622 PyErr_SetString(PyExc_TypeError, "matchfn must be callable");
623 return NULL;
623 return NULL;
624 }
624 }
625 /* compact ourselves first to avoid double-frees later when we
625 /* compact ourselves first to avoid double-frees later when we
626 * compact tmp so that it doesn't have random pointers to our
626 * compact tmp so that it doesn't have random pointers to our
627 * underlying from_malloc-data (self->pydata is safe) */
627 * underlying from_malloc-data (self->pydata is safe) */
628 if (compact(self) != 0) {
628 if (compact(self) != 0) {
629 goto nomem;
629 goto nomem;
630 }
630 }
631 copy = PyObject_New(lazymanifest, &lazymanifestType);
631 copy = PyObject_New(lazymanifest, &lazymanifestType);
632 copy->dirty = true;
632 copy->dirty = true;
633 copy->lines = malloc(self->maxlines * sizeof(line));
633 copy->lines = malloc(self->maxlines * sizeof(line));
634 if (!copy->lines) {
634 if (!copy->lines) {
635 goto nomem;
635 goto nomem;
636 }
636 }
637 copy->maxlines = self->maxlines;
637 copy->maxlines = self->maxlines;
638 copy->numlines = 0;
638 copy->numlines = 0;
639 copy->pydata = self->pydata;
639 copy->pydata = self->pydata;
640 Py_INCREF(self->pydata);
640 Py_INCREF(self->pydata);
641 for (i = 0; i < self->numlines; i++) {
641 for (i = 0; i < self->numlines; i++) {
642 PyObject *arg = PyString_FromString(self->lines[i].start);
642 PyObject *arg = PyString_FromString(self->lines[i].start);
643 PyObject *arglist = PyTuple_Pack(1, arg);
643 PyObject *arglist = PyTuple_Pack(1, arg);
644 PyObject *result = PyObject_CallObject(matchfn, arglist);
644 PyObject *result = PyObject_CallObject(matchfn, arglist);
645 Py_DECREF(arglist);
645 Py_DECREF(arglist);
646 Py_DECREF(arg);
646 Py_DECREF(arg);
647 /* if the callback raised an exception, just let it
647 /* if the callback raised an exception, just let it
648 * through and give up */
648 * through and give up */
649 if (!result) {
649 if (!result) {
650 free(copy->lines);
650 free(copy->lines);
651 Py_DECREF(self->pydata);
651 Py_DECREF(self->pydata);
652 return NULL;
652 return NULL;
653 }
653 }
654 if (PyObject_IsTrue(result)) {
654 if (PyObject_IsTrue(result)) {
655 assert(!(self->lines[i].from_malloc));
655 assert(!(self->lines[i].from_malloc));
656 copy->lines[copy->numlines++] = self->lines[i];
656 copy->lines[copy->numlines++] = self->lines[i];
657 }
657 }
658 Py_DECREF(result);
658 Py_DECREF(result);
659 }
659 }
660 copy->livelines = copy->numlines;
660 copy->livelines = copy->numlines;
661 return copy;
661 return copy;
662 nomem:
662 nomem:
663 PyErr_NoMemory();
663 PyErr_NoMemory();
664 Py_XDECREF(copy);
664 Py_XDECREF(copy);
665 return NULL;
665 return NULL;
666 }
666 }
667
667
668 static PyObject *lazymanifest_diff(lazymanifest *self, PyObject *args)
668 static PyObject *lazymanifest_diff(lazymanifest *self, PyObject *args)
669 {
669 {
670 lazymanifest *other;
670 lazymanifest *other;
671 PyObject *pyclean = NULL;
671 PyObject *pyclean = NULL;
672 bool listclean;
672 bool listclean;
673 PyObject *emptyTup = NULL, *ret = NULL;
673 PyObject *emptyTup = NULL, *ret = NULL;
674 PyObject *es;
674 PyObject *es;
675 int sneedle = 0, oneedle = 0;
675 int sneedle = 0, oneedle = 0;
676 if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) {
676 if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) {
677 return NULL;
677 return NULL;
678 }
678 }
679 listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean);
679 listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean);
680 es = PyString_FromString("");
680 es = PyString_FromString("");
681 if (!es) {
681 if (!es) {
682 goto nomem;
682 goto nomem;
683 }
683 }
684 emptyTup = PyTuple_Pack(2, Py_None, es);
684 emptyTup = PyTuple_Pack(2, Py_None, es);
685 Py_DECREF(es);
685 Py_DECREF(es);
686 if (!emptyTup) {
686 if (!emptyTup) {
687 goto nomem;
687 goto nomem;
688 }
688 }
689 ret = PyDict_New();
689 ret = PyDict_New();
690 if (!ret) {
690 if (!ret) {
691 goto nomem;
691 goto nomem;
692 }
692 }
693 while (sneedle != self->numlines || oneedle != other->numlines) {
693 while (sneedle != self->numlines || oneedle != other->numlines) {
694 line *left = self->lines + sneedle;
694 line *left = self->lines + sneedle;
695 line *right = other->lines + oneedle;
695 line *right = other->lines + oneedle;
696 int result;
696 int result;
697 PyObject *key;
697 PyObject *key;
698 PyObject *outer;
698 PyObject *outer;
699 /* If we're looking at a deleted entry and it's not
699 /* If we're looking at a deleted entry and it's not
700 * the end of the manifest, just skip it. */
700 * the end of the manifest, just skip it. */
701 if (left->deleted && sneedle < self->numlines) {
701 if (left->deleted && sneedle < self->numlines) {
702 sneedle++;
702 sneedle++;
703 continue;
703 continue;
704 }
704 }
705 if (right->deleted && oneedle < other->numlines) {
705 if (right->deleted && oneedle < other->numlines) {
706 oneedle++;
706 oneedle++;
707 continue;
707 continue;
708 }
708 }
709 /* if we're at the end of either manifest, then we
709 /* if we're at the end of either manifest, then we
710 * know the remaining items are adds so we can skip
710 * know the remaining items are adds so we can skip
711 * the strcmp. */
711 * the strcmp. */
712 if (sneedle == self->numlines) {
712 if (sneedle == self->numlines) {
713 result = 1;
713 result = 1;
714 } else if (oneedle == other->numlines) {
714 } else if (oneedle == other->numlines) {
715 result = -1;
715 result = -1;
716 } else {
716 } else {
717 result = linecmp(left, right);
717 result = linecmp(left, right);
718 }
718 }
719 key = result <= 0 ?
719 key = result <= 0 ?
720 PyString_FromString(left->start) :
720 PyString_FromString(left->start) :
721 PyString_FromString(right->start);
721 PyString_FromString(right->start);
722 if (!key)
722 if (!key)
723 goto nomem;
723 goto nomem;
724 if (result < 0) {
724 if (result < 0) {
725 PyObject *l = hashflags(left);
725 PyObject *l = hashflags(left);
726 if (!l) {
726 if (!l) {
727 goto nomem;
727 goto nomem;
728 }
728 }
729 outer = PyTuple_Pack(2, l, emptyTup);
729 outer = PyTuple_Pack(2, l, emptyTup);
730 Py_DECREF(l);
730 Py_DECREF(l);
731 if (!outer) {
731 if (!outer) {
732 goto nomem;
732 goto nomem;
733 }
733 }
734 PyDict_SetItem(ret, key, outer);
734 PyDict_SetItem(ret, key, outer);
735 Py_DECREF(outer);
735 Py_DECREF(outer);
736 sneedle++;
736 sneedle++;
737 } else if (result > 0) {
737 } else if (result > 0) {
738 PyObject *r = hashflags(right);
738 PyObject *r = hashflags(right);
739 if (!r) {
739 if (!r) {
740 goto nomem;
740 goto nomem;
741 }
741 }
742 outer = PyTuple_Pack(2, emptyTup, r);
742 outer = PyTuple_Pack(2, emptyTup, r);
743 Py_DECREF(r);
743 Py_DECREF(r);
744 if (!outer) {
744 if (!outer) {
745 goto nomem;
745 goto nomem;
746 }
746 }
747 PyDict_SetItem(ret, key, outer);
747 PyDict_SetItem(ret, key, outer);
748 Py_DECREF(outer);
748 Py_DECREF(outer);
749 oneedle++;
749 oneedle++;
750 } else {
750 } else {
751 /* file exists in both manifests */
751 /* file exists in both manifests */
752 if (left->len != right->len
752 if (left->len != right->len
753 || memcmp(left->start, right->start, left->len)
753 || memcmp(left->start, right->start, left->len)
754 || left->hash_suffix != right->hash_suffix) {
754 || left->hash_suffix != right->hash_suffix) {
755 PyObject *l = hashflags(left);
755 PyObject *l = hashflags(left);
756 PyObject *r;
756 PyObject *r;
757 if (!l) {
757 if (!l) {
758 goto nomem;
758 goto nomem;
759 }
759 }
760 r = hashflags(right);
760 r = hashflags(right);
761 if (!r) {
761 if (!r) {
762 Py_DECREF(l);
762 Py_DECREF(l);
763 goto nomem;
763 goto nomem;
764 }
764 }
765 outer = PyTuple_Pack(2, l, r);
765 outer = PyTuple_Pack(2, l, r);
766 Py_DECREF(l);
766 Py_DECREF(l);
767 Py_DECREF(r);
767 Py_DECREF(r);
768 if (!outer) {
768 if (!outer) {
769 goto nomem;
769 goto nomem;
770 }
770 }
771 PyDict_SetItem(ret, key, outer);
771 PyDict_SetItem(ret, key, outer);
772 Py_DECREF(outer);
772 Py_DECREF(outer);
773 } else if (listclean) {
773 } else if (listclean) {
774 PyDict_SetItem(ret, key, Py_None);
774 PyDict_SetItem(ret, key, Py_None);
775 }
775 }
776 sneedle++;
776 sneedle++;
777 oneedle++;
777 oneedle++;
778 }
778 }
779 Py_DECREF(key);
779 Py_DECREF(key);
780 }
780 }
781 Py_DECREF(emptyTup);
781 Py_DECREF(emptyTup);
782 return ret;
782 return ret;
783 nomem:
783 nomem:
784 PyErr_NoMemory();
784 PyErr_NoMemory();
785 Py_XDECREF(ret);
785 Py_XDECREF(ret);
786 Py_XDECREF(emptyTup);
786 Py_XDECREF(emptyTup);
787 return NULL;
787 return NULL;
788 }
788 }
789
789
790 static PyMethodDef lazymanifest_methods[] = {
790 static PyMethodDef lazymanifest_methods[] = {
791 {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS,
791 {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS,
792 "Make a copy of this lazymanifest."},
792 "Make a copy of this lazymanifest."},
793 {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O,
793 {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O,
794 "Make a copy of this manifest filtered by matchfn."},
794 "Make a copy of this manifest filtered by matchfn."},
795 {"diff", (PyCFunction)lazymanifest_diff, METH_VARARGS,
795 {"diff", (PyCFunction)lazymanifest_diff, METH_VARARGS,
796 "Compare this lazymanifest to another one."},
796 "Compare this lazymanifest to another one."},
797 {"text", (PyCFunction)lazymanifest_text, METH_NOARGS,
797 {"text", (PyCFunction)lazymanifest_text, METH_NOARGS,
798 "Encode this manifest to text."},
798 "Encode this manifest to text."},
799 {NULL},
799 {NULL},
800 };
800 };
801
801
802 static PyTypeObject lazymanifestType = {
802 static PyTypeObject lazymanifestType = {
803 PyObject_HEAD_INIT(NULL)
803 PyObject_HEAD_INIT(NULL)
804 0, /* ob_size */
804 0, /* ob_size */
805 "parsers.lazymanifest", /* tp_name */
805 "parsers.lazymanifest", /* tp_name */
806 sizeof(lazymanifest), /* tp_basicsize */
806 sizeof(lazymanifest), /* tp_basicsize */
807 0, /* tp_itemsize */
807 0, /* tp_itemsize */
808 (destructor)lazymanifest_dealloc, /* tp_dealloc */
808 (destructor)lazymanifest_dealloc, /* tp_dealloc */
809 0, /* tp_print */
809 0, /* tp_print */
810 0, /* tp_getattr */
810 0, /* tp_getattr */
811 0, /* tp_setattr */
811 0, /* tp_setattr */
812 0, /* tp_compare */
812 0, /* tp_compare */
813 0, /* tp_repr */
813 0, /* tp_repr */
814 0, /* tp_as_number */
814 0, /* tp_as_number */
815 &lazymanifest_seq_meths, /* tp_as_sequence */
815 &lazymanifest_seq_meths, /* tp_as_sequence */
816 &lazymanifest_mapping_methods, /* tp_as_mapping */
816 &lazymanifest_mapping_methods, /* tp_as_mapping */
817 0, /* tp_hash */
817 0, /* tp_hash */
818 0, /* tp_call */
818 0, /* tp_call */
819 0, /* tp_str */
819 0, /* tp_str */
820 0, /* tp_getattro */
820 0, /* tp_getattro */
821 0, /* tp_setattro */
821 0, /* tp_setattro */
822 0, /* tp_as_buffer */
822 0, /* tp_as_buffer */
823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_SEQUENCE_IN, /* tp_flags */
823 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_SEQUENCE_IN, /* tp_flags */
824 "TODO(augie)", /* tp_doc */
824 "TODO(augie)", /* tp_doc */
825 0, /* tp_traverse */
825 0, /* tp_traverse */
826 0, /* tp_clear */
826 0, /* tp_clear */
827 0, /* tp_richcompare */
827 0, /* tp_richcompare */
828 0, /* tp_weaklistoffset */
828 0, /* tp_weaklistoffset */
829 (getiterfunc)lazymanifest_getiter, /* tp_iter */
829 (getiterfunc)lazymanifest_getiter, /* tp_iter */
830 0, /* tp_iternext */
830 0, /* tp_iternext */
831 lazymanifest_methods, /* tp_methods */
831 lazymanifest_methods, /* tp_methods */
832 0, /* tp_members */
832 0, /* tp_members */
833 0, /* tp_getset */
833 0, /* tp_getset */
834 0, /* tp_base */
834 0, /* tp_base */
835 0, /* tp_dict */
835 0, /* tp_dict */
836 0, /* tp_descr_get */
836 0, /* tp_descr_get */
837 0, /* tp_descr_set */
837 0, /* tp_descr_set */
838 0, /* tp_dictoffset */
838 0, /* tp_dictoffset */
839 (initproc)lazymanifest_init, /* tp_init */
839 (initproc)lazymanifest_init, /* tp_init */
840 0, /* tp_alloc */
840 0, /* tp_alloc */
841 };
841 };
842
842
843 void manifest_module_init(PyObject * mod)
843 void manifest_module_init(PyObject * mod)
844 {
844 {
845 lazymanifestType.tp_new = PyType_GenericNew;
845 lazymanifestType.tp_new = PyType_GenericNew;
846 if (PyType_Ready(&lazymanifestType) < 0)
846 if (PyType_Ready(&lazymanifestType) < 0)
847 return;
847 return;
848 Py_INCREF(&lazymanifestType);
848 Py_INCREF(&lazymanifestType);
849
849
850 PyModule_AddObject(mod, "lazymanifest",
850 PyModule_AddObject(mod, "lazymanifest",
851 (PyObject *)&lazymanifestType);
851 (PyObject *)&lazymanifestType);
852 }
852 }
General Comments 0
You need to be logged in to leave comments. Login now