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