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