1 | n/a | // namespace object implementation |
---|
2 | n/a | |
---|
3 | n/a | #include "Python.h" |
---|
4 | n/a | #include "structmember.h" |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | typedef struct { |
---|
8 | n/a | PyObject_HEAD |
---|
9 | n/a | PyObject *ns_dict; |
---|
10 | n/a | } _PyNamespaceObject; |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | static PyMemberDef namespace_members[] = { |
---|
14 | n/a | {"__dict__", T_OBJECT, offsetof(_PyNamespaceObject, ns_dict), READONLY}, |
---|
15 | n/a | {NULL} |
---|
16 | n/a | }; |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | // Methods |
---|
20 | n/a | |
---|
21 | n/a | static PyObject * |
---|
22 | n/a | namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
23 | n/a | { |
---|
24 | n/a | PyObject *self; |
---|
25 | n/a | |
---|
26 | n/a | assert(type != NULL && type->tp_alloc != NULL); |
---|
27 | n/a | self = type->tp_alloc(type, 0); |
---|
28 | n/a | if (self != NULL) { |
---|
29 | n/a | _PyNamespaceObject *ns = (_PyNamespaceObject *)self; |
---|
30 | n/a | ns->ns_dict = PyDict_New(); |
---|
31 | n/a | if (ns->ns_dict == NULL) { |
---|
32 | n/a | Py_DECREF(ns); |
---|
33 | n/a | return NULL; |
---|
34 | n/a | } |
---|
35 | n/a | } |
---|
36 | n/a | return self; |
---|
37 | n/a | } |
---|
38 | n/a | |
---|
39 | n/a | |
---|
40 | n/a | static int |
---|
41 | n/a | namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds) |
---|
42 | n/a | { |
---|
43 | n/a | // ignore args if it's NULL or empty |
---|
44 | n/a | if (args != NULL) { |
---|
45 | n/a | Py_ssize_t argcount = PyObject_Size(args); |
---|
46 | n/a | if (argcount < 0) |
---|
47 | n/a | return -1; |
---|
48 | n/a | else if (argcount > 0) { |
---|
49 | n/a | PyErr_Format(PyExc_TypeError, "no positional arguments expected"); |
---|
50 | n/a | return -1; |
---|
51 | n/a | } |
---|
52 | n/a | } |
---|
53 | n/a | if (kwds == NULL) |
---|
54 | n/a | return 0; |
---|
55 | n/a | return PyDict_Update(ns->ns_dict, kwds); |
---|
56 | n/a | } |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | static void |
---|
60 | n/a | namespace_dealloc(_PyNamespaceObject *ns) |
---|
61 | n/a | { |
---|
62 | n/a | PyObject_GC_UnTrack(ns); |
---|
63 | n/a | Py_CLEAR(ns->ns_dict); |
---|
64 | n/a | Py_TYPE(ns)->tp_free((PyObject *)ns); |
---|
65 | n/a | } |
---|
66 | n/a | |
---|
67 | n/a | |
---|
68 | n/a | static PyObject * |
---|
69 | n/a | namespace_repr(PyObject *ns) |
---|
70 | n/a | { |
---|
71 | n/a | int i, loop_error = 0; |
---|
72 | n/a | PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL; |
---|
73 | n/a | PyObject *key; |
---|
74 | n/a | PyObject *separator, *pairsrepr, *repr = NULL; |
---|
75 | n/a | const char * name; |
---|
76 | n/a | |
---|
77 | n/a | name = (Py_TYPE(ns) == &_PyNamespace_Type) ? "namespace" |
---|
78 | n/a | : ns->ob_type->tp_name; |
---|
79 | n/a | |
---|
80 | n/a | i = Py_ReprEnter(ns); |
---|
81 | n/a | if (i != 0) { |
---|
82 | n/a | return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL; |
---|
83 | n/a | } |
---|
84 | n/a | |
---|
85 | n/a | pairs = PyList_New(0); |
---|
86 | n/a | if (pairs == NULL) |
---|
87 | n/a | goto error; |
---|
88 | n/a | |
---|
89 | n/a | d = ((_PyNamespaceObject *)ns)->ns_dict; |
---|
90 | n/a | assert(d != NULL); |
---|
91 | n/a | Py_INCREF(d); |
---|
92 | n/a | |
---|
93 | n/a | keys = PyDict_Keys(d); |
---|
94 | n/a | if (keys == NULL) |
---|
95 | n/a | goto error; |
---|
96 | n/a | if (PyList_Sort(keys) != 0) |
---|
97 | n/a | goto error; |
---|
98 | n/a | |
---|
99 | n/a | keys_iter = PyObject_GetIter(keys); |
---|
100 | n/a | if (keys_iter == NULL) |
---|
101 | n/a | goto error; |
---|
102 | n/a | |
---|
103 | n/a | while ((key = PyIter_Next(keys_iter)) != NULL) { |
---|
104 | n/a | if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) { |
---|
105 | n/a | PyObject *value, *item; |
---|
106 | n/a | |
---|
107 | n/a | value = PyDict_GetItem(d, key); |
---|
108 | n/a | assert(value != NULL); |
---|
109 | n/a | |
---|
110 | n/a | item = PyUnicode_FromFormat("%S=%R", key, value); |
---|
111 | n/a | if (item == NULL) { |
---|
112 | n/a | loop_error = 1; |
---|
113 | n/a | } |
---|
114 | n/a | else { |
---|
115 | n/a | loop_error = PyList_Append(pairs, item); |
---|
116 | n/a | Py_DECREF(item); |
---|
117 | n/a | } |
---|
118 | n/a | } |
---|
119 | n/a | |
---|
120 | n/a | Py_DECREF(key); |
---|
121 | n/a | if (loop_error) |
---|
122 | n/a | goto error; |
---|
123 | n/a | } |
---|
124 | n/a | |
---|
125 | n/a | separator = PyUnicode_FromString(", "); |
---|
126 | n/a | if (separator == NULL) |
---|
127 | n/a | goto error; |
---|
128 | n/a | |
---|
129 | n/a | pairsrepr = PyUnicode_Join(separator, pairs); |
---|
130 | n/a | Py_DECREF(separator); |
---|
131 | n/a | if (pairsrepr == NULL) |
---|
132 | n/a | goto error; |
---|
133 | n/a | |
---|
134 | n/a | repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr); |
---|
135 | n/a | Py_DECREF(pairsrepr); |
---|
136 | n/a | |
---|
137 | n/a | error: |
---|
138 | n/a | Py_XDECREF(pairs); |
---|
139 | n/a | Py_XDECREF(d); |
---|
140 | n/a | Py_XDECREF(keys); |
---|
141 | n/a | Py_XDECREF(keys_iter); |
---|
142 | n/a | Py_ReprLeave(ns); |
---|
143 | n/a | |
---|
144 | n/a | return repr; |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | |
---|
148 | n/a | static int |
---|
149 | n/a | namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg) |
---|
150 | n/a | { |
---|
151 | n/a | Py_VISIT(ns->ns_dict); |
---|
152 | n/a | return 0; |
---|
153 | n/a | } |
---|
154 | n/a | |
---|
155 | n/a | |
---|
156 | n/a | static int |
---|
157 | n/a | namespace_clear(_PyNamespaceObject *ns) |
---|
158 | n/a | { |
---|
159 | n/a | Py_CLEAR(ns->ns_dict); |
---|
160 | n/a | return 0; |
---|
161 | n/a | } |
---|
162 | n/a | |
---|
163 | n/a | |
---|
164 | n/a | static PyObject * |
---|
165 | n/a | namespace_richcompare(PyObject *self, PyObject *other, int op) |
---|
166 | n/a | { |
---|
167 | n/a | if (PyObject_TypeCheck(self, &_PyNamespace_Type) && |
---|
168 | n/a | PyObject_TypeCheck(other, &_PyNamespace_Type)) |
---|
169 | n/a | return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict, |
---|
170 | n/a | ((_PyNamespaceObject *)other)->ns_dict, op); |
---|
171 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
172 | n/a | } |
---|
173 | n/a | |
---|
174 | n/a | |
---|
175 | n/a | PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling"); |
---|
176 | n/a | |
---|
177 | n/a | static PyObject * |
---|
178 | n/a | namespace_reduce(_PyNamespaceObject *ns) |
---|
179 | n/a | { |
---|
180 | n/a | PyObject *result, *args = PyTuple_New(0); |
---|
181 | n/a | |
---|
182 | n/a | if (!args) |
---|
183 | n/a | return NULL; |
---|
184 | n/a | |
---|
185 | n/a | result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict); |
---|
186 | n/a | Py_DECREF(args); |
---|
187 | n/a | return result; |
---|
188 | n/a | } |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | static PyMethodDef namespace_methods[] = { |
---|
192 | n/a | {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS, |
---|
193 | n/a | namespace_reduce__doc__}, |
---|
194 | n/a | {NULL, NULL} // sentinel |
---|
195 | n/a | }; |
---|
196 | n/a | |
---|
197 | n/a | |
---|
198 | n/a | PyDoc_STRVAR(namespace_doc, |
---|
199 | n/a | "A simple attribute-based namespace.\n\ |
---|
200 | n/a | \n\ |
---|
201 | n/a | SimpleNamespace(**kwargs)"); |
---|
202 | n/a | |
---|
203 | n/a | PyTypeObject _PyNamespace_Type = { |
---|
204 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
205 | n/a | "types.SimpleNamespace", /* tp_name */ |
---|
206 | n/a | sizeof(_PyNamespaceObject), /* tp_size */ |
---|
207 | n/a | 0, /* tp_itemsize */ |
---|
208 | n/a | (destructor)namespace_dealloc, /* tp_dealloc */ |
---|
209 | n/a | 0, /* tp_print */ |
---|
210 | n/a | 0, /* tp_getattr */ |
---|
211 | n/a | 0, /* tp_setattr */ |
---|
212 | n/a | 0, /* tp_reserved */ |
---|
213 | n/a | (reprfunc)namespace_repr, /* tp_repr */ |
---|
214 | n/a | 0, /* tp_as_number */ |
---|
215 | n/a | 0, /* tp_as_sequence */ |
---|
216 | n/a | 0, /* tp_as_mapping */ |
---|
217 | n/a | 0, /* tp_hash */ |
---|
218 | n/a | 0, /* tp_call */ |
---|
219 | n/a | 0, /* tp_str */ |
---|
220 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
221 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
---|
222 | n/a | 0, /* tp_as_buffer */ |
---|
223 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
---|
224 | n/a | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
225 | n/a | namespace_doc, /* tp_doc */ |
---|
226 | n/a | (traverseproc)namespace_traverse, /* tp_traverse */ |
---|
227 | n/a | (inquiry)namespace_clear, /* tp_clear */ |
---|
228 | n/a | namespace_richcompare, /* tp_richcompare */ |
---|
229 | n/a | 0, /* tp_weaklistoffset */ |
---|
230 | n/a | 0, /* tp_iter */ |
---|
231 | n/a | 0, /* tp_iternext */ |
---|
232 | n/a | namespace_methods, /* tp_methods */ |
---|
233 | n/a | namespace_members, /* tp_members */ |
---|
234 | n/a | 0, /* tp_getset */ |
---|
235 | n/a | 0, /* tp_base */ |
---|
236 | n/a | 0, /* tp_dict */ |
---|
237 | n/a | 0, /* tp_descr_get */ |
---|
238 | n/a | 0, /* tp_descr_set */ |
---|
239 | n/a | offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */ |
---|
240 | n/a | (initproc)namespace_init, /* tp_init */ |
---|
241 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
242 | n/a | (newfunc)namespace_new, /* tp_new */ |
---|
243 | n/a | PyObject_GC_Del, /* tp_free */ |
---|
244 | n/a | }; |
---|
245 | n/a | |
---|
246 | n/a | |
---|
247 | n/a | PyObject * |
---|
248 | n/a | _PyNamespace_New(PyObject *kwds) |
---|
249 | n/a | { |
---|
250 | n/a | PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL); |
---|
251 | n/a | if (ns == NULL) |
---|
252 | n/a | return NULL; |
---|
253 | n/a | |
---|
254 | n/a | if (kwds == NULL) |
---|
255 | n/a | return ns; |
---|
256 | n/a | if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) { |
---|
257 | n/a | Py_DECREF(ns); |
---|
258 | n/a | return NULL; |
---|
259 | n/a | } |
---|
260 | n/a | |
---|
261 | n/a | return (PyObject *)ns; |
---|
262 | n/a | } |
---|