1 // namespace object implementation
2
3 #include "Python.h"
4 #include "pycore_modsupport.h" // _PyArg_NoPositional()
5 #include "pycore_namespace.h" // _PyNamespace_Type
6
7 #include <stddef.h> // offsetof()
8
9
10 typedef struct {
11 PyObject_HEAD
12 PyObject *ns_dict;
13 } _PyNamespaceObject;
14
15
16 static PyMemberDef namespace_members[] = {
17 {"__dict__", _Py_T_OBJECT, offsetof(_PyNamespaceObject, ns_dict), Py_READONLY},
18 {NULL}
19 };
20
21
22 // Methods
23
24 static PyObject *
namespace_new(PyTypeObject * type,PyObject * args,PyObject * kwds)25 namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26 {
27 PyObject *self;
28
29 assert(type != NULL && type->tp_alloc != NULL);
30 self = type->tp_alloc(type, 0);
31 if (self != NULL) {
32 _PyNamespaceObject *ns = (_PyNamespaceObject *)self;
33 ns->ns_dict = PyDict_New();
34 if (ns->ns_dict == NULL) {
35 Py_DECREF(ns);
36 return NULL;
37 }
38 }
39 return self;
40 }
41
42
43 static int
namespace_init(_PyNamespaceObject * ns,PyObject * args,PyObject * kwds)44 namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds)
45 {
46 PyObject *arg = NULL;
47 if (!PyArg_UnpackTuple(args, _PyType_Name(Py_TYPE(ns)), 0, 1, &arg)) {
48 return -1;
49 }
50 if (arg != NULL) {
51 PyObject *dict;
52 if (PyDict_CheckExact(arg)) {
53 dict = Py_NewRef(arg);
54 }
55 else {
56 dict = PyObject_CallOneArg((PyObject *)&PyDict_Type, arg);
57 if (dict == NULL) {
58 return -1;
59 }
60 }
61 int err = (!PyArg_ValidateKeywordArguments(dict) ||
62 PyDict_Update(ns->ns_dict, dict) < 0);
63 Py_DECREF(dict);
64 if (err) {
65 return -1;
66 }
67 }
68 if (kwds == NULL) {
69 return 0;
70 }
71 if (!PyArg_ValidateKeywordArguments(kwds)) {
72 return -1;
73 }
74 return PyDict_Update(ns->ns_dict, kwds);
75 }
76
77
78 static void
namespace_dealloc(_PyNamespaceObject * ns)79 namespace_dealloc(_PyNamespaceObject *ns)
80 {
81 PyObject_GC_UnTrack(ns);
82 Py_CLEAR(ns->ns_dict);
83 Py_TYPE(ns)->tp_free((PyObject *)ns);
84 }
85
86
87 static PyObject *
namespace_repr(PyObject * ns)88 namespace_repr(PyObject *ns)
89 {
90 int i, loop_error = 0;
91 PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL;
92 PyObject *key;
93 PyObject *separator, *pairsrepr, *repr = NULL;
94 const char * name;
95
96 name = Py_IS_TYPE(ns, &_PyNamespace_Type) ? "namespace"
97 : Py_TYPE(ns)->tp_name;
98
99 i = Py_ReprEnter(ns);
100 if (i != 0) {
101 return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL;
102 }
103
104 pairs = PyList_New(0);
105 if (pairs == NULL)
106 goto error;
107
108 assert(((_PyNamespaceObject *)ns)->ns_dict != NULL);
109 d = Py_NewRef(((_PyNamespaceObject *)ns)->ns_dict);
110
111 keys = PyDict_Keys(d);
112 if (keys == NULL)
113 goto error;
114
115 keys_iter = PyObject_GetIter(keys);
116 if (keys_iter == NULL)
117 goto error;
118
119 while ((key = PyIter_Next(keys_iter)) != NULL) {
120 if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
121 PyObject *value, *item;
122
123 value = PyDict_GetItemWithError(d, key);
124 if (value != NULL) {
125 item = PyUnicode_FromFormat("%U=%R", key, value);
126 if (item == NULL) {
127 loop_error = 1;
128 }
129 else {
130 loop_error = PyList_Append(pairs, item);
131 Py_DECREF(item);
132 }
133 }
134 else if (PyErr_Occurred()) {
135 loop_error = 1;
136 }
137 }
138
139 Py_DECREF(key);
140 if (loop_error)
141 goto error;
142 }
143
144 separator = PyUnicode_FromString(", ");
145 if (separator == NULL)
146 goto error;
147
148 pairsrepr = PyUnicode_Join(separator, pairs);
149 Py_DECREF(separator);
150 if (pairsrepr == NULL)
151 goto error;
152
153 repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
154 Py_DECREF(pairsrepr);
155
156 error:
157 Py_XDECREF(pairs);
158 Py_XDECREF(d);
159 Py_XDECREF(keys);
160 Py_XDECREF(keys_iter);
161 Py_ReprLeave(ns);
162
163 return repr;
164 }
165
166
167 static int
namespace_traverse(_PyNamespaceObject * ns,visitproc visit,void * arg)168 namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg)
169 {
170 Py_VISIT(ns->ns_dict);
171 return 0;
172 }
173
174
175 static int
namespace_clear(_PyNamespaceObject * ns)176 namespace_clear(_PyNamespaceObject *ns)
177 {
178 Py_CLEAR(ns->ns_dict);
179 return 0;
180 }
181
182
183 static PyObject *
namespace_richcompare(PyObject * self,PyObject * other,int op)184 namespace_richcompare(PyObject *self, PyObject *other, int op)
185 {
186 if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
187 PyObject_TypeCheck(other, &_PyNamespace_Type))
188 return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
189 ((_PyNamespaceObject *)other)->ns_dict, op);
190 Py_RETURN_NOTIMPLEMENTED;
191 }
192
193
194 PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
195
196 static PyObject *
namespace_reduce(_PyNamespaceObject * ns,PyObject * Py_UNUSED (ignored))197 namespace_reduce(_PyNamespaceObject *ns, PyObject *Py_UNUSED(ignored))
198 {
199 PyObject *result, *args = PyTuple_New(0);
200
201 if (!args)
202 return NULL;
203
204 result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
205 Py_DECREF(args);
206 return result;
207 }
208
209
210 static PyObject *
namespace_replace(PyObject * self,PyObject * args,PyObject * kwargs)211 namespace_replace(PyObject *self, PyObject *args, PyObject *kwargs)
212 {
213 if (!_PyArg_NoPositional("__replace__", args)) {
214 return NULL;
215 }
216
217 PyObject *result = PyObject_CallNoArgs((PyObject *)Py_TYPE(self));
218 if (!result) {
219 return NULL;
220 }
221 if (PyDict_Update(((_PyNamespaceObject*)result)->ns_dict,
222 ((_PyNamespaceObject*)self)->ns_dict) < 0)
223 {
224 Py_DECREF(result);
225 return NULL;
226 }
227 if (kwargs) {
228 if (PyDict_Update(((_PyNamespaceObject*)result)->ns_dict, kwargs) < 0) {
229 Py_DECREF(result);
230 return NULL;
231 }
232 }
233 return result;
234 }
235
236
237 static PyMethodDef namespace_methods[] = {
238 {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
239 namespace_reduce__doc__},
240 {"__replace__", _PyCFunction_CAST(namespace_replace), METH_VARARGS|METH_KEYWORDS,
241 PyDoc_STR("__replace__($self, /, **changes)\n--\n\n"
242 "Return a copy of the namespace object with new values for the specified attributes.")},
243 {NULL, NULL} // sentinel
244 };
245
246
247 PyDoc_STRVAR(namespace_doc,
248 "SimpleNamespace(mapping_or_iterable=(), /, **kwargs)\n\
249 --\n\n\
250 A simple attribute-based namespace.");
251
252 PyTypeObject _PyNamespace_Type = {
253 PyVarObject_HEAD_INIT(&PyType_Type, 0)
254 "types.SimpleNamespace", /* tp_name */
255 sizeof(_PyNamespaceObject), /* tp_basicsize */
256 0, /* tp_itemsize */
257 (destructor)namespace_dealloc, /* tp_dealloc */
258 0, /* tp_vectorcall_offset */
259 0, /* tp_getattr */
260 0, /* tp_setattr */
261 0, /* tp_as_async */
262 (reprfunc)namespace_repr, /* tp_repr */
263 0, /* tp_as_number */
264 0, /* tp_as_sequence */
265 0, /* tp_as_mapping */
266 0, /* tp_hash */
267 0, /* tp_call */
268 0, /* tp_str */
269 PyObject_GenericGetAttr, /* tp_getattro */
270 PyObject_GenericSetAttr, /* tp_setattro */
271 0, /* tp_as_buffer */
272 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
273 Py_TPFLAGS_BASETYPE, /* tp_flags */
274 namespace_doc, /* tp_doc */
275 (traverseproc)namespace_traverse, /* tp_traverse */
276 (inquiry)namespace_clear, /* tp_clear */
277 namespace_richcompare, /* tp_richcompare */
278 0, /* tp_weaklistoffset */
279 0, /* tp_iter */
280 0, /* tp_iternext */
281 namespace_methods, /* tp_methods */
282 namespace_members, /* tp_members */
283 0, /* tp_getset */
284 0, /* tp_base */
285 0, /* tp_dict */
286 0, /* tp_descr_get */
287 0, /* tp_descr_set */
288 offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
289 (initproc)namespace_init, /* tp_init */
290 PyType_GenericAlloc, /* tp_alloc */
291 (newfunc)namespace_new, /* tp_new */
292 PyObject_GC_Del, /* tp_free */
293 };
294
295
296 PyObject *
_PyNamespace_New(PyObject * kwds)297 _PyNamespace_New(PyObject *kwds)
298 {
299 PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
300 if (ns == NULL)
301 return NULL;
302
303 if (kwds == NULL)
304 return ns;
305 if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
306 Py_DECREF(ns);
307 return NULL;
308 }
309
310 return (PyObject *)ns;
311 }
312