##// END OF EJS Templates
dirs: give formatting oversight to clang-format...
Augie Fackler -
r43507:ea62d7b0 default
parent child Browse files
Show More
@@ -1,103 +1,102 b''
1 # Files that just need to be migrated to the formatter.
1 # Files that just need to be migrated to the formatter.
2 # Do not add new files here!
2 # Do not add new files here!
3 mercurial/cext/dirs.c
4 mercurial/cext/manifest.c
3 mercurial/cext/manifest.c
5 mercurial/cext/osutil.c
4 mercurial/cext/osutil.c
6 # Vendored code that we should never format:
5 # Vendored code that we should never format:
7 contrib/python-zstandard/c-ext/bufferutil.c
6 contrib/python-zstandard/c-ext/bufferutil.c
8 contrib/python-zstandard/c-ext/compressionchunker.c
7 contrib/python-zstandard/c-ext/compressionchunker.c
9 contrib/python-zstandard/c-ext/compressiondict.c
8 contrib/python-zstandard/c-ext/compressiondict.c
10 contrib/python-zstandard/c-ext/compressionparams.c
9 contrib/python-zstandard/c-ext/compressionparams.c
11 contrib/python-zstandard/c-ext/compressionreader.c
10 contrib/python-zstandard/c-ext/compressionreader.c
12 contrib/python-zstandard/c-ext/compressionwriter.c
11 contrib/python-zstandard/c-ext/compressionwriter.c
13 contrib/python-zstandard/c-ext/compressobj.c
12 contrib/python-zstandard/c-ext/compressobj.c
14 contrib/python-zstandard/c-ext/compressor.c
13 contrib/python-zstandard/c-ext/compressor.c
15 contrib/python-zstandard/c-ext/compressoriterator.c
14 contrib/python-zstandard/c-ext/compressoriterator.c
16 contrib/python-zstandard/c-ext/constants.c
15 contrib/python-zstandard/c-ext/constants.c
17 contrib/python-zstandard/c-ext/decompressionreader.c
16 contrib/python-zstandard/c-ext/decompressionreader.c
18 contrib/python-zstandard/c-ext/decompressionwriter.c
17 contrib/python-zstandard/c-ext/decompressionwriter.c
19 contrib/python-zstandard/c-ext/decompressobj.c
18 contrib/python-zstandard/c-ext/decompressobj.c
20 contrib/python-zstandard/c-ext/decompressor.c
19 contrib/python-zstandard/c-ext/decompressor.c
21 contrib/python-zstandard/c-ext/decompressoriterator.c
20 contrib/python-zstandard/c-ext/decompressoriterator.c
22 contrib/python-zstandard/c-ext/frameparams.c
21 contrib/python-zstandard/c-ext/frameparams.c
23 contrib/python-zstandard/c-ext/python-zstandard.h
22 contrib/python-zstandard/c-ext/python-zstandard.h
24 contrib/python-zstandard/zstd.c
23 contrib/python-zstandard/zstd.c
25 contrib/python-zstandard/zstd/common/bitstream.h
24 contrib/python-zstandard/zstd/common/bitstream.h
26 contrib/python-zstandard/zstd/common/compiler.h
25 contrib/python-zstandard/zstd/common/compiler.h
27 contrib/python-zstandard/zstd/common/cpu.h
26 contrib/python-zstandard/zstd/common/cpu.h
28 contrib/python-zstandard/zstd/common/debug.c
27 contrib/python-zstandard/zstd/common/debug.c
29 contrib/python-zstandard/zstd/common/debug.h
28 contrib/python-zstandard/zstd/common/debug.h
30 contrib/python-zstandard/zstd/common/entropy_common.c
29 contrib/python-zstandard/zstd/common/entropy_common.c
31 contrib/python-zstandard/zstd/common/error_private.c
30 contrib/python-zstandard/zstd/common/error_private.c
32 contrib/python-zstandard/zstd/common/error_private.h
31 contrib/python-zstandard/zstd/common/error_private.h
33 contrib/python-zstandard/zstd/common/fse_decompress.c
32 contrib/python-zstandard/zstd/common/fse_decompress.c
34 contrib/python-zstandard/zstd/common/fse.h
33 contrib/python-zstandard/zstd/common/fse.h
35 contrib/python-zstandard/zstd/common/huf.h
34 contrib/python-zstandard/zstd/common/huf.h
36 contrib/python-zstandard/zstd/common/mem.h
35 contrib/python-zstandard/zstd/common/mem.h
37 contrib/python-zstandard/zstd/common/pool.c
36 contrib/python-zstandard/zstd/common/pool.c
38 contrib/python-zstandard/zstd/common/pool.h
37 contrib/python-zstandard/zstd/common/pool.h
39 contrib/python-zstandard/zstd/common/threading.c
38 contrib/python-zstandard/zstd/common/threading.c
40 contrib/python-zstandard/zstd/common/threading.h
39 contrib/python-zstandard/zstd/common/threading.h
41 contrib/python-zstandard/zstd/common/xxhash.c
40 contrib/python-zstandard/zstd/common/xxhash.c
42 contrib/python-zstandard/zstd/common/xxhash.h
41 contrib/python-zstandard/zstd/common/xxhash.h
43 contrib/python-zstandard/zstd/common/zstd_common.c
42 contrib/python-zstandard/zstd/common/zstd_common.c
44 contrib/python-zstandard/zstd/common/zstd_errors.h
43 contrib/python-zstandard/zstd/common/zstd_errors.h
45 contrib/python-zstandard/zstd/common/zstd_internal.h
44 contrib/python-zstandard/zstd/common/zstd_internal.h
46 contrib/python-zstandard/zstd/compress/fse_compress.c
45 contrib/python-zstandard/zstd/compress/fse_compress.c
47 contrib/python-zstandard/zstd/compress/hist.c
46 contrib/python-zstandard/zstd/compress/hist.c
48 contrib/python-zstandard/zstd/compress/hist.h
47 contrib/python-zstandard/zstd/compress/hist.h
49 contrib/python-zstandard/zstd/compress/huf_compress.c
48 contrib/python-zstandard/zstd/compress/huf_compress.c
50 contrib/python-zstandard/zstd/compress/zstd_compress.c
49 contrib/python-zstandard/zstd/compress/zstd_compress.c
51 contrib/python-zstandard/zstd/compress/zstd_compress_internal.h
50 contrib/python-zstandard/zstd/compress/zstd_compress_internal.h
52 contrib/python-zstandard/zstd/compress/zstd_compress_literals.c
51 contrib/python-zstandard/zstd/compress/zstd_compress_literals.c
53 contrib/python-zstandard/zstd/compress/zstd_compress_literals.h
52 contrib/python-zstandard/zstd/compress/zstd_compress_literals.h
54 contrib/python-zstandard/zstd/compress/zstd_compress_sequences.c
53 contrib/python-zstandard/zstd/compress/zstd_compress_sequences.c
55 contrib/python-zstandard/zstd/compress/zstd_compress_sequences.h
54 contrib/python-zstandard/zstd/compress/zstd_compress_sequences.h
56 contrib/python-zstandard/zstd/compress/zstd_double_fast.c
55 contrib/python-zstandard/zstd/compress/zstd_double_fast.c
57 contrib/python-zstandard/zstd/compress/zstd_double_fast.h
56 contrib/python-zstandard/zstd/compress/zstd_double_fast.h
58 contrib/python-zstandard/zstd/compress/zstd_fast.c
57 contrib/python-zstandard/zstd/compress/zstd_fast.c
59 contrib/python-zstandard/zstd/compress/zstd_fast.h
58 contrib/python-zstandard/zstd/compress/zstd_fast.h
60 contrib/python-zstandard/zstd/compress/zstd_lazy.c
59 contrib/python-zstandard/zstd/compress/zstd_lazy.c
61 contrib/python-zstandard/zstd/compress/zstd_lazy.h
60 contrib/python-zstandard/zstd/compress/zstd_lazy.h
62 contrib/python-zstandard/zstd/compress/zstd_ldm.c
61 contrib/python-zstandard/zstd/compress/zstd_ldm.c
63 contrib/python-zstandard/zstd/compress/zstd_ldm.h
62 contrib/python-zstandard/zstd/compress/zstd_ldm.h
64 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
63 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
65 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
64 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
66 contrib/python-zstandard/zstd/compress/zstd_opt.c
65 contrib/python-zstandard/zstd/compress/zstd_opt.c
67 contrib/python-zstandard/zstd/compress/zstd_opt.h
66 contrib/python-zstandard/zstd/compress/zstd_opt.h
68 contrib/python-zstandard/zstd/decompress/huf_decompress.c
67 contrib/python-zstandard/zstd/decompress/huf_decompress.c
69 contrib/python-zstandard/zstd/decompress/zstd_ddict.c
68 contrib/python-zstandard/zstd/decompress/zstd_ddict.c
70 contrib/python-zstandard/zstd/decompress/zstd_ddict.h
69 contrib/python-zstandard/zstd/decompress/zstd_ddict.h
71 contrib/python-zstandard/zstd/decompress/zstd_decompress_block.c
70 contrib/python-zstandard/zstd/decompress/zstd_decompress_block.c
72 contrib/python-zstandard/zstd/decompress/zstd_decompress_block.h
71 contrib/python-zstandard/zstd/decompress/zstd_decompress_block.h
73 contrib/python-zstandard/zstd/decompress/zstd_decompress_internal.h
72 contrib/python-zstandard/zstd/decompress/zstd_decompress_internal.h
74 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
73 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
75 contrib/python-zstandard/zstd/deprecated/zbuff_common.c
74 contrib/python-zstandard/zstd/deprecated/zbuff_common.c
76 contrib/python-zstandard/zstd/deprecated/zbuff_compress.c
75 contrib/python-zstandard/zstd/deprecated/zbuff_compress.c
77 contrib/python-zstandard/zstd/deprecated/zbuff_decompress.c
76 contrib/python-zstandard/zstd/deprecated/zbuff_decompress.c
78 contrib/python-zstandard/zstd/deprecated/zbuff.h
77 contrib/python-zstandard/zstd/deprecated/zbuff.h
79 contrib/python-zstandard/zstd/dictBuilder/cover.c
78 contrib/python-zstandard/zstd/dictBuilder/cover.c
80 contrib/python-zstandard/zstd/dictBuilder/cover.h
79 contrib/python-zstandard/zstd/dictBuilder/cover.h
81 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
80 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
82 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
81 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
83 contrib/python-zstandard/zstd/dictBuilder/fastcover.c
82 contrib/python-zstandard/zstd/dictBuilder/fastcover.c
84 contrib/python-zstandard/zstd/dictBuilder/zdict.c
83 contrib/python-zstandard/zstd/dictBuilder/zdict.c
85 contrib/python-zstandard/zstd/dictBuilder/zdict.h
84 contrib/python-zstandard/zstd/dictBuilder/zdict.h
86 contrib/python-zstandard/zstd/zstd.h
85 contrib/python-zstandard/zstd/zstd.h
87 hgext/fsmonitor/pywatchman/bser.c
86 hgext/fsmonitor/pywatchman/bser.c
88 mercurial/thirdparty/xdiff/xdiff.h
87 mercurial/thirdparty/xdiff/xdiff.h
89 mercurial/thirdparty/xdiff/xdiffi.c
88 mercurial/thirdparty/xdiff/xdiffi.c
90 mercurial/thirdparty/xdiff/xdiffi.h
89 mercurial/thirdparty/xdiff/xdiffi.h
91 mercurial/thirdparty/xdiff/xemit.c
90 mercurial/thirdparty/xdiff/xemit.c
92 mercurial/thirdparty/xdiff/xemit.h
91 mercurial/thirdparty/xdiff/xemit.h
93 mercurial/thirdparty/xdiff/xhistogram.c
92 mercurial/thirdparty/xdiff/xhistogram.c
94 mercurial/thirdparty/xdiff/xinclude.h
93 mercurial/thirdparty/xdiff/xinclude.h
95 mercurial/thirdparty/xdiff/xmacros.h
94 mercurial/thirdparty/xdiff/xmacros.h
96 mercurial/thirdparty/xdiff/xmerge.c
95 mercurial/thirdparty/xdiff/xmerge.c
97 mercurial/thirdparty/xdiff/xpatience.c
96 mercurial/thirdparty/xdiff/xpatience.c
98 mercurial/thirdparty/xdiff/xprepare.c
97 mercurial/thirdparty/xdiff/xprepare.c
99 mercurial/thirdparty/xdiff/xprepare.h
98 mercurial/thirdparty/xdiff/xprepare.h
100 mercurial/thirdparty/xdiff/xtypes.h
99 mercurial/thirdparty/xdiff/xtypes.h
101 mercurial/thirdparty/xdiff/xutils.c
100 mercurial/thirdparty/xdiff/xutils.c
102 mercurial/thirdparty/xdiff/xutils.h
101 mercurial/thirdparty/xdiff/xutils.h
103 mercurial/thirdparty/zope/interface/_zope_interface_coptimizations.c
102 mercurial/thirdparty/zope/interface/_zope_interface_coptimizations.c
@@ -1,305 +1,305 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
12
13 #include "util.h"
13 #include "util.h"
14
14
15 #ifdef IS_PY3K
15 #ifdef IS_PY3K
16 #define PYLONG_VALUE(o) ((PyLongObject *)o)->ob_digit[1]
16 #define PYLONG_VALUE(o) ((PyLongObject *)o)->ob_digit[1]
17 #else
17 #else
18 #define PYLONG_VALUE(o) PyInt_AS_LONG(o)
18 #define PYLONG_VALUE(o) PyInt_AS_LONG(o)
19 #endif
19 #endif
20
20
21 /*
21 /*
22 * This is a multiset of directory names, built from the files that
22 * This is a multiset of directory names, built from the files that
23 * appear in a dirstate or manifest.
23 * appear in a dirstate or manifest.
24 *
24 *
25 * A few implementation notes:
25 * A few implementation notes:
26 *
26 *
27 * We modify Python integers for refcounting, but those integers are
27 * We modify Python integers for refcounting, but those integers are
28 * never visible to Python code.
28 * never visible to Python code.
29 */
29 */
30 /* clang-format off */
30 /* clang-format off */
31 typedef struct {
31 typedef struct {
32 PyObject_HEAD
32 PyObject_HEAD
33 PyObject *dict;
33 PyObject *dict;
34 } dirsObject;
34 } dirsObject;
35 /* clang-format on */
35 /* clang-format on */
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 if (pos == -1) {
44 if (pos == -1) {
45 return 0;
45 return 0;
46 }
46 }
47
47
48 return pos;
48 return pos;
49 }
49 }
50
50
51 static int _addpath(PyObject *dirs, PyObject *path)
51 static int _addpath(PyObject *dirs, PyObject *path)
52 {
52 {
53 const char *cpath = PyBytes_AS_STRING(path);
53 const char *cpath = PyBytes_AS_STRING(path);
54 Py_ssize_t pos = PyBytes_GET_SIZE(path);
54 Py_ssize_t pos = PyBytes_GET_SIZE(path);
55 PyObject *key = NULL;
55 PyObject *key = NULL;
56 int ret = -1;
56 int ret = -1;
57
57
58 /* This loop is super critical for performance. That's why we inline
58 /* This loop is super critical for performance. That's why we inline
59 * access to Python structs instead of going through a supported API.
59 * access to Python structs instead of going through a supported API.
60 * The implementation, therefore, is heavily dependent on CPython
60 * The implementation, therefore, is heavily dependent on CPython
61 * implementation details. We also commit violations of the Python
61 * implementation details. We also commit violations of the Python
62 * "protocol" such as mutating immutable objects. But since we only
62 * "protocol" such as mutating immutable objects. But since we only
63 * mutate objects created in this function or in other well-defined
63 * mutate objects created in this function or in other well-defined
64 * locations, the references are known so these violations should go
64 * locations, the references are known so these violations should go
65 * unnoticed. */
65 * unnoticed. */
66 while ((pos = _finddir(cpath, pos - 1)) != -1) {
66 while ((pos = _finddir(cpath, pos - 1)) != -1) {
67 PyObject *val;
67 PyObject *val;
68
68
69 key = PyBytes_FromStringAndSize(cpath, pos);
69 key = PyBytes_FromStringAndSize(cpath, pos);
70 if (key == NULL)
70 if (key == NULL)
71 goto bail;
71 goto bail;
72
72
73 val = PyDict_GetItem(dirs, key);
73 val = PyDict_GetItem(dirs, key);
74 if (val != NULL) {
74 if (val != NULL) {
75 PYLONG_VALUE(val) += 1;
75 PYLONG_VALUE(val) += 1;
76 Py_CLEAR(key);
76 Py_CLEAR(key);
77 break;
77 break;
78 }
78 }
79
79
80 /* Force Python to not reuse a small shared int. */
80 /* Force Python to not reuse a small shared int. */
81 #ifdef IS_PY3K
81 #ifdef IS_PY3K
82 val = PyLong_FromLong(0x1eadbeef);
82 val = PyLong_FromLong(0x1eadbeef);
83 #else
83 #else
84 val = PyInt_FromLong(0x1eadbeef);
84 val = PyInt_FromLong(0x1eadbeef);
85 #endif
85 #endif
86
86
87 if (val == NULL)
87 if (val == NULL)
88 goto bail;
88 goto bail;
89
89
90 PYLONG_VALUE(val) = 1;
90 PYLONG_VALUE(val) = 1;
91 ret = PyDict_SetItem(dirs, key, val);
91 ret = PyDict_SetItem(dirs, key, val);
92 Py_DECREF(val);
92 Py_DECREF(val);
93 if (ret == -1)
93 if (ret == -1)
94 goto bail;
94 goto bail;
95 Py_CLEAR(key);
95 Py_CLEAR(key);
96 }
96 }
97 ret = 0;
97 ret = 0;
98
98
99 bail:
99 bail:
100 Py_XDECREF(key);
100 Py_XDECREF(key);
101
101
102 return ret;
102 return ret;
103 }
103 }
104
104
105 static int _delpath(PyObject *dirs, PyObject *path)
105 static int _delpath(PyObject *dirs, PyObject *path)
106 {
106 {
107 char *cpath = PyBytes_AS_STRING(path);
107 char *cpath = PyBytes_AS_STRING(path);
108 Py_ssize_t pos = PyBytes_GET_SIZE(path);
108 Py_ssize_t pos = PyBytes_GET_SIZE(path);
109 PyObject *key = NULL;
109 PyObject *key = NULL;
110 int ret = -1;
110 int ret = -1;
111
111
112 while ((pos = _finddir(cpath, pos - 1)) != -1) {
112 while ((pos = _finddir(cpath, pos - 1)) != -1) {
113 PyObject *val;
113 PyObject *val;
114
114
115 key = PyBytes_FromStringAndSize(cpath, pos);
115 key = PyBytes_FromStringAndSize(cpath, pos);
116
116
117 if (key == NULL)
117 if (key == NULL)
118 goto bail;
118 goto bail;
119
119
120 val = PyDict_GetItem(dirs, key);
120 val = PyDict_GetItem(dirs, key);
121 if (val == NULL) {
121 if (val == NULL) {
122 PyErr_SetString(PyExc_ValueError,
122 PyErr_SetString(PyExc_ValueError,
123 "expected a value, found none");
123 "expected a value, found none");
124 goto bail;
124 goto bail;
125 }
125 }
126
126
127 if (--PYLONG_VALUE(val) <= 0) {
127 if (--PYLONG_VALUE(val) <= 0) {
128 if (PyDict_DelItem(dirs, key) == -1)
128 if (PyDict_DelItem(dirs, key) == -1)
129 goto bail;
129 goto bail;
130 } else
130 } else
131 break;
131 break;
132 Py_CLEAR(key);
132 Py_CLEAR(key);
133 }
133 }
134 ret = 0;
134 ret = 0;
135
135
136 bail:
136 bail:
137 Py_XDECREF(key);
137 Py_XDECREF(key);
138
138
139 return ret;
139 return ret;
140 }
140 }
141
141
142 static int dirs_fromdict(PyObject *dirs, PyObject *source, char skipchar)
142 static int dirs_fromdict(PyObject *dirs, PyObject *source, char skipchar)
143 {
143 {
144 PyObject *key, *value;
144 PyObject *key, *value;
145 Py_ssize_t pos = 0;
145 Py_ssize_t pos = 0;
146
146
147 while (PyDict_Next(source, &pos, &key, &value)) {
147 while (PyDict_Next(source, &pos, &key, &value)) {
148 if (!PyBytes_Check(key)) {
148 if (!PyBytes_Check(key)) {
149 PyErr_SetString(PyExc_TypeError, "expected string key");
149 PyErr_SetString(PyExc_TypeError, "expected string key");
150 return -1;
150 return -1;
151 }
151 }
152 if (skipchar) {
152 if (skipchar) {
153 if (!dirstate_tuple_check(value)) {
153 if (!dirstate_tuple_check(value)) {
154 PyErr_SetString(PyExc_TypeError,
154 PyErr_SetString(PyExc_TypeError,
155 "expected a dirstate tuple");
155 "expected a dirstate tuple");
156 return -1;
156 return -1;
157 }
157 }
158 if (((dirstateTupleObject *)value)->state == skipchar)
158 if (((dirstateTupleObject *)value)->state == skipchar)
159 continue;
159 continue;
160 }
160 }
161
161
162 if (_addpath(dirs, key) == -1)
162 if (_addpath(dirs, key) == -1)
163 return -1;
163 return -1;
164 }
164 }
165
165
166 return 0;
166 return 0;
167 }
167 }
168
168
169 static int dirs_fromiter(PyObject *dirs, PyObject *source)
169 static int dirs_fromiter(PyObject *dirs, PyObject *source)
170 {
170 {
171 PyObject *iter, *item = NULL;
171 PyObject *iter, *item = NULL;
172 int ret;
172 int ret;
173
173
174 iter = PyObject_GetIter(source);
174 iter = PyObject_GetIter(source);
175 if (iter == NULL)
175 if (iter == NULL)
176 return -1;
176 return -1;
177
177
178 while ((item = PyIter_Next(iter)) != NULL) {
178 while ((item = PyIter_Next(iter)) != NULL) {
179 if (!PyBytes_Check(item)) {
179 if (!PyBytes_Check(item)) {
180 PyErr_SetString(PyExc_TypeError, "expected string");
180 PyErr_SetString(PyExc_TypeError, "expected string");
181 break;
181 break;
182 }
182 }
183
183
184 if (_addpath(dirs, item) == -1)
184 if (_addpath(dirs, item) == -1)
185 break;
185 break;
186 Py_CLEAR(item);
186 Py_CLEAR(item);
187 }
187 }
188
188
189 ret = PyErr_Occurred() ? -1 : 0;
189 ret = PyErr_Occurred() ? -1 : 0;
190 Py_DECREF(iter);
190 Py_DECREF(iter);
191 Py_XDECREF(item);
191 Py_XDECREF(item);
192 return ret;
192 return ret;
193 }
193 }
194
194
195 /*
195 /*
196 * Calculate a refcounted set of directory names for the files in a
196 * Calculate a refcounted set of directory names for the files in a
197 * dirstate.
197 * dirstate.
198 */
198 */
199 static int dirs_init(dirsObject *self, PyObject *args)
199 static int dirs_init(dirsObject *self, PyObject *args)
200 {
200 {
201 PyObject *dirs = NULL, *source = NULL;
201 PyObject *dirs = NULL, *source = NULL;
202 char skipchar = 0;
202 char skipchar = 0;
203 int ret = -1;
203 int ret = -1;
204
204
205 self->dict = NULL;
205 self->dict = NULL;
206
206
207 if (!PyArg_ParseTuple(args, "|Oc:__init__", &source, &skipchar))
207 if (!PyArg_ParseTuple(args, "|Oc:__init__", &source, &skipchar))
208 return -1;
208 return -1;
209
209
210 dirs = PyDict_New();
210 dirs = PyDict_New();
211
211
212 if (dirs == NULL)
212 if (dirs == NULL)
213 return -1;
213 return -1;
214
214
215 if (source == NULL)
215 if (source == NULL)
216 ret = 0;
216 ret = 0;
217 else if (PyDict_Check(source))
217 else if (PyDict_Check(source))
218 ret = dirs_fromdict(dirs, source, skipchar);
218 ret = dirs_fromdict(dirs, source, skipchar);
219 else if (skipchar)
219 else if (skipchar)
220 PyErr_SetString(PyExc_ValueError,
220 PyErr_SetString(PyExc_ValueError,
221 "skip character is only supported "
221 "skip character is only supported "
222 "with a dict source");
222 "with a dict source");
223 else
223 else
224 ret = dirs_fromiter(dirs, source);
224 ret = dirs_fromiter(dirs, source);
225
225
226 if (ret == -1)
226 if (ret == -1)
227 Py_XDECREF(dirs);
227 Py_XDECREF(dirs);
228 else
228 else
229 self->dict = dirs;
229 self->dict = dirs;
230
230
231 return ret;
231 return ret;
232 }
232 }
233
233
234 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
234 PyObject *dirs_addpath(dirsObject *self, PyObject *args)
235 {
235 {
236 PyObject *path;
236 PyObject *path;
237
237
238 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
238 if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path))
239 return NULL;
239 return NULL;
240
240
241 if (_addpath(self->dict, path) == -1)
241 if (_addpath(self->dict, path) == -1)
242 return NULL;
242 return NULL;
243
243
244 Py_RETURN_NONE;
244 Py_RETURN_NONE;
245 }
245 }
246
246
247 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
247 static PyObject *dirs_delpath(dirsObject *self, PyObject *args)
248 {
248 {
249 PyObject *path;
249 PyObject *path;
250
250
251 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
251 if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path))
252 return NULL;
252 return NULL;
253
253
254 if (_delpath(self->dict, path) == -1)
254 if (_delpath(self->dict, path) == -1)
255 return NULL;
255 return NULL;
256
256
257 Py_RETURN_NONE;
257 Py_RETURN_NONE;
258 }
258 }
259
259
260 static int dirs_contains(dirsObject *self, PyObject *value)
260 static int dirs_contains(dirsObject *self, PyObject *value)
261 {
261 {
262 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
262 return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0;
263 }
263 }
264
264
265 static void dirs_dealloc(dirsObject *self)
265 static void dirs_dealloc(dirsObject *self)
266 {
266 {
267 Py_XDECREF(self->dict);
267 Py_XDECREF(self->dict);
268 PyObject_Del(self);
268 PyObject_Del(self);
269 }
269 }
270
270
271 static PyObject *dirs_iter(dirsObject *self)
271 static PyObject *dirs_iter(dirsObject *self)
272 {
272 {
273 return PyObject_GetIter(self->dict);
273 return PyObject_GetIter(self->dict);
274 }
274 }
275
275
276 static PySequenceMethods dirs_sequence_methods;
276 static PySequenceMethods dirs_sequence_methods;
277
277
278 static PyMethodDef dirs_methods[] = {
278 static PyMethodDef dirs_methods[] = {
279 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
279 {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"},
280 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
280 {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"},
281 {NULL} /* Sentinel */
281 {NULL} /* Sentinel */
282 };
282 };
283
283
284 static PyTypeObject dirsType = { PyVarObject_HEAD_INIT(NULL, 0) };
284 static PyTypeObject dirsType = {PyVarObject_HEAD_INIT(NULL, 0)};
285
285
286 void dirs_module_init(PyObject *mod)
286 void dirs_module_init(PyObject *mod)
287 {
287 {
288 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
288 dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains;
289 dirsType.tp_name = "parsers.dirs";
289 dirsType.tp_name = "parsers.dirs";
290 dirsType.tp_new = PyType_GenericNew;
290 dirsType.tp_new = PyType_GenericNew;
291 dirsType.tp_basicsize = sizeof(dirsObject);
291 dirsType.tp_basicsize = sizeof(dirsObject);
292 dirsType.tp_dealloc = (destructor)dirs_dealloc;
292 dirsType.tp_dealloc = (destructor)dirs_dealloc;
293 dirsType.tp_as_sequence = &dirs_sequence_methods;
293 dirsType.tp_as_sequence = &dirs_sequence_methods;
294 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
294 dirsType.tp_flags = Py_TPFLAGS_DEFAULT;
295 dirsType.tp_doc = "dirs";
295 dirsType.tp_doc = "dirs";
296 dirsType.tp_iter = (getiterfunc)dirs_iter;
296 dirsType.tp_iter = (getiterfunc)dirs_iter;
297 dirsType.tp_methods = dirs_methods;
297 dirsType.tp_methods = dirs_methods;
298 dirsType.tp_init = (initproc)dirs_init;
298 dirsType.tp_init = (initproc)dirs_init;
299
299
300 if (PyType_Ready(&dirsType) < 0)
300 if (PyType_Ready(&dirsType) < 0)
301 return;
301 return;
302 Py_INCREF(&dirsType);
302 Py_INCREF(&dirsType);
303
303
304 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
304 PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType);
305 }
305 }
General Comments 0
You need to be logged in to leave comments. Login now