namespaceobject.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. 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, PyObject *Py_UNUSED(ignored))
  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_basicsize */
  161. 0, /* tp_itemsize */
  162. (destructor)namespace_dealloc, /* tp_dealloc */
  163. 0, /* tp_vectorcall_offset */
  164. 0, /* tp_getattr */
  165. 0, /* tp_setattr */
  166. 0, /* tp_as_async */
  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. }