Show More
@@ -1,753 +1,754 b'' | |||||
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 | #define PY_SSIZE_T_CLEAN | |||
10 | #include <Python.h> |
|
11 | #include <Python.h> | |
11 | #include <ctype.h> |
|
12 | #include <ctype.h> | |
12 | #include <stddef.h> |
|
13 | #include <stddef.h> | |
13 | #include <string.h> |
|
14 | #include <string.h> | |
14 |
|
15 | |||
15 | #include "bitmanipulation.h" |
|
16 | #include "bitmanipulation.h" | |
16 | #include "charencode.h" |
|
17 | #include "charencode.h" | |
17 | #include "util.h" |
|
18 | #include "util.h" | |
18 |
|
19 | |||
19 | #ifdef IS_PY3K |
|
20 | #ifdef IS_PY3K | |
20 | /* The mapping of Python types is meant to be temporary to get Python |
|
21 | /* The mapping of Python types is meant to be temporary to get Python | |
21 | * 3 to compile. We should remove this once Python 3 support is fully |
|
22 | * 3 to compile. We should remove this once Python 3 support is fully | |
22 | * supported and proper types are used in the extensions themselves. */ |
|
23 | * supported and proper types are used in the extensions themselves. */ | |
23 | #define PyInt_Check PyLong_Check |
|
24 | #define PyInt_Check PyLong_Check | |
24 | #define PyInt_FromLong PyLong_FromLong |
|
25 | #define PyInt_FromLong PyLong_FromLong | |
25 | #define PyInt_FromSsize_t PyLong_FromSsize_t |
|
26 | #define PyInt_FromSsize_t PyLong_FromSsize_t | |
26 | #define PyInt_AsLong PyLong_AsLong |
|
27 | #define PyInt_AsLong PyLong_AsLong | |
27 | #endif |
|
28 | #endif | |
28 |
|
29 | |||
29 | static const char *const versionerrortext = "Python minor version mismatch"; |
|
30 | static const char *const versionerrortext = "Python minor version mismatch"; | |
30 |
|
31 | |||
31 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) |
|
32 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) | |
32 | { |
|
33 | { | |
33 | Py_ssize_t expected_size; |
|
34 | Py_ssize_t expected_size; | |
34 |
|
35 | |||
35 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) { |
|
36 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) { | |
36 | return NULL; |
|
37 | return NULL; | |
37 | } |
|
38 | } | |
38 |
|
39 | |||
39 | return _dict_new_presized(expected_size); |
|
40 | return _dict_new_presized(expected_size); | |
40 | } |
|
41 | } | |
41 |
|
42 | |||
42 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, |
|
43 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, | |
43 | int size, int mtime) |
|
44 | int size, int mtime) | |
44 | { |
|
45 | { | |
45 | dirstateTupleObject *t = |
|
46 | dirstateTupleObject *t = | |
46 | PyObject_New(dirstateTupleObject, &dirstateTupleType); |
|
47 | PyObject_New(dirstateTupleObject, &dirstateTupleType); | |
47 | if (!t) { |
|
48 | if (!t) { | |
48 | return NULL; |
|
49 | return NULL; | |
49 | } |
|
50 | } | |
50 | t->state = state; |
|
51 | t->state = state; | |
51 | t->mode = mode; |
|
52 | t->mode = mode; | |
52 | t->size = size; |
|
53 | t->size = size; | |
53 | t->mtime = mtime; |
|
54 | t->mtime = mtime; | |
54 | return t; |
|
55 | return t; | |
55 | } |
|
56 | } | |
56 |
|
57 | |||
57 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, |
|
58 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, | |
58 | PyObject *kwds) |
|
59 | PyObject *kwds) | |
59 | { |
|
60 | { | |
60 | /* We do all the initialization here and not a tp_init function because |
|
61 | /* We do all the initialization here and not a tp_init function because | |
61 | * dirstate_tuple is immutable. */ |
|
62 | * dirstate_tuple is immutable. */ | |
62 | dirstateTupleObject *t; |
|
63 | dirstateTupleObject *t; | |
63 | char state; |
|
64 | char state; | |
64 | int size, mode, mtime; |
|
65 | int size, mode, mtime; | |
65 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) { |
|
66 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) { | |
66 | return NULL; |
|
67 | return NULL; | |
67 | } |
|
68 | } | |
68 |
|
69 | |||
69 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); |
|
70 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); | |
70 | if (!t) { |
|
71 | if (!t) { | |
71 | return NULL; |
|
72 | return NULL; | |
72 | } |
|
73 | } | |
73 | t->state = state; |
|
74 | t->state = state; | |
74 | t->mode = mode; |
|
75 | t->mode = mode; | |
75 | t->size = size; |
|
76 | t->size = size; | |
76 | t->mtime = mtime; |
|
77 | t->mtime = mtime; | |
77 |
|
78 | |||
78 | return (PyObject *)t; |
|
79 | return (PyObject *)t; | |
79 | } |
|
80 | } | |
80 |
|
81 | |||
81 | static void dirstate_tuple_dealloc(PyObject *o) |
|
82 | static void dirstate_tuple_dealloc(PyObject *o) | |
82 | { |
|
83 | { | |
83 | PyObject_Del(o); |
|
84 | PyObject_Del(o); | |
84 | } |
|
85 | } | |
85 |
|
86 | |||
86 | static Py_ssize_t dirstate_tuple_length(PyObject *o) |
|
87 | static Py_ssize_t dirstate_tuple_length(PyObject *o) | |
87 | { |
|
88 | { | |
88 | return 4; |
|
89 | return 4; | |
89 | } |
|
90 | } | |
90 |
|
91 | |||
91 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) |
|
92 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) | |
92 | { |
|
93 | { | |
93 | dirstateTupleObject *t = (dirstateTupleObject *)o; |
|
94 | dirstateTupleObject *t = (dirstateTupleObject *)o; | |
94 | switch (i) { |
|
95 | switch (i) { | |
95 | case 0: |
|
96 | case 0: | |
96 | return PyBytes_FromStringAndSize(&t->state, 1); |
|
97 | return PyBytes_FromStringAndSize(&t->state, 1); | |
97 | case 1: |
|
98 | case 1: | |
98 | return PyInt_FromLong(t->mode); |
|
99 | return PyInt_FromLong(t->mode); | |
99 | case 2: |
|
100 | case 2: | |
100 | return PyInt_FromLong(t->size); |
|
101 | return PyInt_FromLong(t->size); | |
101 | case 3: |
|
102 | case 3: | |
102 | return PyInt_FromLong(t->mtime); |
|
103 | return PyInt_FromLong(t->mtime); | |
103 | default: |
|
104 | default: | |
104 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
105 | PyErr_SetString(PyExc_IndexError, "index out of range"); | |
105 | return NULL; |
|
106 | return NULL; | |
106 | } |
|
107 | } | |
107 | } |
|
108 | } | |
108 |
|
109 | |||
109 | static PySequenceMethods dirstate_tuple_sq = { |
|
110 | static PySequenceMethods dirstate_tuple_sq = { | |
110 | dirstate_tuple_length, /* sq_length */ |
|
111 | dirstate_tuple_length, /* sq_length */ | |
111 | 0, /* sq_concat */ |
|
112 | 0, /* sq_concat */ | |
112 | 0, /* sq_repeat */ |
|
113 | 0, /* sq_repeat */ | |
113 | dirstate_tuple_item, /* sq_item */ |
|
114 | dirstate_tuple_item, /* sq_item */ | |
114 | 0, /* sq_ass_item */ |
|
115 | 0, /* sq_ass_item */ | |
115 | 0, /* sq_contains */ |
|
116 | 0, /* sq_contains */ | |
116 | 0, /* sq_inplace_concat */ |
|
117 | 0, /* sq_inplace_concat */ | |
117 | 0 /* sq_inplace_repeat */ |
|
118 | 0 /* sq_inplace_repeat */ | |
118 | }; |
|
119 | }; | |
119 |
|
120 | |||
120 | PyTypeObject dirstateTupleType = { |
|
121 | PyTypeObject dirstateTupleType = { | |
121 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
122 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
122 | "dirstate_tuple", /* tp_name */ |
|
123 | "dirstate_tuple", /* tp_name */ | |
123 | sizeof(dirstateTupleObject), /* tp_basicsize */ |
|
124 | sizeof(dirstateTupleObject), /* tp_basicsize */ | |
124 | 0, /* tp_itemsize */ |
|
125 | 0, /* tp_itemsize */ | |
125 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ |
|
126 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ | |
126 | 0, /* tp_print */ |
|
127 | 0, /* tp_print */ | |
127 | 0, /* tp_getattr */ |
|
128 | 0, /* tp_getattr */ | |
128 | 0, /* tp_setattr */ |
|
129 | 0, /* tp_setattr */ | |
129 | 0, /* tp_compare */ |
|
130 | 0, /* tp_compare */ | |
130 | 0, /* tp_repr */ |
|
131 | 0, /* tp_repr */ | |
131 | 0, /* tp_as_number */ |
|
132 | 0, /* tp_as_number */ | |
132 | &dirstate_tuple_sq, /* tp_as_sequence */ |
|
133 | &dirstate_tuple_sq, /* tp_as_sequence */ | |
133 | 0, /* tp_as_mapping */ |
|
134 | 0, /* tp_as_mapping */ | |
134 | 0, /* tp_hash */ |
|
135 | 0, /* tp_hash */ | |
135 | 0, /* tp_call */ |
|
136 | 0, /* tp_call */ | |
136 | 0, /* tp_str */ |
|
137 | 0, /* tp_str */ | |
137 | 0, /* tp_getattro */ |
|
138 | 0, /* tp_getattro */ | |
138 | 0, /* tp_setattro */ |
|
139 | 0, /* tp_setattro */ | |
139 | 0, /* tp_as_buffer */ |
|
140 | 0, /* tp_as_buffer */ | |
140 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
141 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
141 | "dirstate tuple", /* tp_doc */ |
|
142 | "dirstate tuple", /* tp_doc */ | |
142 | 0, /* tp_traverse */ |
|
143 | 0, /* tp_traverse */ | |
143 | 0, /* tp_clear */ |
|
144 | 0, /* tp_clear */ | |
144 | 0, /* tp_richcompare */ |
|
145 | 0, /* tp_richcompare */ | |
145 | 0, /* tp_weaklistoffset */ |
|
146 | 0, /* tp_weaklistoffset */ | |
146 | 0, /* tp_iter */ |
|
147 | 0, /* tp_iter */ | |
147 | 0, /* tp_iternext */ |
|
148 | 0, /* tp_iternext */ | |
148 | 0, /* tp_methods */ |
|
149 | 0, /* tp_methods */ | |
149 | 0, /* tp_members */ |
|
150 | 0, /* tp_members */ | |
150 | 0, /* tp_getset */ |
|
151 | 0, /* tp_getset */ | |
151 | 0, /* tp_base */ |
|
152 | 0, /* tp_base */ | |
152 | 0, /* tp_dict */ |
|
153 | 0, /* tp_dict */ | |
153 | 0, /* tp_descr_get */ |
|
154 | 0, /* tp_descr_get */ | |
154 | 0, /* tp_descr_set */ |
|
155 | 0, /* tp_descr_set */ | |
155 | 0, /* tp_dictoffset */ |
|
156 | 0, /* tp_dictoffset */ | |
156 | 0, /* tp_init */ |
|
157 | 0, /* tp_init */ | |
157 | 0, /* tp_alloc */ |
|
158 | 0, /* tp_alloc */ | |
158 | dirstate_tuple_new, /* tp_new */ |
|
159 | dirstate_tuple_new, /* tp_new */ | |
159 | }; |
|
160 | }; | |
160 |
|
161 | |||
161 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
162 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) | |
162 | { |
|
163 | { | |
163 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
164 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; | |
164 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
165 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; | |
165 | char state, *cur, *str, *cpos; |
|
166 | char state, *cur, *str, *cpos; | |
166 | int mode, size, mtime; |
|
167 | int mode, size, mtime; | |
167 |
unsigned int flen, |
|
168 | unsigned int flen, pos = 40; | |
168 | int readlen; |
|
169 | Py_ssize_t len = 40; | |
|
170 | Py_ssize_t readlen; | |||
169 |
|
171 | |||
170 | if (!PyArg_ParseTuple( |
|
172 | if (!PyArg_ParseTuple( | |
171 | args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"), |
|
173 | args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"), | |
172 | &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) { |
|
174 | &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) { | |
173 | goto quit; |
|
175 | goto quit; | |
174 | } |
|
176 | } | |
175 |
|
177 | |||
176 | len = readlen; |
|
178 | len = readlen; | |
177 |
|
179 | |||
178 | /* read parents */ |
|
180 | /* read parents */ | |
179 | if (len < 40) { |
|
181 | if (len < 40) { | |
180 | PyErr_SetString(PyExc_ValueError, |
|
182 | PyErr_SetString(PyExc_ValueError, | |
181 | "too little data for parents"); |
|
183 | "too little data for parents"); | |
182 | goto quit; |
|
184 | goto quit; | |
183 | } |
|
185 | } | |
184 |
|
186 | |||
185 | parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, 20, str + 20, 20); |
|
187 | parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, 20, str + 20, 20); | |
186 | if (!parents) { |
|
188 | if (!parents) { | |
187 | goto quit; |
|
189 | goto quit; | |
188 | } |
|
190 | } | |
189 |
|
191 | |||
190 | /* read filenames */ |
|
192 | /* read filenames */ | |
191 | while (pos >= 40 && pos < len) { |
|
193 | while (pos >= 40 && pos < len) { | |
192 | if (pos + 17 > len) { |
|
194 | if (pos + 17 > len) { | |
193 | PyErr_SetString(PyExc_ValueError, |
|
195 | PyErr_SetString(PyExc_ValueError, | |
194 | "overflow in dirstate"); |
|
196 | "overflow in dirstate"); | |
195 | goto quit; |
|
197 | goto quit; | |
196 | } |
|
198 | } | |
197 | cur = str + pos; |
|
199 | cur = str + pos; | |
198 | /* unpack header */ |
|
200 | /* unpack header */ | |
199 | state = *cur; |
|
201 | state = *cur; | |
200 | mode = getbe32(cur + 1); |
|
202 | mode = getbe32(cur + 1); | |
201 | size = getbe32(cur + 5); |
|
203 | size = getbe32(cur + 5); | |
202 | mtime = getbe32(cur + 9); |
|
204 | mtime = getbe32(cur + 9); | |
203 | flen = getbe32(cur + 13); |
|
205 | flen = getbe32(cur + 13); | |
204 | pos += 17; |
|
206 | pos += 17; | |
205 | cur += 17; |
|
207 | cur += 17; | |
206 | if (flen > len - pos) { |
|
208 | if (flen > len - pos) { | |
207 | PyErr_SetString(PyExc_ValueError, |
|
209 | PyErr_SetString(PyExc_ValueError, | |
208 | "overflow in dirstate"); |
|
210 | "overflow in dirstate"); | |
209 | goto quit; |
|
211 | goto quit; | |
210 | } |
|
212 | } | |
211 |
|
213 | |||
212 | entry = |
|
214 | entry = | |
213 | (PyObject *)make_dirstate_tuple(state, mode, size, mtime); |
|
215 | (PyObject *)make_dirstate_tuple(state, mode, size, mtime); | |
214 | cpos = memchr(cur, 0, flen); |
|
216 | cpos = memchr(cur, 0, flen); | |
215 | if (cpos) { |
|
217 | if (cpos) { | |
216 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
218 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); | |
217 | cname = PyBytes_FromStringAndSize( |
|
219 | cname = PyBytes_FromStringAndSize( | |
218 | cpos + 1, flen - (cpos - cur) - 1); |
|
220 | cpos + 1, flen - (cpos - cur) - 1); | |
219 | if (!fname || !cname || |
|
221 | if (!fname || !cname || | |
220 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
222 | PyDict_SetItem(cmap, fname, cname) == -1 || | |
221 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
223 | PyDict_SetItem(dmap, fname, entry) == -1) { | |
222 | goto quit; |
|
224 | goto quit; | |
223 | } |
|
225 | } | |
224 | Py_DECREF(cname); |
|
226 | Py_DECREF(cname); | |
225 | } else { |
|
227 | } else { | |
226 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
228 | fname = PyBytes_FromStringAndSize(cur, flen); | |
227 | if (!fname || |
|
229 | if (!fname || | |
228 | PyDict_SetItem(dmap, fname, entry) == -1) { |
|
230 | PyDict_SetItem(dmap, fname, entry) == -1) { | |
229 | goto quit; |
|
231 | goto quit; | |
230 | } |
|
232 | } | |
231 | } |
|
233 | } | |
232 | Py_DECREF(fname); |
|
234 | Py_DECREF(fname); | |
233 | Py_DECREF(entry); |
|
235 | Py_DECREF(entry); | |
234 | fname = cname = entry = NULL; |
|
236 | fname = cname = entry = NULL; | |
235 | pos += flen; |
|
237 | pos += flen; | |
236 | } |
|
238 | } | |
237 |
|
239 | |||
238 | ret = parents; |
|
240 | ret = parents; | |
239 | Py_INCREF(ret); |
|
241 | Py_INCREF(ret); | |
240 | quit: |
|
242 | quit: | |
241 | Py_XDECREF(fname); |
|
243 | Py_XDECREF(fname); | |
242 | Py_XDECREF(cname); |
|
244 | Py_XDECREF(cname); | |
243 | Py_XDECREF(entry); |
|
245 | Py_XDECREF(entry); | |
244 | Py_XDECREF(parents); |
|
246 | Py_XDECREF(parents); | |
245 | return ret; |
|
247 | return ret; | |
246 | } |
|
248 | } | |
247 |
|
249 | |||
248 | /* |
|
250 | /* | |
249 | * Build a set of non-normal and other parent entries from the dirstate dmap |
|
251 | * Build a set of non-normal and other parent entries from the dirstate dmap | |
250 | */ |
|
252 | */ | |
251 | static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args) |
|
253 | static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args) | |
252 | { |
|
254 | { | |
253 | PyObject *dmap, *fname, *v; |
|
255 | PyObject *dmap, *fname, *v; | |
254 | PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL; |
|
256 | PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL; | |
255 | Py_ssize_t pos; |
|
257 | Py_ssize_t pos; | |
256 |
|
258 | |||
257 | if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, |
|
259 | if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, | |
258 | &dmap)) { |
|
260 | &dmap)) { | |
259 | goto bail; |
|
261 | goto bail; | |
260 | } |
|
262 | } | |
261 |
|
263 | |||
262 | nonnset = PySet_New(NULL); |
|
264 | nonnset = PySet_New(NULL); | |
263 | if (nonnset == NULL) { |
|
265 | if (nonnset == NULL) { | |
264 | goto bail; |
|
266 | goto bail; | |
265 | } |
|
267 | } | |
266 |
|
268 | |||
267 | otherpset = PySet_New(NULL); |
|
269 | otherpset = PySet_New(NULL); | |
268 | if (otherpset == NULL) { |
|
270 | if (otherpset == NULL) { | |
269 | goto bail; |
|
271 | goto bail; | |
270 | } |
|
272 | } | |
271 |
|
273 | |||
272 | pos = 0; |
|
274 | pos = 0; | |
273 | while (PyDict_Next(dmap, &pos, &fname, &v)) { |
|
275 | while (PyDict_Next(dmap, &pos, &fname, &v)) { | |
274 | dirstateTupleObject *t; |
|
276 | dirstateTupleObject *t; | |
275 | if (!dirstate_tuple_check(v)) { |
|
277 | if (!dirstate_tuple_check(v)) { | |
276 | PyErr_SetString(PyExc_TypeError, |
|
278 | PyErr_SetString(PyExc_TypeError, | |
277 | "expected a dirstate tuple"); |
|
279 | "expected a dirstate tuple"); | |
278 | goto bail; |
|
280 | goto bail; | |
279 | } |
|
281 | } | |
280 | t = (dirstateTupleObject *)v; |
|
282 | t = (dirstateTupleObject *)v; | |
281 |
|
283 | |||
282 | if (t->state == 'n' && t->size == -2) { |
|
284 | if (t->state == 'n' && t->size == -2) { | |
283 | if (PySet_Add(otherpset, fname) == -1) { |
|
285 | if (PySet_Add(otherpset, fname) == -1) { | |
284 | goto bail; |
|
286 | goto bail; | |
285 | } |
|
287 | } | |
286 | } |
|
288 | } | |
287 |
|
289 | |||
288 | if (t->state == 'n' && t->mtime != -1) { |
|
290 | if (t->state == 'n' && t->mtime != -1) { | |
289 | continue; |
|
291 | continue; | |
290 | } |
|
292 | } | |
291 | if (PySet_Add(nonnset, fname) == -1) { |
|
293 | if (PySet_Add(nonnset, fname) == -1) { | |
292 | goto bail; |
|
294 | goto bail; | |
293 | } |
|
295 | } | |
294 | } |
|
296 | } | |
295 |
|
297 | |||
296 | result = Py_BuildValue("(OO)", nonnset, otherpset); |
|
298 | result = Py_BuildValue("(OO)", nonnset, otherpset); | |
297 | if (result == NULL) { |
|
299 | if (result == NULL) { | |
298 | goto bail; |
|
300 | goto bail; | |
299 | } |
|
301 | } | |
300 | Py_DECREF(nonnset); |
|
302 | Py_DECREF(nonnset); | |
301 | Py_DECREF(otherpset); |
|
303 | Py_DECREF(otherpset); | |
302 | return result; |
|
304 | return result; | |
303 | bail: |
|
305 | bail: | |
304 | Py_XDECREF(nonnset); |
|
306 | Py_XDECREF(nonnset); | |
305 | Py_XDECREF(otherpset); |
|
307 | Py_XDECREF(otherpset); | |
306 | Py_XDECREF(result); |
|
308 | Py_XDECREF(result); | |
307 | return NULL; |
|
309 | return NULL; | |
308 | } |
|
310 | } | |
309 |
|
311 | |||
310 | /* |
|
312 | /* | |
311 | * Efficiently pack a dirstate object into its on-disk format. |
|
313 | * Efficiently pack a dirstate object into its on-disk format. | |
312 | */ |
|
314 | */ | |
313 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
315 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) | |
314 | { |
|
316 | { | |
315 | PyObject *packobj = NULL; |
|
317 | PyObject *packobj = NULL; | |
316 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; |
|
318 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; | |
317 | Py_ssize_t nbytes, pos, l; |
|
319 | Py_ssize_t nbytes, pos, l; | |
318 | PyObject *k, *v = NULL, *pn; |
|
320 | PyObject *k, *v = NULL, *pn; | |
319 | char *p, *s; |
|
321 | char *p, *s; | |
320 | int now; |
|
322 | int now; | |
321 |
|
323 | |||
322 | if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map, |
|
324 | if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map, | |
323 | &PyDict_Type, ©map, &PyTuple_Type, &pl, |
|
325 | &PyDict_Type, ©map, &PyTuple_Type, &pl, | |
324 | &now)) { |
|
326 | &now)) { | |
325 | return NULL; |
|
327 | return NULL; | |
326 | } |
|
328 | } | |
327 |
|
329 | |||
328 | if (PyTuple_Size(pl) != 2) { |
|
330 | if (PyTuple_Size(pl) != 2) { | |
329 | PyErr_SetString(PyExc_TypeError, "expected 2-element tuple"); |
|
331 | PyErr_SetString(PyExc_TypeError, "expected 2-element tuple"); | |
330 | return NULL; |
|
332 | return NULL; | |
331 | } |
|
333 | } | |
332 |
|
334 | |||
333 | /* Figure out how much we need to allocate. */ |
|
335 | /* Figure out how much we need to allocate. */ | |
334 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
336 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { | |
335 | PyObject *c; |
|
337 | PyObject *c; | |
336 | if (!PyBytes_Check(k)) { |
|
338 | if (!PyBytes_Check(k)) { | |
337 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
339 | PyErr_SetString(PyExc_TypeError, "expected string key"); | |
338 | goto bail; |
|
340 | goto bail; | |
339 | } |
|
341 | } | |
340 | nbytes += PyBytes_GET_SIZE(k) + 17; |
|
342 | nbytes += PyBytes_GET_SIZE(k) + 17; | |
341 | c = PyDict_GetItem(copymap, k); |
|
343 | c = PyDict_GetItem(copymap, k); | |
342 | if (c) { |
|
344 | if (c) { | |
343 | if (!PyBytes_Check(c)) { |
|
345 | if (!PyBytes_Check(c)) { | |
344 | PyErr_SetString(PyExc_TypeError, |
|
346 | PyErr_SetString(PyExc_TypeError, | |
345 | "expected string key"); |
|
347 | "expected string key"); | |
346 | goto bail; |
|
348 | goto bail; | |
347 | } |
|
349 | } | |
348 | nbytes += PyBytes_GET_SIZE(c) + 1; |
|
350 | nbytes += PyBytes_GET_SIZE(c) + 1; | |
349 | } |
|
351 | } | |
350 | } |
|
352 | } | |
351 |
|
353 | |||
352 | packobj = PyBytes_FromStringAndSize(NULL, nbytes); |
|
354 | packobj = PyBytes_FromStringAndSize(NULL, nbytes); | |
353 | if (packobj == NULL) { |
|
355 | if (packobj == NULL) { | |
354 | goto bail; |
|
356 | goto bail; | |
355 | } |
|
357 | } | |
356 |
|
358 | |||
357 | p = PyBytes_AS_STRING(packobj); |
|
359 | p = PyBytes_AS_STRING(packobj); | |
358 |
|
360 | |||
359 | pn = PyTuple_GET_ITEM(pl, 0); |
|
361 | pn = PyTuple_GET_ITEM(pl, 0); | |
360 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
362 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { | |
361 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
363 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); | |
362 | goto bail; |
|
364 | goto bail; | |
363 | } |
|
365 | } | |
364 | memcpy(p, s, l); |
|
366 | memcpy(p, s, l); | |
365 | p += 20; |
|
367 | p += 20; | |
366 | pn = PyTuple_GET_ITEM(pl, 1); |
|
368 | pn = PyTuple_GET_ITEM(pl, 1); | |
367 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
369 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { | |
368 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
370 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); | |
369 | goto bail; |
|
371 | goto bail; | |
370 | } |
|
372 | } | |
371 | memcpy(p, s, l); |
|
373 | memcpy(p, s, l); | |
372 | p += 20; |
|
374 | p += 20; | |
373 |
|
375 | |||
374 | for (pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
376 | for (pos = 0; PyDict_Next(map, &pos, &k, &v);) { | |
375 | dirstateTupleObject *tuple; |
|
377 | dirstateTupleObject *tuple; | |
376 | char state; |
|
378 | char state; | |
377 | int mode, size, mtime; |
|
379 | int mode, size, mtime; | |
378 | Py_ssize_t len, l; |
|
380 | Py_ssize_t len, l; | |
379 | PyObject *o; |
|
381 | PyObject *o; | |
380 | char *t; |
|
382 | char *t; | |
381 |
|
383 | |||
382 | if (!dirstate_tuple_check(v)) { |
|
384 | if (!dirstate_tuple_check(v)) { | |
383 | PyErr_SetString(PyExc_TypeError, |
|
385 | PyErr_SetString(PyExc_TypeError, | |
384 | "expected a dirstate tuple"); |
|
386 | "expected a dirstate tuple"); | |
385 | goto bail; |
|
387 | goto bail; | |
386 | } |
|
388 | } | |
387 | tuple = (dirstateTupleObject *)v; |
|
389 | tuple = (dirstateTupleObject *)v; | |
388 |
|
390 | |||
389 | state = tuple->state; |
|
391 | state = tuple->state; | |
390 | mode = tuple->mode; |
|
392 | mode = tuple->mode; | |
391 | size = tuple->size; |
|
393 | size = tuple->size; | |
392 | mtime = tuple->mtime; |
|
394 | mtime = tuple->mtime; | |
393 | if (state == 'n' && mtime == now) { |
|
395 | if (state == 'n' && mtime == now) { | |
394 | /* See pure/parsers.py:pack_dirstate for why we do |
|
396 | /* See pure/parsers.py:pack_dirstate for why we do | |
395 | * this. */ |
|
397 | * this. */ | |
396 | mtime = -1; |
|
398 | mtime = -1; | |
397 | mtime_unset = (PyObject *)make_dirstate_tuple( |
|
399 | mtime_unset = (PyObject *)make_dirstate_tuple( | |
398 | state, mode, size, mtime); |
|
400 | state, mode, size, mtime); | |
399 | if (!mtime_unset) { |
|
401 | if (!mtime_unset) { | |
400 | goto bail; |
|
402 | goto bail; | |
401 | } |
|
403 | } | |
402 | if (PyDict_SetItem(map, k, mtime_unset) == -1) { |
|
404 | if (PyDict_SetItem(map, k, mtime_unset) == -1) { | |
403 | goto bail; |
|
405 | goto bail; | |
404 | } |
|
406 | } | |
405 | Py_DECREF(mtime_unset); |
|
407 | Py_DECREF(mtime_unset); | |
406 | mtime_unset = NULL; |
|
408 | mtime_unset = NULL; | |
407 | } |
|
409 | } | |
408 | *p++ = state; |
|
410 | *p++ = state; | |
409 | putbe32((uint32_t)mode, p); |
|
411 | putbe32((uint32_t)mode, p); | |
410 | putbe32((uint32_t)size, p + 4); |
|
412 | putbe32((uint32_t)size, p + 4); | |
411 | putbe32((uint32_t)mtime, p + 8); |
|
413 | putbe32((uint32_t)mtime, p + 8); | |
412 | t = p + 12; |
|
414 | t = p + 12; | |
413 | p += 16; |
|
415 | p += 16; | |
414 | len = PyBytes_GET_SIZE(k); |
|
416 | len = PyBytes_GET_SIZE(k); | |
415 | memcpy(p, PyBytes_AS_STRING(k), len); |
|
417 | memcpy(p, PyBytes_AS_STRING(k), len); | |
416 | p += len; |
|
418 | p += len; | |
417 | o = PyDict_GetItem(copymap, k); |
|
419 | o = PyDict_GetItem(copymap, k); | |
418 | if (o) { |
|
420 | if (o) { | |
419 | *p++ = '\0'; |
|
421 | *p++ = '\0'; | |
420 | l = PyBytes_GET_SIZE(o); |
|
422 | l = PyBytes_GET_SIZE(o); | |
421 | memcpy(p, PyBytes_AS_STRING(o), l); |
|
423 | memcpy(p, PyBytes_AS_STRING(o), l); | |
422 | p += l; |
|
424 | p += l; | |
423 | len += l + 1; |
|
425 | len += l + 1; | |
424 | } |
|
426 | } | |
425 | putbe32((uint32_t)len, t); |
|
427 | putbe32((uint32_t)len, t); | |
426 | } |
|
428 | } | |
427 |
|
429 | |||
428 | pos = p - PyBytes_AS_STRING(packobj); |
|
430 | pos = p - PyBytes_AS_STRING(packobj); | |
429 | if (pos != nbytes) { |
|
431 | if (pos != nbytes) { | |
430 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
432 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", | |
431 | (long)pos, (long)nbytes); |
|
433 | (long)pos, (long)nbytes); | |
432 | goto bail; |
|
434 | goto bail; | |
433 | } |
|
435 | } | |
434 |
|
436 | |||
435 | return packobj; |
|
437 | return packobj; | |
436 | bail: |
|
438 | bail: | |
437 | Py_XDECREF(mtime_unset); |
|
439 | Py_XDECREF(mtime_unset); | |
438 | Py_XDECREF(packobj); |
|
440 | Py_XDECREF(packobj); | |
439 | Py_XDECREF(v); |
|
441 | Py_XDECREF(v); | |
440 | return NULL; |
|
442 | return NULL; | |
441 | } |
|
443 | } | |
442 |
|
444 | |||
443 | #define BUMPED_FIX 1 |
|
445 | #define BUMPED_FIX 1 | |
444 | #define USING_SHA_256 2 |
|
446 | #define USING_SHA_256 2 | |
445 | #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1) |
|
447 | #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1) | |
446 |
|
448 | |||
447 | static PyObject *readshas(const char *source, unsigned char num, |
|
449 | static PyObject *readshas(const char *source, unsigned char num, | |
448 | Py_ssize_t hashwidth) |
|
450 | Py_ssize_t hashwidth) | |
449 | { |
|
451 | { | |
450 | int i; |
|
452 | int i; | |
451 | PyObject *list = PyTuple_New(num); |
|
453 | PyObject *list = PyTuple_New(num); | |
452 | if (list == NULL) { |
|
454 | if (list == NULL) { | |
453 | return NULL; |
|
455 | return NULL; | |
454 | } |
|
456 | } | |
455 | for (i = 0; i < num; i++) { |
|
457 | for (i = 0; i < num; i++) { | |
456 | PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth); |
|
458 | PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth); | |
457 | if (hash == NULL) { |
|
459 | if (hash == NULL) { | |
458 | Py_DECREF(list); |
|
460 | Py_DECREF(list); | |
459 | return NULL; |
|
461 | return NULL; | |
460 | } |
|
462 | } | |
461 | PyTuple_SET_ITEM(list, i, hash); |
|
463 | PyTuple_SET_ITEM(list, i, hash); | |
462 | source += hashwidth; |
|
464 | source += hashwidth; | |
463 | } |
|
465 | } | |
464 | return list; |
|
466 | return list; | |
465 | } |
|
467 | } | |
466 |
|
468 | |||
467 | static PyObject *fm1readmarker(const char *databegin, const char *dataend, |
|
469 | static PyObject *fm1readmarker(const char *databegin, const char *dataend, | |
468 | uint32_t *msize) |
|
470 | uint32_t *msize) | |
469 | { |
|
471 | { | |
470 | const char *data = databegin; |
|
472 | const char *data = databegin; | |
471 | const char *meta; |
|
473 | const char *meta; | |
472 |
|
474 | |||
473 | double mtime; |
|
475 | double mtime; | |
474 | int16_t tz; |
|
476 | int16_t tz; | |
475 | uint16_t flags; |
|
477 | uint16_t flags; | |
476 | unsigned char nsuccs, nparents, nmetadata; |
|
478 | unsigned char nsuccs, nparents, nmetadata; | |
477 | Py_ssize_t hashwidth = 20; |
|
479 | Py_ssize_t hashwidth = 20; | |
478 |
|
480 | |||
479 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; |
|
481 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; | |
480 | PyObject *metadata = NULL, *ret = NULL; |
|
482 | PyObject *metadata = NULL, *ret = NULL; | |
481 | int i; |
|
483 | int i; | |
482 |
|
484 | |||
483 | if (data + FM1_HEADER_SIZE > dataend) { |
|
485 | if (data + FM1_HEADER_SIZE > dataend) { | |
484 | goto overflow; |
|
486 | goto overflow; | |
485 | } |
|
487 | } | |
486 |
|
488 | |||
487 | *msize = getbe32(data); |
|
489 | *msize = getbe32(data); | |
488 | data += 4; |
|
490 | data += 4; | |
489 | mtime = getbefloat64(data); |
|
491 | mtime = getbefloat64(data); | |
490 | data += 8; |
|
492 | data += 8; | |
491 | tz = getbeint16(data); |
|
493 | tz = getbeint16(data); | |
492 | data += 2; |
|
494 | data += 2; | |
493 | flags = getbeuint16(data); |
|
495 | flags = getbeuint16(data); | |
494 | data += 2; |
|
496 | data += 2; | |
495 |
|
497 | |||
496 | if (flags & USING_SHA_256) { |
|
498 | if (flags & USING_SHA_256) { | |
497 | hashwidth = 32; |
|
499 | hashwidth = 32; | |
498 | } |
|
500 | } | |
499 |
|
501 | |||
500 | nsuccs = (unsigned char)(*data++); |
|
502 | nsuccs = (unsigned char)(*data++); | |
501 | nparents = (unsigned char)(*data++); |
|
503 | nparents = (unsigned char)(*data++); | |
502 | nmetadata = (unsigned char)(*data++); |
|
504 | nmetadata = (unsigned char)(*data++); | |
503 |
|
505 | |||
504 | if (databegin + *msize > dataend) { |
|
506 | if (databegin + *msize > dataend) { | |
505 | goto overflow; |
|
507 | goto overflow; | |
506 | } |
|
508 | } | |
507 | dataend = databegin + *msize; /* narrow down to marker size */ |
|
509 | dataend = databegin + *msize; /* narrow down to marker size */ | |
508 |
|
510 | |||
509 | if (data + hashwidth > dataend) { |
|
511 | if (data + hashwidth > dataend) { | |
510 | goto overflow; |
|
512 | goto overflow; | |
511 | } |
|
513 | } | |
512 | prec = PyBytes_FromStringAndSize(data, hashwidth); |
|
514 | prec = PyBytes_FromStringAndSize(data, hashwidth); | |
513 | data += hashwidth; |
|
515 | data += hashwidth; | |
514 | if (prec == NULL) { |
|
516 | if (prec == NULL) { | |
515 | goto bail; |
|
517 | goto bail; | |
516 | } |
|
518 | } | |
517 |
|
519 | |||
518 | if (data + nsuccs * hashwidth > dataend) { |
|
520 | if (data + nsuccs * hashwidth > dataend) { | |
519 | goto overflow; |
|
521 | goto overflow; | |
520 | } |
|
522 | } | |
521 | succs = readshas(data, nsuccs, hashwidth); |
|
523 | succs = readshas(data, nsuccs, hashwidth); | |
522 | if (succs == NULL) { |
|
524 | if (succs == NULL) { | |
523 | goto bail; |
|
525 | goto bail; | |
524 | } |
|
526 | } | |
525 | data += nsuccs * hashwidth; |
|
527 | data += nsuccs * hashwidth; | |
526 |
|
528 | |||
527 | if (nparents == 1 || nparents == 2) { |
|
529 | if (nparents == 1 || nparents == 2) { | |
528 | if (data + nparents * hashwidth > dataend) { |
|
530 | if (data + nparents * hashwidth > dataend) { | |
529 | goto overflow; |
|
531 | goto overflow; | |
530 | } |
|
532 | } | |
531 | parents = readshas(data, nparents, hashwidth); |
|
533 | parents = readshas(data, nparents, hashwidth); | |
532 | if (parents == NULL) { |
|
534 | if (parents == NULL) { | |
533 | goto bail; |
|
535 | goto bail; | |
534 | } |
|
536 | } | |
535 | data += nparents * hashwidth; |
|
537 | data += nparents * hashwidth; | |
536 | } else { |
|
538 | } else { | |
537 | parents = Py_None; |
|
539 | parents = Py_None; | |
538 | Py_INCREF(parents); |
|
540 | Py_INCREF(parents); | |
539 | } |
|
541 | } | |
540 |
|
542 | |||
541 | if (data + 2 * nmetadata > dataend) { |
|
543 | if (data + 2 * nmetadata > dataend) { | |
542 | goto overflow; |
|
544 | goto overflow; | |
543 | } |
|
545 | } | |
544 | meta = data + (2 * nmetadata); |
|
546 | meta = data + (2 * nmetadata); | |
545 | metadata = PyTuple_New(nmetadata); |
|
547 | metadata = PyTuple_New(nmetadata); | |
546 | if (metadata == NULL) { |
|
548 | if (metadata == NULL) { | |
547 | goto bail; |
|
549 | goto bail; | |
548 | } |
|
550 | } | |
549 | for (i = 0; i < nmetadata; i++) { |
|
551 | for (i = 0; i < nmetadata; i++) { | |
550 | PyObject *tmp, *left = NULL, *right = NULL; |
|
552 | PyObject *tmp, *left = NULL, *right = NULL; | |
551 | Py_ssize_t leftsize = (unsigned char)(*data++); |
|
553 | Py_ssize_t leftsize = (unsigned char)(*data++); | |
552 | Py_ssize_t rightsize = (unsigned char)(*data++); |
|
554 | Py_ssize_t rightsize = (unsigned char)(*data++); | |
553 | if (meta + leftsize + rightsize > dataend) { |
|
555 | if (meta + leftsize + rightsize > dataend) { | |
554 | goto overflow; |
|
556 | goto overflow; | |
555 | } |
|
557 | } | |
556 | left = PyBytes_FromStringAndSize(meta, leftsize); |
|
558 | left = PyBytes_FromStringAndSize(meta, leftsize); | |
557 | meta += leftsize; |
|
559 | meta += leftsize; | |
558 | right = PyBytes_FromStringAndSize(meta, rightsize); |
|
560 | right = PyBytes_FromStringAndSize(meta, rightsize); | |
559 | meta += rightsize; |
|
561 | meta += rightsize; | |
560 | tmp = PyTuple_New(2); |
|
562 | tmp = PyTuple_New(2); | |
561 | if (!left || !right || !tmp) { |
|
563 | if (!left || !right || !tmp) { | |
562 | Py_XDECREF(left); |
|
564 | Py_XDECREF(left); | |
563 | Py_XDECREF(right); |
|
565 | Py_XDECREF(right); | |
564 | Py_XDECREF(tmp); |
|
566 | Py_XDECREF(tmp); | |
565 | goto bail; |
|
567 | goto bail; | |
566 | } |
|
568 | } | |
567 | PyTuple_SET_ITEM(tmp, 0, left); |
|
569 | PyTuple_SET_ITEM(tmp, 0, left); | |
568 | PyTuple_SET_ITEM(tmp, 1, right); |
|
570 | PyTuple_SET_ITEM(tmp, 1, right); | |
569 | PyTuple_SET_ITEM(metadata, i, tmp); |
|
571 | PyTuple_SET_ITEM(metadata, i, tmp); | |
570 | } |
|
572 | } | |
571 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime, |
|
573 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime, | |
572 | (int)tz * 60, parents); |
|
574 | (int)tz * 60, parents); | |
573 | goto bail; /* return successfully */ |
|
575 | goto bail; /* return successfully */ | |
574 |
|
576 | |||
575 | overflow: |
|
577 | overflow: | |
576 | PyErr_SetString(PyExc_ValueError, "overflow in obsstore"); |
|
578 | PyErr_SetString(PyExc_ValueError, "overflow in obsstore"); | |
577 | bail: |
|
579 | bail: | |
578 | Py_XDECREF(prec); |
|
580 | Py_XDECREF(prec); | |
579 | Py_XDECREF(succs); |
|
581 | Py_XDECREF(succs); | |
580 | Py_XDECREF(metadata); |
|
582 | Py_XDECREF(metadata); | |
581 | Py_XDECREF(parents); |
|
583 | Py_XDECREF(parents); | |
582 | return ret; |
|
584 | return ret; | |
583 | } |
|
585 | } | |
584 |
|
586 | |||
585 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) |
|
587 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) | |
586 | { |
|
588 | { | |
587 | const char *data, *dataend; |
|
589 | const char *data, *dataend; | |
588 | int datalen; |
|
590 | Py_ssize_t datalen, offset, stop; | |
589 | Py_ssize_t offset, stop; |
|
|||
590 | PyObject *markers = NULL; |
|
591 | PyObject *markers = NULL; | |
591 |
|
592 | |||
592 | if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen, |
|
593 | if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen, | |
593 | &offset, &stop)) { |
|
594 | &offset, &stop)) { | |
594 | return NULL; |
|
595 | return NULL; | |
595 | } |
|
596 | } | |
596 | if (offset < 0) { |
|
597 | if (offset < 0) { | |
597 | PyErr_SetString(PyExc_ValueError, |
|
598 | PyErr_SetString(PyExc_ValueError, | |
598 | "invalid negative offset in fm1readmarkers"); |
|
599 | "invalid negative offset in fm1readmarkers"); | |
599 | return NULL; |
|
600 | return NULL; | |
600 | } |
|
601 | } | |
601 | if (stop > datalen) { |
|
602 | if (stop > datalen) { | |
602 | PyErr_SetString( |
|
603 | PyErr_SetString( | |
603 | PyExc_ValueError, |
|
604 | PyExc_ValueError, | |
604 | "stop longer than data length in fm1readmarkers"); |
|
605 | "stop longer than data length in fm1readmarkers"); | |
605 | return NULL; |
|
606 | return NULL; | |
606 | } |
|
607 | } | |
607 | dataend = data + datalen; |
|
608 | dataend = data + datalen; | |
608 | data += offset; |
|
609 | data += offset; | |
609 | markers = PyList_New(0); |
|
610 | markers = PyList_New(0); | |
610 | if (!markers) { |
|
611 | if (!markers) { | |
611 | return NULL; |
|
612 | return NULL; | |
612 | } |
|
613 | } | |
613 | while (offset < stop) { |
|
614 | while (offset < stop) { | |
614 | uint32_t msize; |
|
615 | uint32_t msize; | |
615 | int error; |
|
616 | int error; | |
616 | PyObject *record = fm1readmarker(data, dataend, &msize); |
|
617 | PyObject *record = fm1readmarker(data, dataend, &msize); | |
617 | if (!record) { |
|
618 | if (!record) { | |
618 | goto bail; |
|
619 | goto bail; | |
619 | } |
|
620 | } | |
620 | error = PyList_Append(markers, record); |
|
621 | error = PyList_Append(markers, record); | |
621 | Py_DECREF(record); |
|
622 | Py_DECREF(record); | |
622 | if (error) { |
|
623 | if (error) { | |
623 | goto bail; |
|
624 | goto bail; | |
624 | } |
|
625 | } | |
625 | data += msize; |
|
626 | data += msize; | |
626 | offset += msize; |
|
627 | offset += msize; | |
627 | } |
|
628 | } | |
628 | return markers; |
|
629 | return markers; | |
629 | bail: |
|
630 | bail: | |
630 | Py_DECREF(markers); |
|
631 | Py_DECREF(markers); | |
631 | return NULL; |
|
632 | return NULL; | |
632 | } |
|
633 | } | |
633 |
|
634 | |||
634 | static char parsers_doc[] = "Efficient content parsing."; |
|
635 | static char parsers_doc[] = "Efficient content parsing."; | |
635 |
|
636 | |||
636 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
637 | PyObject *encodedir(PyObject *self, PyObject *args); | |
637 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
638 | PyObject *pathencode(PyObject *self, PyObject *args); | |
638 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
639 | PyObject *lowerencode(PyObject *self, PyObject *args); | |
639 | PyObject *parse_index2(PyObject *self, PyObject *args); |
|
640 | PyObject *parse_index2(PyObject *self, PyObject *args); | |
640 |
|
641 | |||
641 | static PyMethodDef methods[] = { |
|
642 | static PyMethodDef methods[] = { | |
642 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
643 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, | |
643 | {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS, |
|
644 | {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS, | |
644 | "create a set containing non-normal and other parent entries of given " |
|
645 | "create a set containing non-normal and other parent entries of given " | |
645 | "dirstate\n"}, |
|
646 | "dirstate\n"}, | |
646 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
647 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, | |
647 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
648 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, | |
648 | {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"}, |
|
649 | {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"}, | |
649 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, |
|
650 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, | |
650 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, |
|
651 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, | |
651 | {"dict_new_presized", dict_new_presized, METH_VARARGS, |
|
652 | {"dict_new_presized", dict_new_presized, METH_VARARGS, | |
652 | "construct a dict with an expected size\n"}, |
|
653 | "construct a dict with an expected size\n"}, | |
653 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, |
|
654 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, | |
654 | "make file foldmap\n"}, |
|
655 | "make file foldmap\n"}, | |
655 | {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS, |
|
656 | {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS, | |
656 | "escape a UTF-8 byte string to JSON (fast path)\n"}, |
|
657 | "escape a UTF-8 byte string to JSON (fast path)\n"}, | |
657 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
658 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, | |
658 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
659 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, | |
659 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
660 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, | |
660 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, |
|
661 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, | |
661 | "parse v1 obsolete markers\n"}, |
|
662 | "parse v1 obsolete markers\n"}, | |
662 | {NULL, NULL}}; |
|
663 | {NULL, NULL}}; | |
663 |
|
664 | |||
664 | void dirs_module_init(PyObject *mod); |
|
665 | void dirs_module_init(PyObject *mod); | |
665 | void manifest_module_init(PyObject *mod); |
|
666 | void manifest_module_init(PyObject *mod); | |
666 | void revlog_module_init(PyObject *mod); |
|
667 | void revlog_module_init(PyObject *mod); | |
667 |
|
668 | |||
668 | static const int version = 12; |
|
669 | static const int version = 12; | |
669 |
|
670 | |||
670 | static void module_init(PyObject *mod) |
|
671 | static void module_init(PyObject *mod) | |
671 | { |
|
672 | { | |
672 | PyModule_AddIntConstant(mod, "version", version); |
|
673 | PyModule_AddIntConstant(mod, "version", version); | |
673 |
|
674 | |||
674 | /* This module constant has two purposes. First, it lets us unit test |
|
675 | /* This module constant has two purposes. First, it lets us unit test | |
675 | * the ImportError raised without hard-coding any error text. This |
|
676 | * the ImportError raised without hard-coding any error text. This | |
676 | * means we can change the text in the future without breaking tests, |
|
677 | * means we can change the text in the future without breaking tests, | |
677 | * even across changesets without a recompile. Second, its presence |
|
678 | * even across changesets without a recompile. Second, its presence | |
678 | * can be used to determine whether the version-checking logic is |
|
679 | * can be used to determine whether the version-checking logic is | |
679 | * present, which also helps in testing across changesets without a |
|
680 | * present, which also helps in testing across changesets without a | |
680 | * recompile. Note that this means the pure-Python version of parsers |
|
681 | * recompile. Note that this means the pure-Python version of parsers | |
681 | * should not have this module constant. */ |
|
682 | * should not have this module constant. */ | |
682 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); |
|
683 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); | |
683 |
|
684 | |||
684 | dirs_module_init(mod); |
|
685 | dirs_module_init(mod); | |
685 | manifest_module_init(mod); |
|
686 | manifest_module_init(mod); | |
686 | revlog_module_init(mod); |
|
687 | revlog_module_init(mod); | |
687 |
|
688 | |||
688 | if (PyType_Ready(&dirstateTupleType) < 0) { |
|
689 | if (PyType_Ready(&dirstateTupleType) < 0) { | |
689 | return; |
|
690 | return; | |
690 | } |
|
691 | } | |
691 | Py_INCREF(&dirstateTupleType); |
|
692 | Py_INCREF(&dirstateTupleType); | |
692 | PyModule_AddObject(mod, "dirstatetuple", |
|
693 | PyModule_AddObject(mod, "dirstatetuple", | |
693 | (PyObject *)&dirstateTupleType); |
|
694 | (PyObject *)&dirstateTupleType); | |
694 | } |
|
695 | } | |
695 |
|
696 | |||
696 | static int check_python_version(void) |
|
697 | static int check_python_version(void) | |
697 | { |
|
698 | { | |
698 | PyObject *sys = PyImport_ImportModule("sys"), *ver; |
|
699 | PyObject *sys = PyImport_ImportModule("sys"), *ver; | |
699 | long hexversion; |
|
700 | long hexversion; | |
700 | if (!sys) { |
|
701 | if (!sys) { | |
701 | return -1; |
|
702 | return -1; | |
702 | } |
|
703 | } | |
703 | ver = PyObject_GetAttrString(sys, "hexversion"); |
|
704 | ver = PyObject_GetAttrString(sys, "hexversion"); | |
704 | Py_DECREF(sys); |
|
705 | Py_DECREF(sys); | |
705 | if (!ver) { |
|
706 | if (!ver) { | |
706 | return -1; |
|
707 | return -1; | |
707 | } |
|
708 | } | |
708 | hexversion = PyInt_AsLong(ver); |
|
709 | hexversion = PyInt_AsLong(ver); | |
709 | Py_DECREF(ver); |
|
710 | Py_DECREF(ver); | |
710 | /* sys.hexversion is a 32-bit number by default, so the -1 case |
|
711 | /* sys.hexversion is a 32-bit number by default, so the -1 case | |
711 | * should only occur in unusual circumstances (e.g. if sys.hexversion |
|
712 | * should only occur in unusual circumstances (e.g. if sys.hexversion | |
712 | * is manually set to an invalid value). */ |
|
713 | * is manually set to an invalid value). */ | |
713 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { |
|
714 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { | |
714 | PyErr_Format(PyExc_ImportError, |
|
715 | PyErr_Format(PyExc_ImportError, | |
715 | "%s: The Mercurial extension " |
|
716 | "%s: The Mercurial extension " | |
716 | "modules were compiled with Python " PY_VERSION |
|
717 | "modules were compiled with Python " PY_VERSION | |
717 | ", but " |
|
718 | ", but " | |
718 | "Mercurial is currently using Python with " |
|
719 | "Mercurial is currently using Python with " | |
719 | "sys.hexversion=%ld: " |
|
720 | "sys.hexversion=%ld: " | |
720 | "Python %s\n at: %s", |
|
721 | "Python %s\n at: %s", | |
721 | versionerrortext, hexversion, Py_GetVersion(), |
|
722 | versionerrortext, hexversion, Py_GetVersion(), | |
722 | Py_GetProgramFullPath()); |
|
723 | Py_GetProgramFullPath()); | |
723 | return -1; |
|
724 | return -1; | |
724 | } |
|
725 | } | |
725 | return 0; |
|
726 | return 0; | |
726 | } |
|
727 | } | |
727 |
|
728 | |||
728 | #ifdef IS_PY3K |
|
729 | #ifdef IS_PY3K | |
729 | static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers", |
|
730 | static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers", | |
730 | parsers_doc, -1, methods}; |
|
731 | parsers_doc, -1, methods}; | |
731 |
|
732 | |||
732 | PyMODINIT_FUNC PyInit_parsers(void) |
|
733 | PyMODINIT_FUNC PyInit_parsers(void) | |
733 | { |
|
734 | { | |
734 | PyObject *mod; |
|
735 | PyObject *mod; | |
735 |
|
736 | |||
736 | if (check_python_version() == -1) |
|
737 | if (check_python_version() == -1) | |
737 | return NULL; |
|
738 | return NULL; | |
738 | mod = PyModule_Create(&parsers_module); |
|
739 | mod = PyModule_Create(&parsers_module); | |
739 | module_init(mod); |
|
740 | module_init(mod); | |
740 | return mod; |
|
741 | return mod; | |
741 | } |
|
742 | } | |
742 | #else |
|
743 | #else | |
743 | PyMODINIT_FUNC initparsers(void) |
|
744 | PyMODINIT_FUNC initparsers(void) | |
744 | { |
|
745 | { | |
745 | PyObject *mod; |
|
746 | PyObject *mod; | |
746 |
|
747 | |||
747 | if (check_python_version() == -1) { |
|
748 | if (check_python_version() == -1) { | |
748 | return; |
|
749 | return; | |
749 | } |
|
750 | } | |
750 | mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
751 | mod = Py_InitModule3("parsers", methods, parsers_doc); | |
751 | module_init(mod); |
|
752 | module_init(mod); | |
752 | } |
|
753 | } | |
753 | #endif |
|
754 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now