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.

262 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. /* 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_TypeCheck(self, &_PyNamespace_Type) &&
  132. PyObject_TypeCheck(other, &_PyNamespace_Type))
  133. return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
  134. ((_PyNamespaceObject *)other)->ns_dict, op);
  135. Py_RETURN_NOTIMPLEMENTED;
  136. }
  137. PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
  138. static PyObject *
  139. namespace_reduce(_PyNamespaceObject *ns)
  140. {
  141. PyObject *result, *args = PyTuple_New(0);
  142. if (!args)
  143. return NULL;
  144. result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
  145. Py_DECREF(args);
  146. return result;
  147. }
  148. static PyMethodDef namespace_methods[] = {
  149. {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
  150. namespace_reduce__doc__},
  151. {NULL, NULL} /* sentinel */
  152. };
  153. PyDoc_STRVAR(namespace_doc,
  154. "A simple attribute-based namespace.\n\
  155. \n\
  156. SimpleNamespace(**kwargs)");
  157. PyTypeObject _PyNamespace_Type = {
  158. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  159. "types.SimpleNamespace", /* tp_name */
  160. sizeof(_PyNamespaceObject), /* tp_size */
  161. 0, /* tp_itemsize */
  162. (destructor)namespace_dealloc, /* tp_dealloc */
  163. 0, /* tp_print */
  164. 0, /* tp_getattr */
  165. 0, /* tp_setattr */
  166. 0, /* tp_reserved */
  167. (reprfunc)namespace_repr, /* tp_repr */
  168. 0, /* tp_as_number */
  169. 0, /* tp_as_sequence */
  170. 0, /* tp_as_mapping */
  171. 0, /* tp_hash */
  172. 0, /* tp_call */
  173. 0, /* tp_str */
  174. PyObject_GenericGetAttr, /* tp_getattro */
  175. PyObject_GenericSetAttr, /* tp_setattro */
  176. 0, /* tp_as_buffer */
  177. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  178. Py_TPFLAGS_BASETYPE, /* tp_flags */
  179. namespace_doc, /* tp_doc */
  180. (traverseproc)namespace_traverse, /* tp_traverse */
  181. (inquiry)namespace_clear, /* tp_clear */
  182. namespace_richcompare, /* tp_richcompare */
  183. 0, /* tp_weaklistoffset */
  184. 0, /* tp_iter */
  185. 0, /* tp_iternext */
  186. namespace_methods, /* tp_methods */
  187. namespace_members, /* tp_members */
  188. 0, /* tp_getset */
  189. 0, /* tp_base */
  190. 0, /* tp_dict */
  191. 0, /* tp_descr_get */
  192. 0, /* tp_descr_set */
  193. offsetof(_PyNamespaceObject, ns_dict), /* tp_dictoffset */
  194. (initproc)namespace_init, /* tp_init */
  195. PyType_GenericAlloc, /* tp_alloc */
  196. (newfunc)namespace_new, /* tp_new */
  197. PyObject_GC_Del, /* tp_free */
  198. };
  199. PyObject *
  200. _PyNamespace_New(PyObject *kwds)
  201. {
  202. PyObject *ns = namespace_new(&_PyNamespace_Type, NULL, NULL);
  203. if (ns == NULL)
  204. return NULL;
  205. if (kwds == NULL)
  206. return ns;
  207. if (PyDict_Update(((_PyNamespaceObject *)ns)->ns_dict, kwds) != 0) {
  208. Py_DECREF(ns);
  209. return NULL;
  210. }
  211. return (PyObject *)ns;
  212. }