You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.2 KiB

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