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