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