##// END OF EJS Templates
parsers: use Py_INCREF safely
Bryan O'Sullivan -
r19726:b3c8c6f2 default
parent child Browse files
Show More
@@ -1,1953 +1,1954 b''
1 1 /*
2 2 parsers.c - efficient content parsing
3 3
4 4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
5 5
6 6 This software may be used and distributed according to the terms of
7 7 the GNU General Public License, incorporated herein by reference.
8 8 */
9 9
10 10 #include <Python.h>
11 11 #include <ctype.h>
12 12 #include <stddef.h>
13 13 #include <string.h>
14 14
15 15 #include "util.h"
16 16
17 17 static int8_t hextable[256] = {
18 18 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
19 19 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
20 20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
21 21 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
22 22 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
23 23 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
24 24 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
25 25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
26 26 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27 27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
34 34 };
35 35
36 36 static inline int hexdigit(const char *p, Py_ssize_t off)
37 37 {
38 38 int8_t val = hextable[(unsigned char)p[off]];
39 39
40 40 if (val >= 0) {
41 41 return val;
42 42 }
43 43
44 44 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
45 45 return 0;
46 46 }
47 47
48 48 /*
49 49 * Turn a hex-encoded string into binary.
50 50 */
51 51 static PyObject *unhexlify(const char *str, int len)
52 52 {
53 53 PyObject *ret;
54 54 char *d;
55 55 int i;
56 56
57 57 ret = PyBytes_FromStringAndSize(NULL, len / 2);
58 58
59 59 if (!ret)
60 60 return NULL;
61 61
62 62 d = PyBytes_AsString(ret);
63 63
64 64 for (i = 0; i < len;) {
65 65 int hi = hexdigit(str, i++);
66 66 int lo = hexdigit(str, i++);
67 67 *d++ = (hi << 4) | lo;
68 68 }
69 69
70 70 return ret;
71 71 }
72 72
73 73 /*
74 74 * This code assumes that a manifest is stitched together with newline
75 75 * ('\n') characters.
76 76 */
77 77 static PyObject *parse_manifest(PyObject *self, PyObject *args)
78 78 {
79 79 PyObject *mfdict, *fdict;
80 80 char *str, *cur, *start, *zero;
81 81 int len;
82 82
83 83 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
84 84 &PyDict_Type, &mfdict,
85 85 &PyDict_Type, &fdict,
86 86 &str, &len))
87 87 goto quit;
88 88
89 89 for (start = cur = str, zero = NULL; cur < str + len; cur++) {
90 90 PyObject *file = NULL, *node = NULL;
91 91 PyObject *flags = NULL;
92 92 ptrdiff_t nlen;
93 93
94 94 if (!*cur) {
95 95 zero = cur;
96 96 continue;
97 97 }
98 98 else if (*cur != '\n')
99 99 continue;
100 100
101 101 if (!zero) {
102 102 PyErr_SetString(PyExc_ValueError,
103 103 "manifest entry has no separator");
104 104 goto quit;
105 105 }
106 106
107 107 file = PyBytes_FromStringAndSize(start, zero - start);
108 108
109 109 if (!file)
110 110 goto bail;
111 111
112 112 nlen = cur - zero - 1;
113 113
114 114 node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen);
115 115 if (!node)
116 116 goto bail;
117 117
118 118 if (nlen > 40) {
119 119 flags = PyBytes_FromStringAndSize(zero + 41,
120 120 nlen - 40);
121 121 if (!flags)
122 122 goto bail;
123 123
124 124 if (PyDict_SetItem(fdict, file, flags) == -1)
125 125 goto bail;
126 126 }
127 127
128 128 if (PyDict_SetItem(mfdict, file, node) == -1)
129 129 goto bail;
130 130
131 131 start = cur + 1;
132 132 zero = NULL;
133 133
134 134 Py_XDECREF(flags);
135 135 Py_XDECREF(node);
136 136 Py_XDECREF(file);
137 137 continue;
138 138 bail:
139 139 Py_XDECREF(flags);
140 140 Py_XDECREF(node);
141 141 Py_XDECREF(file);
142 142 goto quit;
143 143 }
144 144
145 145 if (len > 0 && *(cur - 1) != '\n') {
146 146 PyErr_SetString(PyExc_ValueError,
147 147 "manifest contains trailing garbage");
148 148 goto quit;
149 149 }
150 150
151 151 Py_INCREF(Py_None);
152 152 return Py_None;
153 153 quit:
154 154 return NULL;
155 155 }
156 156
157 157 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
158 158 {
159 159 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
160 160 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
161 161 char state, *str, *cur, *end, *cpos;
162 162 int mode, size, mtime;
163 163 unsigned int flen;
164 164 int len;
165 165
166 166 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
167 167 &PyDict_Type, &dmap,
168 168 &PyDict_Type, &cmap,
169 169 &str, &len))
170 170 goto quit;
171 171
172 172 /* read parents */
173 173 if (len < 40)
174 174 goto quit;
175 175
176 176 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
177 177 if (!parents)
178 178 goto quit;
179 179
180 180 /* read filenames */
181 181 cur = str + 40;
182 182 end = str + len;
183 183
184 184 while (cur < end - 17) {
185 185 /* unpack header */
186 186 state = *cur;
187 187 mode = getbe32(cur + 1);
188 188 size = getbe32(cur + 5);
189 189 mtime = getbe32(cur + 9);
190 190 flen = getbe32(cur + 13);
191 191 cur += 17;
192 192 if (cur + flen > end || cur + flen < cur) {
193 193 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
194 194 goto quit;
195 195 }
196 196
197 197 entry = Py_BuildValue("ciii", state, mode, size, mtime);
198 198 if (!entry)
199 199 goto quit;
200 200 PyObject_GC_UnTrack(entry); /* don't waste time with this */
201 201
202 202 cpos = memchr(cur, 0, flen);
203 203 if (cpos) {
204 204 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
205 205 cname = PyBytes_FromStringAndSize(cpos + 1,
206 206 flen - (cpos - cur) - 1);
207 207 if (!fname || !cname ||
208 208 PyDict_SetItem(cmap, fname, cname) == -1 ||
209 209 PyDict_SetItem(dmap, fname, entry) == -1)
210 210 goto quit;
211 211 Py_DECREF(cname);
212 212 } else {
213 213 fname = PyBytes_FromStringAndSize(cur, flen);
214 214 if (!fname ||
215 215 PyDict_SetItem(dmap, fname, entry) == -1)
216 216 goto quit;
217 217 }
218 218 cur += flen;
219 219 Py_DECREF(fname);
220 220 Py_DECREF(entry);
221 221 fname = cname = entry = NULL;
222 222 }
223 223
224 224 ret = parents;
225 225 Py_INCREF(ret);
226 226 quit:
227 227 Py_XDECREF(fname);
228 228 Py_XDECREF(cname);
229 229 Py_XDECREF(entry);
230 230 Py_XDECREF(parents);
231 231 return ret;
232 232 }
233 233
234 234 static inline int getintat(PyObject *tuple, int off, uint32_t *v)
235 235 {
236 236 PyObject *o = PyTuple_GET_ITEM(tuple, off);
237 237 long val;
238 238
239 239 if (PyInt_Check(o))
240 240 val = PyInt_AS_LONG(o);
241 241 else if (PyLong_Check(o)) {
242 242 val = PyLong_AsLong(o);
243 243 if (val == -1 && PyErr_Occurred())
244 244 return -1;
245 245 } else {
246 246 PyErr_SetString(PyExc_TypeError, "expected an int or long");
247 247 return -1;
248 248 }
249 249 if (LONG_MAX > INT_MAX && (val > INT_MAX || val < INT_MIN)) {
250 250 PyErr_SetString(PyExc_OverflowError,
251 251 "Python value to large to convert to uint32_t");
252 252 return -1;
253 253 }
254 254 *v = (uint32_t)val;
255 255 return 0;
256 256 }
257 257
258 258 static PyObject *dirstate_unset;
259 259
260 260 /*
261 261 * Efficiently pack a dirstate object into its on-disk format.
262 262 */
263 263 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
264 264 {
265 265 PyObject *packobj = NULL;
266 266 PyObject *map, *copymap, *pl;
267 267 Py_ssize_t nbytes, pos, l;
268 268 PyObject *k, *v, *pn;
269 269 char *p, *s;
270 270 double now;
271 271
272 272 if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate",
273 273 &PyDict_Type, &map, &PyDict_Type, &copymap,
274 274 &pl, &now))
275 275 return NULL;
276 276
277 277 if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) {
278 278 PyErr_SetString(PyExc_TypeError, "expected 2-element sequence");
279 279 return NULL;
280 280 }
281 281
282 282 /* Figure out how much we need to allocate. */
283 283 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
284 284 PyObject *c;
285 285 if (!PyString_Check(k)) {
286 286 PyErr_SetString(PyExc_TypeError, "expected string key");
287 287 goto bail;
288 288 }
289 289 nbytes += PyString_GET_SIZE(k) + 17;
290 290 c = PyDict_GetItem(copymap, k);
291 291 if (c) {
292 292 if (!PyString_Check(c)) {
293 293 PyErr_SetString(PyExc_TypeError,
294 294 "expected string key");
295 295 goto bail;
296 296 }
297 297 nbytes += PyString_GET_SIZE(c) + 1;
298 298 }
299 299 }
300 300
301 301 packobj = PyString_FromStringAndSize(NULL, nbytes);
302 302 if (packobj == NULL)
303 303 goto bail;
304 304
305 305 p = PyString_AS_STRING(packobj);
306 306
307 307 pn = PySequence_ITEM(pl, 0);
308 308 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
309 309 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
310 310 goto bail;
311 311 }
312 312 memcpy(p, s, l);
313 313 p += 20;
314 314 pn = PySequence_ITEM(pl, 1);
315 315 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
316 316 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
317 317 goto bail;
318 318 }
319 319 memcpy(p, s, l);
320 320 p += 20;
321 321
322 322 for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) {
323 323 uint32_t mode, size, mtime;
324 324 Py_ssize_t len, l;
325 325 PyObject *o;
326 326 char *s, *t;
327 327
328 328 if (!PyTuple_Check(v) || PyTuple_GET_SIZE(v) != 4) {
329 329 PyErr_SetString(PyExc_TypeError, "expected a 4-tuple");
330 330 goto bail;
331 331 }
332 332 o = PyTuple_GET_ITEM(v, 0);
333 333 if (PyString_AsStringAndSize(o, &s, &l) == -1 || l != 1) {
334 334 PyErr_SetString(PyExc_TypeError, "expected one byte");
335 335 goto bail;
336 336 }
337 337 *p++ = *s;
338 338 if (getintat(v, 1, &mode) == -1)
339 339 goto bail;
340 340 if (getintat(v, 2, &size) == -1)
341 341 goto bail;
342 342 if (getintat(v, 3, &mtime) == -1)
343 343 goto bail;
344 344 if (*s == 'n' && mtime == (uint32_t)now) {
345 345 /* See pure/parsers.py:pack_dirstate for why we do
346 346 * this. */
347 347 if (PyDict_SetItem(map, k, dirstate_unset) == -1)
348 348 goto bail;
349 349 mtime = -1;
350 350 }
351 351 putbe32(mode, p);
352 352 putbe32(size, p + 4);
353 353 putbe32(mtime, p + 8);
354 354 t = p + 12;
355 355 p += 16;
356 356 len = PyString_GET_SIZE(k);
357 357 memcpy(p, PyString_AS_STRING(k), len);
358 358 p += len;
359 359 o = PyDict_GetItem(copymap, k);
360 360 if (o) {
361 361 *p++ = '\0';
362 362 l = PyString_GET_SIZE(o);
363 363 memcpy(p, PyString_AS_STRING(o), l);
364 364 p += l;
365 365 len += l + 1;
366 366 }
367 367 putbe32((uint32_t)len, t);
368 368 }
369 369
370 370 pos = p - PyString_AS_STRING(packobj);
371 371 if (pos != nbytes) {
372 372 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
373 373 (long)pos, (long)nbytes);
374 374 goto bail;
375 375 }
376 376
377 377 return packobj;
378 378 bail:
379 379 Py_XDECREF(packobj);
380 380 return NULL;
381 381 }
382 382
383 383 /*
384 384 * A base-16 trie for fast node->rev mapping.
385 385 *
386 386 * Positive value is index of the next node in the trie
387 387 * Negative value is a leaf: -(rev + 1)
388 388 * Zero is empty
389 389 */
390 390 typedef struct {
391 391 int children[16];
392 392 } nodetree;
393 393
394 394 /*
395 395 * This class has two behaviours.
396 396 *
397 397 * When used in a list-like way (with integer keys), we decode an
398 398 * entry in a RevlogNG index file on demand. Our last entry is a
399 399 * sentinel, always a nullid. We have limited support for
400 400 * integer-keyed insert and delete, only at elements right before the
401 401 * sentinel.
402 402 *
403 403 * With string keys, we lazily perform a reverse mapping from node to
404 404 * rev, using a base-16 trie.
405 405 */
406 406 typedef struct {
407 407 PyObject_HEAD
408 408 /* Type-specific fields go here. */
409 409 PyObject *data; /* raw bytes of index */
410 410 PyObject **cache; /* cached tuples */
411 411 const char **offsets; /* populated on demand */
412 412 Py_ssize_t raw_length; /* original number of elements */
413 413 Py_ssize_t length; /* current number of elements */
414 414 PyObject *added; /* populated on demand */
415 415 PyObject *headrevs; /* cache, invalidated on changes */
416 416 nodetree *nt; /* base-16 trie */
417 417 int ntlength; /* # nodes in use */
418 418 int ntcapacity; /* # nodes allocated */
419 419 int ntdepth; /* maximum depth of tree */
420 420 int ntsplits; /* # splits performed */
421 421 int ntrev; /* last rev scanned */
422 422 int ntlookups; /* # lookups */
423 423 int ntmisses; /* # lookups that miss the cache */
424 424 int inlined;
425 425 } indexObject;
426 426
427 427 static Py_ssize_t index_length(const indexObject *self)
428 428 {
429 429 if (self->added == NULL)
430 430 return self->length;
431 431 return self->length + PyList_GET_SIZE(self->added);
432 432 }
433 433
434 434 static PyObject *nullentry;
435 435 static const char nullid[20];
436 436
437 437 static long inline_scan(indexObject *self, const char **offsets);
438 438
439 439 #if LONG_MAX == 0x7fffffffL
440 440 static char *tuple_format = "Kiiiiiis#";
441 441 #else
442 442 static char *tuple_format = "kiiiiiis#";
443 443 #endif
444 444
445 445 /* A RevlogNG v1 index entry is 64 bytes long. */
446 446 static const long v1_hdrsize = 64;
447 447
448 448 /*
449 449 * Return a pointer to the beginning of a RevlogNG record.
450 450 */
451 451 static const char *index_deref(indexObject *self, Py_ssize_t pos)
452 452 {
453 453 if (self->inlined && pos > 0) {
454 454 if (self->offsets == NULL) {
455 455 self->offsets = malloc(self->raw_length *
456 456 sizeof(*self->offsets));
457 457 if (self->offsets == NULL)
458 458 return (const char *)PyErr_NoMemory();
459 459 inline_scan(self, self->offsets);
460 460 }
461 461 return self->offsets[pos];
462 462 }
463 463
464 464 return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
465 465 }
466 466
467 467 /*
468 468 * RevlogNG format (all in big endian, data may be inlined):
469 469 * 6 bytes: offset
470 470 * 2 bytes: flags
471 471 * 4 bytes: compressed length
472 472 * 4 bytes: uncompressed length
473 473 * 4 bytes: base revision
474 474 * 4 bytes: link revision
475 475 * 4 bytes: parent 1 revision
476 476 * 4 bytes: parent 2 revision
477 477 * 32 bytes: nodeid (only 20 bytes used)
478 478 */
479 479 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
480 480 {
481 481 uint64_t offset_flags;
482 482 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
483 483 const char *c_node_id;
484 484 const char *data;
485 485 Py_ssize_t length = index_length(self);
486 486 PyObject *entry;
487 487
488 488 if (pos < 0)
489 489 pos += length;
490 490
491 491 if (pos < 0 || pos >= length) {
492 492 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
493 493 return NULL;
494 494 }
495 495
496 496 if (pos == length - 1) {
497 497 Py_INCREF(nullentry);
498 498 return nullentry;
499 499 }
500 500
501 501 if (pos >= self->length - 1) {
502 502 PyObject *obj;
503 503 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
504 504 Py_INCREF(obj);
505 505 return obj;
506 506 }
507 507
508 508 if (self->cache) {
509 509 if (self->cache[pos]) {
510 510 Py_INCREF(self->cache[pos]);
511 511 return self->cache[pos];
512 512 }
513 513 } else {
514 514 self->cache = calloc(self->raw_length, sizeof(PyObject *));
515 515 if (self->cache == NULL)
516 516 return PyErr_NoMemory();
517 517 }
518 518
519 519 data = index_deref(self, pos);
520 520 if (data == NULL)
521 521 return NULL;
522 522
523 523 offset_flags = getbe32(data + 4);
524 524 if (pos == 0) /* mask out version number for the first entry */
525 525 offset_flags &= 0xFFFF;
526 526 else {
527 527 uint32_t offset_high = getbe32(data);
528 528 offset_flags |= ((uint64_t)offset_high) << 32;
529 529 }
530 530
531 531 comp_len = getbe32(data + 8);
532 532 uncomp_len = getbe32(data + 12);
533 533 base_rev = getbe32(data + 16);
534 534 link_rev = getbe32(data + 20);
535 535 parent_1 = getbe32(data + 24);
536 536 parent_2 = getbe32(data + 28);
537 537 c_node_id = data + 32;
538 538
539 539 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
540 540 uncomp_len, base_rev, link_rev,
541 541 parent_1, parent_2, c_node_id, 20);
542 542
543 if (entry)
543 if (entry) {
544 544 PyObject_GC_UnTrack(entry);
545 Py_INCREF(entry);
546 }
545 547
546 548 self->cache[pos] = entry;
547 Py_INCREF(entry);
548 549
549 550 return entry;
550 551 }
551 552
552 553 /*
553 554 * Return the 20-byte SHA of the node corresponding to the given rev.
554 555 */
555 556 static const char *index_node(indexObject *self, Py_ssize_t pos)
556 557 {
557 558 Py_ssize_t length = index_length(self);
558 559 const char *data;
559 560
560 561 if (pos == length - 1 || pos == INT_MAX)
561 562 return nullid;
562 563
563 564 if (pos >= length)
564 565 return NULL;
565 566
566 567 if (pos >= self->length - 1) {
567 568 PyObject *tuple, *str;
568 569 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
569 570 str = PyTuple_GetItem(tuple, 7);
570 571 return str ? PyString_AS_STRING(str) : NULL;
571 572 }
572 573
573 574 data = index_deref(self, pos);
574 575 return data ? data + 32 : NULL;
575 576 }
576 577
577 578 static int nt_insert(indexObject *self, const char *node, int rev);
578 579
579 580 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
580 581 {
581 582 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
582 583 return -1;
583 584 if (*nodelen == 20)
584 585 return 0;
585 586 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
586 587 return -1;
587 588 }
588 589
589 590 static PyObject *index_insert(indexObject *self, PyObject *args)
590 591 {
591 592 PyObject *obj;
592 593 char *node;
593 594 long offset;
594 595 Py_ssize_t len, nodelen;
595 596
596 597 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
597 598 return NULL;
598 599
599 600 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
600 601 PyErr_SetString(PyExc_TypeError, "8-tuple required");
601 602 return NULL;
602 603 }
603 604
604 605 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
605 606 return NULL;
606 607
607 608 len = index_length(self);
608 609
609 610 if (offset < 0)
610 611 offset += len;
611 612
612 613 if (offset != len - 1) {
613 614 PyErr_SetString(PyExc_IndexError,
614 615 "insert only supported at index -1");
615 616 return NULL;
616 617 }
617 618
618 619 if (offset > INT_MAX) {
619 620 PyErr_SetString(PyExc_ValueError,
620 621 "currently only 2**31 revs supported");
621 622 return NULL;
622 623 }
623 624
624 625 if (self->added == NULL) {
625 626 self->added = PyList_New(0);
626 627 if (self->added == NULL)
627 628 return NULL;
628 629 }
629 630
630 631 if (PyList_Append(self->added, obj) == -1)
631 632 return NULL;
632 633
633 634 if (self->nt)
634 635 nt_insert(self, node, (int)offset);
635 636
636 637 Py_CLEAR(self->headrevs);
637 638 Py_RETURN_NONE;
638 639 }
639 640
640 641 static void _index_clearcaches(indexObject *self)
641 642 {
642 643 if (self->cache) {
643 644 Py_ssize_t i;
644 645
645 646 for (i = 0; i < self->raw_length; i++)
646 647 Py_CLEAR(self->cache[i]);
647 648 free(self->cache);
648 649 self->cache = NULL;
649 650 }
650 651 if (self->offsets) {
651 652 free(self->offsets);
652 653 self->offsets = NULL;
653 654 }
654 655 if (self->nt) {
655 656 free(self->nt);
656 657 self->nt = NULL;
657 658 }
658 659 Py_CLEAR(self->headrevs);
659 660 }
660 661
661 662 static PyObject *index_clearcaches(indexObject *self)
662 663 {
663 664 _index_clearcaches(self);
664 665 self->ntlength = self->ntcapacity = 0;
665 666 self->ntdepth = self->ntsplits = 0;
666 667 self->ntrev = -1;
667 668 self->ntlookups = self->ntmisses = 0;
668 669 Py_RETURN_NONE;
669 670 }
670 671
671 672 static PyObject *index_stats(indexObject *self)
672 673 {
673 674 PyObject *obj = PyDict_New();
674 675
675 676 if (obj == NULL)
676 677 return NULL;
677 678
678 679 #define istat(__n, __d) \
679 680 if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
680 681 goto bail;
681 682
682 683 if (self->added) {
683 684 Py_ssize_t len = PyList_GET_SIZE(self->added);
684 685 if (PyDict_SetItemString(obj, "index entries added",
685 686 PyInt_FromSsize_t(len)) == -1)
686 687 goto bail;
687 688 }
688 689
689 690 if (self->raw_length != self->length - 1)
690 691 istat(raw_length, "revs on disk");
691 692 istat(length, "revs in memory");
692 693 istat(ntcapacity, "node trie capacity");
693 694 istat(ntdepth, "node trie depth");
694 695 istat(ntlength, "node trie count");
695 696 istat(ntlookups, "node trie lookups");
696 697 istat(ntmisses, "node trie misses");
697 698 istat(ntrev, "node trie last rev scanned");
698 699 istat(ntsplits, "node trie splits");
699 700
700 701 #undef istat
701 702
702 703 return obj;
703 704
704 705 bail:
705 706 Py_XDECREF(obj);
706 707 return NULL;
707 708 }
708 709
709 710 /*
710 711 * When we cache a list, we want to be sure the caller can't mutate
711 712 * the cached copy.
712 713 */
713 714 static PyObject *list_copy(PyObject *list)
714 715 {
715 716 Py_ssize_t len = PyList_GET_SIZE(list);
716 717 PyObject *newlist = PyList_New(len);
717 718 Py_ssize_t i;
718 719
719 720 if (newlist == NULL)
720 721 return NULL;
721 722
722 723 for (i = 0; i < len; i++) {
723 724 PyObject *obj = PyList_GET_ITEM(list, i);
724 725 Py_INCREF(obj);
725 726 PyList_SET_ITEM(newlist, i, obj);
726 727 }
727 728
728 729 return newlist;
729 730 }
730 731
731 732 static PyObject *index_headrevs(indexObject *self)
732 733 {
733 734 Py_ssize_t i, len, addlen;
734 735 char *nothead = NULL;
735 736 PyObject *heads;
736 737
737 738 if (self->headrevs)
738 739 return list_copy(self->headrevs);
739 740
740 741 len = index_length(self) - 1;
741 742 heads = PyList_New(0);
742 743 if (heads == NULL)
743 744 goto bail;
744 745 if (len == 0) {
745 746 PyObject *nullid = PyInt_FromLong(-1);
746 747 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
747 748 Py_XDECREF(nullid);
748 749 goto bail;
749 750 }
750 751 goto done;
751 752 }
752 753
753 754 nothead = calloc(len, 1);
754 755 if (nothead == NULL)
755 756 goto bail;
756 757
757 758 for (i = 0; i < self->raw_length; i++) {
758 759 const char *data = index_deref(self, i);
759 760 int parent_1 = getbe32(data + 24);
760 761 int parent_2 = getbe32(data + 28);
761 762 if (parent_1 >= 0)
762 763 nothead[parent_1] = 1;
763 764 if (parent_2 >= 0)
764 765 nothead[parent_2] = 1;
765 766 }
766 767
767 768 addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
768 769
769 770 for (i = 0; i < addlen; i++) {
770 771 PyObject *rev = PyList_GET_ITEM(self->added, i);
771 772 PyObject *p1 = PyTuple_GET_ITEM(rev, 5);
772 773 PyObject *p2 = PyTuple_GET_ITEM(rev, 6);
773 774 long parent_1, parent_2;
774 775
775 776 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
776 777 PyErr_SetString(PyExc_TypeError,
777 778 "revlog parents are invalid");
778 779 goto bail;
779 780 }
780 781 parent_1 = PyInt_AS_LONG(p1);
781 782 parent_2 = PyInt_AS_LONG(p2);
782 783 if (parent_1 >= 0)
783 784 nothead[parent_1] = 1;
784 785 if (parent_2 >= 0)
785 786 nothead[parent_2] = 1;
786 787 }
787 788
788 789 for (i = 0; i < len; i++) {
789 790 PyObject *head;
790 791
791 792 if (nothead[i])
792 793 continue;
793 794 head = PyInt_FromLong(i);
794 795 if (head == NULL || PyList_Append(heads, head) == -1) {
795 796 Py_XDECREF(head);
796 797 goto bail;
797 798 }
798 799 }
799 800
800 801 done:
801 802 self->headrevs = heads;
802 803 free(nothead);
803 804 return list_copy(self->headrevs);
804 805 bail:
805 806 Py_XDECREF(heads);
806 807 free(nothead);
807 808 return NULL;
808 809 }
809 810
810 811 static inline int nt_level(const char *node, Py_ssize_t level)
811 812 {
812 813 int v = node[level>>1];
813 814 if (!(level & 1))
814 815 v >>= 4;
815 816 return v & 0xf;
816 817 }
817 818
818 819 /*
819 820 * Return values:
820 821 *
821 822 * -4: match is ambiguous (multiple candidates)
822 823 * -2: not found
823 824 * rest: valid rev
824 825 */
825 826 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
826 827 int hex)
827 828 {
828 829 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
829 830 int level, maxlevel, off;
830 831
831 832 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
832 833 return -1;
833 834
834 835 if (self->nt == NULL)
835 836 return -2;
836 837
837 838 if (hex)
838 839 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
839 840 else
840 841 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
841 842
842 843 for (level = off = 0; level < maxlevel; level++) {
843 844 int k = getnybble(node, level);
844 845 nodetree *n = &self->nt[off];
845 846 int v = n->children[k];
846 847
847 848 if (v < 0) {
848 849 const char *n;
849 850 Py_ssize_t i;
850 851
851 852 v = -v - 1;
852 853 n = index_node(self, v);
853 854 if (n == NULL)
854 855 return -2;
855 856 for (i = level; i < maxlevel; i++)
856 857 if (getnybble(node, i) != nt_level(n, i))
857 858 return -2;
858 859 return v;
859 860 }
860 861 if (v == 0)
861 862 return -2;
862 863 off = v;
863 864 }
864 865 /* multiple matches against an ambiguous prefix */
865 866 return -4;
866 867 }
867 868
868 869 static int nt_new(indexObject *self)
869 870 {
870 871 if (self->ntlength == self->ntcapacity) {
871 872 self->ntcapacity *= 2;
872 873 self->nt = realloc(self->nt,
873 874 self->ntcapacity * sizeof(nodetree));
874 875 if (self->nt == NULL) {
875 876 PyErr_SetString(PyExc_MemoryError, "out of memory");
876 877 return -1;
877 878 }
878 879 memset(&self->nt[self->ntlength], 0,
879 880 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
880 881 }
881 882 return self->ntlength++;
882 883 }
883 884
884 885 static int nt_insert(indexObject *self, const char *node, int rev)
885 886 {
886 887 int level = 0;
887 888 int off = 0;
888 889
889 890 while (level < 40) {
890 891 int k = nt_level(node, level);
891 892 nodetree *n;
892 893 int v;
893 894
894 895 n = &self->nt[off];
895 896 v = n->children[k];
896 897
897 898 if (v == 0) {
898 899 n->children[k] = -rev - 1;
899 900 return 0;
900 901 }
901 902 if (v < 0) {
902 903 const char *oldnode = index_node(self, -v - 1);
903 904 int noff;
904 905
905 906 if (!oldnode || !memcmp(oldnode, node, 20)) {
906 907 n->children[k] = -rev - 1;
907 908 return 0;
908 909 }
909 910 noff = nt_new(self);
910 911 if (noff == -1)
911 912 return -1;
912 913 /* self->nt may have been changed by realloc */
913 914 self->nt[off].children[k] = noff;
914 915 off = noff;
915 916 n = &self->nt[off];
916 917 n->children[nt_level(oldnode, ++level)] = v;
917 918 if (level > self->ntdepth)
918 919 self->ntdepth = level;
919 920 self->ntsplits += 1;
920 921 } else {
921 922 level += 1;
922 923 off = v;
923 924 }
924 925 }
925 926
926 927 return -1;
927 928 }
928 929
929 930 static int nt_init(indexObject *self)
930 931 {
931 932 if (self->nt == NULL) {
932 933 self->ntcapacity = self->raw_length < 4
933 934 ? 4 : self->raw_length / 2;
934 935 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
935 936 if (self->nt == NULL) {
936 937 PyErr_NoMemory();
937 938 return -1;
938 939 }
939 940 self->ntlength = 1;
940 941 self->ntrev = (int)index_length(self) - 1;
941 942 self->ntlookups = 1;
942 943 self->ntmisses = 0;
943 944 if (nt_insert(self, nullid, INT_MAX) == -1)
944 945 return -1;
945 946 }
946 947 return 0;
947 948 }
948 949
949 950 /*
950 951 * Return values:
951 952 *
952 953 * -3: error (exception set)
953 954 * -2: not found (no exception set)
954 955 * rest: valid rev
955 956 */
956 957 static int index_find_node(indexObject *self,
957 958 const char *node, Py_ssize_t nodelen)
958 959 {
959 960 int rev;
960 961
961 962 self->ntlookups++;
962 963 rev = nt_find(self, node, nodelen, 0);
963 964 if (rev >= -1)
964 965 return rev;
965 966
966 967 if (nt_init(self) == -1)
967 968 return -3;
968 969
969 970 /*
970 971 * For the first handful of lookups, we scan the entire index,
971 972 * and cache only the matching nodes. This optimizes for cases
972 973 * like "hg tip", where only a few nodes are accessed.
973 974 *
974 975 * After that, we cache every node we visit, using a single
975 976 * scan amortized over multiple lookups. This gives the best
976 977 * bulk performance, e.g. for "hg log".
977 978 */
978 979 if (self->ntmisses++ < 4) {
979 980 for (rev = self->ntrev - 1; rev >= 0; rev--) {
980 981 const char *n = index_node(self, rev);
981 982 if (n == NULL)
982 983 return -2;
983 984 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
984 985 if (nt_insert(self, n, rev) == -1)
985 986 return -3;
986 987 break;
987 988 }
988 989 }
989 990 } else {
990 991 for (rev = self->ntrev - 1; rev >= 0; rev--) {
991 992 const char *n = index_node(self, rev);
992 993 if (n == NULL) {
993 994 self->ntrev = rev + 1;
994 995 return -2;
995 996 }
996 997 if (nt_insert(self, n, rev) == -1) {
997 998 self->ntrev = rev + 1;
998 999 return -3;
999 1000 }
1000 1001 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1001 1002 break;
1002 1003 }
1003 1004 }
1004 1005 self->ntrev = rev;
1005 1006 }
1006 1007
1007 1008 if (rev >= 0)
1008 1009 return rev;
1009 1010 return -2;
1010 1011 }
1011 1012
1012 1013 static PyObject *raise_revlog_error(void)
1013 1014 {
1014 1015 static PyObject *errclass;
1015 1016 PyObject *mod = NULL, *errobj;
1016 1017
1017 1018 if (errclass == NULL) {
1018 1019 PyObject *dict;
1019 1020
1020 1021 mod = PyImport_ImportModule("mercurial.error");
1021 1022 if (mod == NULL)
1022 1023 goto classfail;
1023 1024
1024 1025 dict = PyModule_GetDict(mod);
1025 1026 if (dict == NULL)
1026 1027 goto classfail;
1027 1028
1028 1029 errclass = PyDict_GetItemString(dict, "RevlogError");
1029 1030 if (errclass == NULL) {
1030 1031 PyErr_SetString(PyExc_SystemError,
1031 1032 "could not find RevlogError");
1032 1033 goto classfail;
1033 1034 }
1034 1035 Py_INCREF(errclass);
1035 1036 }
1036 1037
1037 1038 errobj = PyObject_CallFunction(errclass, NULL);
1038 1039 if (errobj == NULL)
1039 1040 return NULL;
1040 1041 PyErr_SetObject(errclass, errobj);
1041 1042 return errobj;
1042 1043
1043 1044 classfail:
1044 1045 Py_XDECREF(mod);
1045 1046 return NULL;
1046 1047 }
1047 1048
1048 1049 static PyObject *index_getitem(indexObject *self, PyObject *value)
1049 1050 {
1050 1051 char *node;
1051 1052 Py_ssize_t nodelen;
1052 1053 int rev;
1053 1054
1054 1055 if (PyInt_Check(value))
1055 1056 return index_get(self, PyInt_AS_LONG(value));
1056 1057
1057 1058 if (node_check(value, &node, &nodelen) == -1)
1058 1059 return NULL;
1059 1060 rev = index_find_node(self, node, nodelen);
1060 1061 if (rev >= -1)
1061 1062 return PyInt_FromLong(rev);
1062 1063 if (rev == -2)
1063 1064 raise_revlog_error();
1064 1065 return NULL;
1065 1066 }
1066 1067
1067 1068 static int nt_partialmatch(indexObject *self, const char *node,
1068 1069 Py_ssize_t nodelen)
1069 1070 {
1070 1071 int rev;
1071 1072
1072 1073 if (nt_init(self) == -1)
1073 1074 return -3;
1074 1075
1075 1076 if (self->ntrev > 0) {
1076 1077 /* ensure that the radix tree is fully populated */
1077 1078 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1078 1079 const char *n = index_node(self, rev);
1079 1080 if (n == NULL)
1080 1081 return -2;
1081 1082 if (nt_insert(self, n, rev) == -1)
1082 1083 return -3;
1083 1084 }
1084 1085 self->ntrev = rev;
1085 1086 }
1086 1087
1087 1088 return nt_find(self, node, nodelen, 1);
1088 1089 }
1089 1090
1090 1091 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1091 1092 {
1092 1093 const char *fullnode;
1093 1094 int nodelen;
1094 1095 char *node;
1095 1096 int rev, i;
1096 1097
1097 1098 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
1098 1099 return NULL;
1099 1100
1100 1101 if (nodelen < 4) {
1101 1102 PyErr_SetString(PyExc_ValueError, "key too short");
1102 1103 return NULL;
1103 1104 }
1104 1105
1105 1106 if (nodelen > 40) {
1106 1107 PyErr_SetString(PyExc_ValueError, "key too long");
1107 1108 return NULL;
1108 1109 }
1109 1110
1110 1111 for (i = 0; i < nodelen; i++)
1111 1112 hexdigit(node, i);
1112 1113 if (PyErr_Occurred()) {
1113 1114 /* input contains non-hex characters */
1114 1115 PyErr_Clear();
1115 1116 Py_RETURN_NONE;
1116 1117 }
1117 1118
1118 1119 rev = nt_partialmatch(self, node, nodelen);
1119 1120
1120 1121 switch (rev) {
1121 1122 case -4:
1122 1123 raise_revlog_error();
1123 1124 case -3:
1124 1125 return NULL;
1125 1126 case -2:
1126 1127 Py_RETURN_NONE;
1127 1128 case -1:
1128 1129 return PyString_FromStringAndSize(nullid, 20);
1129 1130 }
1130 1131
1131 1132 fullnode = index_node(self, rev);
1132 1133 if (fullnode == NULL) {
1133 1134 PyErr_Format(PyExc_IndexError,
1134 1135 "could not access rev %d", rev);
1135 1136 return NULL;
1136 1137 }
1137 1138 return PyString_FromStringAndSize(fullnode, 20);
1138 1139 }
1139 1140
1140 1141 static PyObject *index_m_get(indexObject *self, PyObject *args)
1141 1142 {
1142 1143 Py_ssize_t nodelen;
1143 1144 PyObject *val;
1144 1145 char *node;
1145 1146 int rev;
1146 1147
1147 1148 if (!PyArg_ParseTuple(args, "O", &val))
1148 1149 return NULL;
1149 1150 if (node_check(val, &node, &nodelen) == -1)
1150 1151 return NULL;
1151 1152 rev = index_find_node(self, node, nodelen);
1152 1153 if (rev == -3)
1153 1154 return NULL;
1154 1155 if (rev == -2)
1155 1156 Py_RETURN_NONE;
1156 1157 return PyInt_FromLong(rev);
1157 1158 }
1158 1159
1159 1160 static int index_contains(indexObject *self, PyObject *value)
1160 1161 {
1161 1162 char *node;
1162 1163 Py_ssize_t nodelen;
1163 1164
1164 1165 if (PyInt_Check(value)) {
1165 1166 long rev = PyInt_AS_LONG(value);
1166 1167 return rev >= -1 && rev < index_length(self);
1167 1168 }
1168 1169
1169 1170 if (node_check(value, &node, &nodelen) == -1)
1170 1171 return -1;
1171 1172
1172 1173 switch (index_find_node(self, node, nodelen)) {
1173 1174 case -3:
1174 1175 return -1;
1175 1176 case -2:
1176 1177 return 0;
1177 1178 default:
1178 1179 return 1;
1179 1180 }
1180 1181 }
1181 1182
1182 1183 static inline void index_get_parents(indexObject *self, int rev, int *ps)
1183 1184 {
1184 1185 if (rev >= self->length - 1) {
1185 1186 PyObject *tuple = PyList_GET_ITEM(self->added,
1186 1187 rev - self->length + 1);
1187 1188 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
1188 1189 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
1189 1190 } else {
1190 1191 const char *data = index_deref(self, rev);
1191 1192 ps[0] = getbe32(data + 24);
1192 1193 ps[1] = getbe32(data + 28);
1193 1194 }
1194 1195 }
1195 1196
1196 1197 typedef uint64_t bitmask;
1197 1198
1198 1199 /*
1199 1200 * Given a disjoint set of revs, return all candidates for the
1200 1201 * greatest common ancestor. In revset notation, this is the set
1201 1202 * "heads(::a and ::b and ...)"
1202 1203 */
1203 1204 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1204 1205 int revcount)
1205 1206 {
1206 1207 const bitmask allseen = (1ull << revcount) - 1;
1207 1208 const bitmask poison = 1ull << revcount;
1208 1209 PyObject *gca = PyList_New(0);
1209 1210 int i, v, interesting, left;
1210 1211 int maxrev = -1;
1211 1212 long sp;
1212 1213 bitmask *seen;
1213 1214
1214 1215 for (i = 0; i < revcount; i++) {
1215 1216 if (revs[i] > maxrev)
1216 1217 maxrev = revs[i];
1217 1218 }
1218 1219
1219 1220 seen = calloc(sizeof(*seen), maxrev + 1);
1220 1221 if (seen == NULL)
1221 1222 return PyErr_NoMemory();
1222 1223
1223 1224 for (i = 0; i < revcount; i++)
1224 1225 seen[revs[i]] = 1ull << i;
1225 1226
1226 1227 interesting = left = revcount;
1227 1228
1228 1229 for (v = maxrev; v >= 0 && interesting; v--) {
1229 1230 long sv = seen[v];
1230 1231 int parents[2];
1231 1232
1232 1233 if (!sv)
1233 1234 continue;
1234 1235
1235 1236 if (sv < poison) {
1236 1237 interesting -= 1;
1237 1238 if (sv == allseen) {
1238 1239 PyObject *obj = PyInt_FromLong(v);
1239 1240 if (obj == NULL)
1240 1241 goto bail;
1241 1242 if (PyList_Append(gca, obj) == -1) {
1242 1243 Py_DECREF(obj);
1243 1244 goto bail;
1244 1245 }
1245 1246 sv |= poison;
1246 1247 for (i = 0; i < revcount; i++) {
1247 1248 if (revs[i] == v) {
1248 1249 if (--left <= 1)
1249 1250 goto done;
1250 1251 break;
1251 1252 }
1252 1253 }
1253 1254 }
1254 1255 }
1255 1256 index_get_parents(self, v, parents);
1256 1257
1257 1258 for (i = 0; i < 2; i++) {
1258 1259 int p = parents[i];
1259 1260 if (p == -1)
1260 1261 continue;
1261 1262 sp = seen[p];
1262 1263 if (sv < poison) {
1263 1264 if (sp == 0) {
1264 1265 seen[p] = sv;
1265 1266 interesting++;
1266 1267 }
1267 1268 else if (sp != sv)
1268 1269 seen[p] |= sv;
1269 1270 } else {
1270 1271 if (sp && sp < poison)
1271 1272 interesting--;
1272 1273 seen[p] = sv;
1273 1274 }
1274 1275 }
1275 1276 }
1276 1277
1277 1278 done:
1278 1279 free(seen);
1279 1280 return gca;
1280 1281 bail:
1281 1282 free(seen);
1282 1283 Py_XDECREF(gca);
1283 1284 return NULL;
1284 1285 }
1285 1286
1286 1287 /*
1287 1288 * Given a disjoint set of revs, return the subset with the longest
1288 1289 * path to the root.
1289 1290 */
1290 1291 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1291 1292 {
1292 1293 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1293 1294 static const Py_ssize_t capacity = 24;
1294 1295 int *depth, *interesting = NULL;
1295 1296 int i, j, v, ninteresting;
1296 1297 PyObject *dict = NULL, *keys;
1297 1298 long *seen = NULL;
1298 1299 int maxrev = -1;
1299 1300 long final;
1300 1301
1301 1302 if (revcount > capacity) {
1302 1303 PyErr_Format(PyExc_OverflowError,
1303 1304 "bitset size (%ld) > capacity (%ld)",
1304 1305 (long)revcount, (long)capacity);
1305 1306 return NULL;
1306 1307 }
1307 1308
1308 1309 for (i = 0; i < revcount; i++) {
1309 1310 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1310 1311 if (n > maxrev)
1311 1312 maxrev = n;
1312 1313 }
1313 1314
1314 1315 depth = calloc(sizeof(*depth), maxrev + 1);
1315 1316 if (depth == NULL)
1316 1317 return PyErr_NoMemory();
1317 1318
1318 1319 seen = calloc(sizeof(*seen), maxrev + 1);
1319 1320 if (seen == NULL) {
1320 1321 PyErr_NoMemory();
1321 1322 goto bail;
1322 1323 }
1323 1324
1324 1325 interesting = calloc(sizeof(*interesting), 2 << revcount);
1325 1326 if (interesting == NULL) {
1326 1327 PyErr_NoMemory();
1327 1328 goto bail;
1328 1329 }
1329 1330
1330 1331 if (PyList_Sort(revs) == -1)
1331 1332 goto bail;
1332 1333
1333 1334 for (i = 0; i < revcount; i++) {
1334 1335 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1335 1336 long b = 1l << i;
1336 1337 depth[n] = 1;
1337 1338 seen[n] = b;
1338 1339 interesting[b] = 1;
1339 1340 }
1340 1341
1341 1342 ninteresting = (int)revcount;
1342 1343
1343 1344 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1344 1345 int dv = depth[v];
1345 1346 int parents[2];
1346 1347 long sv;
1347 1348
1348 1349 if (dv == 0)
1349 1350 continue;
1350 1351
1351 1352 sv = seen[v];
1352 1353 index_get_parents(self, v, parents);
1353 1354
1354 1355 for (i = 0; i < 2; i++) {
1355 1356 int p = parents[i];
1356 1357 long nsp, sp;
1357 1358 int dp;
1358 1359
1359 1360 if (p == -1)
1360 1361 continue;
1361 1362
1362 1363 dp = depth[p];
1363 1364 nsp = sp = seen[p];
1364 1365 if (dp <= dv) {
1365 1366 depth[p] = dv + 1;
1366 1367 if (sp != sv) {
1367 1368 interesting[sv] += 1;
1368 1369 nsp = seen[p] = sv;
1369 1370 if (sp) {
1370 1371 interesting[sp] -= 1;
1371 1372 if (interesting[sp] == 0)
1372 1373 ninteresting -= 1;
1373 1374 }
1374 1375 }
1375 1376 }
1376 1377 else if (dv == dp - 1) {
1377 1378 nsp = sp | sv;
1378 1379 if (nsp == sp)
1379 1380 continue;
1380 1381 seen[p] = nsp;
1381 1382 interesting[sp] -= 1;
1382 1383 if (interesting[sp] == 0 && interesting[nsp] > 0)
1383 1384 ninteresting -= 1;
1384 1385 interesting[nsp] += 1;
1385 1386 }
1386 1387 }
1387 1388 interesting[sv] -= 1;
1388 1389 if (interesting[sv] == 0)
1389 1390 ninteresting -= 1;
1390 1391 }
1391 1392
1392 1393 final = 0;
1393 1394 j = ninteresting;
1394 1395 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1395 1396 if (interesting[i] == 0)
1396 1397 continue;
1397 1398 final |= i;
1398 1399 j -= 1;
1399 1400 }
1400 1401 if (final == 0)
1401 1402 return PyList_New(0);
1402 1403
1403 1404 dict = PyDict_New();
1404 1405 if (dict == NULL)
1405 1406 goto bail;
1406 1407
1407 1408 for (i = 0; i < revcount; i++) {
1408 1409 PyObject *key;
1409 1410
1410 1411 if ((final & (1 << i)) == 0)
1411 1412 continue;
1412 1413
1413 1414 key = PyList_GET_ITEM(revs, i);
1414 1415 Py_INCREF(key);
1415 1416 Py_INCREF(Py_None);
1416 1417 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1417 1418 Py_DECREF(key);
1418 1419 Py_DECREF(Py_None);
1419 1420 goto bail;
1420 1421 }
1421 1422 }
1422 1423
1423 1424 keys = PyDict_Keys(dict);
1424 1425
1425 1426 free(depth);
1426 1427 free(seen);
1427 1428 free(interesting);
1428 1429 Py_DECREF(dict);
1429 1430
1430 1431 return keys;
1431 1432 bail:
1432 1433 free(depth);
1433 1434 free(seen);
1434 1435 free(interesting);
1435 1436 Py_XDECREF(dict);
1436 1437
1437 1438 return NULL;
1438 1439 }
1439 1440
1440 1441 /*
1441 1442 * Given a (possibly overlapping) set of revs, return the greatest
1442 1443 * common ancestors: those with the longest path to the root.
1443 1444 */
1444 1445 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1445 1446 {
1446 1447 PyObject *ret = NULL, *gca = NULL;
1447 1448 Py_ssize_t argcount, i, len;
1448 1449 bitmask repeat = 0;
1449 1450 int revcount = 0;
1450 1451 int *revs;
1451 1452
1452 1453 argcount = PySequence_Length(args);
1453 1454 revs = malloc(argcount * sizeof(*revs));
1454 1455 if (argcount > 0 && revs == NULL)
1455 1456 return PyErr_NoMemory();
1456 1457 len = index_length(self) - 1;
1457 1458
1458 1459 for (i = 0; i < argcount; i++) {
1459 1460 static const int capacity = 24;
1460 1461 PyObject *obj = PySequence_GetItem(args, i);
1461 1462 bitmask x;
1462 1463 long val;
1463 1464
1464 1465 if (!PyInt_Check(obj)) {
1465 1466 PyErr_SetString(PyExc_TypeError,
1466 1467 "arguments must all be ints");
1467 1468 goto bail;
1468 1469 }
1469 1470 val = PyInt_AsLong(obj);
1470 1471 if (val == -1) {
1471 1472 ret = PyList_New(0);
1472 1473 goto done;
1473 1474 }
1474 1475 if (val < 0 || val >= len) {
1475 1476 PyErr_SetString(PyExc_IndexError,
1476 1477 "index out of range");
1477 1478 goto bail;
1478 1479 }
1479 1480 /* this cheesy bloom filter lets us avoid some more
1480 1481 * expensive duplicate checks in the common set-is-disjoint
1481 1482 * case */
1482 1483 x = 1ull << (val & 0x3f);
1483 1484 if (repeat & x) {
1484 1485 int k;
1485 1486 for (k = 0; k < revcount; k++) {
1486 1487 if (val == revs[k])
1487 1488 goto duplicate;
1488 1489 }
1489 1490 }
1490 1491 else repeat |= x;
1491 1492 if (revcount >= capacity) {
1492 1493 PyErr_Format(PyExc_OverflowError,
1493 1494 "bitset size (%d) > capacity (%d)",
1494 1495 revcount, capacity);
1495 1496 goto bail;
1496 1497 }
1497 1498 revs[revcount++] = (int)val;
1498 1499 duplicate:;
1499 1500 }
1500 1501
1501 1502 if (revcount == 0) {
1502 1503 ret = PyList_New(0);
1503 1504 goto done;
1504 1505 }
1505 1506 if (revcount == 1) {
1506 1507 PyObject *obj;
1507 1508 ret = PyList_New(1);
1508 1509 if (ret == NULL)
1509 1510 goto bail;
1510 1511 obj = PyInt_FromLong(revs[0]);
1511 1512 if (obj == NULL)
1512 1513 goto bail;
1513 1514 PyList_SET_ITEM(ret, 0, obj);
1514 1515 goto done;
1515 1516 }
1516 1517
1517 1518 gca = find_gca_candidates(self, revs, revcount);
1518 1519 if (gca == NULL)
1519 1520 goto bail;
1520 1521
1521 1522 if (PyList_GET_SIZE(gca) <= 1) {
1522 1523 ret = gca;
1523 1524 Py_INCREF(gca);
1524 1525 }
1525 1526 else if (PyList_GET_SIZE(gca) == 1) {
1526 1527 ret = PyList_GET_ITEM(gca, 0);
1527 1528 Py_INCREF(ret);
1528 1529 }
1529 1530 else ret = find_deepest(self, gca);
1530 1531
1531 1532 done:
1532 1533 free(revs);
1533 1534 Py_XDECREF(gca);
1534 1535
1535 1536 return ret;
1536 1537
1537 1538 bail:
1538 1539 free(revs);
1539 1540 Py_XDECREF(gca);
1540 1541 Py_XDECREF(ret);
1541 1542 return NULL;
1542 1543 }
1543 1544
1544 1545 /*
1545 1546 * Invalidate any trie entries introduced by added revs.
1546 1547 */
1547 1548 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
1548 1549 {
1549 1550 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1550 1551
1551 1552 for (i = start; i < len; i++) {
1552 1553 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1553 1554 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1554 1555
1555 1556 nt_insert(self, PyString_AS_STRING(node), -1);
1556 1557 }
1557 1558
1558 1559 if (start == 0)
1559 1560 Py_CLEAR(self->added);
1560 1561 }
1561 1562
1562 1563 /*
1563 1564 * Delete a numeric range of revs, which must be at the end of the
1564 1565 * range, but exclude the sentinel nullid entry.
1565 1566 */
1566 1567 static int index_slice_del(indexObject *self, PyObject *item)
1567 1568 {
1568 1569 Py_ssize_t start, stop, step, slicelength;
1569 1570 Py_ssize_t length = index_length(self);
1570 1571 int ret = 0;
1571 1572
1572 1573 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1573 1574 &start, &stop, &step, &slicelength) < 0)
1574 1575 return -1;
1575 1576
1576 1577 if (slicelength <= 0)
1577 1578 return 0;
1578 1579
1579 1580 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1580 1581 stop = start;
1581 1582
1582 1583 if (step < 0) {
1583 1584 stop = start + 1;
1584 1585 start = stop + step*(slicelength - 1) - 1;
1585 1586 step = -step;
1586 1587 }
1587 1588
1588 1589 if (step != 1) {
1589 1590 PyErr_SetString(PyExc_ValueError,
1590 1591 "revlog index delete requires step size of 1");
1591 1592 return -1;
1592 1593 }
1593 1594
1594 1595 if (stop != length - 1) {
1595 1596 PyErr_SetString(PyExc_IndexError,
1596 1597 "revlog index deletion indices are invalid");
1597 1598 return -1;
1598 1599 }
1599 1600
1600 1601 if (start < self->length - 1) {
1601 1602 if (self->nt) {
1602 1603 Py_ssize_t i;
1603 1604
1604 1605 for (i = start + 1; i < self->length - 1; i++) {
1605 1606 const char *node = index_node(self, i);
1606 1607
1607 1608 if (node)
1608 1609 nt_insert(self, node, -1);
1609 1610 }
1610 1611 if (self->added)
1611 1612 nt_invalidate_added(self, 0);
1612 1613 if (self->ntrev > start)
1613 1614 self->ntrev = (int)start;
1614 1615 }
1615 1616 self->length = start + 1;
1616 1617 if (start < self->raw_length) {
1617 1618 if (self->cache) {
1618 1619 Py_ssize_t i;
1619 1620 for (i = start; i < self->raw_length; i++)
1620 1621 Py_CLEAR(self->cache[i]);
1621 1622 }
1622 1623 self->raw_length = start;
1623 1624 }
1624 1625 goto done;
1625 1626 }
1626 1627
1627 1628 if (self->nt) {
1628 1629 nt_invalidate_added(self, start - self->length + 1);
1629 1630 if (self->ntrev > start)
1630 1631 self->ntrev = (int)start;
1631 1632 }
1632 1633 if (self->added)
1633 1634 ret = PyList_SetSlice(self->added, start - self->length + 1,
1634 1635 PyList_GET_SIZE(self->added), NULL);
1635 1636 done:
1636 1637 Py_CLEAR(self->headrevs);
1637 1638 return ret;
1638 1639 }
1639 1640
1640 1641 /*
1641 1642 * Supported ops:
1642 1643 *
1643 1644 * slice deletion
1644 1645 * string assignment (extend node->rev mapping)
1645 1646 * string deletion (shrink node->rev mapping)
1646 1647 */
1647 1648 static int index_assign_subscript(indexObject *self, PyObject *item,
1648 1649 PyObject *value)
1649 1650 {
1650 1651 char *node;
1651 1652 Py_ssize_t nodelen;
1652 1653 long rev;
1653 1654
1654 1655 if (PySlice_Check(item) && value == NULL)
1655 1656 return index_slice_del(self, item);
1656 1657
1657 1658 if (node_check(item, &node, &nodelen) == -1)
1658 1659 return -1;
1659 1660
1660 1661 if (value == NULL)
1661 1662 return self->nt ? nt_insert(self, node, -1) : 0;
1662 1663 rev = PyInt_AsLong(value);
1663 1664 if (rev > INT_MAX || rev < 0) {
1664 1665 if (!PyErr_Occurred())
1665 1666 PyErr_SetString(PyExc_ValueError, "rev out of range");
1666 1667 return -1;
1667 1668 }
1668 1669 return nt_insert(self, node, (int)rev);
1669 1670 }
1670 1671
1671 1672 /*
1672 1673 * Find all RevlogNG entries in an index that has inline data. Update
1673 1674 * the optional "offsets" table with those entries.
1674 1675 */
1675 1676 static long inline_scan(indexObject *self, const char **offsets)
1676 1677 {
1677 1678 const char *data = PyString_AS_STRING(self->data);
1678 1679 const char *end = data + PyString_GET_SIZE(self->data);
1679 1680 long incr = v1_hdrsize;
1680 1681 Py_ssize_t len = 0;
1681 1682
1682 1683 while (data + v1_hdrsize <= end) {
1683 1684 uint32_t comp_len;
1684 1685 const char *old_data;
1685 1686 /* 3rd element of header is length of compressed inline data */
1686 1687 comp_len = getbe32(data + 8);
1687 1688 incr = v1_hdrsize + comp_len;
1688 1689 if (incr < v1_hdrsize)
1689 1690 break;
1690 1691 if (offsets)
1691 1692 offsets[len] = data;
1692 1693 len++;
1693 1694 old_data = data;
1694 1695 data += incr;
1695 1696 if (data <= old_data)
1696 1697 break;
1697 1698 }
1698 1699
1699 1700 if (data != end && data + v1_hdrsize != end) {
1700 1701 if (!PyErr_Occurred())
1701 1702 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1702 1703 return -1;
1703 1704 }
1704 1705
1705 1706 return len;
1706 1707 }
1707 1708
1708 1709 static int index_init(indexObject *self, PyObject *args)
1709 1710 {
1710 1711 PyObject *data_obj, *inlined_obj;
1711 1712 Py_ssize_t size;
1712 1713
1713 1714 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1714 1715 return -1;
1715 1716 if (!PyString_Check(data_obj)) {
1716 1717 PyErr_SetString(PyExc_TypeError, "data is not a string");
1717 1718 return -1;
1718 1719 }
1719 1720 size = PyString_GET_SIZE(data_obj);
1720 1721
1721 1722 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1722 1723 self->data = data_obj;
1723 1724 self->cache = NULL;
1724 1725
1725 1726 self->added = NULL;
1726 1727 self->headrevs = NULL;
1727 1728 self->offsets = NULL;
1728 1729 self->nt = NULL;
1729 1730 self->ntlength = self->ntcapacity = 0;
1730 1731 self->ntdepth = self->ntsplits = 0;
1731 1732 self->ntlookups = self->ntmisses = 0;
1732 1733 self->ntrev = -1;
1733 1734 Py_INCREF(self->data);
1734 1735
1735 1736 if (self->inlined) {
1736 1737 long len = inline_scan(self, NULL);
1737 1738 if (len == -1)
1738 1739 goto bail;
1739 1740 self->raw_length = len;
1740 1741 self->length = len + 1;
1741 1742 } else {
1742 1743 if (size % v1_hdrsize) {
1743 1744 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1744 1745 goto bail;
1745 1746 }
1746 1747 self->raw_length = size / v1_hdrsize;
1747 1748 self->length = self->raw_length + 1;
1748 1749 }
1749 1750
1750 1751 return 0;
1751 1752 bail:
1752 1753 return -1;
1753 1754 }
1754 1755
1755 1756 static PyObject *index_nodemap(indexObject *self)
1756 1757 {
1757 1758 Py_INCREF(self);
1758 1759 return (PyObject *)self;
1759 1760 }
1760 1761
1761 1762 static void index_dealloc(indexObject *self)
1762 1763 {
1763 1764 _index_clearcaches(self);
1764 1765 Py_DECREF(self->data);
1765 1766 Py_XDECREF(self->added);
1766 1767 PyObject_Del(self);
1767 1768 }
1768 1769
1769 1770 static PySequenceMethods index_sequence_methods = {
1770 1771 (lenfunc)index_length, /* sq_length */
1771 1772 0, /* sq_concat */
1772 1773 0, /* sq_repeat */
1773 1774 (ssizeargfunc)index_get, /* sq_item */
1774 1775 0, /* sq_slice */
1775 1776 0, /* sq_ass_item */
1776 1777 0, /* sq_ass_slice */
1777 1778 (objobjproc)index_contains, /* sq_contains */
1778 1779 };
1779 1780
1780 1781 static PyMappingMethods index_mapping_methods = {
1781 1782 (lenfunc)index_length, /* mp_length */
1782 1783 (binaryfunc)index_getitem, /* mp_subscript */
1783 1784 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1784 1785 };
1785 1786
1786 1787 static PyMethodDef index_methods[] = {
1787 1788 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
1788 1789 "return the gca set of the given revs"},
1789 1790 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1790 1791 "clear the index caches"},
1791 1792 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1792 1793 "get an index entry"},
1793 1794 {"headrevs", (PyCFunction)index_headrevs, METH_NOARGS,
1794 1795 "get head revisions"},
1795 1796 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1796 1797 "insert an index entry"},
1797 1798 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
1798 1799 "match a potentially ambiguous node ID"},
1799 1800 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1800 1801 "stats for the index"},
1801 1802 {NULL} /* Sentinel */
1802 1803 };
1803 1804
1804 1805 static PyGetSetDef index_getset[] = {
1805 1806 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1806 1807 {NULL} /* Sentinel */
1807 1808 };
1808 1809
1809 1810 static PyTypeObject indexType = {
1810 1811 PyObject_HEAD_INIT(NULL)
1811 1812 0, /* ob_size */
1812 1813 "parsers.index", /* tp_name */
1813 1814 sizeof(indexObject), /* tp_basicsize */
1814 1815 0, /* tp_itemsize */
1815 1816 (destructor)index_dealloc, /* tp_dealloc */
1816 1817 0, /* tp_print */
1817 1818 0, /* tp_getattr */
1818 1819 0, /* tp_setattr */
1819 1820 0, /* tp_compare */
1820 1821 0, /* tp_repr */
1821 1822 0, /* tp_as_number */
1822 1823 &index_sequence_methods, /* tp_as_sequence */
1823 1824 &index_mapping_methods, /* tp_as_mapping */
1824 1825 0, /* tp_hash */
1825 1826 0, /* tp_call */
1826 1827 0, /* tp_str */
1827 1828 0, /* tp_getattro */
1828 1829 0, /* tp_setattro */
1829 1830 0, /* tp_as_buffer */
1830 1831 Py_TPFLAGS_DEFAULT, /* tp_flags */
1831 1832 "revlog index", /* tp_doc */
1832 1833 0, /* tp_traverse */
1833 1834 0, /* tp_clear */
1834 1835 0, /* tp_richcompare */
1835 1836 0, /* tp_weaklistoffset */
1836 1837 0, /* tp_iter */
1837 1838 0, /* tp_iternext */
1838 1839 index_methods, /* tp_methods */
1839 1840 0, /* tp_members */
1840 1841 index_getset, /* tp_getset */
1841 1842 0, /* tp_base */
1842 1843 0, /* tp_dict */
1843 1844 0, /* tp_descr_get */
1844 1845 0, /* tp_descr_set */
1845 1846 0, /* tp_dictoffset */
1846 1847 (initproc)index_init, /* tp_init */
1847 1848 0, /* tp_alloc */
1848 1849 };
1849 1850
1850 1851 /*
1851 1852 * returns a tuple of the form (index, index, cache) with elements as
1852 1853 * follows:
1853 1854 *
1854 1855 * index: an index object that lazily parses RevlogNG records
1855 1856 * cache: if data is inlined, a tuple (index_file_content, 0), else None
1856 1857 *
1857 1858 * added complications are for backwards compatibility
1858 1859 */
1859 1860 static PyObject *parse_index2(PyObject *self, PyObject *args)
1860 1861 {
1861 1862 PyObject *tuple = NULL, *cache = NULL;
1862 1863 indexObject *idx;
1863 1864 int ret;
1864 1865
1865 1866 idx = PyObject_New(indexObject, &indexType);
1866 1867 if (idx == NULL)
1867 1868 goto bail;
1868 1869
1869 1870 ret = index_init(idx, args);
1870 1871 if (ret == -1)
1871 1872 goto bail;
1872 1873
1873 1874 if (idx->inlined) {
1874 1875 cache = Py_BuildValue("iO", 0, idx->data);
1875 1876 if (cache == NULL)
1876 1877 goto bail;
1877 1878 } else {
1878 1879 cache = Py_None;
1879 1880 Py_INCREF(cache);
1880 1881 }
1881 1882
1882 1883 tuple = Py_BuildValue("NN", idx, cache);
1883 1884 if (!tuple)
1884 1885 goto bail;
1885 1886 return tuple;
1886 1887
1887 1888 bail:
1888 1889 Py_XDECREF(idx);
1889 1890 Py_XDECREF(cache);
1890 1891 Py_XDECREF(tuple);
1891 1892 return NULL;
1892 1893 }
1893 1894
1894 1895 static char parsers_doc[] = "Efficient content parsing.";
1895 1896
1896 1897 PyObject *encodedir(PyObject *self, PyObject *args);
1897 1898 PyObject *pathencode(PyObject *self, PyObject *args);
1898 1899 PyObject *lowerencode(PyObject *self, PyObject *args);
1899 1900
1900 1901 static PyMethodDef methods[] = {
1901 1902 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
1902 1903 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
1903 1904 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1904 1905 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
1905 1906 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
1906 1907 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
1907 1908 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
1908 1909 {NULL, NULL}
1909 1910 };
1910 1911
1911 1912 void dirs_module_init(PyObject *mod);
1912 1913
1913 1914 static void module_init(PyObject *mod)
1914 1915 {
1915 1916 dirs_module_init(mod);
1916 1917
1917 1918 indexType.tp_new = PyType_GenericNew;
1918 1919 if (PyType_Ready(&indexType) < 0)
1919 1920 return;
1920 1921 Py_INCREF(&indexType);
1921 1922
1922 1923 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
1923 1924
1924 1925 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
1925 1926 -1, -1, -1, -1, nullid, 20);
1926 1927 if (nullentry)
1927 1928 PyObject_GC_UnTrack(nullentry);
1928 1929
1929 1930 dirstate_unset = Py_BuildValue("ciii", 'n', 0, -1, -1);
1930 1931 }
1931 1932
1932 1933 #ifdef IS_PY3K
1933 1934 static struct PyModuleDef parsers_module = {
1934 1935 PyModuleDef_HEAD_INIT,
1935 1936 "parsers",
1936 1937 parsers_doc,
1937 1938 -1,
1938 1939 methods
1939 1940 };
1940 1941
1941 1942 PyMODINIT_FUNC PyInit_parsers(void)
1942 1943 {
1943 1944 PyObject *mod = PyModule_Create(&parsers_module);
1944 1945 module_init(mod);
1945 1946 return mod;
1946 1947 }
1947 1948 #else
1948 1949 PyMODINIT_FUNC initparsers(void)
1949 1950 {
1950 1951 PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
1951 1952 module_init(mod);
1952 1953 }
1953 1954 #endif
General Comments 0
You need to be logged in to leave comments. Login now