namespaceobject.c 7.5 KB

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