blob: fa37ed250d30a4b7f777748785aca8433c5c0d36 [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"
Victor Stinner4a21e572020-04-15 02:35:41 +02004#include "structmember.h" // PyMemberDef
Barry Warsaw409da152012-06-03 16:18:47 -04005
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 }
Serhiy Storchaka79ba4712017-10-07 22:59:35 +030047 if (kwds == NULL) {
Barry Warsaw409da152012-06-03 16:18:47 -040048 return 0;
Serhiy Storchaka79ba4712017-10-07 22:59:35 +030049 }
50 if (!PyArg_ValidateKeywordArguments(kwds)) {
51 return -1;
52 }
Barry Warsaw409da152012-06-03 16:18:47 -040053 return PyDict_Update(ns->ns_dict, kwds);
54}
55
56
57static void
58namespace_dealloc(_PyNamespaceObject *ns)
59{
60 PyObject_GC_UnTrack(ns);
61 Py_CLEAR(ns->ns_dict);
62 Py_TYPE(ns)->tp_free((PyObject *)ns);
63}
64
65
66static PyObject *
Eric Snowb5c8f922013-02-16 16:32:39 -070067namespace_repr(PyObject *ns)
Barry Warsaw409da152012-06-03 16:18:47 -040068{
69 int i, loop_error = 0;
70 PyObject *pairs = NULL, *d = NULL, *keys = NULL, *keys_iter = NULL;
71 PyObject *key;
72 PyObject *separator, *pairsrepr, *repr = NULL;
Eric Snowb5c8f922013-02-16 16:32:39 -070073 const char * name;
Barry Warsaw409da152012-06-03 16:18:47 -040074
Andy Lesterdffe4c02020-03-04 07:15:20 -060075 name = Py_IS_TYPE(ns, &_PyNamespace_Type) ? "namespace"
Victor Stinner58ac7002020-02-07 03:04:21 +010076 : Py_TYPE(ns)->tp_name;
Eric Snowb5c8f922013-02-16 16:32:39 -070077
78 i = Py_ReprEnter(ns);
Barry Warsaw409da152012-06-03 16:18:47 -040079 if (i != 0) {
Eric Snowb5c8f922013-02-16 16:32:39 -070080 return i > 0 ? PyUnicode_FromFormat("%s(...)", name) : NULL;
Barry Warsaw409da152012-06-03 16:18:47 -040081 }
82
83 pairs = PyList_New(0);
84 if (pairs == NULL)
85 goto error;
86
87 d = ((_PyNamespaceObject *)ns)->ns_dict;
88 assert(d != NULL);
89 Py_INCREF(d);
90
91 keys = PyDict_Keys(d);
92 if (keys == NULL)
93 goto error;
Barry Warsaw409da152012-06-03 16:18:47 -040094
95 keys_iter = PyObject_GetIter(keys);
96 if (keys_iter == NULL)
97 goto error;
98
99 while ((key = PyIter_Next(keys_iter)) != NULL) {
Victor Stinner59799a82013-11-13 14:17:30 +0100100 if (PyUnicode_Check(key) && PyUnicode_GET_LENGTH(key) > 0) {
Barry Warsaw409da152012-06-03 16:18:47 -0400101 PyObject *value, *item;
102
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200103 value = PyDict_GetItemWithError(d, key);
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +0200104 if (value != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200105 item = PyUnicode_FromFormat("%U=%R", key, value);
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +0200106 if (item == NULL) {
107 loop_error = 1;
108 }
109 else {
110 loop_error = PyList_Append(pairs, item);
111 Py_DECREF(item);
112 }
Barry Warsaw409da152012-06-03 16:18:47 -0400113 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200114 else if (PyErr_Occurred()) {
115 loop_error = 1;
116 }
Barry Warsaw409da152012-06-03 16:18:47 -0400117 }
118
119 Py_DECREF(key);
120 if (loop_error)
121 goto error;
122 }
123
124 separator = PyUnicode_FromString(", ");
125 if (separator == NULL)
126 goto error;
127
128 pairsrepr = PyUnicode_Join(separator, pairs);
129 Py_DECREF(separator);
130 if (pairsrepr == NULL)
131 goto error;
132
Eric Snowb5c8f922013-02-16 16:32:39 -0700133 repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
Barry Warsaw409da152012-06-03 16:18:47 -0400134 Py_DECREF(pairsrepr);
135
136error:
137 Py_XDECREF(pairs);
138 Py_XDECREF(d);
139 Py_XDECREF(keys);
140 Py_XDECREF(keys_iter);
Eric Snowb5c8f922013-02-16 16:32:39 -0700141 Py_ReprLeave(ns);
Barry Warsaw409da152012-06-03 16:18:47 -0400142
143 return repr;
144}
145
146
147static int
148namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg)
149{
150 Py_VISIT(ns->ns_dict);
151 return 0;
152}
153
154
155static int
156namespace_clear(_PyNamespaceObject *ns)
157{
158 Py_CLEAR(ns->ns_dict);
159 return 0;
160}
161
162
Eric Snowb5c8f922013-02-16 16:32:39 -0700163static PyObject *
164namespace_richcompare(PyObject *self, PyObject *other, int op)
165{
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300166 if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
167 PyObject_TypeCheck(other, &_PyNamespace_Type))
Eric Snowb5c8f922013-02-16 16:32:39 -0700168 return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
169 ((_PyNamespaceObject *)other)->ns_dict, op);
Serhiy Storchaka08d230a2015-05-22 11:02:49 +0300170 Py_RETURN_NOTIMPLEMENTED;
Eric Snowb5c8f922013-02-16 16:32:39 -0700171}
172
173
Eric Snow9d05c8c2013-02-16 18:20:32 -0700174PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
175
176static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530177namespace_reduce(_PyNamespaceObject *ns, PyObject *Py_UNUSED(ignored))
Eric Snow9d05c8c2013-02-16 18:20:32 -0700178{
179 PyObject *result, *args = PyTuple_New(0);
180
181 if (!args)
182 return NULL;
183
184 result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
185 Py_DECREF(args);
186 return result;
187}
188
189
190static PyMethodDef namespace_methods[] = {
191 {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
192 namespace_reduce__doc__},
Benjamin Petersond39206a2016-09-07 14:12:36 -0700193 {NULL, NULL} // sentinel
Eric Snow9d05c8c2013-02-16 18:20:32 -0700194};
195
196
Barry Warsaw409da152012-06-03 16:18:47 -0400197PyDoc_STRVAR(namespace_doc,
198"A simple attribute-based namespace.\n\
199\n\
Eric Snowb5c8f922013-02-16 16:32:39 -0700200SimpleNamespace(**kwargs)");
Barry Warsaw409da152012-06-03 16:18:47 -0400201
202PyTypeObject _PyNamespace_Type = {
203 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Eric Snowb5c8f922013-02-16 16:32:39 -0700204 "types.SimpleNamespace", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200205 sizeof(_PyNamespaceObject), /* tp_basicsize */
Barry Warsaw409da152012-06-03 16:18:47 -0400206 0, /* tp_itemsize */
207 (destructor)namespace_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200208 0, /* tp_vectorcall_offset */
Barry Warsaw409da152012-06-03 16:18:47 -0400209 0, /* tp_getattr */
210 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200211 0, /* tp_as_async */
Barry Warsaw409da152012-06-03 16:18:47 -0400212 (reprfunc)namespace_repr, /* tp_repr */
213 0, /* tp_as_number */
214 0, /* tp_as_sequence */
215 0, /* tp_as_mapping */
216 0, /* tp_hash */
217 0, /* tp_call */
218 0, /* tp_str */
219 PyObject_GenericGetAttr, /* tp_getattro */
220 PyObject_GenericSetAttr, /* tp_setattro */
221 0, /* tp_as_buffer */
222 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
223 Py_TPFLAGS_BASETYPE, /* tp_flags */
224 namespace_doc, /* tp_doc */
225 (traverseproc)namespace_traverse, /* tp_traverse */
226 (inquiry)namespace_clear, /* tp_clear */
Eric Snowb5c8f922013-02-16 16:32:39 -0700227 namespace_richcompare, /* tp_richcompare */
Barry Warsaw409da152012-06-03 16:18:47 -0400228 0, /* tp_weaklistoffset */
229 0, /* tp_iter */
230 0, /* tp_iternext */
Eric Snow9d05c8c2013-02-16 18:20:32 -0700231 namespace_methods, /* tp_methods */
Barry Warsaw409da152012-06-03 16:18:47 -0400232 namespace_members, /* tp_members */
233 0, /* tp_getset */
234 0, /* tp_base */
235 0, /* tp_dict */
236 0, /* tp_descr_get */
237 0, /* tp_descr_set */
238 offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
239 (initproc)namespace_init, /* tp_init */
240 PyType_GenericAlloc, /* tp_alloc */
241 (newfunc)namespace_new, /* tp_new */
242 PyObject_GC_Del, /* tp_free */
243};
244
245
246PyObject *
247_PyNamespace_New(PyObject *kwds)
248{
249 PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
250 if (ns == NULL)
251 return NULL;
252
253 if (kwds == NULL)
254 return ns;
255 if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
256 Py_DECREF(ns);
257 return NULL;
258 }
259
260 return (PyObject *)ns;
261}