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