blob: 6deca961a4f11796c4e9b0e8f6f34815c5b8b298 [file] [log] [blame]
Benjamin Petersond39206a2016-09-07 14:12:36 -07001// namespace object implementation
Barry Warsaw409da152012-06-03 16:18:47 -04002
3#include "Python.h"
4#include "structmember.h"
5
6
7typedef struct {
8 PyObject_HEAD
9 PyObject *ns_dict;
10} _PyNamespaceObject;
11
12
13static PyMemberDef namespace_members[] = {
14 {"__dict__", T_OBJECT, offsetof(_PyNamespaceObject, ns_dict), READONLY},
15 {NULL}
16};
17
18
Benjamin Petersond39206a2016-09-07 14:12:36 -070019// Methods
Barry Warsaw409da152012-06-03 16:18:47 -040020
21static PyObject *
22namespace_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23{
Eric Snow547298c2012-10-16 22:35:38 -070024 PyObject *self;
Barry Warsaw409da152012-06-03 16:18:47 -040025
Eric Snow547298c2012-10-16 22:35:38 -070026 assert(type != NULL && type->tp_alloc != NULL);
27 self = type->tp_alloc(type, 0);
28 if (self != NULL) {
29 _PyNamespaceObject *ns = (_PyNamespaceObject *)self;
30 ns->ns_dict = PyDict_New();
31 if (ns->ns_dict == NULL) {
32 Py_DECREF(ns);
33 return NULL;
34 }
Barry Warsaw409da152012-06-03 16:18:47 -040035 }
Eric Snow547298c2012-10-16 22:35:38 -070036 return self;
Barry Warsaw409da152012-06-03 16:18:47 -040037}
38
39
40static int
41namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds)
42{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030043 if (PyTuple_GET_SIZE(args) != 0) {
44 PyErr_Format(PyExc_TypeError, "no positional arguments expected");
45 return -1;
Barry Warsaw409da152012-06-03 16:18:47 -040046 }
47 if (kwds == NULL)
48 return 0;
49 return PyDict_Update(ns->ns_dict, kwds);
50}
51
52
53static void
54namespace_dealloc(_PyNamespaceObject *ns)
55{
56 PyObject_GC_UnTrack(ns);
57 Py_CLEAR(ns->ns_dict);
58 Py_TYPE(ns)->tp_free((PyObject *)ns);
59}
60
61
62static PyObject *
Eric Snowb5c8f922013-02-16 16:32:39 -070063namespace_repr(PyObject *ns)
Barry Warsaw409da152012-06-03 16:18:47 -040064{
65 int i, loop_error = 0;
66 PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL;
67 PyObject *key;
68 PyObject *separator, *pairsrepr, *repr = NULL;
Eric Snowb5c8f922013-02-16 16:32:39 -070069 const char * name;
Barry Warsaw409da152012-06-03 16:18:47 -040070
Eric Snowb5c8f922013-02-16 16:32:39 -070071 name = (Py_TYPE(ns) == &_PyNamespace_Type) ? "namespace"
72 : ns->ob_type->tp_name;
73
74 i = Py_ReprEnter(ns);
Barry Warsaw409da152012-06-03 16:18:47 -040075 if (i != 0) {
Eric Snowb5c8f922013-02-16 16:32:39 -070076 return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL;
Barry Warsaw409da152012-06-03 16:18:47 -040077 }
78
79 pairs = PyList_New(0);
80 if (pairs == NULL)
81 goto error;
82
83 d = ((_PyNamespaceObject *)ns)->ns_dict;
84 assert(d != NULL);
85 Py_INCREF(d);
86
87 keys = PyDict_Keys(d);
88 if (keys == NULL)
89 goto error;
90 if (PyList_Sort(keys) != 0)
91 goto error;
92
93 keys_iter = PyObject_GetIter(keys);
94 if (keys_iter == NULL)
95 goto error;
96
97 while ((key = PyIter_Next(keys_iter)) != NULL) {
Victor Stinner59799a82013-11-13 14:17:30 +010098 if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
Barry Warsaw409da152012-06-03 16:18:47 -040099 PyObject *value, *item;
100
101 value = PyDict_GetItem(d, key);
102 assert(value != NULL);
103
104 item = PyUnicode_FromFormat("%S=%R", key, value);
105 if (item == NULL) {
106 loop_error = 1;
107 }
108 else {
109 loop_error = PyList_Append(pairs, item);
110 Py_DECREF(item);
111 }
112 }
113
114 Py_DECREF(key);
115 if (loop_error)
116 goto error;
117 }
118
119 separator = PyUnicode_FromString(", ");
120 if (separator == NULL)
121 goto error;
122
123 pairsrepr = PyUnicode_Join(separator, pairs);
124 Py_DECREF(separator);
125 if (pairsrepr == NULL)
126 goto error;
127
Eric Snowb5c8f922013-02-16 16:32:39 -0700128 repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
Barry Warsaw409da152012-06-03 16:18:47 -0400129 Py_DECREF(pairsrepr);
130
131error:
132 Py_XDECREF(pairs);
133 Py_XDECREF(d);
134 Py_XDECREF(keys);
135 Py_XDECREF(keys_iter);
Eric Snowb5c8f922013-02-16 16:32:39 -0700136 Py_ReprLeave(ns);
Barry Warsaw409da152012-06-03 16:18:47 -0400137
138 return repr;
139}
140
141
142static int
143namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg)
144{
145 Py_VISIT(ns->ns_dict);
146 return 0;
147}
148
149
150static int
151namespace_clear(_PyNamespaceObject *ns)
152{
153 Py_CLEAR(ns->ns_dict);
154 return 0;
155}
156
157
Eric Snowb5c8f922013-02-16 16:32:39 -0700158static PyObject *
159namespace_richcompare(PyObject *self, PyObject *other, int op)
160{
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300161 if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
162 PyObject_TypeCheck(other, &_PyNamespace_Type))
Eric Snowb5c8f922013-02-16 16:32:39 -0700163 return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
164 ((_PyNamespaceObject *)other)->ns_dict, op);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300165 Py_RETURN_NOTIMPLEMENTED;
Eric Snowb5c8f922013-02-16 16:32:39 -0700166}
167
168
Eric Snow9d05c8c2013-02-16 18:20:32 -0700169PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
170
171static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200172namespace_reduce(_PyNamespaceObject *ns)
Eric Snow9d05c8c2013-02-16 18:20:32 -0700173{
174 PyObject *result, *args = PyTuple_New(0);
175
176 if (!args)
177 return NULL;
178
179 result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
180 Py_DECREF(args);
181 return result;
182}
183
184
185static PyMethodDef namespace_methods[] = {
186 {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
187 namespace_reduce__doc__},
Benjamin Petersond39206a2016-09-07 14:12:36 -0700188 {NULL, NULL} // sentinel
Eric Snow9d05c8c2013-02-16 18:20:32 -0700189};
190
191
Barry Warsaw409da152012-06-03 16:18:47 -0400192PyDoc_STRVAR(namespace_doc,
193"A simple attribute-based namespace.\n\
194\n\
Eric Snowb5c8f922013-02-16 16:32:39 -0700195SimpleNamespace(**kwargs)");
Barry Warsaw409da152012-06-03 16:18:47 -0400196
197PyTypeObject _PyNamespace_Type = {
198 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Eric Snowb5c8f922013-02-16 16:32:39 -0700199 "types.SimpleNamespace", /* tp_name */
Barry Warsaw409da152012-06-03 16:18:47 -0400200 sizeof(_PyNamespaceObject), /* tp_size */
201 0, /* tp_itemsize */
202 (destructor)namespace_dealloc, /* tp_dealloc */
203 0, /* tp_print */
204 0, /* tp_getattr */
205 0, /* tp_setattr */
206 0, /* tp_reserved */
207 (reprfunc)namespace_repr, /* tp_repr */
208 0, /* tp_as_number */
209 0, /* tp_as_sequence */
210 0, /* tp_as_mapping */
211 0, /* tp_hash */
212 0, /* tp_call */
213 0, /* tp_str */
214 PyObject_GenericGetAttr, /* tp_getattro */
215 PyObject_GenericSetAttr, /* tp_setattro */
216 0, /* tp_as_buffer */
217 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
218 Py_TPFLAGS_BASETYPE, /* tp_flags */
219 namespace_doc, /* tp_doc */
220 (traverseproc)namespace_traverse, /* tp_traverse */
221 (inquiry)namespace_clear, /* tp_clear */
Eric Snowb5c8f922013-02-16 16:32:39 -0700222 namespace_richcompare, /* tp_richcompare */
Barry Warsaw409da152012-06-03 16:18:47 -0400223 0, /* tp_weaklistoffset */
224 0, /* tp_iter */
225 0, /* tp_iternext */
Eric Snow9d05c8c2013-02-16 18:20:32 -0700226 namespace_methods, /* tp_methods */
Barry Warsaw409da152012-06-03 16:18:47 -0400227 namespace_members, /* tp_members */
228 0, /* tp_getset */
229 0, /* tp_base */
230 0, /* tp_dict */
231 0, /* tp_descr_get */
232 0, /* tp_descr_set */
233 offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
234 (initproc)namespace_init, /* tp_init */
235 PyType_GenericAlloc, /* tp_alloc */
236 (newfunc)namespace_new, /* tp_new */
237 PyObject_GC_Del, /* tp_free */
238};
239
240
241PyObject *
242_PyNamespace_New(PyObject *kwds)
243{
244 PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
245 if (ns == NULL)
246 return NULL;
247
248 if (kwds == NULL)
249 return ns;
250 if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
251 Py_DECREF(ns);
252 return NULL;
253 }
254
255 return (PyObject *)ns;
256}