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