##// END OF EJS Templates
dirs: document Py_SIZE weirdness...
Gregory Szorc -
r30159:fb5504d7 default
parent child Browse files
Show More
@@ -1,313 +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,
80 * assigning to what looks like a function seems wrong. */
79 Py_SIZE(key) = pos;
81 Py_SIZE(key) = pos;
80 ((PyBytesObject *)key)->ob_sval[pos] = '\0';
82 ((PyBytesObject *)key)->ob_sval[pos] = '\0';
81
83
82 val = PyDict_GetItem(dirs, key);
84 val = PyDict_GetItem(dirs, key);
83 if (val != NULL) {
85 if (val != NULL) {
84 PYLONG_VALUE(val) += 1;
86 PYLONG_VALUE(val) += 1;
85 break;
87 break;
86 }
88 }
87
89
88 /* Force Python to not reuse a small shared int. */
90 /* Force Python to not reuse a small shared int. */
89 #ifdef IS_PY3K
91 #ifdef IS_PY3K
90 val = PyLong_FromLong(0x1eadbeef);
92 val = PyLong_FromLong(0x1eadbeef);
91 #else
93 #else
92 val = PyInt_FromLong(0x1eadbeef);
94 val = PyInt_FromLong(0x1eadbeef);
93 #endif
95 #endif
94
96
95 if (val == NULL)
97 if (val == NULL)
96 goto bail;
98 goto bail;
97
99
98 PYLONG_VALUE(val) = 1;
100 PYLONG_VALUE(val) = 1;
99 ret = PyDict_SetItem(dirs, key, val);
101 ret = PyDict_SetItem(dirs, key, val);
100 Py_DECREF(val);
102 Py_DECREF(val);
101 if (ret == -1)
103 if (ret == -1)
102 goto bail;
104 goto bail;
103 Py_CLEAR(key);
105 Py_CLEAR(key);
104 }
106 }
105 ret = 0;
107 ret = 0;
106
108
107 bail:
109 bail:
108 Py_XDECREF(key);
110 Py_XDECREF(key);
109
111
110 return ret;
112 return ret;
111 }
113 }
112
114
113 static int _delpath(PyObject *dirs, PyObject *path)
115 static int _delpath(PyObject *dirs, PyObject *path)
114 {
116 {
115 char *cpath = PyBytes_AS_STRING(path);
117 char *cpath = PyBytes_AS_STRING(path);
116 Py_ssize_t pos = PyBytes_GET_SIZE(path);
118 Py_ssize_t pos = PyBytes_GET_SIZE(path);
117 PyObject *key = NULL;
119 PyObject *key = NULL;
118 int ret = -1;
120 int ret = -1;
119
121
120 while ((pos = _finddir(cpath, pos - 1)) != -1) {
122 while ((pos = _finddir(cpath, pos - 1)) != -1) {
121 PyObject *val;
123 PyObject *val;
122
124
123 key = PyBytes_FromStringAndSize(cpath, pos);
125 key = PyBytes_FromStringAndSize(cpath, pos);
124
126
125 if (key == NULL)
127 if (key == NULL)
126 goto bail;
128 goto bail;
127
129
128 val = PyDict_GetItem(dirs, key);
130 val = PyDict_GetItem(dirs, key);
129 if (val == NULL) {
131 if (val == NULL) {
130 PyErr_SetString(PyExc_ValueError,
132 PyErr_SetString(PyExc_ValueError,
131 "expected a value, found none");
133 "expected a value, found none");
132 goto bail;
134 goto bail;
133 }
135 }
134
136
135 if (--PYLONG_VALUE(val) <= 0) {
137 if (--PYLONG_VALUE(val) <= 0) {
136 if (PyDict_DelItem(dirs, key) == -1)
138 if (PyDict_DelItem(dirs, key) == -1)
137 goto bail;
139 goto bail;
138 } else
140 } else
139 break;
141 break;
140 Py_CLEAR(key);
142 Py_CLEAR(key);
141 }
143 }
142 ret = 0;
144 ret = 0;
143
145
144 bail:
146 bail:
145 Py_XDECREF(key);
147 Py_XDECREF(key);
146
148
147 return ret;
149 return ret;
148 }
150 }
149
151
150 static int dirs_fromdict(PyObject *dirs, PyObject *source, char skipchar)
152 static int dirs_fromdict(PyObject *dirs, PyObject *source, char skipchar)
151 {
153 {
152 PyObject *key, *value;
154 PyObject *key, *value;
153 Py_ssize_t pos = 0;
155 Py_ssize_t pos = 0;
154
156
155 while (PyDict_Next(source, &pos, &key, &value)) {
157 while (PyDict_Next(source, &pos, &key, &value)) {
156 if (!PyBytes_Check(key)) {
158 if (!PyBytes_Check(key)) {
157 PyErr_SetString(PyExc_TypeError, "expected string key");
159 PyErr_SetString(PyExc_TypeError, "expected string key");
158 return -1;
160 return -1;
159 }
161 }
160 if (skipchar) {
162 if (skipchar) {
161 if (!dirstate_tuple_check(value)) {
163 if (!dirstate_tuple_check(value)) {
162 PyErr_SetString(PyExc_TypeError,
164 PyErr_SetString(PyExc_TypeError,
163 "expected a dirstate tuple");
165 "expected a dirstate tuple");
164 return -1;
166 return -1;
165 }
167 }
166 if (((dirstateTupleObject *)value)->state == skipchar)
168 if (((dirstateTupleObject *)value)->state == skipchar)
167 continue;
169 continue;
168 }
170 }
169
171
170 if (_addpath(dirs, key) == -1)
172 if (_addpath(dirs, key) == -1)
171 return -1;
173 return -1;
172 }
174 }
173
175
174 return 0;
176 return 0;
175 }
177 }
176
178
177 static int dirs_fromiter(PyObject *dirs, PyObject *source)
179 static int dirs_fromiter(PyObject *dirs, PyObject *source)
178 {
180 {
179 PyObject *iter, *item = NULL;
181 PyObject *iter, *item = NULL;
180 int ret;
182 int ret;
181
183
182 iter = PyObject_GetIter(source);
184 iter = PyObject_GetIter(source);
183 if (iter == NULL)
185 if (iter == NULL)
184 return -1;
186 return -1;
185
187
186 while ((item = PyIter_Next(iter)) != NULL) {
188 while ((item = PyIter_Next(iter)) != NULL) {
187 if (!PyBytes_Check(item)) {
189 if (!PyBytes_Check(item)) {
188 PyErr_SetString(PyExc_TypeError, "expected string");
190 PyErr_SetString(PyExc_TypeError, "expected string");
189 break;
191 break;
190 }
192 }
191
193
192 if (_addpath(dirs, item) == -1)
194 if (_addpath(dirs, item) == -1)
193 break;
195 break;
194 Py_CLEAR(item);
196 Py_CLEAR(item);
195 }
197 }
196
198
197 ret = PyErr_Occurred() ? -1 : 0;
199 ret = PyErr_Occurred() ? -1 : 0;
198 Py_DECREF(iter);
200 Py_DECREF(iter);
199 Py_XDECREF(item);
201 Py_XDECREF(item);
200 return ret;
202 return ret;
201 }
203 }
202
204
203 /*
205 /*
204 * 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
205 * dirstate.
207 * dirstate.
206 */
208 */
207 static int dirs_init(dirsObject *self, PyObject *args)
209 static int dirs_init(dirsObject *self, PyObject *args)
208 {
210 {
209 PyObject *dirs = NULL, *source = NULL;
211 PyObject *dirs = NULL, *source = NULL;
210 char skipchar = 0;
212 char skipchar = 0;
211 int ret = -1;
213 int ret = -1;
212
214
213 self->dict = NULL;
215 self->dict = NULL;
214
216
215 if (!PyArg_ParseTuple(args, "|Oc:__init__", &source, &skipchar))
217 if (!PyArg_ParseTuple(args, "|Oc:__init__", &source, &skipchar))
216 return -1;
218 return -1;
217
219
218 dirs = PyDict_New();
220 dirs = PyDict_New();
219
221
220 if (dirs == NULL)
222 if (dirs == NULL)
221 return -1;
223 return -1;
222
224
223 if (source == NULL)
225 if (source == NULL)
224 ret = 0;
226 ret = 0;
225 else if (PyDict_Check(source))
227 else if (PyDict_Check(source))
226 ret = dirs_fromdict(dirs, source, skipchar);
228 ret = dirs_fromdict(dirs, source, skipchar);
227 else if (skipchar)
229 else if (skipchar)
228 PyErr_SetString(PyExc_ValueError,
230 PyErr_SetString(PyExc_ValueError,
229 "skip character is only supported "
231 "skip character is only supported "
230 "with a dict source");
232 "with a dict source");
231 else
233 else
232 ret = dirs_fromiter(dirs, source);
234 ret = dirs_fromiter(dirs, source);
233
235
234 if (ret == -1)
236 if (ret == -1)
235 Py_XDECREF(dirs);
237 Py_XDECREF(dirs);
236 else
238 else
237 self->dict = dirs;
239 self->dict = dirs;
238
240
239 return ret;
241 return ret;
240 }
242 }
241
243
242 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
244 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
243 {
245 {
244 PyObject *path;
246 PyObject *path;
245
247
246 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
248 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
247 return NULL;
249 return NULL;
248
250
249 if (_addpath(self->dict, path) == -1)
251 if (_addpath(self->dict, path) == -1)
250 return NULL;
252 return NULL;
251
253
252 Py_RETURN_NONE;
254 Py_RETURN_NONE;
253 }
255 }
254
256
255 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
257 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
256 {
258 {
257 PyObject *path;
259 PyObject *path;
258
260
259 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
261 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
260 return NULL;
262 return NULL;
261
263
262 if (_delpath(self->dict, path) == -1)
264 if (_delpath(self->dict, path) == -1)
263 return NULL;
265 return NULL;
264
266
265 Py_RETURN_NONE;
267 Py_RETURN_NONE;
266 }
268 }
267
269
268 static int dirs_contains(dirsObject *self, PyObject *value)
270 static int dirs_contains(dirsObject *self, PyObject *value)
269 {
271 {
270 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
272 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
271 }
273 }
272
274
273 static void dirs_dealloc(dirsObject *self)
275 static void dirs_dealloc(dirsObject *self)
274 {
276 {
275 Py_XDECREF(self->dict);
277 Py_XDECREF(self->dict);
276 PyObject_Del(self);
278 PyObject_Del(self);
277 }
279 }
278
280
279 static PyObject *dirs_iter(dirsObject *self)
281 static PyObject *dirs_iter(dirsObject *self)
280 {
282 {
281 return PyObject_GetIter(self->dict);
283 return PyObject_GetIter(self->dict);
282 }
284 }
283
285
284 static PySequenceMethods dirs_sequence_methods;
286 static PySequenceMethods dirs_sequence_methods;
285
287
286 static PyMethodDef dirs_methods[] = {
288 static PyMethodDef dirs_methods[] = {
287 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
289 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
288 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
290 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
289 {NULL} /* Sentinel */
291 {NULL} /* Sentinel */
290 };
292 };
291
293
292 static PyTypeObject dirsType = { PyObject_HEAD_INIT(NULL) };
294 static PyTypeObject dirsType = { PyObject_HEAD_INIT(NULL) };
293
295
294 void dirs_module_init(PyObject *mod)
296 void dirs_module_init(PyObject *mod)
295 {
297 {
296 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
298 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
297 dirsType.tp_name = "parsers.dirs";
299 dirsType.tp_name = "parsers.dirs";
298 dirsType.tp_new = PyType_GenericNew;
300 dirsType.tp_new = PyType_GenericNew;
299 dirsType.tp_basicsize = sizeof(dirsObject);
301 dirsType.tp_basicsize = sizeof(dirsObject);
300 dirsType.tp_dealloc = (destructor)dirs_dealloc;
302 dirsType.tp_dealloc = (destructor)dirs_dealloc;
301 dirsType.tp_as_sequence = &dirs_sequence_methods;
303 dirsType.tp_as_sequence = &dirs_sequence_methods;
302 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
304 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
303 dirsType.tp_doc = "dirs";
305 dirsType.tp_doc = "dirs";
304 dirsType.tp_iter = (getiterfunc)dirs_iter;
306 dirsType.tp_iter = (getiterfunc)dirs_iter;
305 dirsType.tp_methods = dirs_methods;
307 dirsType.tp_methods = dirs_methods;
306 dirsType.tp_init = (initproc)dirs_init;
308 dirsType.tp_init = (initproc)dirs_init;
307
309
308 if (PyType_Ready(&dirsType) < 0)
310 if (PyType_Ready(&dirsType) < 0)
309 return;
311 return;
310 Py_INCREF(&dirsType);
312 Py_INCREF(&dirsType);
311
313
312 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
314 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
313 }
315 }
General Comments 0
You need to be logged in to leave comments. Login now