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