##// END OF EJS Templates
cext: unconditionally use PyLong_FromLong()...
Gregory Szorc -
r49667:9b0f1734 default
parent child Browse files
Show More
@@ -1,330 +1,326 b''
1 /*
1 /*
2 dirs.c - dynamic directory diddling for dirstates
2 dirs.c - dynamic directory diddling for dirstates
3
3
4 Copyright 2013 Facebook
4 Copyright 2013 Facebook
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 #define PY_SSIZE_T_CLEAN
11 #include <Python.h>
11 #include <Python.h>
12 #include <string.h>
12 #include <string.h>
13
13
14 #include "util.h"
14 #include "util.h"
15
15
16 #ifdef IS_PY3K
16 #ifdef IS_PY3K
17 #define PYLONG_VALUE(o) ((PyLongObject *)o)->ob_digit[0]
17 #define PYLONG_VALUE(o) ((PyLongObject *)o)->ob_digit[0]
18 #else
18 #else
19 #define PYLONG_VALUE(o) PyInt_AS_LONG(o)
19 #define PYLONG_VALUE(o) PyInt_AS_LONG(o)
20 #endif
20 #endif
21
21
22 /*
22 /*
23 * This is a multiset of directory names, built from the files that
23 * This is a multiset of directory names, built from the files that
24 * appear in a dirstate or manifest.
24 * appear in a dirstate or manifest.
25 *
25 *
26 * A few implementation notes:
26 * A few implementation notes:
27 *
27 *
28 * We modify Python integers for refcounting, but those integers are
28 * We modify Python integers for refcounting, but those integers are
29 * never visible to Python code.
29 * never visible to Python code.
30 */
30 */
31 /* clang-format off */
31 /* clang-format off */
32 typedef struct {
32 typedef struct {
33 PyObject_HEAD
33 PyObject_HEAD
34 PyObject *dict;
34 PyObject *dict;
35 } dirsObject;
35 } dirsObject;
36 /* clang-format on */
36 /* clang-format on */
37
37
38 static inline Py_ssize_t _finddir(const char *path, Py_ssize_t pos)
38 static inline Py_ssize_t _finddir(const char *path, Py_ssize_t pos)
39 {
39 {
40 while (pos != -1) {
40 while (pos != -1) {
41 if (path[pos] == '/')
41 if (path[pos] == '/')
42 break;
42 break;
43 pos -= 1;
43 pos -= 1;
44 }
44 }
45 if (pos == -1) {
45 if (pos == -1) {
46 return 0;
46 return 0;
47 }
47 }
48
48
49 return pos;
49 return pos;
50 }
50 }
51
51
52 /* Mercurial will fail to run on directory hierarchies deeper than
52 /* Mercurial will fail to run on directory hierarchies deeper than
53 * this constant, so we should try and keep this constant as big as
53 * this constant, so we should try and keep this constant as big as
54 * possible.
54 * possible.
55 */
55 */
56 #define MAX_DIRS_DEPTH 2048
56 #define MAX_DIRS_DEPTH 2048
57
57
58 static int _addpath(PyObject *dirs, PyObject *path)
58 static int _addpath(PyObject *dirs, PyObject *path)
59 {
59 {
60 const char *cpath = PyBytes_AS_STRING(path);
60 const char *cpath = PyBytes_AS_STRING(path);
61 Py_ssize_t pos = PyBytes_GET_SIZE(path);
61 Py_ssize_t pos = PyBytes_GET_SIZE(path);
62 PyObject *key = NULL;
62 PyObject *key = NULL;
63 int ret = -1;
63 int ret = -1;
64 size_t num_slashes = 0;
64 size_t num_slashes = 0;
65
65
66 /* This loop is super critical for performance. That's why we inline
66 /* This loop is super critical for performance. That's why we inline
67 * access to Python structs instead of going through a supported API.
67 * access to Python structs instead of going through a supported API.
68 * The implementation, therefore, is heavily dependent on CPython
68 * The implementation, therefore, is heavily dependent on CPython
69 * implementation details. We also commit violations of the Python
69 * implementation details. We also commit violations of the Python
70 * "protocol" such as mutating immutable objects. But since we only
70 * "protocol" such as mutating immutable objects. But since we only
71 * mutate objects created in this function or in other well-defined
71 * mutate objects created in this function or in other well-defined
72 * locations, the references are known so these violations should go
72 * locations, the references are known so these violations should go
73 * unnoticed. */
73 * unnoticed. */
74 while ((pos = _finddir(cpath, pos - 1)) != -1) {
74 while ((pos = _finddir(cpath, pos - 1)) != -1) {
75 PyObject *val;
75 PyObject *val;
76 ++num_slashes;
76 ++num_slashes;
77 if (num_slashes > MAX_DIRS_DEPTH) {
77 if (num_slashes > MAX_DIRS_DEPTH) {
78 PyErr_SetString(PyExc_ValueError,
78 PyErr_SetString(PyExc_ValueError,
79 "Directory hierarchy too deep.");
79 "Directory hierarchy too deep.");
80 goto bail;
80 goto bail;
81 }
81 }
82
82
83 /* Sniff for trailing slashes, a marker of an invalid input. */
83 /* Sniff for trailing slashes, a marker of an invalid input. */
84 if (pos > 0 && cpath[pos - 1] == '/') {
84 if (pos > 0 && cpath[pos - 1] == '/') {
85 PyErr_SetString(
85 PyErr_SetString(
86 PyExc_ValueError,
86 PyExc_ValueError,
87 "found invalid consecutive slashes in path");
87 "found invalid consecutive slashes in path");
88 goto bail;
88 goto bail;
89 }
89 }
90
90
91 key = PyBytes_FromStringAndSize(cpath, pos);
91 key = PyBytes_FromStringAndSize(cpath, pos);
92 if (key == NULL)
92 if (key == NULL)
93 goto bail;
93 goto bail;
94
94
95 val = PyDict_GetItem(dirs, key);
95 val = PyDict_GetItem(dirs, key);
96 if (val != NULL) {
96 if (val != NULL) {
97 PYLONG_VALUE(val) += 1;
97 PYLONG_VALUE(val) += 1;
98 Py_CLEAR(key);
98 Py_CLEAR(key);
99 break;
99 break;
100 }
100 }
101
101
102 /* Force Python to not reuse a small shared int. */
102 /* Force Python to not reuse a small shared int. */
103 #ifdef IS_PY3K
104 val = PyLong_FromLong(0x1eadbeef);
103 val = PyLong_FromLong(0x1eadbeef);
105 #else
106 val = PyInt_FromLong(0x1eadbeef);
107 #endif
108
104
109 if (val == NULL)
105 if (val == NULL)
110 goto bail;
106 goto bail;
111
107
112 PYLONG_VALUE(val) = 1;
108 PYLONG_VALUE(val) = 1;
113 ret = PyDict_SetItem(dirs, key, val);
109 ret = PyDict_SetItem(dirs, key, val);
114 Py_DECREF(val);
110 Py_DECREF(val);
115 if (ret == -1)
111 if (ret == -1)
116 goto bail;
112 goto bail;
117 Py_CLEAR(key);
113 Py_CLEAR(key);
118 }
114 }
119 ret = 0;
115 ret = 0;
120
116
121 bail:
117 bail:
122 Py_XDECREF(key);
118 Py_XDECREF(key);
123
119
124 return ret;
120 return ret;
125 }
121 }
126
122
127 static int _delpath(PyObject *dirs, PyObject *path)
123 static int _delpath(PyObject *dirs, PyObject *path)
128 {
124 {
129 char *cpath = PyBytes_AS_STRING(path);
125 char *cpath = PyBytes_AS_STRING(path);
130 Py_ssize_t pos = PyBytes_GET_SIZE(path);
126 Py_ssize_t pos = PyBytes_GET_SIZE(path);
131 PyObject *key = NULL;
127 PyObject *key = NULL;
132 int ret = -1;
128 int ret = -1;
133
129
134 while ((pos = _finddir(cpath, pos - 1)) != -1) {
130 while ((pos = _finddir(cpath, pos - 1)) != -1) {
135 PyObject *val;
131 PyObject *val;
136
132
137 key = PyBytes_FromStringAndSize(cpath, pos);
133 key = PyBytes_FromStringAndSize(cpath, pos);
138
134
139 if (key == NULL)
135 if (key == NULL)
140 goto bail;
136 goto bail;
141
137
142 val = PyDict_GetItem(dirs, key);
138 val = PyDict_GetItem(dirs, key);
143 if (val == NULL) {
139 if (val == NULL) {
144 PyErr_SetString(PyExc_ValueError,
140 PyErr_SetString(PyExc_ValueError,
145 "expected a value, found none");
141 "expected a value, found none");
146 goto bail;
142 goto bail;
147 }
143 }
148
144
149 if (--PYLONG_VALUE(val) <= 0) {
145 if (--PYLONG_VALUE(val) <= 0) {
150 if (PyDict_DelItem(dirs, key) == -1)
146 if (PyDict_DelItem(dirs, key) == -1)
151 goto bail;
147 goto bail;
152 } else
148 } else
153 break;
149 break;
154 Py_CLEAR(key);
150 Py_CLEAR(key);
155 }
151 }
156 ret = 0;
152 ret = 0;
157
153
158 bail:
154 bail:
159 Py_XDECREF(key);
155 Py_XDECREF(key);
160
156
161 return ret;
157 return ret;
162 }
158 }
163
159
164 static int dirs_fromdict(PyObject *dirs, PyObject *source, bool only_tracked)
160 static int dirs_fromdict(PyObject *dirs, PyObject *source, bool only_tracked)
165 {
161 {
166 PyObject *key, *value;
162 PyObject *key, *value;
167 Py_ssize_t pos = 0;
163 Py_ssize_t pos = 0;
168
164
169 while (PyDict_Next(source, &pos, &key, &value)) {
165 while (PyDict_Next(source, &pos, &key, &value)) {
170 if (!PyBytes_Check(key)) {
166 if (!PyBytes_Check(key)) {
171 PyErr_SetString(PyExc_TypeError, "expected string key");
167 PyErr_SetString(PyExc_TypeError, "expected string key");
172 return -1;
168 return -1;
173 }
169 }
174 if (only_tracked) {
170 if (only_tracked) {
175 if (!dirstate_tuple_check(value)) {
171 if (!dirstate_tuple_check(value)) {
176 PyErr_SetString(PyExc_TypeError,
172 PyErr_SetString(PyExc_TypeError,
177 "expected a dirstate tuple");
173 "expected a dirstate tuple");
178 return -1;
174 return -1;
179 }
175 }
180 if (!(((dirstateItemObject *)value)->flags &
176 if (!(((dirstateItemObject *)value)->flags &
181 dirstate_flag_wc_tracked))
177 dirstate_flag_wc_tracked))
182 continue;
178 continue;
183 }
179 }
184
180
185 if (_addpath(dirs, key) == -1)
181 if (_addpath(dirs, key) == -1)
186 return -1;
182 return -1;
187 }
183 }
188
184
189 return 0;
185 return 0;
190 }
186 }
191
187
192 static int dirs_fromiter(PyObject *dirs, PyObject *source)
188 static int dirs_fromiter(PyObject *dirs, PyObject *source)
193 {
189 {
194 PyObject *iter, *item = NULL;
190 PyObject *iter, *item = NULL;
195 int ret;
191 int ret;
196
192
197 iter = PyObject_GetIter(source);
193 iter = PyObject_GetIter(source);
198 if (iter == NULL)
194 if (iter == NULL)
199 return -1;
195 return -1;
200
196
201 while ((item = PyIter_Next(iter)) != NULL) {
197 while ((item = PyIter_Next(iter)) != NULL) {
202 if (!PyBytes_Check(item)) {
198 if (!PyBytes_Check(item)) {
203 PyErr_SetString(PyExc_TypeError, "expected string");
199 PyErr_SetString(PyExc_TypeError, "expected string");
204 break;
200 break;
205 }
201 }
206
202
207 if (_addpath(dirs, item) == -1)
203 if (_addpath(dirs, item) == -1)
208 break;
204 break;
209 Py_CLEAR(item);
205 Py_CLEAR(item);
210 }
206 }
211
207
212 ret = PyErr_Occurred() ? -1 : 0;
208 ret = PyErr_Occurred() ? -1 : 0;
213 Py_DECREF(iter);
209 Py_DECREF(iter);
214 Py_XDECREF(item);
210 Py_XDECREF(item);
215 return ret;
211 return ret;
216 }
212 }
217
213
218 /*
214 /*
219 * Calculate a refcounted set of directory names for the files in a
215 * Calculate a refcounted set of directory names for the files in a
220 * dirstate.
216 * dirstate.
221 */
217 */
222 static int dirs_init(dirsObject *self, PyObject *args, PyObject *kwargs)
218 static int dirs_init(dirsObject *self, PyObject *args, PyObject *kwargs)
223 {
219 {
224 PyObject *dirs = NULL, *source = NULL;
220 PyObject *dirs = NULL, *source = NULL;
225 int only_tracked = 0;
221 int only_tracked = 0;
226 int ret = -1;
222 int ret = -1;
227 static char *keywords_name[] = {"map", "only_tracked", NULL};
223 static char *keywords_name[] = {"map", "only_tracked", NULL};
228
224
229 self->dict = NULL;
225 self->dict = NULL;
230
226
231 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:__init__",
227 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:__init__",
232 keywords_name, &source, &only_tracked))
228 keywords_name, &source, &only_tracked))
233 return -1;
229 return -1;
234
230
235 dirs = PyDict_New();
231 dirs = PyDict_New();
236
232
237 if (dirs == NULL)
233 if (dirs == NULL)
238 return -1;
234 return -1;
239
235
240 if (source == NULL)
236 if (source == NULL)
241 ret = 0;
237 ret = 0;
242 else if (PyDict_Check(source))
238 else if (PyDict_Check(source))
243 ret = dirs_fromdict(dirs, source, (bool)only_tracked);
239 ret = dirs_fromdict(dirs, source, (bool)only_tracked);
244 else if (only_tracked)
240 else if (only_tracked)
245 PyErr_SetString(PyExc_ValueError,
241 PyErr_SetString(PyExc_ValueError,
246 "`only_tracked` is only supported "
242 "`only_tracked` is only supported "
247 "with a dict source");
243 "with a dict source");
248 else
244 else
249 ret = dirs_fromiter(dirs, source);
245 ret = dirs_fromiter(dirs, source);
250
246
251 if (ret == -1)
247 if (ret == -1)
252 Py_XDECREF(dirs);
248 Py_XDECREF(dirs);
253 else
249 else
254 self->dict = dirs;
250 self->dict = dirs;
255
251
256 return ret;
252 return ret;
257 }
253 }
258
254
259 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
255 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
260 {
256 {
261 PyObject *path;
257 PyObject *path;
262
258
263 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
259 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
264 return NULL;
260 return NULL;
265
261
266 if (_addpath(self->dict, path) == -1)
262 if (_addpath(self->dict, path) == -1)
267 return NULL;
263 return NULL;
268
264
269 Py_RETURN_NONE;
265 Py_RETURN_NONE;
270 }
266 }
271
267
272 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
268 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
273 {
269 {
274 PyObject *path;
270 PyObject *path;
275
271
276 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
272 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
277 return NULL;
273 return NULL;
278
274
279 if (_delpath(self->dict, path) == -1)
275 if (_delpath(self->dict, path) == -1)
280 return NULL;
276 return NULL;
281
277
282 Py_RETURN_NONE;
278 Py_RETURN_NONE;
283 }
279 }
284
280
285 static int dirs_contains(dirsObject *self, PyObject *value)
281 static int dirs_contains(dirsObject *self, PyObject *value)
286 {
282 {
287 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
283 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
288 }
284 }
289
285
290 static void dirs_dealloc(dirsObject *self)
286 static void dirs_dealloc(dirsObject *self)
291 {
287 {
292 Py_XDECREF(self->dict);
288 Py_XDECREF(self->dict);
293 PyObject_Del(self);
289 PyObject_Del(self);
294 }
290 }
295
291
296 static PyObject *dirs_iter(dirsObject *self)
292 static PyObject *dirs_iter(dirsObject *self)
297 {
293 {
298 return PyObject_GetIter(self->dict);
294 return PyObject_GetIter(self->dict);
299 }
295 }
300
296
301 static PySequenceMethods dirs_sequence_methods;
297 static PySequenceMethods dirs_sequence_methods;
302
298
303 static PyMethodDef dirs_methods[] = {
299 static PyMethodDef dirs_methods[] = {
304 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
300 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
305 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
301 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
306 {NULL} /* Sentinel */
302 {NULL} /* Sentinel */
307 };
303 };
308
304
309 static PyTypeObject dirsType = {PyVarObject_HEAD_INIT(NULL, 0)};
305 static PyTypeObject dirsType = {PyVarObject_HEAD_INIT(NULL, 0)};
310
306
311 void dirs_module_init(PyObject *mod)
307 void dirs_module_init(PyObject *mod)
312 {
308 {
313 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
309 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
314 dirsType.tp_name = "parsers.dirs";
310 dirsType.tp_name = "parsers.dirs";
315 dirsType.tp_new = PyType_GenericNew;
311 dirsType.tp_new = PyType_GenericNew;
316 dirsType.tp_basicsize = sizeof(dirsObject);
312 dirsType.tp_basicsize = sizeof(dirsObject);
317 dirsType.tp_dealloc = (destructor)dirs_dealloc;
313 dirsType.tp_dealloc = (destructor)dirs_dealloc;
318 dirsType.tp_as_sequence = &dirs_sequence_methods;
314 dirsType.tp_as_sequence = &dirs_sequence_methods;
319 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
315 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
320 dirsType.tp_doc = "dirs";
316 dirsType.tp_doc = "dirs";
321 dirsType.tp_iter = (getiterfunc)dirs_iter;
317 dirsType.tp_iter = (getiterfunc)dirs_iter;
322 dirsType.tp_methods = dirs_methods;
318 dirsType.tp_methods = dirs_methods;
323 dirsType.tp_init = (initproc)dirs_init;
319 dirsType.tp_init = (initproc)dirs_init;
324
320
325 if (PyType_Ready(&dirsType) < 0)
321 if (PyType_Ready(&dirsType) < 0)
326 return;
322 return;
327 Py_INCREF(&dirsType);
323 Py_INCREF(&dirsType);
328
324
329 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
325 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
330 }
326 }
General Comments 0
You need to be logged in to leave comments. Login now