Show More
@@ -1,1004 +1,1004 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | parsers.c - efficient content parsing |
|
3 | 3 | |
|
4 | 4 | Copyright 2008 Olivia Mackall <olivia@selenic.com> and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #define PY_SSIZE_T_CLEAN |
|
11 | 11 | #include <Python.h> |
|
12 | 12 | #include <ctype.h> |
|
13 | 13 | #include <stddef.h> |
|
14 | 14 | #include <string.h> |
|
15 | 15 | |
|
16 | 16 | #include "bitmanipulation.h" |
|
17 | 17 | #include "charencode.h" |
|
18 | 18 | #include "util.h" |
|
19 | 19 | |
|
20 | 20 | #ifdef IS_PY3K |
|
21 | 21 | /* The mapping of Python types is meant to be temporary to get Python |
|
22 | 22 | * 3 to compile. We should remove this once Python 3 support is fully |
|
23 | 23 | * supported and proper types are used in the extensions themselves. */ |
|
24 | 24 | #define PyInt_Check PyLong_Check |
|
25 | 25 | #define PyInt_FromLong PyLong_FromLong |
|
26 | 26 | #define PyInt_FromSsize_t PyLong_FromSsize_t |
|
27 | 27 | #define PyInt_AsLong PyLong_AsLong |
|
28 | 28 | #endif |
|
29 | 29 | |
|
30 | 30 | static const char *const versionerrortext = "Python minor version mismatch"; |
|
31 | 31 | |
|
32 | 32 | static const int dirstate_v1_from_p2 = -2; |
|
33 | 33 | static const int dirstate_v1_nonnormal = -1; |
|
34 | 34 | static const int ambiguous_time = -1; |
|
35 | 35 | |
|
36 | 36 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) |
|
37 | 37 | { |
|
38 | 38 | Py_ssize_t expected_size; |
|
39 | 39 | |
|
40 | 40 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) { |
|
41 | 41 | return NULL; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | return _dict_new_presized(expected_size); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | static inline dirstateItemObject *make_dirstate_item(char state, int mode, |
|
48 | 48 | int size, int mtime) |
|
49 | 49 | { |
|
50 | 50 | dirstateItemObject *t = |
|
51 | 51 | PyObject_New(dirstateItemObject, &dirstateItemType); |
|
52 | 52 | if (!t) { |
|
53 | 53 | return NULL; |
|
54 | 54 | } |
|
55 | 55 | t->state = state; |
|
56 | 56 | t->mode = mode; |
|
57 | 57 | t->size = size; |
|
58 | 58 | t->mtime = mtime; |
|
59 | 59 | return t; |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args, |
|
63 | 63 | PyObject *kwds) |
|
64 | 64 | { |
|
65 | 65 | /* We do all the initialization here and not a tp_init function because |
|
66 | 66 | * dirstate_item is immutable. */ |
|
67 | 67 | dirstateItemObject *t; |
|
68 | 68 | char state; |
|
69 | 69 | int size, mode, mtime; |
|
70 | 70 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) { |
|
71 | 71 | return NULL; |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1); |
|
75 | 75 | if (!t) { |
|
76 | 76 | return NULL; |
|
77 | 77 | } |
|
78 | 78 | t->state = state; |
|
79 | 79 | t->mode = mode; |
|
80 | 80 | t->size = size; |
|
81 | 81 | t->mtime = mtime; |
|
82 | 82 | |
|
83 | 83 | return (PyObject *)t; |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | static void dirstate_item_dealloc(PyObject *o) |
|
87 | 87 | { |
|
88 | 88 | PyObject_Del(o); |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | static Py_ssize_t dirstate_item_length(PyObject *o) |
|
92 | 92 | { |
|
93 | 93 | return 4; |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | static PyObject *dirstate_item_item(PyObject *o, Py_ssize_t i) |
|
97 | 97 | { |
|
98 | 98 | dirstateItemObject *t = (dirstateItemObject *)o; |
|
99 | 99 | switch (i) { |
|
100 | 100 | case 0: |
|
101 | 101 | return PyBytes_FromStringAndSize(&t->state, 1); |
|
102 | 102 | case 1: |
|
103 | 103 | return PyInt_FromLong(t->mode); |
|
104 | 104 | case 2: |
|
105 | 105 | return PyInt_FromLong(t->size); |
|
106 | 106 | case 3: |
|
107 | 107 | return PyInt_FromLong(t->mtime); |
|
108 | 108 | default: |
|
109 | 109 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
110 | 110 | return NULL; |
|
111 | 111 | } |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | static PySequenceMethods dirstate_item_sq = { |
|
115 | 115 | dirstate_item_length, /* sq_length */ |
|
116 | 116 | 0, /* sq_concat */ |
|
117 | 117 | 0, /* sq_repeat */ |
|
118 | 118 | dirstate_item_item, /* sq_item */ |
|
119 | 119 | 0, /* sq_ass_item */ |
|
120 | 120 | 0, /* sq_contains */ |
|
121 | 121 | 0, /* sq_inplace_concat */ |
|
122 | 122 | 0 /* sq_inplace_repeat */ |
|
123 | 123 | }; |
|
124 | 124 | |
|
125 | 125 | static PyObject *dirstate_item_v1_state(dirstateItemObject *self) |
|
126 | 126 | { |
|
127 | 127 | return PyBytes_FromStringAndSize(&self->state, 1); |
|
128 | 128 | }; |
|
129 | 129 | |
|
130 | 130 | static PyObject *dirstate_item_v1_mode(dirstateItemObject *self) |
|
131 | 131 | { |
|
132 | 132 | return PyInt_FromLong(self->mode); |
|
133 | 133 | }; |
|
134 | 134 | |
|
135 | 135 | static PyObject *dirstate_item_v1_size(dirstateItemObject *self) |
|
136 | 136 | { |
|
137 | 137 | return PyInt_FromLong(self->size); |
|
138 | 138 | }; |
|
139 | 139 | |
|
140 | 140 | static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self) |
|
141 | 141 | { |
|
142 | 142 | return PyInt_FromLong(self->mtime); |
|
143 | 143 | }; |
|
144 | 144 | |
|
145 | 145 | static PyObject *dirstate_item_need_delay(dirstateItemObject *self, |
|
146 | 146 | PyObject *value) |
|
147 | 147 | { |
|
148 | 148 | long now; |
|
149 | 149 | if (!pylong_to_long(value, &now)) { |
|
150 | 150 | return NULL; |
|
151 | 151 | } |
|
152 | 152 | if (self->state == 'n' && self->mtime == now) { |
|
153 | 153 | Py_RETURN_TRUE; |
|
154 | 154 | } else { |
|
155 | 155 | Py_RETURN_FALSE; |
|
156 | 156 | } |
|
157 | 157 | }; |
|
158 | 158 | |
|
159 | 159 | /* This will never change since it's bound to V1, unlike `make_dirstate_item` |
|
160 | 160 | */ |
|
161 | 161 | static inline dirstateItemObject * |
|
162 | 162 | dirstate_item_from_v1_data(char state, int mode, int size, int mtime) |
|
163 | 163 | { |
|
164 | 164 | dirstateItemObject *t = |
|
165 | 165 | PyObject_New(dirstateItemObject, &dirstateItemType); |
|
166 | 166 | if (!t) { |
|
167 | 167 | return NULL; |
|
168 | 168 | } |
|
169 | 169 | t->state = state; |
|
170 | 170 | t->mode = mode; |
|
171 | 171 | t->size = size; |
|
172 | 172 | t->mtime = mtime; |
|
173 | 173 | return t; |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | /* This will never change since it's bound to V1, unlike `dirstate_item_new` */ |
|
177 | 177 | static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype, |
|
178 | 178 | PyObject *args) |
|
179 | 179 | { |
|
180 | 180 | /* We do all the initialization here and not a tp_init function because |
|
181 | 181 | * dirstate_item is immutable. */ |
|
182 | 182 | dirstateItemObject *t; |
|
183 | 183 | char state; |
|
184 | 184 | int size, mode, mtime; |
|
185 | 185 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) { |
|
186 | 186 | return NULL; |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1); |
|
190 | 190 | if (!t) { |
|
191 | 191 | return NULL; |
|
192 | 192 | } |
|
193 | 193 | t->state = state; |
|
194 | 194 | t->mode = mode; |
|
195 | 195 | t->size = size; |
|
196 | 196 | t->mtime = mtime; |
|
197 | 197 | |
|
198 | 198 | return (PyObject *)t; |
|
199 | 199 | }; |
|
200 | 200 | |
|
201 | 201 | /* This means the next status call will have to actually check its content |
|
202 | 202 | to make sure it is correct. */ |
|
203 | 203 | static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self) |
|
204 | 204 | { |
|
205 | 205 | self->mtime = ambiguous_time; |
|
206 | 206 | Py_RETURN_NONE; |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | static PyObject *dirstate_item_set_untracked(dirstateItemObject *self) |
|
210 | 210 | { |
|
211 | 211 | if (self->state == 'm') { |
|
212 | 212 | self->size = dirstate_v1_nonnormal; |
|
213 | 213 | } else if (self->state == 'n' && self->size == dirstate_v1_from_p2) { |
|
214 | 214 | self->size = dirstate_v1_from_p2; |
|
215 | 215 | } else { |
|
216 | 216 | self->size = 0; |
|
217 | 217 | } |
|
218 | 218 | self->state = 'r'; |
|
219 | 219 | self->mode = 0; |
|
220 | 220 | self->mtime = 0; |
|
221 | 221 | Py_RETURN_NONE; |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | static PyMethodDef dirstate_item_methods[] = { |
|
225 | 225 | {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS, |
|
226 | 226 | "return a \"state\" suitable for v1 serialization"}, |
|
227 | 227 | {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS, |
|
228 | 228 | "return a \"mode\" suitable for v1 serialization"}, |
|
229 | 229 | {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS, |
|
230 | 230 | "return a \"size\" suitable for v1 serialization"}, |
|
231 | 231 | {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS, |
|
232 | 232 | "return a \"mtime\" suitable for v1 serialization"}, |
|
233 | 233 | {"need_delay", (PyCFunction)dirstate_item_need_delay, METH_O, |
|
234 | 234 | "True if the stored mtime would be ambiguous with the current time"}, |
|
235 |
{"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth, |
|
|
236 | "build a new DirstateItem object from V1 data"}, | |
|
235 | {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth, | |
|
236 | METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V1 data"}, | |
|
237 | 237 | {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty, |
|
238 | 238 | METH_NOARGS, "mark a file as \"possibly dirty\""}, |
|
239 | 239 | {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS, |
|
240 | 240 | "mark a file as \"untracked\""}, |
|
241 | 241 | {NULL} /* Sentinel */ |
|
242 | 242 | }; |
|
243 | 243 | |
|
244 | 244 | static PyObject *dirstate_item_get_mode(dirstateItemObject *self) |
|
245 | 245 | { |
|
246 | 246 | return PyInt_FromLong(self->mode); |
|
247 | 247 | }; |
|
248 | 248 | |
|
249 | 249 | static PyObject *dirstate_item_get_size(dirstateItemObject *self) |
|
250 | 250 | { |
|
251 | 251 | return PyInt_FromLong(self->size); |
|
252 | 252 | }; |
|
253 | 253 | |
|
254 | 254 | static PyObject *dirstate_item_get_mtime(dirstateItemObject *self) |
|
255 | 255 | { |
|
256 | 256 | return PyInt_FromLong(self->mtime); |
|
257 | 257 | }; |
|
258 | 258 | |
|
259 | 259 | static PyObject *dirstate_item_get_state(dirstateItemObject *self) |
|
260 | 260 | { |
|
261 | 261 | return PyBytes_FromStringAndSize(&self->state, 1); |
|
262 | 262 | }; |
|
263 | 263 | |
|
264 | 264 | static PyObject *dirstate_item_get_tracked(dirstateItemObject *self) |
|
265 | 265 | { |
|
266 | 266 | if (self->state == 'a' || self->state == 'm' || self->state == 'n') { |
|
267 | 267 | Py_RETURN_TRUE; |
|
268 | 268 | } else { |
|
269 | 269 | Py_RETURN_FALSE; |
|
270 | 270 | } |
|
271 | 271 | }; |
|
272 | 272 | |
|
273 | 273 | static PyObject *dirstate_item_get_added(dirstateItemObject *self) |
|
274 | 274 | { |
|
275 | 275 | if (self->state == 'a') { |
|
276 | 276 | Py_RETURN_TRUE; |
|
277 | 277 | } else { |
|
278 | 278 | Py_RETURN_FALSE; |
|
279 | 279 | } |
|
280 | 280 | }; |
|
281 | 281 | |
|
282 | 282 | static PyObject *dirstate_item_get_merged(dirstateItemObject *self) |
|
283 | 283 | { |
|
284 | 284 | if (self->state == 'm') { |
|
285 | 285 | Py_RETURN_TRUE; |
|
286 | 286 | } else { |
|
287 | 287 | Py_RETURN_FALSE; |
|
288 | 288 | } |
|
289 | 289 | }; |
|
290 | 290 | |
|
291 | 291 | static PyObject *dirstate_item_get_merged_removed(dirstateItemObject *self) |
|
292 | 292 | { |
|
293 | 293 | if (self->state == 'r' && self->size == dirstate_v1_nonnormal) { |
|
294 | 294 | Py_RETURN_TRUE; |
|
295 | 295 | } else { |
|
296 | 296 | Py_RETURN_FALSE; |
|
297 | 297 | } |
|
298 | 298 | }; |
|
299 | 299 | |
|
300 | 300 | static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self) |
|
301 | 301 | { |
|
302 | 302 | if (self->state == 'n' && self->size == dirstate_v1_from_p2) { |
|
303 | 303 | Py_RETURN_TRUE; |
|
304 | 304 | } else { |
|
305 | 305 | Py_RETURN_FALSE; |
|
306 | 306 | } |
|
307 | 307 | }; |
|
308 | 308 | |
|
309 | 309 | static PyObject *dirstate_item_get_from_p2_removed(dirstateItemObject *self) |
|
310 | 310 | { |
|
311 | 311 | if (self->state == 'r' && self->size == dirstate_v1_from_p2) { |
|
312 | 312 | Py_RETURN_TRUE; |
|
313 | 313 | } else { |
|
314 | 314 | Py_RETURN_FALSE; |
|
315 | 315 | } |
|
316 | 316 | }; |
|
317 | 317 | |
|
318 | 318 | static PyObject *dirstate_item_get_removed(dirstateItemObject *self) |
|
319 | 319 | { |
|
320 | 320 | if (self->state == 'r') { |
|
321 | 321 | Py_RETURN_TRUE; |
|
322 | 322 | } else { |
|
323 | 323 | Py_RETURN_FALSE; |
|
324 | 324 | } |
|
325 | 325 | }; |
|
326 | 326 | |
|
327 | 327 | static PyObject *dm_nonnormal(dirstateItemObject *self) |
|
328 | 328 | { |
|
329 | 329 | if (self->state != 'n' || self->mtime == ambiguous_time) { |
|
330 | 330 | Py_RETURN_TRUE; |
|
331 | 331 | } else { |
|
332 | 332 | Py_RETURN_FALSE; |
|
333 | 333 | } |
|
334 | 334 | }; |
|
335 | 335 | static PyObject *dm_otherparent(dirstateItemObject *self) |
|
336 | 336 | { |
|
337 | 337 | if (self->size == dirstate_v1_from_p2) { |
|
338 | 338 | Py_RETURN_TRUE; |
|
339 | 339 | } else { |
|
340 | 340 | Py_RETURN_FALSE; |
|
341 | 341 | } |
|
342 | 342 | }; |
|
343 | 343 | |
|
344 | 344 | static PyGetSetDef dirstate_item_getset[] = { |
|
345 | 345 | {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL}, |
|
346 | 346 | {"size", (getter)dirstate_item_get_size, NULL, "size", NULL}, |
|
347 | 347 | {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL}, |
|
348 | 348 | {"state", (getter)dirstate_item_get_state, NULL, "state", NULL}, |
|
349 | 349 | {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL}, |
|
350 | 350 | {"added", (getter)dirstate_item_get_added, NULL, "added", NULL}, |
|
351 | 351 | {"merged_removed", (getter)dirstate_item_get_merged_removed, NULL, |
|
352 | 352 | "merged_removed", NULL}, |
|
353 | 353 | {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL}, |
|
354 | 354 | {"from_p2_removed", (getter)dirstate_item_get_from_p2_removed, NULL, |
|
355 | 355 | "from_p2_removed", NULL}, |
|
356 | 356 | {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL}, |
|
357 | 357 | {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL}, |
|
358 | 358 | {"dm_nonnormal", (getter)dm_nonnormal, NULL, "dm_nonnormal", NULL}, |
|
359 | 359 | {"dm_otherparent", (getter)dm_otherparent, NULL, "dm_otherparent", NULL}, |
|
360 | 360 | {NULL} /* Sentinel */ |
|
361 | 361 | }; |
|
362 | 362 | |
|
363 | 363 | PyTypeObject dirstateItemType = { |
|
364 | 364 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
365 | 365 | "dirstate_tuple", /* tp_name */ |
|
366 | 366 | sizeof(dirstateItemObject), /* tp_basicsize */ |
|
367 | 367 | 0, /* tp_itemsize */ |
|
368 | 368 | (destructor)dirstate_item_dealloc, /* tp_dealloc */ |
|
369 | 369 | 0, /* tp_print */ |
|
370 | 370 | 0, /* tp_getattr */ |
|
371 | 371 | 0, /* tp_setattr */ |
|
372 | 372 | 0, /* tp_compare */ |
|
373 | 373 | 0, /* tp_repr */ |
|
374 | 374 | 0, /* tp_as_number */ |
|
375 | 375 | &dirstate_item_sq, /* tp_as_sequence */ |
|
376 | 376 | 0, /* tp_as_mapping */ |
|
377 | 377 | 0, /* tp_hash */ |
|
378 | 378 | 0, /* tp_call */ |
|
379 | 379 | 0, /* tp_str */ |
|
380 | 380 | 0, /* tp_getattro */ |
|
381 | 381 | 0, /* tp_setattro */ |
|
382 | 382 | 0, /* tp_as_buffer */ |
|
383 | 383 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
384 | 384 | "dirstate tuple", /* tp_doc */ |
|
385 | 385 | 0, /* tp_traverse */ |
|
386 | 386 | 0, /* tp_clear */ |
|
387 | 387 | 0, /* tp_richcompare */ |
|
388 | 388 | 0, /* tp_weaklistoffset */ |
|
389 | 389 | 0, /* tp_iter */ |
|
390 | 390 | 0, /* tp_iternext */ |
|
391 | 391 | dirstate_item_methods, /* tp_methods */ |
|
392 | 392 | 0, /* tp_members */ |
|
393 | 393 | dirstate_item_getset, /* tp_getset */ |
|
394 | 394 | 0, /* tp_base */ |
|
395 | 395 | 0, /* tp_dict */ |
|
396 | 396 | 0, /* tp_descr_get */ |
|
397 | 397 | 0, /* tp_descr_set */ |
|
398 | 398 | 0, /* tp_dictoffset */ |
|
399 | 399 | 0, /* tp_init */ |
|
400 | 400 | 0, /* tp_alloc */ |
|
401 | 401 | dirstate_item_new, /* tp_new */ |
|
402 | 402 | }; |
|
403 | 403 | |
|
404 | 404 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
405 | 405 | { |
|
406 | 406 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
407 | 407 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
408 | 408 | char state, *cur, *str, *cpos; |
|
409 | 409 | int mode, size, mtime; |
|
410 | 410 | unsigned int flen, pos = 40; |
|
411 | 411 | Py_ssize_t len = 40; |
|
412 | 412 | Py_ssize_t readlen; |
|
413 | 413 | |
|
414 | 414 | if (!PyArg_ParseTuple( |
|
415 | 415 | args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"), |
|
416 | 416 | &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) { |
|
417 | 417 | goto quit; |
|
418 | 418 | } |
|
419 | 419 | |
|
420 | 420 | len = readlen; |
|
421 | 421 | |
|
422 | 422 | /* read parents */ |
|
423 | 423 | if (len < 40) { |
|
424 | 424 | PyErr_SetString(PyExc_ValueError, |
|
425 | 425 | "too little data for parents"); |
|
426 | 426 | goto quit; |
|
427 | 427 | } |
|
428 | 428 | |
|
429 | 429 | parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20, |
|
430 | 430 | str + 20, (Py_ssize_t)20); |
|
431 | 431 | if (!parents) { |
|
432 | 432 | goto quit; |
|
433 | 433 | } |
|
434 | 434 | |
|
435 | 435 | /* read filenames */ |
|
436 | 436 | while (pos >= 40 && pos < len) { |
|
437 | 437 | if (pos + 17 > len) { |
|
438 | 438 | PyErr_SetString(PyExc_ValueError, |
|
439 | 439 | "overflow in dirstate"); |
|
440 | 440 | goto quit; |
|
441 | 441 | } |
|
442 | 442 | cur = str + pos; |
|
443 | 443 | /* unpack header */ |
|
444 | 444 | state = *cur; |
|
445 | 445 | mode = getbe32(cur + 1); |
|
446 | 446 | size = getbe32(cur + 5); |
|
447 | 447 | mtime = getbe32(cur + 9); |
|
448 | 448 | flen = getbe32(cur + 13); |
|
449 | 449 | pos += 17; |
|
450 | 450 | cur += 17; |
|
451 | 451 | if (flen > len - pos) { |
|
452 | 452 | PyErr_SetString(PyExc_ValueError, |
|
453 | 453 | "overflow in dirstate"); |
|
454 | 454 | goto quit; |
|
455 | 455 | } |
|
456 | 456 | |
|
457 | 457 | entry = (PyObject *)dirstate_item_from_v1_data(state, mode, |
|
458 | 458 | size, mtime); |
|
459 | 459 | cpos = memchr(cur, 0, flen); |
|
460 | 460 | if (cpos) { |
|
461 | 461 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
462 | 462 | cname = PyBytes_FromStringAndSize( |
|
463 | 463 | cpos + 1, flen - (cpos - cur) - 1); |
|
464 | 464 | if (!fname || !cname || |
|
465 | 465 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
466 | 466 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
467 | 467 | goto quit; |
|
468 | 468 | } |
|
469 | 469 | Py_DECREF(cname); |
|
470 | 470 | } else { |
|
471 | 471 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
472 | 472 | if (!fname || |
|
473 | 473 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
474 | 474 | goto quit; |
|
475 | 475 | } |
|
476 | 476 | } |
|
477 | 477 | Py_DECREF(fname); |
|
478 | 478 | Py_DECREF(entry); |
|
479 | 479 | fname = cname = entry = NULL; |
|
480 | 480 | pos += flen; |
|
481 | 481 | } |
|
482 | 482 | |
|
483 | 483 | ret = parents; |
|
484 | 484 | Py_INCREF(ret); |
|
485 | 485 | quit: |
|
486 | 486 | Py_XDECREF(fname); |
|
487 | 487 | Py_XDECREF(cname); |
|
488 | 488 | Py_XDECREF(entry); |
|
489 | 489 | Py_XDECREF(parents); |
|
490 | 490 | return ret; |
|
491 | 491 | } |
|
492 | 492 | |
|
493 | 493 | /* |
|
494 | 494 | * Build a set of non-normal and other parent entries from the dirstate dmap |
|
495 | 495 | */ |
|
496 | 496 | static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args) |
|
497 | 497 | { |
|
498 | 498 | PyObject *dmap, *fname, *v; |
|
499 | 499 | PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL; |
|
500 | 500 | Py_ssize_t pos; |
|
501 | 501 | |
|
502 | 502 | if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, |
|
503 | 503 | &dmap)) { |
|
504 | 504 | goto bail; |
|
505 | 505 | } |
|
506 | 506 | |
|
507 | 507 | nonnset = PySet_New(NULL); |
|
508 | 508 | if (nonnset == NULL) { |
|
509 | 509 | goto bail; |
|
510 | 510 | } |
|
511 | 511 | |
|
512 | 512 | otherpset = PySet_New(NULL); |
|
513 | 513 | if (otherpset == NULL) { |
|
514 | 514 | goto bail; |
|
515 | 515 | } |
|
516 | 516 | |
|
517 | 517 | pos = 0; |
|
518 | 518 | while (PyDict_Next(dmap, &pos, &fname, &v)) { |
|
519 | 519 | dirstateItemObject *t; |
|
520 | 520 | if (!dirstate_tuple_check(v)) { |
|
521 | 521 | PyErr_SetString(PyExc_TypeError, |
|
522 | 522 | "expected a dirstate tuple"); |
|
523 | 523 | goto bail; |
|
524 | 524 | } |
|
525 | 525 | t = (dirstateItemObject *)v; |
|
526 | 526 | |
|
527 | 527 | if (t->state == 'n' && t->size == -2) { |
|
528 | 528 | if (PySet_Add(otherpset, fname) == -1) { |
|
529 | 529 | goto bail; |
|
530 | 530 | } |
|
531 | 531 | } |
|
532 | 532 | |
|
533 | 533 | if (t->state == 'n' && t->mtime != -1) { |
|
534 | 534 | continue; |
|
535 | 535 | } |
|
536 | 536 | if (PySet_Add(nonnset, fname) == -1) { |
|
537 | 537 | goto bail; |
|
538 | 538 | } |
|
539 | 539 | } |
|
540 | 540 | |
|
541 | 541 | result = Py_BuildValue("(OO)", nonnset, otherpset); |
|
542 | 542 | if (result == NULL) { |
|
543 | 543 | goto bail; |
|
544 | 544 | } |
|
545 | 545 | Py_DECREF(nonnset); |
|
546 | 546 | Py_DECREF(otherpset); |
|
547 | 547 | return result; |
|
548 | 548 | bail: |
|
549 | 549 | Py_XDECREF(nonnset); |
|
550 | 550 | Py_XDECREF(otherpset); |
|
551 | 551 | Py_XDECREF(result); |
|
552 | 552 | return NULL; |
|
553 | 553 | } |
|
554 | 554 | |
|
555 | 555 | /* |
|
556 | 556 | * Efficiently pack a dirstate object into its on-disk format. |
|
557 | 557 | */ |
|
558 | 558 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
559 | 559 | { |
|
560 | 560 | PyObject *packobj = NULL; |
|
561 | 561 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; |
|
562 | 562 | Py_ssize_t nbytes, pos, l; |
|
563 | 563 | PyObject *k, *v = NULL, *pn; |
|
564 | 564 | char *p, *s; |
|
565 | 565 | int now; |
|
566 | 566 | |
|
567 | 567 | if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map, |
|
568 | 568 | &PyDict_Type, ©map, &PyTuple_Type, &pl, |
|
569 | 569 | &now)) { |
|
570 | 570 | return NULL; |
|
571 | 571 | } |
|
572 | 572 | |
|
573 | 573 | if (PyTuple_Size(pl) != 2) { |
|
574 | 574 | PyErr_SetString(PyExc_TypeError, "expected 2-element tuple"); |
|
575 | 575 | return NULL; |
|
576 | 576 | } |
|
577 | 577 | |
|
578 | 578 | /* Figure out how much we need to allocate. */ |
|
579 | 579 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
580 | 580 | PyObject *c; |
|
581 | 581 | if (!PyBytes_Check(k)) { |
|
582 | 582 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
583 | 583 | goto bail; |
|
584 | 584 | } |
|
585 | 585 | nbytes += PyBytes_GET_SIZE(k) + 17; |
|
586 | 586 | c = PyDict_GetItem(copymap, k); |
|
587 | 587 | if (c) { |
|
588 | 588 | if (!PyBytes_Check(c)) { |
|
589 | 589 | PyErr_SetString(PyExc_TypeError, |
|
590 | 590 | "expected string key"); |
|
591 | 591 | goto bail; |
|
592 | 592 | } |
|
593 | 593 | nbytes += PyBytes_GET_SIZE(c) + 1; |
|
594 | 594 | } |
|
595 | 595 | } |
|
596 | 596 | |
|
597 | 597 | packobj = PyBytes_FromStringAndSize(NULL, nbytes); |
|
598 | 598 | if (packobj == NULL) { |
|
599 | 599 | goto bail; |
|
600 | 600 | } |
|
601 | 601 | |
|
602 | 602 | p = PyBytes_AS_STRING(packobj); |
|
603 | 603 | |
|
604 | 604 | pn = PyTuple_GET_ITEM(pl, 0); |
|
605 | 605 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
606 | 606 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
607 | 607 | goto bail; |
|
608 | 608 | } |
|
609 | 609 | memcpy(p, s, l); |
|
610 | 610 | p += 20; |
|
611 | 611 | pn = PyTuple_GET_ITEM(pl, 1); |
|
612 | 612 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
613 | 613 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
614 | 614 | goto bail; |
|
615 | 615 | } |
|
616 | 616 | memcpy(p, s, l); |
|
617 | 617 | p += 20; |
|
618 | 618 | |
|
619 | 619 | for (pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
620 | 620 | dirstateItemObject *tuple; |
|
621 | 621 | char state; |
|
622 | 622 | int mode, size, mtime; |
|
623 | 623 | Py_ssize_t len, l; |
|
624 | 624 | PyObject *o; |
|
625 | 625 | char *t; |
|
626 | 626 | |
|
627 | 627 | if (!dirstate_tuple_check(v)) { |
|
628 | 628 | PyErr_SetString(PyExc_TypeError, |
|
629 | 629 | "expected a dirstate tuple"); |
|
630 | 630 | goto bail; |
|
631 | 631 | } |
|
632 | 632 | tuple = (dirstateItemObject *)v; |
|
633 | 633 | |
|
634 | 634 | state = tuple->state; |
|
635 | 635 | mode = tuple->mode; |
|
636 | 636 | size = tuple->size; |
|
637 | 637 | mtime = tuple->mtime; |
|
638 | 638 | if (state == 'n' && mtime == now) { |
|
639 | 639 | /* See pure/parsers.py:pack_dirstate for why we do |
|
640 | 640 | * this. */ |
|
641 | 641 | mtime = -1; |
|
642 | 642 | mtime_unset = (PyObject *)make_dirstate_item( |
|
643 | 643 | state, mode, size, mtime); |
|
644 | 644 | if (!mtime_unset) { |
|
645 | 645 | goto bail; |
|
646 | 646 | } |
|
647 | 647 | if (PyDict_SetItem(map, k, mtime_unset) == -1) { |
|
648 | 648 | goto bail; |
|
649 | 649 | } |
|
650 | 650 | Py_DECREF(mtime_unset); |
|
651 | 651 | mtime_unset = NULL; |
|
652 | 652 | } |
|
653 | 653 | *p++ = state; |
|
654 | 654 | putbe32((uint32_t)mode, p); |
|
655 | 655 | putbe32((uint32_t)size, p + 4); |
|
656 | 656 | putbe32((uint32_t)mtime, p + 8); |
|
657 | 657 | t = p + 12; |
|
658 | 658 | p += 16; |
|
659 | 659 | len = PyBytes_GET_SIZE(k); |
|
660 | 660 | memcpy(p, PyBytes_AS_STRING(k), len); |
|
661 | 661 | p += len; |
|
662 | 662 | o = PyDict_GetItem(copymap, k); |
|
663 | 663 | if (o) { |
|
664 | 664 | *p++ = '\0'; |
|
665 | 665 | l = PyBytes_GET_SIZE(o); |
|
666 | 666 | memcpy(p, PyBytes_AS_STRING(o), l); |
|
667 | 667 | p += l; |
|
668 | 668 | len += l + 1; |
|
669 | 669 | } |
|
670 | 670 | putbe32((uint32_t)len, t); |
|
671 | 671 | } |
|
672 | 672 | |
|
673 | 673 | pos = p - PyBytes_AS_STRING(packobj); |
|
674 | 674 | if (pos != nbytes) { |
|
675 | 675 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
676 | 676 | (long)pos, (long)nbytes); |
|
677 | 677 | goto bail; |
|
678 | 678 | } |
|
679 | 679 | |
|
680 | 680 | return packobj; |
|
681 | 681 | bail: |
|
682 | 682 | Py_XDECREF(mtime_unset); |
|
683 | 683 | Py_XDECREF(packobj); |
|
684 | 684 | Py_XDECREF(v); |
|
685 | 685 | return NULL; |
|
686 | 686 | } |
|
687 | 687 | |
|
688 | 688 | #define BUMPED_FIX 1 |
|
689 | 689 | #define USING_SHA_256 2 |
|
690 | 690 | #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1) |
|
691 | 691 | |
|
692 | 692 | static PyObject *readshas(const char *source, unsigned char num, |
|
693 | 693 | Py_ssize_t hashwidth) |
|
694 | 694 | { |
|
695 | 695 | int i; |
|
696 | 696 | PyObject *list = PyTuple_New(num); |
|
697 | 697 | if (list == NULL) { |
|
698 | 698 | return NULL; |
|
699 | 699 | } |
|
700 | 700 | for (i = 0; i < num; i++) { |
|
701 | 701 | PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth); |
|
702 | 702 | if (hash == NULL) { |
|
703 | 703 | Py_DECREF(list); |
|
704 | 704 | return NULL; |
|
705 | 705 | } |
|
706 | 706 | PyTuple_SET_ITEM(list, i, hash); |
|
707 | 707 | source += hashwidth; |
|
708 | 708 | } |
|
709 | 709 | return list; |
|
710 | 710 | } |
|
711 | 711 | |
|
712 | 712 | static PyObject *fm1readmarker(const char *databegin, const char *dataend, |
|
713 | 713 | uint32_t *msize) |
|
714 | 714 | { |
|
715 | 715 | const char *data = databegin; |
|
716 | 716 | const char *meta; |
|
717 | 717 | |
|
718 | 718 | double mtime; |
|
719 | 719 | int16_t tz; |
|
720 | 720 | uint16_t flags; |
|
721 | 721 | unsigned char nsuccs, nparents, nmetadata; |
|
722 | 722 | Py_ssize_t hashwidth = 20; |
|
723 | 723 | |
|
724 | 724 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; |
|
725 | 725 | PyObject *metadata = NULL, *ret = NULL; |
|
726 | 726 | int i; |
|
727 | 727 | |
|
728 | 728 | if (data + FM1_HEADER_SIZE > dataend) { |
|
729 | 729 | goto overflow; |
|
730 | 730 | } |
|
731 | 731 | |
|
732 | 732 | *msize = getbe32(data); |
|
733 | 733 | data += 4; |
|
734 | 734 | mtime = getbefloat64(data); |
|
735 | 735 | data += 8; |
|
736 | 736 | tz = getbeint16(data); |
|
737 | 737 | data += 2; |
|
738 | 738 | flags = getbeuint16(data); |
|
739 | 739 | data += 2; |
|
740 | 740 | |
|
741 | 741 | if (flags & USING_SHA_256) { |
|
742 | 742 | hashwidth = 32; |
|
743 | 743 | } |
|
744 | 744 | |
|
745 | 745 | nsuccs = (unsigned char)(*data++); |
|
746 | 746 | nparents = (unsigned char)(*data++); |
|
747 | 747 | nmetadata = (unsigned char)(*data++); |
|
748 | 748 | |
|
749 | 749 | if (databegin + *msize > dataend) { |
|
750 | 750 | goto overflow; |
|
751 | 751 | } |
|
752 | 752 | dataend = databegin + *msize; /* narrow down to marker size */ |
|
753 | 753 | |
|
754 | 754 | if (data + hashwidth > dataend) { |
|
755 | 755 | goto overflow; |
|
756 | 756 | } |
|
757 | 757 | prec = PyBytes_FromStringAndSize(data, hashwidth); |
|
758 | 758 | data += hashwidth; |
|
759 | 759 | if (prec == NULL) { |
|
760 | 760 | goto bail; |
|
761 | 761 | } |
|
762 | 762 | |
|
763 | 763 | if (data + nsuccs * hashwidth > dataend) { |
|
764 | 764 | goto overflow; |
|
765 | 765 | } |
|
766 | 766 | succs = readshas(data, nsuccs, hashwidth); |
|
767 | 767 | if (succs == NULL) { |
|
768 | 768 | goto bail; |
|
769 | 769 | } |
|
770 | 770 | data += nsuccs * hashwidth; |
|
771 | 771 | |
|
772 | 772 | if (nparents == 1 || nparents == 2) { |
|
773 | 773 | if (data + nparents * hashwidth > dataend) { |
|
774 | 774 | goto overflow; |
|
775 | 775 | } |
|
776 | 776 | parents = readshas(data, nparents, hashwidth); |
|
777 | 777 | if (parents == NULL) { |
|
778 | 778 | goto bail; |
|
779 | 779 | } |
|
780 | 780 | data += nparents * hashwidth; |
|
781 | 781 | } else { |
|
782 | 782 | parents = Py_None; |
|
783 | 783 | Py_INCREF(parents); |
|
784 | 784 | } |
|
785 | 785 | |
|
786 | 786 | if (data + 2 * nmetadata > dataend) { |
|
787 | 787 | goto overflow; |
|
788 | 788 | } |
|
789 | 789 | meta = data + (2 * nmetadata); |
|
790 | 790 | metadata = PyTuple_New(nmetadata); |
|
791 | 791 | if (metadata == NULL) { |
|
792 | 792 | goto bail; |
|
793 | 793 | } |
|
794 | 794 | for (i = 0; i < nmetadata; i++) { |
|
795 | 795 | PyObject *tmp, *left = NULL, *right = NULL; |
|
796 | 796 | Py_ssize_t leftsize = (unsigned char)(*data++); |
|
797 | 797 | Py_ssize_t rightsize = (unsigned char)(*data++); |
|
798 | 798 | if (meta + leftsize + rightsize > dataend) { |
|
799 | 799 | goto overflow; |
|
800 | 800 | } |
|
801 | 801 | left = PyBytes_FromStringAndSize(meta, leftsize); |
|
802 | 802 | meta += leftsize; |
|
803 | 803 | right = PyBytes_FromStringAndSize(meta, rightsize); |
|
804 | 804 | meta += rightsize; |
|
805 | 805 | tmp = PyTuple_New(2); |
|
806 | 806 | if (!left || !right || !tmp) { |
|
807 | 807 | Py_XDECREF(left); |
|
808 | 808 | Py_XDECREF(right); |
|
809 | 809 | Py_XDECREF(tmp); |
|
810 | 810 | goto bail; |
|
811 | 811 | } |
|
812 | 812 | PyTuple_SET_ITEM(tmp, 0, left); |
|
813 | 813 | PyTuple_SET_ITEM(tmp, 1, right); |
|
814 | 814 | PyTuple_SET_ITEM(metadata, i, tmp); |
|
815 | 815 | } |
|
816 | 816 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime, |
|
817 | 817 | (int)tz * 60, parents); |
|
818 | 818 | goto bail; /* return successfully */ |
|
819 | 819 | |
|
820 | 820 | overflow: |
|
821 | 821 | PyErr_SetString(PyExc_ValueError, "overflow in obsstore"); |
|
822 | 822 | bail: |
|
823 | 823 | Py_XDECREF(prec); |
|
824 | 824 | Py_XDECREF(succs); |
|
825 | 825 | Py_XDECREF(metadata); |
|
826 | 826 | Py_XDECREF(parents); |
|
827 | 827 | return ret; |
|
828 | 828 | } |
|
829 | 829 | |
|
830 | 830 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) |
|
831 | 831 | { |
|
832 | 832 | const char *data, *dataend; |
|
833 | 833 | Py_ssize_t datalen, offset, stop; |
|
834 | 834 | PyObject *markers = NULL; |
|
835 | 835 | |
|
836 | 836 | if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen, |
|
837 | 837 | &offset, &stop)) { |
|
838 | 838 | return NULL; |
|
839 | 839 | } |
|
840 | 840 | if (offset < 0) { |
|
841 | 841 | PyErr_SetString(PyExc_ValueError, |
|
842 | 842 | "invalid negative offset in fm1readmarkers"); |
|
843 | 843 | return NULL; |
|
844 | 844 | } |
|
845 | 845 | if (stop > datalen) { |
|
846 | 846 | PyErr_SetString( |
|
847 | 847 | PyExc_ValueError, |
|
848 | 848 | "stop longer than data length in fm1readmarkers"); |
|
849 | 849 | return NULL; |
|
850 | 850 | } |
|
851 | 851 | dataend = data + datalen; |
|
852 | 852 | data += offset; |
|
853 | 853 | markers = PyList_New(0); |
|
854 | 854 | if (!markers) { |
|
855 | 855 | return NULL; |
|
856 | 856 | } |
|
857 | 857 | while (offset < stop) { |
|
858 | 858 | uint32_t msize; |
|
859 | 859 | int error; |
|
860 | 860 | PyObject *record = fm1readmarker(data, dataend, &msize); |
|
861 | 861 | if (!record) { |
|
862 | 862 | goto bail; |
|
863 | 863 | } |
|
864 | 864 | error = PyList_Append(markers, record); |
|
865 | 865 | Py_DECREF(record); |
|
866 | 866 | if (error) { |
|
867 | 867 | goto bail; |
|
868 | 868 | } |
|
869 | 869 | data += msize; |
|
870 | 870 | offset += msize; |
|
871 | 871 | } |
|
872 | 872 | return markers; |
|
873 | 873 | bail: |
|
874 | 874 | Py_DECREF(markers); |
|
875 | 875 | return NULL; |
|
876 | 876 | } |
|
877 | 877 | |
|
878 | 878 | static char parsers_doc[] = "Efficient content parsing."; |
|
879 | 879 | |
|
880 | 880 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
881 | 881 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
882 | 882 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
883 | 883 | PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs); |
|
884 | 884 | |
|
885 | 885 | static PyMethodDef methods[] = { |
|
886 | 886 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
887 | 887 | {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS, |
|
888 | 888 | "create a set containing non-normal and other parent entries of given " |
|
889 | 889 | "dirstate\n"}, |
|
890 | 890 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
891 | 891 | {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS, |
|
892 | 892 | "parse a revlog index\n"}, |
|
893 | 893 | {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"}, |
|
894 | 894 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, |
|
895 | 895 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, |
|
896 | 896 | {"dict_new_presized", dict_new_presized, METH_VARARGS, |
|
897 | 897 | "construct a dict with an expected size\n"}, |
|
898 | 898 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, |
|
899 | 899 | "make file foldmap\n"}, |
|
900 | 900 | {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS, |
|
901 | 901 | "escape a UTF-8 byte string to JSON (fast path)\n"}, |
|
902 | 902 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
903 | 903 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
904 | 904 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
905 | 905 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, |
|
906 | 906 | "parse v1 obsolete markers\n"}, |
|
907 | 907 | {NULL, NULL}}; |
|
908 | 908 | |
|
909 | 909 | void dirs_module_init(PyObject *mod); |
|
910 | 910 | void manifest_module_init(PyObject *mod); |
|
911 | 911 | void revlog_module_init(PyObject *mod); |
|
912 | 912 | |
|
913 | 913 | static const int version = 20; |
|
914 | 914 | |
|
915 | 915 | static void module_init(PyObject *mod) |
|
916 | 916 | { |
|
917 | 917 | PyObject *capsule = NULL; |
|
918 | 918 | PyModule_AddIntConstant(mod, "version", version); |
|
919 | 919 | |
|
920 | 920 | /* This module constant has two purposes. First, it lets us unit test |
|
921 | 921 | * the ImportError raised without hard-coding any error text. This |
|
922 | 922 | * means we can change the text in the future without breaking tests, |
|
923 | 923 | * even across changesets without a recompile. Second, its presence |
|
924 | 924 | * can be used to determine whether the version-checking logic is |
|
925 | 925 | * present, which also helps in testing across changesets without a |
|
926 | 926 | * recompile. Note that this means the pure-Python version of parsers |
|
927 | 927 | * should not have this module constant. */ |
|
928 | 928 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); |
|
929 | 929 | |
|
930 | 930 | dirs_module_init(mod); |
|
931 | 931 | manifest_module_init(mod); |
|
932 | 932 | revlog_module_init(mod); |
|
933 | 933 | |
|
934 | 934 | capsule = PyCapsule_New( |
|
935 | 935 | make_dirstate_item, |
|
936 | 936 | "mercurial.cext.parsers.make_dirstate_item_CAPI", NULL); |
|
937 | 937 | if (capsule != NULL) |
|
938 | 938 | PyModule_AddObject(mod, "make_dirstate_item_CAPI", capsule); |
|
939 | 939 | |
|
940 | 940 | if (PyType_Ready(&dirstateItemType) < 0) { |
|
941 | 941 | return; |
|
942 | 942 | } |
|
943 | 943 | Py_INCREF(&dirstateItemType); |
|
944 | 944 | PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType); |
|
945 | 945 | } |
|
946 | 946 | |
|
947 | 947 | static int check_python_version(void) |
|
948 | 948 | { |
|
949 | 949 | PyObject *sys = PyImport_ImportModule("sys"), *ver; |
|
950 | 950 | long hexversion; |
|
951 | 951 | if (!sys) { |
|
952 | 952 | return -1; |
|
953 | 953 | } |
|
954 | 954 | ver = PyObject_GetAttrString(sys, "hexversion"); |
|
955 | 955 | Py_DECREF(sys); |
|
956 | 956 | if (!ver) { |
|
957 | 957 | return -1; |
|
958 | 958 | } |
|
959 | 959 | hexversion = PyInt_AsLong(ver); |
|
960 | 960 | Py_DECREF(ver); |
|
961 | 961 | /* sys.hexversion is a 32-bit number by default, so the -1 case |
|
962 | 962 | * should only occur in unusual circumstances (e.g. if sys.hexversion |
|
963 | 963 | * is manually set to an invalid value). */ |
|
964 | 964 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { |
|
965 | 965 | PyErr_Format(PyExc_ImportError, |
|
966 | 966 | "%s: The Mercurial extension " |
|
967 | 967 | "modules were compiled with Python " PY_VERSION |
|
968 | 968 | ", but " |
|
969 | 969 | "Mercurial is currently using Python with " |
|
970 | 970 | "sys.hexversion=%ld: " |
|
971 | 971 | "Python %s\n at: %s", |
|
972 | 972 | versionerrortext, hexversion, Py_GetVersion(), |
|
973 | 973 | Py_GetProgramFullPath()); |
|
974 | 974 | return -1; |
|
975 | 975 | } |
|
976 | 976 | return 0; |
|
977 | 977 | } |
|
978 | 978 | |
|
979 | 979 | #ifdef IS_PY3K |
|
980 | 980 | static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers", |
|
981 | 981 | parsers_doc, -1, methods}; |
|
982 | 982 | |
|
983 | 983 | PyMODINIT_FUNC PyInit_parsers(void) |
|
984 | 984 | { |
|
985 | 985 | PyObject *mod; |
|
986 | 986 | |
|
987 | 987 | if (check_python_version() == -1) |
|
988 | 988 | return NULL; |
|
989 | 989 | mod = PyModule_Create(&parsers_module); |
|
990 | 990 | module_init(mod); |
|
991 | 991 | return mod; |
|
992 | 992 | } |
|
993 | 993 | #else |
|
994 | 994 | PyMODINIT_FUNC initparsers(void) |
|
995 | 995 | { |
|
996 | 996 | PyObject *mod; |
|
997 | 997 | |
|
998 | 998 | if (check_python_version() == -1) { |
|
999 | 999 | return; |
|
1000 | 1000 | } |
|
1001 | 1001 | mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
1002 | 1002 | module_init(mod); |
|
1003 | 1003 | } |
|
1004 | 1004 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now