##// END OF EJS Templates
parsers: factor out most of asciilower into an internal function...
Siddharth Agarwal -
r24574:e97a00bf default
parent child Browse files
Show More
@@ -1,2516 +1,2524 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 char *versionerrortext = "Python minor version mismatch";
18 18
19 19 static int8_t hextable[256] = {
20 20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
21 21 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
22 22 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
23 23 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
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, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
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 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36 36 };
37 37
38 38 static char lowertable[128] = {
39 39 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
40 40 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
41 41 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
42 42 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
43 43 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
44 44 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
45 45 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
46 46 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
47 47 '\x40',
48 48 '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */
49 49 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */
50 50 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */
51 51 '\x78', '\x79', '\x7a', /* X-Z */
52 52 '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
53 53 '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67',
54 54 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f',
55 55 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77',
56 56 '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
57 57 };
58 58
59 59 static inline int hexdigit(const char *p, Py_ssize_t off)
60 60 {
61 61 int8_t val = hextable[(unsigned char)p[off]];
62 62
63 63 if (val >= 0) {
64 64 return val;
65 65 }
66 66
67 67 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
68 68 return 0;
69 69 }
70 70
71 71 /*
72 72 * Turn a hex-encoded string into binary.
73 73 */
74 74 PyObject *unhexlify(const char *str, int len)
75 75 {
76 76 PyObject *ret;
77 77 char *d;
78 78 int i;
79 79
80 80 ret = PyBytes_FromStringAndSize(NULL, len / 2);
81 81
82 82 if (!ret)
83 83 return NULL;
84 84
85 85 d = PyBytes_AsString(ret);
86 86
87 87 for (i = 0; i < len;) {
88 88 int hi = hexdigit(str, i++);
89 89 int lo = hexdigit(str, i++);
90 90 *d++ = (hi << 4) | lo;
91 91 }
92 92
93 93 return ret;
94 94 }
95 95
96 static PyObject *asciilower(PyObject *self, PyObject *args)
96 static inline PyObject *_asciilower(PyObject *str_obj)
97 97 {
98 98 char *str, *newstr;
99 int i, len;
99 Py_ssize_t i, len;
100 100 PyObject *newobj = NULL;
101 101
102 if (!PyArg_ParseTuple(args, "s#", &str, &len))
103 goto quit;
102 str = PyBytes_AS_STRING(str_obj);
103 len = PyBytes_GET_SIZE(str_obj);
104 104
105 105 newobj = PyBytes_FromStringAndSize(NULL, len);
106 106 if (!newobj)
107 107 goto quit;
108 108
109 109 newstr = PyBytes_AS_STRING(newobj);
110 110
111 111 for (i = 0; i < len; i++) {
112 112 char c = str[i];
113 113 if (c & 0x80) {
114 114 PyObject *err = PyUnicodeDecodeError_Create(
115 115 "ascii", str, len, i, (i + 1),
116 116 "unexpected code byte");
117 117 PyErr_SetObject(PyExc_UnicodeDecodeError, err);
118 118 Py_XDECREF(err);
119 119 goto quit;
120 120 }
121 121 newstr[i] = lowertable[(unsigned char)c];
122 122 }
123 123
124 124 return newobj;
125 125 quit:
126 126 Py_XDECREF(newobj);
127 127 return NULL;
128 128 }
129 129
130 static PyObject *asciilower(PyObject *self, PyObject *args)
131 {
132 PyObject *str_obj;
133 if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
134 return NULL;
135 return _asciilower(str_obj);
136 }
137
130 138 /*
131 139 * This code assumes that a manifest is stitched together with newline
132 140 * ('\n') characters.
133 141 */
134 142 static PyObject *parse_manifest(PyObject *self, PyObject *args)
135 143 {
136 144 PyObject *mfdict, *fdict;
137 145 char *str, *start, *end;
138 146 int len;
139 147
140 148 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
141 149 &PyDict_Type, &mfdict,
142 150 &PyDict_Type, &fdict,
143 151 &str, &len))
144 152 goto quit;
145 153
146 154 start = str;
147 155 end = str + len;
148 156 while (start < end) {
149 157 PyObject *file = NULL, *node = NULL;
150 158 PyObject *flags = NULL;
151 159 char *zero = NULL, *newline = NULL;
152 160 ptrdiff_t nlen;
153 161
154 162 zero = memchr(start, '\0', end - start);
155 163 if (!zero) {
156 164 PyErr_SetString(PyExc_ValueError,
157 165 "manifest entry has no separator");
158 166 goto quit;
159 167 }
160 168
161 169 newline = memchr(zero + 1, '\n', end - (zero + 1));
162 170 if (!newline) {
163 171 PyErr_SetString(PyExc_ValueError,
164 172 "manifest contains trailing garbage");
165 173 goto quit;
166 174 }
167 175
168 176 file = PyBytes_FromStringAndSize(start, zero - start);
169 177
170 178 if (!file)
171 179 goto bail;
172 180
173 181 nlen = newline - zero - 1;
174 182
175 183 node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen);
176 184 if (!node)
177 185 goto bail;
178 186
179 187 if (nlen > 40) {
180 188 flags = PyBytes_FromStringAndSize(zero + 41,
181 189 nlen - 40);
182 190 if (!flags)
183 191 goto bail;
184 192
185 193 if (PyDict_SetItem(fdict, file, flags) == -1)
186 194 goto bail;
187 195 }
188 196
189 197 if (PyDict_SetItem(mfdict, file, node) == -1)
190 198 goto bail;
191 199
192 200 start = newline + 1;
193 201
194 202 Py_XDECREF(flags);
195 203 Py_XDECREF(node);
196 204 Py_XDECREF(file);
197 205 continue;
198 206 bail:
199 207 Py_XDECREF(flags);
200 208 Py_XDECREF(node);
201 209 Py_XDECREF(file);
202 210 goto quit;
203 211 }
204 212
205 213 Py_INCREF(Py_None);
206 214 return Py_None;
207 215 quit:
208 216 return NULL;
209 217 }
210 218
211 219 static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode,
212 220 int size, int mtime)
213 221 {
214 222 dirstateTupleObject *t = PyObject_New(dirstateTupleObject,
215 223 &dirstateTupleType);
216 224 if (!t)
217 225 return NULL;
218 226 t->state = state;
219 227 t->mode = mode;
220 228 t->size = size;
221 229 t->mtime = mtime;
222 230 return t;
223 231 }
224 232
225 233 static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args,
226 234 PyObject *kwds)
227 235 {
228 236 /* We do all the initialization here and not a tp_init function because
229 237 * dirstate_tuple is immutable. */
230 238 dirstateTupleObject *t;
231 239 char state;
232 240 int size, mode, mtime;
233 241 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime))
234 242 return NULL;
235 243
236 244 t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1);
237 245 if (!t)
238 246 return NULL;
239 247 t->state = state;
240 248 t->mode = mode;
241 249 t->size = size;
242 250 t->mtime = mtime;
243 251
244 252 return (PyObject *)t;
245 253 }
246 254
247 255 static void dirstate_tuple_dealloc(PyObject *o)
248 256 {
249 257 PyObject_Del(o);
250 258 }
251 259
252 260 static Py_ssize_t dirstate_tuple_length(PyObject *o)
253 261 {
254 262 return 4;
255 263 }
256 264
257 265 static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i)
258 266 {
259 267 dirstateTupleObject *t = (dirstateTupleObject *)o;
260 268 switch (i) {
261 269 case 0:
262 270 return PyBytes_FromStringAndSize(&t->state, 1);
263 271 case 1:
264 272 return PyInt_FromLong(t->mode);
265 273 case 2:
266 274 return PyInt_FromLong(t->size);
267 275 case 3:
268 276 return PyInt_FromLong(t->mtime);
269 277 default:
270 278 PyErr_SetString(PyExc_IndexError, "index out of range");
271 279 return NULL;
272 280 }
273 281 }
274 282
275 283 static PySequenceMethods dirstate_tuple_sq = {
276 284 dirstate_tuple_length, /* sq_length */
277 285 0, /* sq_concat */
278 286 0, /* sq_repeat */
279 287 dirstate_tuple_item, /* sq_item */
280 288 0, /* sq_ass_item */
281 289 0, /* sq_contains */
282 290 0, /* sq_inplace_concat */
283 291 0 /* sq_inplace_repeat */
284 292 };
285 293
286 294 PyTypeObject dirstateTupleType = {
287 295 PyVarObject_HEAD_INIT(NULL, 0)
288 296 "dirstate_tuple", /* tp_name */
289 297 sizeof(dirstateTupleObject),/* tp_basicsize */
290 298 0, /* tp_itemsize */
291 299 (destructor)dirstate_tuple_dealloc, /* tp_dealloc */
292 300 0, /* tp_print */
293 301 0, /* tp_getattr */
294 302 0, /* tp_setattr */
295 303 0, /* tp_compare */
296 304 0, /* tp_repr */
297 305 0, /* tp_as_number */
298 306 &dirstate_tuple_sq, /* tp_as_sequence */
299 307 0, /* tp_as_mapping */
300 308 0, /* tp_hash */
301 309 0, /* tp_call */
302 310 0, /* tp_str */
303 311 0, /* tp_getattro */
304 312 0, /* tp_setattro */
305 313 0, /* tp_as_buffer */
306 314 Py_TPFLAGS_DEFAULT, /* tp_flags */
307 315 "dirstate tuple", /* tp_doc */
308 316 0, /* tp_traverse */
309 317 0, /* tp_clear */
310 318 0, /* tp_richcompare */
311 319 0, /* tp_weaklistoffset */
312 320 0, /* tp_iter */
313 321 0, /* tp_iternext */
314 322 0, /* tp_methods */
315 323 0, /* tp_members */
316 324 0, /* tp_getset */
317 325 0, /* tp_base */
318 326 0, /* tp_dict */
319 327 0, /* tp_descr_get */
320 328 0, /* tp_descr_set */
321 329 0, /* tp_dictoffset */
322 330 0, /* tp_init */
323 331 0, /* tp_alloc */
324 332 dirstate_tuple_new, /* tp_new */
325 333 };
326 334
327 335 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
328 336 {
329 337 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
330 338 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
331 339 char state, *cur, *str, *cpos;
332 340 int mode, size, mtime;
333 341 unsigned int flen, len, pos = 40;
334 342 int readlen;
335 343
336 344 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
337 345 &PyDict_Type, &dmap,
338 346 &PyDict_Type, &cmap,
339 347 &str, &readlen))
340 348 goto quit;
341 349
342 350 if (readlen < 0)
343 351 goto quit;
344 352
345 353 len = readlen;
346 354
347 355 /* read parents */
348 356 if (len < 40)
349 357 goto quit;
350 358
351 359 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
352 360 if (!parents)
353 361 goto quit;
354 362
355 363 /* read filenames */
356 364 while (pos >= 40 && pos < len) {
357 365 cur = str + pos;
358 366 /* unpack header */
359 367 state = *cur;
360 368 mode = getbe32(cur + 1);
361 369 size = getbe32(cur + 5);
362 370 mtime = getbe32(cur + 9);
363 371 flen = getbe32(cur + 13);
364 372 pos += 17;
365 373 cur += 17;
366 374 if (flen > len - pos) {
367 375 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
368 376 goto quit;
369 377 }
370 378
371 379 entry = (PyObject *)make_dirstate_tuple(state, mode, size,
372 380 mtime);
373 381 cpos = memchr(cur, 0, flen);
374 382 if (cpos) {
375 383 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
376 384 cname = PyBytes_FromStringAndSize(cpos + 1,
377 385 flen - (cpos - cur) - 1);
378 386 if (!fname || !cname ||
379 387 PyDict_SetItem(cmap, fname, cname) == -1 ||
380 388 PyDict_SetItem(dmap, fname, entry) == -1)
381 389 goto quit;
382 390 Py_DECREF(cname);
383 391 } else {
384 392 fname = PyBytes_FromStringAndSize(cur, flen);
385 393 if (!fname ||
386 394 PyDict_SetItem(dmap, fname, entry) == -1)
387 395 goto quit;
388 396 }
389 397 Py_DECREF(fname);
390 398 Py_DECREF(entry);
391 399 fname = cname = entry = NULL;
392 400 pos += flen;
393 401 }
394 402
395 403 ret = parents;
396 404 Py_INCREF(ret);
397 405 quit:
398 406 Py_XDECREF(fname);
399 407 Py_XDECREF(cname);
400 408 Py_XDECREF(entry);
401 409 Py_XDECREF(parents);
402 410 return ret;
403 411 }
404 412
405 413 /*
406 414 * Efficiently pack a dirstate object into its on-disk format.
407 415 */
408 416 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
409 417 {
410 418 PyObject *packobj = NULL;
411 419 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
412 420 Py_ssize_t nbytes, pos, l;
413 421 PyObject *k, *v = NULL, *pn;
414 422 char *p, *s;
415 423 double now;
416 424
417 425 if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate",
418 426 &PyDict_Type, &map, &PyDict_Type, &copymap,
419 427 &pl, &now))
420 428 return NULL;
421 429
422 430 if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) {
423 431 PyErr_SetString(PyExc_TypeError, "expected 2-element sequence");
424 432 return NULL;
425 433 }
426 434
427 435 /* Figure out how much we need to allocate. */
428 436 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
429 437 PyObject *c;
430 438 if (!PyString_Check(k)) {
431 439 PyErr_SetString(PyExc_TypeError, "expected string key");
432 440 goto bail;
433 441 }
434 442 nbytes += PyString_GET_SIZE(k) + 17;
435 443 c = PyDict_GetItem(copymap, k);
436 444 if (c) {
437 445 if (!PyString_Check(c)) {
438 446 PyErr_SetString(PyExc_TypeError,
439 447 "expected string key");
440 448 goto bail;
441 449 }
442 450 nbytes += PyString_GET_SIZE(c) + 1;
443 451 }
444 452 }
445 453
446 454 packobj = PyString_FromStringAndSize(NULL, nbytes);
447 455 if (packobj == NULL)
448 456 goto bail;
449 457
450 458 p = PyString_AS_STRING(packobj);
451 459
452 460 pn = PySequence_ITEM(pl, 0);
453 461 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
454 462 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
455 463 goto bail;
456 464 }
457 465 memcpy(p, s, l);
458 466 p += 20;
459 467 pn = PySequence_ITEM(pl, 1);
460 468 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
461 469 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
462 470 goto bail;
463 471 }
464 472 memcpy(p, s, l);
465 473 p += 20;
466 474
467 475 for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) {
468 476 dirstateTupleObject *tuple;
469 477 char state;
470 478 uint32_t mode, size, mtime;
471 479 Py_ssize_t len, l;
472 480 PyObject *o;
473 481 char *t;
474 482
475 483 if (!dirstate_tuple_check(v)) {
476 484 PyErr_SetString(PyExc_TypeError,
477 485 "expected a dirstate tuple");
478 486 goto bail;
479 487 }
480 488 tuple = (dirstateTupleObject *)v;
481 489
482 490 state = tuple->state;
483 491 mode = tuple->mode;
484 492 size = tuple->size;
485 493 mtime = tuple->mtime;
486 494 if (state == 'n' && mtime == (uint32_t)now) {
487 495 /* See pure/parsers.py:pack_dirstate for why we do
488 496 * this. */
489 497 mtime = -1;
490 498 mtime_unset = (PyObject *)make_dirstate_tuple(
491 499 state, mode, size, mtime);
492 500 if (!mtime_unset)
493 501 goto bail;
494 502 if (PyDict_SetItem(map, k, mtime_unset) == -1)
495 503 goto bail;
496 504 Py_DECREF(mtime_unset);
497 505 mtime_unset = NULL;
498 506 }
499 507 *p++ = state;
500 508 putbe32(mode, p);
501 509 putbe32(size, p + 4);
502 510 putbe32(mtime, p + 8);
503 511 t = p + 12;
504 512 p += 16;
505 513 len = PyString_GET_SIZE(k);
506 514 memcpy(p, PyString_AS_STRING(k), len);
507 515 p += len;
508 516 o = PyDict_GetItem(copymap, k);
509 517 if (o) {
510 518 *p++ = '\0';
511 519 l = PyString_GET_SIZE(o);
512 520 memcpy(p, PyString_AS_STRING(o), l);
513 521 p += l;
514 522 len += l + 1;
515 523 }
516 524 putbe32((uint32_t)len, t);
517 525 }
518 526
519 527 pos = p - PyString_AS_STRING(packobj);
520 528 if (pos != nbytes) {
521 529 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
522 530 (long)pos, (long)nbytes);
523 531 goto bail;
524 532 }
525 533
526 534 return packobj;
527 535 bail:
528 536 Py_XDECREF(mtime_unset);
529 537 Py_XDECREF(packobj);
530 538 Py_XDECREF(v);
531 539 return NULL;
532 540 }
533 541
534 542 /*
535 543 * A base-16 trie for fast node->rev mapping.
536 544 *
537 545 * Positive value is index of the next node in the trie
538 546 * Negative value is a leaf: -(rev + 1)
539 547 * Zero is empty
540 548 */
541 549 typedef struct {
542 550 int children[16];
543 551 } nodetree;
544 552
545 553 /*
546 554 * This class has two behaviours.
547 555 *
548 556 * When used in a list-like way (with integer keys), we decode an
549 557 * entry in a RevlogNG index file on demand. Our last entry is a
550 558 * sentinel, always a nullid. We have limited support for
551 559 * integer-keyed insert and delete, only at elements right before the
552 560 * sentinel.
553 561 *
554 562 * With string keys, we lazily perform a reverse mapping from node to
555 563 * rev, using a base-16 trie.
556 564 */
557 565 typedef struct {
558 566 PyObject_HEAD
559 567 /* Type-specific fields go here. */
560 568 PyObject *data; /* raw bytes of index */
561 569 PyObject **cache; /* cached tuples */
562 570 const char **offsets; /* populated on demand */
563 571 Py_ssize_t raw_length; /* original number of elements */
564 572 Py_ssize_t length; /* current number of elements */
565 573 PyObject *added; /* populated on demand */
566 574 PyObject *headrevs; /* cache, invalidated on changes */
567 575 PyObject *filteredrevs;/* filtered revs set */
568 576 nodetree *nt; /* base-16 trie */
569 577 int ntlength; /* # nodes in use */
570 578 int ntcapacity; /* # nodes allocated */
571 579 int ntdepth; /* maximum depth of tree */
572 580 int ntsplits; /* # splits performed */
573 581 int ntrev; /* last rev scanned */
574 582 int ntlookups; /* # lookups */
575 583 int ntmisses; /* # lookups that miss the cache */
576 584 int inlined;
577 585 } indexObject;
578 586
579 587 static Py_ssize_t index_length(const indexObject *self)
580 588 {
581 589 if (self->added == NULL)
582 590 return self->length;
583 591 return self->length + PyList_GET_SIZE(self->added);
584 592 }
585 593
586 594 static PyObject *nullentry;
587 595 static const char nullid[20];
588 596
589 597 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
590 598
591 599 #if LONG_MAX == 0x7fffffffL
592 600 static char *tuple_format = "Kiiiiiis#";
593 601 #else
594 602 static char *tuple_format = "kiiiiiis#";
595 603 #endif
596 604
597 605 /* A RevlogNG v1 index entry is 64 bytes long. */
598 606 static const long v1_hdrsize = 64;
599 607
600 608 /*
601 609 * Return a pointer to the beginning of a RevlogNG record.
602 610 */
603 611 static const char *index_deref(indexObject *self, Py_ssize_t pos)
604 612 {
605 613 if (self->inlined && pos > 0) {
606 614 if (self->offsets == NULL) {
607 615 self->offsets = malloc(self->raw_length *
608 616 sizeof(*self->offsets));
609 617 if (self->offsets == NULL)
610 618 return (const char *)PyErr_NoMemory();
611 619 inline_scan(self, self->offsets);
612 620 }
613 621 return self->offsets[pos];
614 622 }
615 623
616 624 return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
617 625 }
618 626
619 627 /*
620 628 * RevlogNG format (all in big endian, data may be inlined):
621 629 * 6 bytes: offset
622 630 * 2 bytes: flags
623 631 * 4 bytes: compressed length
624 632 * 4 bytes: uncompressed length
625 633 * 4 bytes: base revision
626 634 * 4 bytes: link revision
627 635 * 4 bytes: parent 1 revision
628 636 * 4 bytes: parent 2 revision
629 637 * 32 bytes: nodeid (only 20 bytes used)
630 638 */
631 639 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
632 640 {
633 641 uint64_t offset_flags;
634 642 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
635 643 const char *c_node_id;
636 644 const char *data;
637 645 Py_ssize_t length = index_length(self);
638 646 PyObject *entry;
639 647
640 648 if (pos < 0)
641 649 pos += length;
642 650
643 651 if (pos < 0 || pos >= length) {
644 652 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
645 653 return NULL;
646 654 }
647 655
648 656 if (pos == length - 1) {
649 657 Py_INCREF(nullentry);
650 658 return nullentry;
651 659 }
652 660
653 661 if (pos >= self->length - 1) {
654 662 PyObject *obj;
655 663 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
656 664 Py_INCREF(obj);
657 665 return obj;
658 666 }
659 667
660 668 if (self->cache) {
661 669 if (self->cache[pos]) {
662 670 Py_INCREF(self->cache[pos]);
663 671 return self->cache[pos];
664 672 }
665 673 } else {
666 674 self->cache = calloc(self->raw_length, sizeof(PyObject *));
667 675 if (self->cache == NULL)
668 676 return PyErr_NoMemory();
669 677 }
670 678
671 679 data = index_deref(self, pos);
672 680 if (data == NULL)
673 681 return NULL;
674 682
675 683 offset_flags = getbe32(data + 4);
676 684 if (pos == 0) /* mask out version number for the first entry */
677 685 offset_flags &= 0xFFFF;
678 686 else {
679 687 uint32_t offset_high = getbe32(data);
680 688 offset_flags |= ((uint64_t)offset_high) << 32;
681 689 }
682 690
683 691 comp_len = getbe32(data + 8);
684 692 uncomp_len = getbe32(data + 12);
685 693 base_rev = getbe32(data + 16);
686 694 link_rev = getbe32(data + 20);
687 695 parent_1 = getbe32(data + 24);
688 696 parent_2 = getbe32(data + 28);
689 697 c_node_id = data + 32;
690 698
691 699 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
692 700 uncomp_len, base_rev, link_rev,
693 701 parent_1, parent_2, c_node_id, 20);
694 702
695 703 if (entry) {
696 704 PyObject_GC_UnTrack(entry);
697 705 Py_INCREF(entry);
698 706 }
699 707
700 708 self->cache[pos] = entry;
701 709
702 710 return entry;
703 711 }
704 712
705 713 /*
706 714 * Return the 20-byte SHA of the node corresponding to the given rev.
707 715 */
708 716 static const char *index_node(indexObject *self, Py_ssize_t pos)
709 717 {
710 718 Py_ssize_t length = index_length(self);
711 719 const char *data;
712 720
713 721 if (pos == length - 1 || pos == INT_MAX)
714 722 return nullid;
715 723
716 724 if (pos >= length)
717 725 return NULL;
718 726
719 727 if (pos >= self->length - 1) {
720 728 PyObject *tuple, *str;
721 729 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
722 730 str = PyTuple_GetItem(tuple, 7);
723 731 return str ? PyString_AS_STRING(str) : NULL;
724 732 }
725 733
726 734 data = index_deref(self, pos);
727 735 return data ? data + 32 : NULL;
728 736 }
729 737
730 738 static int nt_insert(indexObject *self, const char *node, int rev);
731 739
732 740 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
733 741 {
734 742 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
735 743 return -1;
736 744 if (*nodelen == 20)
737 745 return 0;
738 746 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
739 747 return -1;
740 748 }
741 749
742 750 static PyObject *index_insert(indexObject *self, PyObject *args)
743 751 {
744 752 PyObject *obj;
745 753 char *node;
746 754 int index;
747 755 Py_ssize_t len, nodelen;
748 756
749 757 if (!PyArg_ParseTuple(args, "iO", &index, &obj))
750 758 return NULL;
751 759
752 760 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
753 761 PyErr_SetString(PyExc_TypeError, "8-tuple required");
754 762 return NULL;
755 763 }
756 764
757 765 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
758 766 return NULL;
759 767
760 768 len = index_length(self);
761 769
762 770 if (index < 0)
763 771 index += len;
764 772
765 773 if (index != len - 1) {
766 774 PyErr_SetString(PyExc_IndexError,
767 775 "insert only supported at index -1");
768 776 return NULL;
769 777 }
770 778
771 779 if (self->added == NULL) {
772 780 self->added = PyList_New(0);
773 781 if (self->added == NULL)
774 782 return NULL;
775 783 }
776 784
777 785 if (PyList_Append(self->added, obj) == -1)
778 786 return NULL;
779 787
780 788 if (self->nt)
781 789 nt_insert(self, node, index);
782 790
783 791 Py_CLEAR(self->headrevs);
784 792 Py_RETURN_NONE;
785 793 }
786 794
787 795 static void _index_clearcaches(indexObject *self)
788 796 {
789 797 if (self->cache) {
790 798 Py_ssize_t i;
791 799
792 800 for (i = 0; i < self->raw_length; i++)
793 801 Py_CLEAR(self->cache[i]);
794 802 free(self->cache);
795 803 self->cache = NULL;
796 804 }
797 805 if (self->offsets) {
798 806 free(self->offsets);
799 807 self->offsets = NULL;
800 808 }
801 809 if (self->nt) {
802 810 free(self->nt);
803 811 self->nt = NULL;
804 812 }
805 813 Py_CLEAR(self->headrevs);
806 814 }
807 815
808 816 static PyObject *index_clearcaches(indexObject *self)
809 817 {
810 818 _index_clearcaches(self);
811 819 self->ntlength = self->ntcapacity = 0;
812 820 self->ntdepth = self->ntsplits = 0;
813 821 self->ntrev = -1;
814 822 self->ntlookups = self->ntmisses = 0;
815 823 Py_RETURN_NONE;
816 824 }
817 825
818 826 static PyObject *index_stats(indexObject *self)
819 827 {
820 828 PyObject *obj = PyDict_New();
821 829 PyObject *t = NULL;
822 830
823 831 if (obj == NULL)
824 832 return NULL;
825 833
826 834 #define istat(__n, __d) \
827 835 t = PyInt_FromSsize_t(self->__n); \
828 836 if (!t) \
829 837 goto bail; \
830 838 if (PyDict_SetItemString(obj, __d, t) == -1) \
831 839 goto bail; \
832 840 Py_DECREF(t);
833 841
834 842 if (self->added) {
835 843 Py_ssize_t len = PyList_GET_SIZE(self->added);
836 844 t = PyInt_FromSsize_t(len);
837 845 if (!t)
838 846 goto bail;
839 847 if (PyDict_SetItemString(obj, "index entries added", t) == -1)
840 848 goto bail;
841 849 Py_DECREF(t);
842 850 }
843 851
844 852 if (self->raw_length != self->length - 1)
845 853 istat(raw_length, "revs on disk");
846 854 istat(length, "revs in memory");
847 855 istat(ntcapacity, "node trie capacity");
848 856 istat(ntdepth, "node trie depth");
849 857 istat(ntlength, "node trie count");
850 858 istat(ntlookups, "node trie lookups");
851 859 istat(ntmisses, "node trie misses");
852 860 istat(ntrev, "node trie last rev scanned");
853 861 istat(ntsplits, "node trie splits");
854 862
855 863 #undef istat
856 864
857 865 return obj;
858 866
859 867 bail:
860 868 Py_XDECREF(obj);
861 869 Py_XDECREF(t);
862 870 return NULL;
863 871 }
864 872
865 873 /*
866 874 * When we cache a list, we want to be sure the caller can't mutate
867 875 * the cached copy.
868 876 */
869 877 static PyObject *list_copy(PyObject *list)
870 878 {
871 879 Py_ssize_t len = PyList_GET_SIZE(list);
872 880 PyObject *newlist = PyList_New(len);
873 881 Py_ssize_t i;
874 882
875 883 if (newlist == NULL)
876 884 return NULL;
877 885
878 886 for (i = 0; i < len; i++) {
879 887 PyObject *obj = PyList_GET_ITEM(list, i);
880 888 Py_INCREF(obj);
881 889 PyList_SET_ITEM(newlist, i, obj);
882 890 }
883 891
884 892 return newlist;
885 893 }
886 894
887 895 /* arg should be Py_ssize_t but Python 2.4 do not support the n format */
888 896 static int check_filter(PyObject *filter, unsigned long arg) {
889 897 if (filter) {
890 898 PyObject *arglist, *result;
891 899 int isfiltered;
892 900
893 901 arglist = Py_BuildValue("(k)", arg);
894 902 if (!arglist) {
895 903 return -1;
896 904 }
897 905
898 906 result = PyEval_CallObject(filter, arglist);
899 907 Py_DECREF(arglist);
900 908 if (!result) {
901 909 return -1;
902 910 }
903 911
904 912 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
905 913 * same as this function, so we can just return it directly.*/
906 914 isfiltered = PyObject_IsTrue(result);
907 915 Py_DECREF(result);
908 916 return isfiltered;
909 917 } else {
910 918 return 0;
911 919 }
912 920 }
913 921
914 922 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
915 923 Py_ssize_t marker, char *phases)
916 924 {
917 925 PyObject *iter = NULL;
918 926 PyObject *iter_item = NULL;
919 927 Py_ssize_t min_idx = index_length(self) + 1;
920 928 long iter_item_long;
921 929
922 930 if (PyList_GET_SIZE(list) != 0) {
923 931 iter = PyObject_GetIter(list);
924 932 if (iter == NULL)
925 933 return -2;
926 934 while ((iter_item = PyIter_Next(iter)))
927 935 {
928 936 iter_item_long = PyInt_AS_LONG(iter_item);
929 937 Py_DECREF(iter_item);
930 938 if (iter_item_long < min_idx)
931 939 min_idx = iter_item_long;
932 940 phases[iter_item_long] = marker;
933 941 }
934 942 Py_DECREF(iter);
935 943 }
936 944
937 945 return min_idx;
938 946 }
939 947
940 948 static inline void set_phase_from_parents(char *phases, int parent_1,
941 949 int parent_2, Py_ssize_t i)
942 950 {
943 951 if (parent_1 >= 0 && phases[parent_1] > phases[i])
944 952 phases[i] = phases[parent_1];
945 953 if (parent_2 >= 0 && phases[parent_2] > phases[i])
946 954 phases[i] = phases[parent_2];
947 955 }
948 956
949 957 static PyObject *compute_phases(indexObject *self, PyObject *args)
950 958 {
951 959 PyObject *roots = Py_None;
952 960 PyObject *phaseslist = NULL;
953 961 PyObject *phaseroots = NULL;
954 962 PyObject *rev = NULL;
955 963 PyObject *p1 = NULL;
956 964 PyObject *p2 = NULL;
957 965 Py_ssize_t addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
958 966 Py_ssize_t len = index_length(self) - 1;
959 967 Py_ssize_t numphase = 0;
960 968 Py_ssize_t minrevallphases = 0;
961 969 Py_ssize_t minrevphase = 0;
962 970 Py_ssize_t i = 0;
963 971 int parent_1, parent_2;
964 972 char *phases = NULL;
965 973 const char *data;
966 974
967 975 if (!PyArg_ParseTuple(args, "O", &roots))
968 976 goto release_none;
969 977 if (roots == NULL || !PyList_Check(roots))
970 978 goto release_none;
971 979
972 980 phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
973 981 if (phases == NULL)
974 982 goto release_none;
975 983 /* Put the phase information of all the roots in phases */
976 984 numphase = PyList_GET_SIZE(roots)+1;
977 985 minrevallphases = len + 1;
978 986 for (i = 0; i < numphase-1; i++) {
979 987 phaseroots = PyList_GET_ITEM(roots, i);
980 988 if (!PyList_Check(phaseroots))
981 989 goto release_phases;
982 990 minrevphase = add_roots_get_min(self, phaseroots, i+1, phases);
983 991 if (minrevphase == -2) /* Error from add_roots_get_min */
984 992 goto release_phases;
985 993 minrevallphases = MIN(minrevallphases, minrevphase);
986 994 }
987 995 /* Propagate the phase information from the roots to the revs */
988 996 if (minrevallphases != -1) {
989 997 for (i = minrevallphases; i < self->raw_length; i++) {
990 998 data = index_deref(self, i);
991 999 set_phase_from_parents(phases, getbe32(data+24), getbe32(data+28), i);
992 1000 }
993 1001 for (i = 0; i < addlen; i++) {
994 1002 rev = PyList_GET_ITEM(self->added, i);
995 1003 p1 = PyTuple_GET_ITEM(rev, 5);
996 1004 p2 = PyTuple_GET_ITEM(rev, 6);
997 1005 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
998 1006 PyErr_SetString(PyExc_TypeError, "revlog parents are invalid");
999 1007 goto release_phases;
1000 1008 }
1001 1009 parent_1 = (int)PyInt_AS_LONG(p1);
1002 1010 parent_2 = (int)PyInt_AS_LONG(p2);
1003 1011 set_phase_from_parents(phases, parent_1, parent_2, i+self->raw_length);
1004 1012 }
1005 1013 }
1006 1014 /* Transform phase list to a python list */
1007 1015 phaseslist = PyList_New(len);
1008 1016 if (phaseslist == NULL)
1009 1017 goto release_phases;
1010 1018 for (i = 0; i < len; i++)
1011 1019 PyList_SET_ITEM(phaseslist, i, PyInt_FromLong(phases[i]));
1012 1020
1013 1021 release_phases:
1014 1022 free(phases);
1015 1023 release_none:
1016 1024 return phaseslist;
1017 1025 }
1018 1026
1019 1027 static PyObject *index_headrevs(indexObject *self, PyObject *args)
1020 1028 {
1021 1029 Py_ssize_t i, len, addlen;
1022 1030 char *nothead = NULL;
1023 1031 PyObject *heads = NULL;
1024 1032 PyObject *filter = NULL;
1025 1033 PyObject *filteredrevs = Py_None;
1026 1034
1027 1035 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
1028 1036 return NULL;
1029 1037 }
1030 1038
1031 1039 if (self->headrevs && filteredrevs == self->filteredrevs)
1032 1040 return list_copy(self->headrevs);
1033 1041
1034 1042 Py_DECREF(self->filteredrevs);
1035 1043 self->filteredrevs = filteredrevs;
1036 1044 Py_INCREF(filteredrevs);
1037 1045
1038 1046 if (filteredrevs != Py_None) {
1039 1047 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
1040 1048 if (!filter) {
1041 1049 PyErr_SetString(PyExc_TypeError,
1042 1050 "filteredrevs has no attribute __contains__");
1043 1051 goto bail;
1044 1052 }
1045 1053 }
1046 1054
1047 1055 len = index_length(self) - 1;
1048 1056 heads = PyList_New(0);
1049 1057 if (heads == NULL)
1050 1058 goto bail;
1051 1059 if (len == 0) {
1052 1060 PyObject *nullid = PyInt_FromLong(-1);
1053 1061 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
1054 1062 Py_XDECREF(nullid);
1055 1063 goto bail;
1056 1064 }
1057 1065 goto done;
1058 1066 }
1059 1067
1060 1068 nothead = calloc(len, 1);
1061 1069 if (nothead == NULL)
1062 1070 goto bail;
1063 1071
1064 1072 for (i = 0; i < self->raw_length; i++) {
1065 1073 const char *data;
1066 1074 int parent_1, parent_2, isfiltered;
1067 1075
1068 1076 isfiltered = check_filter(filter, i);
1069 1077 if (isfiltered == -1) {
1070 1078 PyErr_SetString(PyExc_TypeError,
1071 1079 "unable to check filter");
1072 1080 goto bail;
1073 1081 }
1074 1082
1075 1083 if (isfiltered) {
1076 1084 nothead[i] = 1;
1077 1085 continue;
1078 1086 }
1079 1087
1080 1088 data = index_deref(self, i);
1081 1089 parent_1 = getbe32(data + 24);
1082 1090 parent_2 = getbe32(data + 28);
1083 1091
1084 1092 if (parent_1 >= 0)
1085 1093 nothead[parent_1] = 1;
1086 1094 if (parent_2 >= 0)
1087 1095 nothead[parent_2] = 1;
1088 1096 }
1089 1097
1090 1098 addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
1091 1099
1092 1100 for (i = 0; i < addlen; i++) {
1093 1101 PyObject *rev = PyList_GET_ITEM(self->added, i);
1094 1102 PyObject *p1 = PyTuple_GET_ITEM(rev, 5);
1095 1103 PyObject *p2 = PyTuple_GET_ITEM(rev, 6);
1096 1104 long parent_1, parent_2;
1097 1105 int isfiltered;
1098 1106
1099 1107 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
1100 1108 PyErr_SetString(PyExc_TypeError,
1101 1109 "revlog parents are invalid");
1102 1110 goto bail;
1103 1111 }
1104 1112
1105 1113 isfiltered = check_filter(filter, i);
1106 1114 if (isfiltered == -1) {
1107 1115 PyErr_SetString(PyExc_TypeError,
1108 1116 "unable to check filter");
1109 1117 goto bail;
1110 1118 }
1111 1119
1112 1120 if (isfiltered) {
1113 1121 nothead[i] = 1;
1114 1122 continue;
1115 1123 }
1116 1124
1117 1125 parent_1 = PyInt_AS_LONG(p1);
1118 1126 parent_2 = PyInt_AS_LONG(p2);
1119 1127 if (parent_1 >= 0)
1120 1128 nothead[parent_1] = 1;
1121 1129 if (parent_2 >= 0)
1122 1130 nothead[parent_2] = 1;
1123 1131 }
1124 1132
1125 1133 for (i = 0; i < len; i++) {
1126 1134 PyObject *head;
1127 1135
1128 1136 if (nothead[i])
1129 1137 continue;
1130 1138 head = PyInt_FromSsize_t(i);
1131 1139 if (head == NULL || PyList_Append(heads, head) == -1) {
1132 1140 Py_XDECREF(head);
1133 1141 goto bail;
1134 1142 }
1135 1143 }
1136 1144
1137 1145 done:
1138 1146 self->headrevs = heads;
1139 1147 Py_XDECREF(filter);
1140 1148 free(nothead);
1141 1149 return list_copy(self->headrevs);
1142 1150 bail:
1143 1151 Py_XDECREF(filter);
1144 1152 Py_XDECREF(heads);
1145 1153 free(nothead);
1146 1154 return NULL;
1147 1155 }
1148 1156
1149 1157 static inline int nt_level(const char *node, Py_ssize_t level)
1150 1158 {
1151 1159 int v = node[level>>1];
1152 1160 if (!(level & 1))
1153 1161 v >>= 4;
1154 1162 return v & 0xf;
1155 1163 }
1156 1164
1157 1165 /*
1158 1166 * Return values:
1159 1167 *
1160 1168 * -4: match is ambiguous (multiple candidates)
1161 1169 * -2: not found
1162 1170 * rest: valid rev
1163 1171 */
1164 1172 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
1165 1173 int hex)
1166 1174 {
1167 1175 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
1168 1176 int level, maxlevel, off;
1169 1177
1170 1178 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
1171 1179 return -1;
1172 1180
1173 1181 if (self->nt == NULL)
1174 1182 return -2;
1175 1183
1176 1184 if (hex)
1177 1185 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
1178 1186 else
1179 1187 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
1180 1188
1181 1189 for (level = off = 0; level < maxlevel; level++) {
1182 1190 int k = getnybble(node, level);
1183 1191 nodetree *n = &self->nt[off];
1184 1192 int v = n->children[k];
1185 1193
1186 1194 if (v < 0) {
1187 1195 const char *n;
1188 1196 Py_ssize_t i;
1189 1197
1190 1198 v = -v - 1;
1191 1199 n = index_node(self, v);
1192 1200 if (n == NULL)
1193 1201 return -2;
1194 1202 for (i = level; i < maxlevel; i++)
1195 1203 if (getnybble(node, i) != nt_level(n, i))
1196 1204 return -2;
1197 1205 return v;
1198 1206 }
1199 1207 if (v == 0)
1200 1208 return -2;
1201 1209 off = v;
1202 1210 }
1203 1211 /* multiple matches against an ambiguous prefix */
1204 1212 return -4;
1205 1213 }
1206 1214
1207 1215 static int nt_new(indexObject *self)
1208 1216 {
1209 1217 if (self->ntlength == self->ntcapacity) {
1210 1218 self->ntcapacity *= 2;
1211 1219 self->nt = realloc(self->nt,
1212 1220 self->ntcapacity * sizeof(nodetree));
1213 1221 if (self->nt == NULL) {
1214 1222 PyErr_SetString(PyExc_MemoryError, "out of memory");
1215 1223 return -1;
1216 1224 }
1217 1225 memset(&self->nt[self->ntlength], 0,
1218 1226 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
1219 1227 }
1220 1228 return self->ntlength++;
1221 1229 }
1222 1230
1223 1231 static int nt_insert(indexObject *self, const char *node, int rev)
1224 1232 {
1225 1233 int level = 0;
1226 1234 int off = 0;
1227 1235
1228 1236 while (level < 40) {
1229 1237 int k = nt_level(node, level);
1230 1238 nodetree *n;
1231 1239 int v;
1232 1240
1233 1241 n = &self->nt[off];
1234 1242 v = n->children[k];
1235 1243
1236 1244 if (v == 0) {
1237 1245 n->children[k] = -rev - 1;
1238 1246 return 0;
1239 1247 }
1240 1248 if (v < 0) {
1241 1249 const char *oldnode = index_node(self, -v - 1);
1242 1250 int noff;
1243 1251
1244 1252 if (!oldnode || !memcmp(oldnode, node, 20)) {
1245 1253 n->children[k] = -rev - 1;
1246 1254 return 0;
1247 1255 }
1248 1256 noff = nt_new(self);
1249 1257 if (noff == -1)
1250 1258 return -1;
1251 1259 /* self->nt may have been changed by realloc */
1252 1260 self->nt[off].children[k] = noff;
1253 1261 off = noff;
1254 1262 n = &self->nt[off];
1255 1263 n->children[nt_level(oldnode, ++level)] = v;
1256 1264 if (level > self->ntdepth)
1257 1265 self->ntdepth = level;
1258 1266 self->ntsplits += 1;
1259 1267 } else {
1260 1268 level += 1;
1261 1269 off = v;
1262 1270 }
1263 1271 }
1264 1272
1265 1273 return -1;
1266 1274 }
1267 1275
1268 1276 static int nt_init(indexObject *self)
1269 1277 {
1270 1278 if (self->nt == NULL) {
1271 1279 if (self->raw_length > INT_MAX) {
1272 1280 PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
1273 1281 return -1;
1274 1282 }
1275 1283 self->ntcapacity = self->raw_length < 4
1276 1284 ? 4 : (int)self->raw_length / 2;
1277 1285
1278 1286 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
1279 1287 if (self->nt == NULL) {
1280 1288 PyErr_NoMemory();
1281 1289 return -1;
1282 1290 }
1283 1291 self->ntlength = 1;
1284 1292 self->ntrev = (int)index_length(self) - 1;
1285 1293 self->ntlookups = 1;
1286 1294 self->ntmisses = 0;
1287 1295 if (nt_insert(self, nullid, INT_MAX) == -1)
1288 1296 return -1;
1289 1297 }
1290 1298 return 0;
1291 1299 }
1292 1300
1293 1301 /*
1294 1302 * Return values:
1295 1303 *
1296 1304 * -3: error (exception set)
1297 1305 * -2: not found (no exception set)
1298 1306 * rest: valid rev
1299 1307 */
1300 1308 static int index_find_node(indexObject *self,
1301 1309 const char *node, Py_ssize_t nodelen)
1302 1310 {
1303 1311 int rev;
1304 1312
1305 1313 self->ntlookups++;
1306 1314 rev = nt_find(self, node, nodelen, 0);
1307 1315 if (rev >= -1)
1308 1316 return rev;
1309 1317
1310 1318 if (nt_init(self) == -1)
1311 1319 return -3;
1312 1320
1313 1321 /*
1314 1322 * For the first handful of lookups, we scan the entire index,
1315 1323 * and cache only the matching nodes. This optimizes for cases
1316 1324 * like "hg tip", where only a few nodes are accessed.
1317 1325 *
1318 1326 * After that, we cache every node we visit, using a single
1319 1327 * scan amortized over multiple lookups. This gives the best
1320 1328 * bulk performance, e.g. for "hg log".
1321 1329 */
1322 1330 if (self->ntmisses++ < 4) {
1323 1331 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1324 1332 const char *n = index_node(self, rev);
1325 1333 if (n == NULL)
1326 1334 return -2;
1327 1335 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1328 1336 if (nt_insert(self, n, rev) == -1)
1329 1337 return -3;
1330 1338 break;
1331 1339 }
1332 1340 }
1333 1341 } else {
1334 1342 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1335 1343 const char *n = index_node(self, rev);
1336 1344 if (n == NULL) {
1337 1345 self->ntrev = rev + 1;
1338 1346 return -2;
1339 1347 }
1340 1348 if (nt_insert(self, n, rev) == -1) {
1341 1349 self->ntrev = rev + 1;
1342 1350 return -3;
1343 1351 }
1344 1352 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1345 1353 break;
1346 1354 }
1347 1355 }
1348 1356 self->ntrev = rev;
1349 1357 }
1350 1358
1351 1359 if (rev >= 0)
1352 1360 return rev;
1353 1361 return -2;
1354 1362 }
1355 1363
1356 1364 static PyObject *raise_revlog_error(void)
1357 1365 {
1358 1366 static PyObject *errclass;
1359 1367 PyObject *mod = NULL, *errobj;
1360 1368
1361 1369 if (errclass == NULL) {
1362 1370 PyObject *dict;
1363 1371
1364 1372 mod = PyImport_ImportModule("mercurial.error");
1365 1373 if (mod == NULL)
1366 1374 goto classfail;
1367 1375
1368 1376 dict = PyModule_GetDict(mod);
1369 1377 if (dict == NULL)
1370 1378 goto classfail;
1371 1379
1372 1380 errclass = PyDict_GetItemString(dict, "RevlogError");
1373 1381 if (errclass == NULL) {
1374 1382 PyErr_SetString(PyExc_SystemError,
1375 1383 "could not find RevlogError");
1376 1384 goto classfail;
1377 1385 }
1378 1386 Py_INCREF(errclass);
1379 1387 Py_DECREF(mod);
1380 1388 }
1381 1389
1382 1390 errobj = PyObject_CallFunction(errclass, NULL);
1383 1391 if (errobj == NULL)
1384 1392 return NULL;
1385 1393 PyErr_SetObject(errclass, errobj);
1386 1394 return errobj;
1387 1395
1388 1396 classfail:
1389 1397 Py_XDECREF(mod);
1390 1398 return NULL;
1391 1399 }
1392 1400
1393 1401 static PyObject *index_getitem(indexObject *self, PyObject *value)
1394 1402 {
1395 1403 char *node;
1396 1404 Py_ssize_t nodelen;
1397 1405 int rev;
1398 1406
1399 1407 if (PyInt_Check(value))
1400 1408 return index_get(self, PyInt_AS_LONG(value));
1401 1409
1402 1410 if (node_check(value, &node, &nodelen) == -1)
1403 1411 return NULL;
1404 1412 rev = index_find_node(self, node, nodelen);
1405 1413 if (rev >= -1)
1406 1414 return PyInt_FromLong(rev);
1407 1415 if (rev == -2)
1408 1416 raise_revlog_error();
1409 1417 return NULL;
1410 1418 }
1411 1419
1412 1420 static int nt_partialmatch(indexObject *self, const char *node,
1413 1421 Py_ssize_t nodelen)
1414 1422 {
1415 1423 int rev;
1416 1424
1417 1425 if (nt_init(self) == -1)
1418 1426 return -3;
1419 1427
1420 1428 if (self->ntrev > 0) {
1421 1429 /* ensure that the radix tree is fully populated */
1422 1430 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1423 1431 const char *n = index_node(self, rev);
1424 1432 if (n == NULL)
1425 1433 return -2;
1426 1434 if (nt_insert(self, n, rev) == -1)
1427 1435 return -3;
1428 1436 }
1429 1437 self->ntrev = rev;
1430 1438 }
1431 1439
1432 1440 return nt_find(self, node, nodelen, 1);
1433 1441 }
1434 1442
1435 1443 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1436 1444 {
1437 1445 const char *fullnode;
1438 1446 int nodelen;
1439 1447 char *node;
1440 1448 int rev, i;
1441 1449
1442 1450 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
1443 1451 return NULL;
1444 1452
1445 1453 if (nodelen < 4) {
1446 1454 PyErr_SetString(PyExc_ValueError, "key too short");
1447 1455 return NULL;
1448 1456 }
1449 1457
1450 1458 if (nodelen > 40) {
1451 1459 PyErr_SetString(PyExc_ValueError, "key too long");
1452 1460 return NULL;
1453 1461 }
1454 1462
1455 1463 for (i = 0; i < nodelen; i++)
1456 1464 hexdigit(node, i);
1457 1465 if (PyErr_Occurred()) {
1458 1466 /* input contains non-hex characters */
1459 1467 PyErr_Clear();
1460 1468 Py_RETURN_NONE;
1461 1469 }
1462 1470
1463 1471 rev = nt_partialmatch(self, node, nodelen);
1464 1472
1465 1473 switch (rev) {
1466 1474 case -4:
1467 1475 raise_revlog_error();
1468 1476 case -3:
1469 1477 return NULL;
1470 1478 case -2:
1471 1479 Py_RETURN_NONE;
1472 1480 case -1:
1473 1481 return PyString_FromStringAndSize(nullid, 20);
1474 1482 }
1475 1483
1476 1484 fullnode = index_node(self, rev);
1477 1485 if (fullnode == NULL) {
1478 1486 PyErr_Format(PyExc_IndexError,
1479 1487 "could not access rev %d", rev);
1480 1488 return NULL;
1481 1489 }
1482 1490 return PyString_FromStringAndSize(fullnode, 20);
1483 1491 }
1484 1492
1485 1493 static PyObject *index_m_get(indexObject *self, PyObject *args)
1486 1494 {
1487 1495 Py_ssize_t nodelen;
1488 1496 PyObject *val;
1489 1497 char *node;
1490 1498 int rev;
1491 1499
1492 1500 if (!PyArg_ParseTuple(args, "O", &val))
1493 1501 return NULL;
1494 1502 if (node_check(val, &node, &nodelen) == -1)
1495 1503 return NULL;
1496 1504 rev = index_find_node(self, node, nodelen);
1497 1505 if (rev == -3)
1498 1506 return NULL;
1499 1507 if (rev == -2)
1500 1508 Py_RETURN_NONE;
1501 1509 return PyInt_FromLong(rev);
1502 1510 }
1503 1511
1504 1512 static int index_contains(indexObject *self, PyObject *value)
1505 1513 {
1506 1514 char *node;
1507 1515 Py_ssize_t nodelen;
1508 1516
1509 1517 if (PyInt_Check(value)) {
1510 1518 long rev = PyInt_AS_LONG(value);
1511 1519 return rev >= -1 && rev < index_length(self);
1512 1520 }
1513 1521
1514 1522 if (node_check(value, &node, &nodelen) == -1)
1515 1523 return -1;
1516 1524
1517 1525 switch (index_find_node(self, node, nodelen)) {
1518 1526 case -3:
1519 1527 return -1;
1520 1528 case -2:
1521 1529 return 0;
1522 1530 default:
1523 1531 return 1;
1524 1532 }
1525 1533 }
1526 1534
1527 1535 static inline void index_get_parents(indexObject *self, int rev, int *ps)
1528 1536 {
1529 1537 if (rev >= self->length - 1) {
1530 1538 PyObject *tuple = PyList_GET_ITEM(self->added,
1531 1539 rev - self->length + 1);
1532 1540 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
1533 1541 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
1534 1542 } else {
1535 1543 const char *data = index_deref(self, rev);
1536 1544 ps[0] = getbe32(data + 24);
1537 1545 ps[1] = getbe32(data + 28);
1538 1546 }
1539 1547 }
1540 1548
1541 1549 typedef uint64_t bitmask;
1542 1550
1543 1551 /*
1544 1552 * Given a disjoint set of revs, return all candidates for the
1545 1553 * greatest common ancestor. In revset notation, this is the set
1546 1554 * "heads(::a and ::b and ...)"
1547 1555 */
1548 1556 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1549 1557 int revcount)
1550 1558 {
1551 1559 const bitmask allseen = (1ull << revcount) - 1;
1552 1560 const bitmask poison = 1ull << revcount;
1553 1561 PyObject *gca = PyList_New(0);
1554 1562 int i, v, interesting;
1555 1563 int maxrev = -1;
1556 1564 bitmask sp;
1557 1565 bitmask *seen;
1558 1566
1559 1567 if (gca == NULL)
1560 1568 return PyErr_NoMemory();
1561 1569
1562 1570 for (i = 0; i < revcount; i++) {
1563 1571 if (revs[i] > maxrev)
1564 1572 maxrev = revs[i];
1565 1573 }
1566 1574
1567 1575 seen = calloc(sizeof(*seen), maxrev + 1);
1568 1576 if (seen == NULL) {
1569 1577 Py_DECREF(gca);
1570 1578 return PyErr_NoMemory();
1571 1579 }
1572 1580
1573 1581 for (i = 0; i < revcount; i++)
1574 1582 seen[revs[i]] = 1ull << i;
1575 1583
1576 1584 interesting = revcount;
1577 1585
1578 1586 for (v = maxrev; v >= 0 && interesting; v--) {
1579 1587 bitmask sv = seen[v];
1580 1588 int parents[2];
1581 1589
1582 1590 if (!sv)
1583 1591 continue;
1584 1592
1585 1593 if (sv < poison) {
1586 1594 interesting -= 1;
1587 1595 if (sv == allseen) {
1588 1596 PyObject *obj = PyInt_FromLong(v);
1589 1597 if (obj == NULL)
1590 1598 goto bail;
1591 1599 if (PyList_Append(gca, obj) == -1) {
1592 1600 Py_DECREF(obj);
1593 1601 goto bail;
1594 1602 }
1595 1603 sv |= poison;
1596 1604 for (i = 0; i < revcount; i++) {
1597 1605 if (revs[i] == v)
1598 1606 goto done;
1599 1607 }
1600 1608 }
1601 1609 }
1602 1610 index_get_parents(self, v, parents);
1603 1611
1604 1612 for (i = 0; i < 2; i++) {
1605 1613 int p = parents[i];
1606 1614 if (p == -1)
1607 1615 continue;
1608 1616 sp = seen[p];
1609 1617 if (sv < poison) {
1610 1618 if (sp == 0) {
1611 1619 seen[p] = sv;
1612 1620 interesting++;
1613 1621 }
1614 1622 else if (sp != sv)
1615 1623 seen[p] |= sv;
1616 1624 } else {
1617 1625 if (sp && sp < poison)
1618 1626 interesting--;
1619 1627 seen[p] = sv;
1620 1628 }
1621 1629 }
1622 1630 }
1623 1631
1624 1632 done:
1625 1633 free(seen);
1626 1634 return gca;
1627 1635 bail:
1628 1636 free(seen);
1629 1637 Py_XDECREF(gca);
1630 1638 return NULL;
1631 1639 }
1632 1640
1633 1641 /*
1634 1642 * Given a disjoint set of revs, return the subset with the longest
1635 1643 * path to the root.
1636 1644 */
1637 1645 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1638 1646 {
1639 1647 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1640 1648 static const Py_ssize_t capacity = 24;
1641 1649 int *depth, *interesting = NULL;
1642 1650 int i, j, v, ninteresting;
1643 1651 PyObject *dict = NULL, *keys = NULL;
1644 1652 long *seen = NULL;
1645 1653 int maxrev = -1;
1646 1654 long final;
1647 1655
1648 1656 if (revcount > capacity) {
1649 1657 PyErr_Format(PyExc_OverflowError,
1650 1658 "bitset size (%ld) > capacity (%ld)",
1651 1659 (long)revcount, (long)capacity);
1652 1660 return NULL;
1653 1661 }
1654 1662
1655 1663 for (i = 0; i < revcount; i++) {
1656 1664 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1657 1665 if (n > maxrev)
1658 1666 maxrev = n;
1659 1667 }
1660 1668
1661 1669 depth = calloc(sizeof(*depth), maxrev + 1);
1662 1670 if (depth == NULL)
1663 1671 return PyErr_NoMemory();
1664 1672
1665 1673 seen = calloc(sizeof(*seen), maxrev + 1);
1666 1674 if (seen == NULL) {
1667 1675 PyErr_NoMemory();
1668 1676 goto bail;
1669 1677 }
1670 1678
1671 1679 interesting = calloc(sizeof(*interesting), 2 << revcount);
1672 1680 if (interesting == NULL) {
1673 1681 PyErr_NoMemory();
1674 1682 goto bail;
1675 1683 }
1676 1684
1677 1685 if (PyList_Sort(revs) == -1)
1678 1686 goto bail;
1679 1687
1680 1688 for (i = 0; i < revcount; i++) {
1681 1689 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1682 1690 long b = 1l << i;
1683 1691 depth[n] = 1;
1684 1692 seen[n] = b;
1685 1693 interesting[b] = 1;
1686 1694 }
1687 1695
1688 1696 ninteresting = (int)revcount;
1689 1697
1690 1698 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1691 1699 int dv = depth[v];
1692 1700 int parents[2];
1693 1701 long sv;
1694 1702
1695 1703 if (dv == 0)
1696 1704 continue;
1697 1705
1698 1706 sv = seen[v];
1699 1707 index_get_parents(self, v, parents);
1700 1708
1701 1709 for (i = 0; i < 2; i++) {
1702 1710 int p = parents[i];
1703 1711 long nsp, sp;
1704 1712 int dp;
1705 1713
1706 1714 if (p == -1)
1707 1715 continue;
1708 1716
1709 1717 dp = depth[p];
1710 1718 nsp = sp = seen[p];
1711 1719 if (dp <= dv) {
1712 1720 depth[p] = dv + 1;
1713 1721 if (sp != sv) {
1714 1722 interesting[sv] += 1;
1715 1723 nsp = seen[p] = sv;
1716 1724 if (sp) {
1717 1725 interesting[sp] -= 1;
1718 1726 if (interesting[sp] == 0)
1719 1727 ninteresting -= 1;
1720 1728 }
1721 1729 }
1722 1730 }
1723 1731 else if (dv == dp - 1) {
1724 1732 nsp = sp | sv;
1725 1733 if (nsp == sp)
1726 1734 continue;
1727 1735 seen[p] = nsp;
1728 1736 interesting[sp] -= 1;
1729 1737 if (interesting[sp] == 0 && interesting[nsp] > 0)
1730 1738 ninteresting -= 1;
1731 1739 interesting[nsp] += 1;
1732 1740 }
1733 1741 }
1734 1742 interesting[sv] -= 1;
1735 1743 if (interesting[sv] == 0)
1736 1744 ninteresting -= 1;
1737 1745 }
1738 1746
1739 1747 final = 0;
1740 1748 j = ninteresting;
1741 1749 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1742 1750 if (interesting[i] == 0)
1743 1751 continue;
1744 1752 final |= i;
1745 1753 j -= 1;
1746 1754 }
1747 1755 if (final == 0) {
1748 1756 keys = PyList_New(0);
1749 1757 goto bail;
1750 1758 }
1751 1759
1752 1760 dict = PyDict_New();
1753 1761 if (dict == NULL)
1754 1762 goto bail;
1755 1763
1756 1764 for (i = 0; i < revcount; i++) {
1757 1765 PyObject *key;
1758 1766
1759 1767 if ((final & (1 << i)) == 0)
1760 1768 continue;
1761 1769
1762 1770 key = PyList_GET_ITEM(revs, i);
1763 1771 Py_INCREF(key);
1764 1772 Py_INCREF(Py_None);
1765 1773 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1766 1774 Py_DECREF(key);
1767 1775 Py_DECREF(Py_None);
1768 1776 goto bail;
1769 1777 }
1770 1778 }
1771 1779
1772 1780 keys = PyDict_Keys(dict);
1773 1781
1774 1782 bail:
1775 1783 free(depth);
1776 1784 free(seen);
1777 1785 free(interesting);
1778 1786 Py_XDECREF(dict);
1779 1787
1780 1788 return keys;
1781 1789 }
1782 1790
1783 1791 /*
1784 1792 * Given a (possibly overlapping) set of revs, return all the
1785 1793 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1786 1794 */
1787 1795 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1788 1796 {
1789 1797 PyObject *ret = NULL;
1790 1798 Py_ssize_t argcount, i, len;
1791 1799 bitmask repeat = 0;
1792 1800 int revcount = 0;
1793 1801 int *revs;
1794 1802
1795 1803 argcount = PySequence_Length(args);
1796 1804 revs = malloc(argcount * sizeof(*revs));
1797 1805 if (argcount > 0 && revs == NULL)
1798 1806 return PyErr_NoMemory();
1799 1807 len = index_length(self) - 1;
1800 1808
1801 1809 for (i = 0; i < argcount; i++) {
1802 1810 static const int capacity = 24;
1803 1811 PyObject *obj = PySequence_GetItem(args, i);
1804 1812 bitmask x;
1805 1813 long val;
1806 1814
1807 1815 if (!PyInt_Check(obj)) {
1808 1816 PyErr_SetString(PyExc_TypeError,
1809 1817 "arguments must all be ints");
1810 1818 Py_DECREF(obj);
1811 1819 goto bail;
1812 1820 }
1813 1821 val = PyInt_AsLong(obj);
1814 1822 Py_DECREF(obj);
1815 1823 if (val == -1) {
1816 1824 ret = PyList_New(0);
1817 1825 goto done;
1818 1826 }
1819 1827 if (val < 0 || val >= len) {
1820 1828 PyErr_SetString(PyExc_IndexError,
1821 1829 "index out of range");
1822 1830 goto bail;
1823 1831 }
1824 1832 /* this cheesy bloom filter lets us avoid some more
1825 1833 * expensive duplicate checks in the common set-is-disjoint
1826 1834 * case */
1827 1835 x = 1ull << (val & 0x3f);
1828 1836 if (repeat & x) {
1829 1837 int k;
1830 1838 for (k = 0; k < revcount; k++) {
1831 1839 if (val == revs[k])
1832 1840 goto duplicate;
1833 1841 }
1834 1842 }
1835 1843 else repeat |= x;
1836 1844 if (revcount >= capacity) {
1837 1845 PyErr_Format(PyExc_OverflowError,
1838 1846 "bitset size (%d) > capacity (%d)",
1839 1847 revcount, capacity);
1840 1848 goto bail;
1841 1849 }
1842 1850 revs[revcount++] = (int)val;
1843 1851 duplicate:;
1844 1852 }
1845 1853
1846 1854 if (revcount == 0) {
1847 1855 ret = PyList_New(0);
1848 1856 goto done;
1849 1857 }
1850 1858 if (revcount == 1) {
1851 1859 PyObject *obj;
1852 1860 ret = PyList_New(1);
1853 1861 if (ret == NULL)
1854 1862 goto bail;
1855 1863 obj = PyInt_FromLong(revs[0]);
1856 1864 if (obj == NULL)
1857 1865 goto bail;
1858 1866 PyList_SET_ITEM(ret, 0, obj);
1859 1867 goto done;
1860 1868 }
1861 1869
1862 1870 ret = find_gca_candidates(self, revs, revcount);
1863 1871 if (ret == NULL)
1864 1872 goto bail;
1865 1873
1866 1874 done:
1867 1875 free(revs);
1868 1876 return ret;
1869 1877
1870 1878 bail:
1871 1879 free(revs);
1872 1880 Py_XDECREF(ret);
1873 1881 return NULL;
1874 1882 }
1875 1883
1876 1884 /*
1877 1885 * Given a (possibly overlapping) set of revs, return the greatest
1878 1886 * common ancestors: those with the longest path to the root.
1879 1887 */
1880 1888 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1881 1889 {
1882 1890 PyObject *gca = index_commonancestorsheads(self, args);
1883 1891 if (gca == NULL)
1884 1892 return NULL;
1885 1893
1886 1894 if (PyList_GET_SIZE(gca) <= 1) {
1887 1895 Py_INCREF(gca);
1888 1896 return gca;
1889 1897 }
1890 1898
1891 1899 return find_deepest(self, gca);
1892 1900 }
1893 1901
1894 1902 /*
1895 1903 * Invalidate any trie entries introduced by added revs.
1896 1904 */
1897 1905 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
1898 1906 {
1899 1907 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1900 1908
1901 1909 for (i = start; i < len; i++) {
1902 1910 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1903 1911 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1904 1912
1905 1913 nt_insert(self, PyString_AS_STRING(node), -1);
1906 1914 }
1907 1915
1908 1916 if (start == 0)
1909 1917 Py_CLEAR(self->added);
1910 1918 }
1911 1919
1912 1920 /*
1913 1921 * Delete a numeric range of revs, which must be at the end of the
1914 1922 * range, but exclude the sentinel nullid entry.
1915 1923 */
1916 1924 static int index_slice_del(indexObject *self, PyObject *item)
1917 1925 {
1918 1926 Py_ssize_t start, stop, step, slicelength;
1919 1927 Py_ssize_t length = index_length(self);
1920 1928 int ret = 0;
1921 1929
1922 1930 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1923 1931 &start, &stop, &step, &slicelength) < 0)
1924 1932 return -1;
1925 1933
1926 1934 if (slicelength <= 0)
1927 1935 return 0;
1928 1936
1929 1937 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1930 1938 stop = start;
1931 1939
1932 1940 if (step < 0) {
1933 1941 stop = start + 1;
1934 1942 start = stop + step*(slicelength - 1) - 1;
1935 1943 step = -step;
1936 1944 }
1937 1945
1938 1946 if (step != 1) {
1939 1947 PyErr_SetString(PyExc_ValueError,
1940 1948 "revlog index delete requires step size of 1");
1941 1949 return -1;
1942 1950 }
1943 1951
1944 1952 if (stop != length - 1) {
1945 1953 PyErr_SetString(PyExc_IndexError,
1946 1954 "revlog index deletion indices are invalid");
1947 1955 return -1;
1948 1956 }
1949 1957
1950 1958 if (start < self->length - 1) {
1951 1959 if (self->nt) {
1952 1960 Py_ssize_t i;
1953 1961
1954 1962 for (i = start + 1; i < self->length - 1; i++) {
1955 1963 const char *node = index_node(self, i);
1956 1964
1957 1965 if (node)
1958 1966 nt_insert(self, node, -1);
1959 1967 }
1960 1968 if (self->added)
1961 1969 nt_invalidate_added(self, 0);
1962 1970 if (self->ntrev > start)
1963 1971 self->ntrev = (int)start;
1964 1972 }
1965 1973 self->length = start + 1;
1966 1974 if (start < self->raw_length) {
1967 1975 if (self->cache) {
1968 1976 Py_ssize_t i;
1969 1977 for (i = start; i < self->raw_length; i++)
1970 1978 Py_CLEAR(self->cache[i]);
1971 1979 }
1972 1980 self->raw_length = start;
1973 1981 }
1974 1982 goto done;
1975 1983 }
1976 1984
1977 1985 if (self->nt) {
1978 1986 nt_invalidate_added(self, start - self->length + 1);
1979 1987 if (self->ntrev > start)
1980 1988 self->ntrev = (int)start;
1981 1989 }
1982 1990 if (self->added)
1983 1991 ret = PyList_SetSlice(self->added, start - self->length + 1,
1984 1992 PyList_GET_SIZE(self->added), NULL);
1985 1993 done:
1986 1994 Py_CLEAR(self->headrevs);
1987 1995 return ret;
1988 1996 }
1989 1997
1990 1998 /*
1991 1999 * Supported ops:
1992 2000 *
1993 2001 * slice deletion
1994 2002 * string assignment (extend node->rev mapping)
1995 2003 * string deletion (shrink node->rev mapping)
1996 2004 */
1997 2005 static int index_assign_subscript(indexObject *self, PyObject *item,
1998 2006 PyObject *value)
1999 2007 {
2000 2008 char *node;
2001 2009 Py_ssize_t nodelen;
2002 2010 long rev;
2003 2011
2004 2012 if (PySlice_Check(item) && value == NULL)
2005 2013 return index_slice_del(self, item);
2006 2014
2007 2015 if (node_check(item, &node, &nodelen) == -1)
2008 2016 return -1;
2009 2017
2010 2018 if (value == NULL)
2011 2019 return self->nt ? nt_insert(self, node, -1) : 0;
2012 2020 rev = PyInt_AsLong(value);
2013 2021 if (rev > INT_MAX || rev < 0) {
2014 2022 if (!PyErr_Occurred())
2015 2023 PyErr_SetString(PyExc_ValueError, "rev out of range");
2016 2024 return -1;
2017 2025 }
2018 2026
2019 2027 if (nt_init(self) == -1)
2020 2028 return -1;
2021 2029 return nt_insert(self, node, (int)rev);
2022 2030 }
2023 2031
2024 2032 /*
2025 2033 * Find all RevlogNG entries in an index that has inline data. Update
2026 2034 * the optional "offsets" table with those entries.
2027 2035 */
2028 2036 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2029 2037 {
2030 2038 const char *data = PyString_AS_STRING(self->data);
2031 2039 Py_ssize_t pos = 0;
2032 2040 Py_ssize_t end = PyString_GET_SIZE(self->data);
2033 2041 long incr = v1_hdrsize;
2034 2042 Py_ssize_t len = 0;
2035 2043
2036 2044 while (pos + v1_hdrsize <= end && pos >= 0) {
2037 2045 uint32_t comp_len;
2038 2046 /* 3rd element of header is length of compressed inline data */
2039 2047 comp_len = getbe32(data + pos + 8);
2040 2048 incr = v1_hdrsize + comp_len;
2041 2049 if (offsets)
2042 2050 offsets[len] = data + pos;
2043 2051 len++;
2044 2052 pos += incr;
2045 2053 }
2046 2054
2047 2055 if (pos != end) {
2048 2056 if (!PyErr_Occurred())
2049 2057 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2050 2058 return -1;
2051 2059 }
2052 2060
2053 2061 return len;
2054 2062 }
2055 2063
2056 2064 static int index_init(indexObject *self, PyObject *args)
2057 2065 {
2058 2066 PyObject *data_obj, *inlined_obj;
2059 2067 Py_ssize_t size;
2060 2068
2061 2069 /* Initialize before argument-checking to avoid index_dealloc() crash. */
2062 2070 self->raw_length = 0;
2063 2071 self->added = NULL;
2064 2072 self->cache = NULL;
2065 2073 self->data = NULL;
2066 2074 self->headrevs = NULL;
2067 2075 self->filteredrevs = Py_None;
2068 2076 Py_INCREF(Py_None);
2069 2077 self->nt = NULL;
2070 2078 self->offsets = NULL;
2071 2079
2072 2080 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2073 2081 return -1;
2074 2082 if (!PyString_Check(data_obj)) {
2075 2083 PyErr_SetString(PyExc_TypeError, "data is not a string");
2076 2084 return -1;
2077 2085 }
2078 2086 size = PyString_GET_SIZE(data_obj);
2079 2087
2080 2088 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2081 2089 self->data = data_obj;
2082 2090
2083 2091 self->ntlength = self->ntcapacity = 0;
2084 2092 self->ntdepth = self->ntsplits = 0;
2085 2093 self->ntlookups = self->ntmisses = 0;
2086 2094 self->ntrev = -1;
2087 2095 Py_INCREF(self->data);
2088 2096
2089 2097 if (self->inlined) {
2090 2098 Py_ssize_t len = inline_scan(self, NULL);
2091 2099 if (len == -1)
2092 2100 goto bail;
2093 2101 self->raw_length = len;
2094 2102 self->length = len + 1;
2095 2103 } else {
2096 2104 if (size % v1_hdrsize) {
2097 2105 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2098 2106 goto bail;
2099 2107 }
2100 2108 self->raw_length = size / v1_hdrsize;
2101 2109 self->length = self->raw_length + 1;
2102 2110 }
2103 2111
2104 2112 return 0;
2105 2113 bail:
2106 2114 return -1;
2107 2115 }
2108 2116
2109 2117 static PyObject *index_nodemap(indexObject *self)
2110 2118 {
2111 2119 Py_INCREF(self);
2112 2120 return (PyObject *)self;
2113 2121 }
2114 2122
2115 2123 static void index_dealloc(indexObject *self)
2116 2124 {
2117 2125 _index_clearcaches(self);
2118 2126 Py_XDECREF(self->filteredrevs);
2119 2127 Py_XDECREF(self->data);
2120 2128 Py_XDECREF(self->added);
2121 2129 PyObject_Del(self);
2122 2130 }
2123 2131
2124 2132 static PySequenceMethods index_sequence_methods = {
2125 2133 (lenfunc)index_length, /* sq_length */
2126 2134 0, /* sq_concat */
2127 2135 0, /* sq_repeat */
2128 2136 (ssizeargfunc)index_get, /* sq_item */
2129 2137 0, /* sq_slice */
2130 2138 0, /* sq_ass_item */
2131 2139 0, /* sq_ass_slice */
2132 2140 (objobjproc)index_contains, /* sq_contains */
2133 2141 };
2134 2142
2135 2143 static PyMappingMethods index_mapping_methods = {
2136 2144 (lenfunc)index_length, /* mp_length */
2137 2145 (binaryfunc)index_getitem, /* mp_subscript */
2138 2146 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2139 2147 };
2140 2148
2141 2149 static PyMethodDef index_methods[] = {
2142 2150 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2143 2151 "return the gca set of the given revs"},
2144 2152 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2145 2153 METH_VARARGS,
2146 2154 "return the heads of the common ancestors of the given revs"},
2147 2155 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2148 2156 "clear the index caches"},
2149 2157 {"get", (PyCFunction)index_m_get, METH_VARARGS,
2150 2158 "get an index entry"},
2151 2159 {"computephases", (PyCFunction)compute_phases, METH_VARARGS,
2152 2160 "compute phases"},
2153 2161 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2154 2162 "get head revisions"}, /* Can do filtering since 3.2 */
2155 2163 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2156 2164 "get filtered head revisions"}, /* Can always do filtering */
2157 2165 {"insert", (PyCFunction)index_insert, METH_VARARGS,
2158 2166 "insert an index entry"},
2159 2167 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2160 2168 "match a potentially ambiguous node ID"},
2161 2169 {"stats", (PyCFunction)index_stats, METH_NOARGS,
2162 2170 "stats for the index"},
2163 2171 {NULL} /* Sentinel */
2164 2172 };
2165 2173
2166 2174 static PyGetSetDef index_getset[] = {
2167 2175 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2168 2176 {NULL} /* Sentinel */
2169 2177 };
2170 2178
2171 2179 static PyTypeObject indexType = {
2172 2180 PyObject_HEAD_INIT(NULL)
2173 2181 0, /* ob_size */
2174 2182 "parsers.index", /* tp_name */
2175 2183 sizeof(indexObject), /* tp_basicsize */
2176 2184 0, /* tp_itemsize */
2177 2185 (destructor)index_dealloc, /* tp_dealloc */
2178 2186 0, /* tp_print */
2179 2187 0, /* tp_getattr */
2180 2188 0, /* tp_setattr */
2181 2189 0, /* tp_compare */
2182 2190 0, /* tp_repr */
2183 2191 0, /* tp_as_number */
2184 2192 &index_sequence_methods, /* tp_as_sequence */
2185 2193 &index_mapping_methods, /* tp_as_mapping */
2186 2194 0, /* tp_hash */
2187 2195 0, /* tp_call */
2188 2196 0, /* tp_str */
2189 2197 0, /* tp_getattro */
2190 2198 0, /* tp_setattro */
2191 2199 0, /* tp_as_buffer */
2192 2200 Py_TPFLAGS_DEFAULT, /* tp_flags */
2193 2201 "revlog index", /* tp_doc */
2194 2202 0, /* tp_traverse */
2195 2203 0, /* tp_clear */
2196 2204 0, /* tp_richcompare */
2197 2205 0, /* tp_weaklistoffset */
2198 2206 0, /* tp_iter */
2199 2207 0, /* tp_iternext */
2200 2208 index_methods, /* tp_methods */
2201 2209 0, /* tp_members */
2202 2210 index_getset, /* tp_getset */
2203 2211 0, /* tp_base */
2204 2212 0, /* tp_dict */
2205 2213 0, /* tp_descr_get */
2206 2214 0, /* tp_descr_set */
2207 2215 0, /* tp_dictoffset */
2208 2216 (initproc)index_init, /* tp_init */
2209 2217 0, /* tp_alloc */
2210 2218 };
2211 2219
2212 2220 /*
2213 2221 * returns a tuple of the form (index, index, cache) with elements as
2214 2222 * follows:
2215 2223 *
2216 2224 * index: an index object that lazily parses RevlogNG records
2217 2225 * cache: if data is inlined, a tuple (index_file_content, 0), else None
2218 2226 *
2219 2227 * added complications are for backwards compatibility
2220 2228 */
2221 2229 static PyObject *parse_index2(PyObject *self, PyObject *args)
2222 2230 {
2223 2231 PyObject *tuple = NULL, *cache = NULL;
2224 2232 indexObject *idx;
2225 2233 int ret;
2226 2234
2227 2235 idx = PyObject_New(indexObject, &indexType);
2228 2236 if (idx == NULL)
2229 2237 goto bail;
2230 2238
2231 2239 ret = index_init(idx, args);
2232 2240 if (ret == -1)
2233 2241 goto bail;
2234 2242
2235 2243 if (idx->inlined) {
2236 2244 cache = Py_BuildValue("iO", 0, idx->data);
2237 2245 if (cache == NULL)
2238 2246 goto bail;
2239 2247 } else {
2240 2248 cache = Py_None;
2241 2249 Py_INCREF(cache);
2242 2250 }
2243 2251
2244 2252 tuple = Py_BuildValue("NN", idx, cache);
2245 2253 if (!tuple)
2246 2254 goto bail;
2247 2255 return tuple;
2248 2256
2249 2257 bail:
2250 2258 Py_XDECREF(idx);
2251 2259 Py_XDECREF(cache);
2252 2260 Py_XDECREF(tuple);
2253 2261 return NULL;
2254 2262 }
2255 2263
2256 2264 #define BUMPED_FIX 1
2257 2265 #define USING_SHA_256 2
2258 2266
2259 2267 static PyObject *readshas(
2260 2268 const char *source, unsigned char num, Py_ssize_t hashwidth)
2261 2269 {
2262 2270 int i;
2263 2271 PyObject *list = PyTuple_New(num);
2264 2272 if (list == NULL) {
2265 2273 return NULL;
2266 2274 }
2267 2275 for (i = 0; i < num; i++) {
2268 2276 PyObject *hash = PyString_FromStringAndSize(source, hashwidth);
2269 2277 if (hash == NULL) {
2270 2278 Py_DECREF(list);
2271 2279 return NULL;
2272 2280 }
2273 2281 PyTuple_SetItem(list, i, hash);
2274 2282 source += hashwidth;
2275 2283 }
2276 2284 return list;
2277 2285 }
2278 2286
2279 2287 static PyObject *fm1readmarker(const char *data, uint32_t *msize)
2280 2288 {
2281 2289 const char *meta;
2282 2290
2283 2291 double mtime;
2284 2292 int16_t tz;
2285 2293 uint16_t flags;
2286 2294 unsigned char nsuccs, nparents, nmetadata;
2287 2295 Py_ssize_t hashwidth = 20;
2288 2296
2289 2297 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
2290 2298 PyObject *metadata = NULL, *ret = NULL;
2291 2299 int i;
2292 2300
2293 2301 *msize = getbe32(data);
2294 2302 data += 4;
2295 2303 mtime = getbefloat64(data);
2296 2304 data += 8;
2297 2305 tz = getbeint16(data);
2298 2306 data += 2;
2299 2307 flags = getbeuint16(data);
2300 2308 data += 2;
2301 2309
2302 2310 if (flags & USING_SHA_256) {
2303 2311 hashwidth = 32;
2304 2312 }
2305 2313
2306 2314 nsuccs = (unsigned char)(*data++);
2307 2315 nparents = (unsigned char)(*data++);
2308 2316 nmetadata = (unsigned char)(*data++);
2309 2317
2310 2318 prec = PyString_FromStringAndSize(data, hashwidth);
2311 2319 data += hashwidth;
2312 2320 if (prec == NULL) {
2313 2321 goto bail;
2314 2322 }
2315 2323
2316 2324 succs = readshas(data, nsuccs, hashwidth);
2317 2325 if (succs == NULL) {
2318 2326 goto bail;
2319 2327 }
2320 2328 data += nsuccs * hashwidth;
2321 2329
2322 2330 if (nparents == 1 || nparents == 2) {
2323 2331 parents = readshas(data, nparents, hashwidth);
2324 2332 if (parents == NULL) {
2325 2333 goto bail;
2326 2334 }
2327 2335 data += nparents * hashwidth;
2328 2336 } else {
2329 2337 parents = Py_None;
2330 2338 }
2331 2339
2332 2340 meta = data + (2 * nmetadata);
2333 2341 metadata = PyTuple_New(nmetadata);
2334 2342 if (metadata == NULL) {
2335 2343 goto bail;
2336 2344 }
2337 2345 for (i = 0; i < nmetadata; i++) {
2338 2346 PyObject *tmp, *left = NULL, *right = NULL;
2339 2347 Py_ssize_t metasize = (unsigned char)(*data++);
2340 2348 left = PyString_FromStringAndSize(meta, metasize);
2341 2349 meta += metasize;
2342 2350 metasize = (unsigned char)(*data++);
2343 2351 right = PyString_FromStringAndSize(meta, metasize);
2344 2352 meta += metasize;
2345 2353 if (!left || !right) {
2346 2354 Py_XDECREF(left);
2347 2355 Py_XDECREF(right);
2348 2356 goto bail;
2349 2357 }
2350 2358 tmp = PyTuple_Pack(2, left, right);
2351 2359 Py_DECREF(left);
2352 2360 Py_DECREF(right);
2353 2361 if (!tmp) {
2354 2362 goto bail;
2355 2363 }
2356 2364 PyTuple_SetItem(metadata, i, tmp);
2357 2365 }
2358 2366 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags,
2359 2367 metadata, mtime, (int)tz * 60, parents);
2360 2368 bail:
2361 2369 Py_XDECREF(prec);
2362 2370 Py_XDECREF(succs);
2363 2371 Py_XDECREF(metadata);
2364 2372 if (parents != Py_None)
2365 2373 Py_XDECREF(parents);
2366 2374 return ret;
2367 2375 }
2368 2376
2369 2377
2370 2378 static PyObject *fm1readmarkers(PyObject *self, PyObject *args) {
2371 2379 const char *data;
2372 2380 Py_ssize_t datalen;
2373 2381 /* only unsigned long because python 2.4, should be Py_ssize_t */
2374 2382 unsigned long offset, stop;
2375 2383 PyObject *markers = NULL;
2376 2384
2377 2385 /* replace kk with nn when we drop Python 2.4 */
2378 2386 if (!PyArg_ParseTuple(args, "s#kk", &data, &datalen, &offset, &stop)) {
2379 2387 return NULL;
2380 2388 }
2381 2389 data += offset;
2382 2390 markers = PyList_New(0);
2383 2391 if (!markers) {
2384 2392 return NULL;
2385 2393 }
2386 2394 while (offset < stop) {
2387 2395 uint32_t msize;
2388 2396 int error;
2389 2397 PyObject *record = fm1readmarker(data, &msize);
2390 2398 if (!record) {
2391 2399 goto bail;
2392 2400 }
2393 2401 error = PyList_Append(markers, record);
2394 2402 Py_DECREF(record);
2395 2403 if (error) {
2396 2404 goto bail;
2397 2405 }
2398 2406 data += msize;
2399 2407 offset += msize;
2400 2408 }
2401 2409 return markers;
2402 2410 bail:
2403 2411 Py_DECREF(markers);
2404 2412 return NULL;
2405 2413 }
2406 2414
2407 2415 static char parsers_doc[] = "Efficient content parsing.";
2408 2416
2409 2417 PyObject *encodedir(PyObject *self, PyObject *args);
2410 2418 PyObject *pathencode(PyObject *self, PyObject *args);
2411 2419 PyObject *lowerencode(PyObject *self, PyObject *args);
2412 2420
2413 2421 static PyMethodDef methods[] = {
2414 2422 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
2415 2423 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
2416 2424 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
2417 2425 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
2418 2426 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
2419 2427 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
2420 2428 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
2421 2429 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
2422 2430 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
2423 2431 "parse v1 obsolete markers\n"},
2424 2432 {NULL, NULL}
2425 2433 };
2426 2434
2427 2435 void dirs_module_init(PyObject *mod);
2428 2436 void manifest_module_init(PyObject *mod);
2429 2437
2430 2438 static void module_init(PyObject *mod)
2431 2439 {
2432 2440 /* This module constant has two purposes. First, it lets us unit test
2433 2441 * the ImportError raised without hard-coding any error text. This
2434 2442 * means we can change the text in the future without breaking tests,
2435 2443 * even across changesets without a recompile. Second, its presence
2436 2444 * can be used to determine whether the version-checking logic is
2437 2445 * present, which also helps in testing across changesets without a
2438 2446 * recompile. Note that this means the pure-Python version of parsers
2439 2447 * should not have this module constant. */
2440 2448 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
2441 2449
2442 2450 dirs_module_init(mod);
2443 2451 manifest_module_init(mod);
2444 2452
2445 2453 indexType.tp_new = PyType_GenericNew;
2446 2454 if (PyType_Ready(&indexType) < 0 ||
2447 2455 PyType_Ready(&dirstateTupleType) < 0)
2448 2456 return;
2449 2457 Py_INCREF(&indexType);
2450 2458 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2451 2459 Py_INCREF(&dirstateTupleType);
2452 2460 PyModule_AddObject(mod, "dirstatetuple",
2453 2461 (PyObject *)&dirstateTupleType);
2454 2462
2455 2463 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
2456 2464 -1, -1, -1, -1, nullid, 20);
2457 2465 if (nullentry)
2458 2466 PyObject_GC_UnTrack(nullentry);
2459 2467 }
2460 2468
2461 2469 static int check_python_version(void)
2462 2470 {
2463 2471 PyObject *sys = PyImport_ImportModule("sys"), *ver;
2464 2472 long hexversion;
2465 2473 if (!sys)
2466 2474 return -1;
2467 2475 ver = PyObject_GetAttrString(sys, "hexversion");
2468 2476 Py_DECREF(sys);
2469 2477 if (!ver)
2470 2478 return -1;
2471 2479 hexversion = PyInt_AsLong(ver);
2472 2480 Py_DECREF(ver);
2473 2481 /* sys.hexversion is a 32-bit number by default, so the -1 case
2474 2482 * should only occur in unusual circumstances (e.g. if sys.hexversion
2475 2483 * is manually set to an invalid value). */
2476 2484 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
2477 2485 PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension "
2478 2486 "modules were compiled with Python " PY_VERSION ", but "
2479 2487 "Mercurial is currently using Python with sys.hexversion=%ld: "
2480 2488 "Python %s\n at: %s", versionerrortext, hexversion,
2481 2489 Py_GetVersion(), Py_GetProgramFullPath());
2482 2490 return -1;
2483 2491 }
2484 2492 return 0;
2485 2493 }
2486 2494
2487 2495 #ifdef IS_PY3K
2488 2496 static struct PyModuleDef parsers_module = {
2489 2497 PyModuleDef_HEAD_INIT,
2490 2498 "parsers",
2491 2499 parsers_doc,
2492 2500 -1,
2493 2501 methods
2494 2502 };
2495 2503
2496 2504 PyMODINIT_FUNC PyInit_parsers(void)
2497 2505 {
2498 2506 PyObject *mod;
2499 2507
2500 2508 if (check_python_version() == -1)
2501 2509 return;
2502 2510 mod = PyModule_Create(&parsers_module);
2503 2511 module_init(mod);
2504 2512 return mod;
2505 2513 }
2506 2514 #else
2507 2515 PyMODINIT_FUNC initparsers(void)
2508 2516 {
2509 2517 PyObject *mod;
2510 2518
2511 2519 if (check_python_version() == -1)
2512 2520 return;
2513 2521 mod = Py_InitModule3("parsers", methods, parsers_doc);
2514 2522 module_init(mod);
2515 2523 }
2516 2524 #endif
General Comments 0
You need to be logged in to leave comments. Login now