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.

263 lines
7.4 KiB

  1. // namespace object implementation
  2. #include "Python.h"
  3. #include "structmember.h" // PyMemberDef
  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_IS_TYPE(ns, &_PyNamespace_Type) ? "namespace"
  60. : Py_TYPE(ns)->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_GetItemWithError(d, key);
  83. if (value != NULL) {
  84. item = PyUnicode_FromFormat("%U=%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. else if (PyErr_Occurred()) {
  94. loop_error = 1;
  95. }
  96. }
  97. Py_DECREF(key);
  98. if (loop_error)
  99. goto error;
  100. }
  101. separator = PyUnicode_FromString(", ");
  102. if (separator == NULL)
  103. goto error;
  104. pairsrepr = PyUnicode_Join(separator, pairs);
  105. Py_DECREF(separator);
  106. if (pairsrepr == NULL)
  107. goto error;
  108. repr = PyUnicode_FromFormat("%s(%S)", name, pairsrepr);
  109. Py_DECREF(pairsrepr);
  110. error:
  111. Py_XDECREF(pairs);
  112. Py_XDECREF(d);
  113. Py_XDECREF(keys);
  114. Py_XDECREF(keys_iter);
  115. Py_ReprLeave(ns);
  116. return repr;
  117. }
  118. static int
  119. namespace_traverse(_PyNamespaceObject *ns, visitproc visit, void *arg)
  120. {
  121. Py_VISIT(ns->ns_dict);
  122. return 0;
  123. }
  124. static int
  125. namespace_clear(_PyNamespaceObject *ns)
  126. {
  127. Py_CLEAR(ns->ns_dict);
  128. return 0;
  129. }
  130. static PyObject *
  131. namespace_richcompare(PyObject *self, PyObject *other, int op)
  132. {
  133. if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
  134. PyObject_TypeCheck(other, &_PyNamespace_Type))
  135. return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
  136. ((_PyNamespaceObject *)other)->ns_dict, op);
  137. Py_RETURN_NOTIMPLEMENTED;
  138. }
  139. PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
  140. static PyObject *
  141. namespace_reduce(_PyNamespaceObject *ns, PyObject *Py_UNUSED(ignored))
  142. {
  143. PyObject *result, *args = PyTuple_New(0);
  144. if (!args)
  145. return NULL;
  146. result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
  147. Py_DECREF(args);
  148. return result;
  149. }
  150. static PyMethodDef namespace_methods[] = {
  151. {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
  152. namespace_reduce__doc__},
  153. {NULL, NULL} // sentinel
  154. };
  155. PyDoc_STRVAR(namespace_doc,
  156. "A simple attribute-based namespace.\n\
  157. \n\
  158. SimpleNamespace(**kwargs)");
  159. PyTypeObject _PyNamespace_Type = {
  160. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  161. "types.SimpleNamespace", /* tp_name */
  162. sizeof(_PyNamespaceObject), /* tp_basicsize */
  163. 0, /* tp_itemsize */
  164. (destructor)namespace_dealloc, /* tp_dealloc */
  165. 0, /* tp_vectorcall_offset */
  166. 0, /* tp_getattr */
  167. 0, /* tp_setattr */
  168. 0, /* tp_as_async */
  169. (reprfunc)namespace_repr, /* tp_repr */
  170. 0, /* tp_as_number */
  171. 0, /* tp_as_sequence */
  172. 0, /* tp_as_mapping */
  173. 0, /* tp_hash */
  174. 0, /* tp_call */
  175. 0, /* tp_str */
  176. PyObject_GenericGetAttr, /* tp_getattro */
  177. PyObject_GenericSetAttr, /* tp_setattro */
  178. 0, /* tp_as_buffer */
  179. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  180. Py_TPFLAGS_BASETYPE, /* tp_flags */
  181. namespace_doc, /* tp_doc */
  182. (traverseproc)namespace_traverse, /* tp_traverse */
  183. (inquiry)namespace_clear, /* tp_clear */
  184. namespace_richcompare, /* tp_richcompare */
  185. 0, /* tp_weaklistoffset */
  186. 0, /* tp_iter */
  187. 0, /* tp_iternext */
  188. namespace_methods, /* tp_methods */
  189. namespace_members, /* tp_members */
  190. 0, /* tp_getset */
  191. 0, /* tp_base */
  192. 0, /* tp_dict */
  193. 0, /* tp_descr_get */
  194. 0, /* tp_descr_set */
  195. offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
  196. (initproc)namespace_init, /* tp_init */
  197. PyType_GenericAlloc, /* tp_alloc */
  198. (newfunc)namespace_new, /* tp_new */
  199. PyObject_GC_Del, /* tp_free */
  200. };
  201. PyObject *
  202. _PyNamespace_New(PyObject *kwds)
  203. {
  204. PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
  205. if (ns == NULL)
  206. return NULL;
  207. if (kwds == NULL)
  208. return ns;
  209. if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
  210. Py_DECREF(ns);
  211. return NULL;
  212. }
  213. return (PyObject *)ns;
  214. }