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.

586 lines
19 KiB

4 years ago
  1. #include "_multidict_views.h"
  2. #include "_multidict_iter.h"
  3. #include "_pair_list.h"
  4. #include <Python.h>
  5. // fix for VisualC complier used by Python 3.4
  6. #ifdef __GNUC__
  7. #define INLINE inline
  8. #else
  9. #define INLINE
  10. #endif
  11. /* We link this module statically for convenience. If compiled as a shared
  12. library instead, some compilers don't allow addresses of Python objects
  13. defined in other libraries to be used in static initializers here. The
  14. DEFERRED_ADDRESS macro is used to tag the slots where such addresses
  15. appear; the module init function must fill in the tagged slots at runtime.
  16. The argument is for documentation -- the macro ignores it.
  17. */
  18. #define DEFERRED_ADDRESS(ADDR) 0
  19. _Py_IDENTIFIER(impl);
  20. static PyTypeObject multidict_itemsview_type;
  21. static PyTypeObject multidict_valuesview_type;
  22. static PyTypeObject multidict_keysview_type;
  23. static PyObject *viewbaseset_richcmp_func;
  24. static PyObject *viewbaseset_and_func;
  25. static PyObject *viewbaseset_or_func;
  26. static PyObject *viewbaseset_sub_func;
  27. static PyObject *viewbaseset_xor_func;
  28. static PyObject *abc_itemsview_register_func;
  29. static PyObject *abc_keysview_register_func;
  30. static PyObject *abc_valuesview_register_func;
  31. static PyObject *itemsview_isdisjoint_func;
  32. static PyObject *itemsview_repr_func;
  33. static PyObject *keysview_repr_func;
  34. static PyObject *keysview_isdisjoint_func;
  35. static PyObject *valuesview_repr_func;
  36. typedef struct {
  37. PyObject_HEAD
  38. PyObject *md;
  39. } _Multidict_ViewObject;
  40. /********** Base **********/
  41. static INLINE void
  42. _init_view(_Multidict_ViewObject *self, PyObject *md)
  43. {
  44. Py_INCREF(md);
  45. self->md = md;
  46. }
  47. static void
  48. multidict_view_dealloc(_Multidict_ViewObject *self)
  49. {
  50. PyObject_GC_UnTrack(self);
  51. Py_XDECREF(self->md);
  52. PyObject_GC_Del(self);
  53. }
  54. static int
  55. multidict_view_traverse(_Multidict_ViewObject *self, visitproc visit, void *arg)
  56. {
  57. Py_VISIT(self->md);
  58. return 0;
  59. }
  60. static int
  61. multidict_view_clear(_Multidict_ViewObject *self)
  62. {
  63. Py_CLEAR(self->md);
  64. return 0;
  65. }
  66. static Py_ssize_t
  67. multidict_view_len(_Multidict_ViewObject *self)
  68. {
  69. Py_ssize_t ret;
  70. PyObject *impl = _PyObject_CallMethodId(self->md, &PyId_impl, NULL);
  71. if (impl == NULL) {
  72. return 0;
  73. }
  74. ret = pair_list_len(impl);
  75. Py_DECREF(impl);
  76. return ret;
  77. }
  78. static PyObject *
  79. multidict_view_richcompare(PyObject *self, PyObject *other, int op)
  80. {
  81. PyObject *ret;
  82. PyObject *op_obj = PyLong_FromLong(op);
  83. if (op_obj == NULL) {
  84. return NULL;
  85. }
  86. ret = PyObject_CallFunctionObjArgs(
  87. viewbaseset_richcmp_func, self, other, op_obj, NULL);
  88. Py_DECREF(op_obj);
  89. return ret;
  90. }
  91. static PyObject *
  92. multidict_view_and(PyObject *self, PyObject *other)
  93. {
  94. return PyObject_CallFunctionObjArgs(
  95. viewbaseset_and_func, self, other, NULL);
  96. }
  97. static PyObject *
  98. multidict_view_or(PyObject *self, PyObject *other)
  99. {
  100. return PyObject_CallFunctionObjArgs(
  101. viewbaseset_or_func, self, other, NULL);
  102. }
  103. static PyObject *
  104. multidict_view_sub(PyObject *self, PyObject *other)
  105. {
  106. return PyObject_CallFunctionObjArgs(
  107. viewbaseset_sub_func, self, other, NULL);
  108. }
  109. static PyObject *
  110. multidict_view_xor(PyObject *self, PyObject *other)
  111. {
  112. return PyObject_CallFunctionObjArgs(
  113. viewbaseset_xor_func, self, other, NULL);
  114. }
  115. static PyNumberMethods multidict_view_as_number = {
  116. 0, /* nb_add */
  117. (binaryfunc)multidict_view_sub, /* nb_subtract */
  118. 0, /* nb_multiply */
  119. 0, /* nb_remainder */
  120. 0, /* nb_divmod */
  121. 0, /* nb_power */
  122. 0, /* nb_negative */
  123. 0, /* nb_positive */
  124. 0, /* nb_absolute */
  125. 0, /* nb_bool */
  126. 0, /* nb_invert */
  127. 0, /* nb_lshift */
  128. 0, /* nb_rshift */
  129. (binaryfunc)multidict_view_and, /* nb_and */
  130. (binaryfunc)multidict_view_xor, /* nb_xor */
  131. (binaryfunc)multidict_view_or, /* nb_or */
  132. };
  133. /********** Items **********/
  134. PyObject *
  135. multidict_itemsview_new(PyObject *md)
  136. {
  137. _Multidict_ViewObject *mv = PyObject_GC_New(
  138. _Multidict_ViewObject, &multidict_itemsview_type);
  139. if (mv == NULL) {
  140. return NULL;
  141. }
  142. _init_view(mv, md);
  143. return (PyObject *)mv;
  144. }
  145. static PyObject *
  146. multidict_itemsview_iter(_Multidict_ViewObject *self)
  147. {
  148. PyObject *iter;
  149. PyObject *impl = _PyObject_CallMethodId(self->md, &PyId_impl, NULL);
  150. if (impl == NULL) {
  151. return NULL;
  152. }
  153. iter = multidict_items_iter_new(impl);
  154. Py_DECREF(impl);
  155. return iter;
  156. }
  157. static PyObject *
  158. multidict_itemsview_repr(_Multidict_ViewObject *self)
  159. {
  160. return PyObject_CallFunctionObjArgs(
  161. itemsview_repr_func, self, NULL);
  162. }
  163. static PyObject *
  164. multidict_itemsview_isdisjoint(_Multidict_ViewObject *self, PyObject *other)
  165. {
  166. return PyObject_CallFunctionObjArgs(
  167. itemsview_isdisjoint_func, self, other, NULL);
  168. }
  169. PyDoc_STRVAR(itemsview_isdisjoint_doc,
  170. "Return True if two sets have a null intersection.");
  171. static PyMethodDef multidict_itemsview_methods[] = {
  172. {
  173. "isdisjoint",
  174. (PyCFunction)multidict_itemsview_isdisjoint,
  175. METH_O,
  176. itemsview_isdisjoint_doc
  177. },
  178. {
  179. NULL,
  180. NULL
  181. } /* sentinel */
  182. };
  183. static int
  184. multidict_itemsview_contains(_Multidict_ViewObject *self, PyObject *obj)
  185. {
  186. PyObject *akey = NULL,
  187. *aval = NULL,
  188. *bkey = NULL,
  189. *bval = NULL,
  190. *iter = NULL,
  191. *item = NULL;
  192. int ret1, ret2;
  193. if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) {
  194. return 0;
  195. }
  196. bkey = PyTuple_GET_ITEM(obj, 0);
  197. bval = PyTuple_GET_ITEM(obj, 1);
  198. iter = multidict_itemsview_iter(self);
  199. if (iter == NULL) {
  200. return 0;
  201. }
  202. while ((item = PyIter_Next(iter)) != NULL) {
  203. akey = PyTuple_GET_ITEM(item, 0);
  204. aval = PyTuple_GET_ITEM(item, 1);
  205. ret1 = PyObject_RichCompareBool(akey, bkey, Py_EQ);
  206. if (ret1 < 0) {
  207. Py_DECREF(iter);
  208. Py_DECREF(item);
  209. return -1;
  210. }
  211. ret2 = PyObject_RichCompareBool(aval, bval, Py_EQ);
  212. if (ret2 < 0) {
  213. Py_DECREF(iter);
  214. Py_DECREF(item);
  215. return -1;
  216. }
  217. if (ret1 > 0 && ret2 > 0)
  218. {
  219. Py_DECREF(iter);
  220. Py_DECREF(item);
  221. return 1;
  222. }
  223. Py_DECREF(item);
  224. }
  225. Py_DECREF(iter);
  226. if (PyErr_Occurred()) {
  227. return -1;
  228. }
  229. return 0;
  230. }
  231. static PySequenceMethods multidict_itemsview_as_sequence = {
  232. (lenfunc)multidict_view_len, /* sq_length */
  233. 0, /* sq_concat */
  234. 0, /* sq_repeat */
  235. 0, /* sq_item */
  236. 0, /* sq_slice */
  237. 0, /* sq_ass_item */
  238. 0, /* sq_ass_slice */
  239. (objobjproc)multidict_itemsview_contains, /* sq_contains */
  240. };
  241. static PyTypeObject multidict_itemsview_type = {
  242. PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
  243. "_ItemsView", /* tp_name */
  244. sizeof(_Multidict_ViewObject), /* tp_basicsize */
  245. 0, /* tp_itemsize */
  246. (destructor)multidict_view_dealloc, /* tp_dealloc */
  247. 0, /* tp_print */
  248. 0, /* tp_getattr */
  249. 0, /* tp_setattr */
  250. 0, /* tp_reserved */
  251. (reprfunc)multidict_itemsview_repr, /* tp_repr */
  252. &multidict_view_as_number, /* tp_as_number */
  253. &multidict_itemsview_as_sequence, /* tp_as_sequence */
  254. 0, /* tp_as_mapping */
  255. 0, /* tp_hash */
  256. 0, /* tp_call */
  257. 0, /* tp_str */
  258. PyObject_GenericGetAttr, /* tp_getattro */
  259. 0, /* tp_setattro */
  260. 0, /* tp_as_buffer */
  261. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  262. 0, /* tp_doc */
  263. (traverseproc)multidict_view_traverse, /* tp_traverse */
  264. (inquiry)multidict_view_clear, /* tp_clear */
  265. multidict_view_richcompare, /* tp_richcompare */
  266. 0, /* tp_weaklistoffset */
  267. (getiterfunc)multidict_itemsview_iter, /* tp_iter */
  268. 0, /* tp_iternext */
  269. multidict_itemsview_methods, /* tp_methods */
  270. };
  271. /********** Keys **********/
  272. PyObject *
  273. multidict_keysview_new(PyObject *md)
  274. {
  275. _Multidict_ViewObject *mv = PyObject_GC_New(
  276. _Multidict_ViewObject, &multidict_keysview_type);
  277. if (mv == NULL) {
  278. return NULL;
  279. }
  280. _init_view(mv, md);
  281. return (PyObject *)mv;
  282. }
  283. static PyObject *
  284. multidict_keysview_iter(_Multidict_ViewObject *self)
  285. {
  286. PyObject *iter;
  287. PyObject *impl = _PyObject_CallMethodId(self->md, &PyId_impl, NULL);
  288. if (impl == NULL) {
  289. return NULL;
  290. }
  291. iter = multidict_keys_iter_new(impl);
  292. Py_DECREF(impl);
  293. return iter;
  294. }
  295. static PyObject *
  296. multidict_keysview_repr(_Multidict_ViewObject *self)
  297. {
  298. return PyObject_CallFunctionObjArgs(
  299. keysview_repr_func, self, NULL);
  300. }
  301. static PyObject *
  302. multidict_keysview_isdisjoint(_Multidict_ViewObject *self, PyObject *other)
  303. {
  304. return PyObject_CallFunctionObjArgs(
  305. keysview_isdisjoint_func, self, other, NULL);
  306. }
  307. PyDoc_STRVAR(keysview_isdisjoint_doc,
  308. "Return True if two sets have a null intersection.");
  309. static PyMethodDef multidict_keysview_methods[] = {
  310. {
  311. "isdisjoint",
  312. (PyCFunction)multidict_keysview_isdisjoint,
  313. METH_O,
  314. keysview_isdisjoint_doc
  315. },
  316. {
  317. NULL,
  318. NULL
  319. } /* sentinel */
  320. };
  321. static int
  322. multidict_keysview_contains(_Multidict_ViewObject *self, PyObject *key)
  323. {
  324. int ret;
  325. PyObject *impl = _PyObject_CallMethodId(self->md, &PyId_impl, NULL);
  326. if (impl == NULL) {
  327. return -1;
  328. }
  329. ret = pair_list_contains(impl, key);
  330. Py_DECREF(impl);
  331. return ret;
  332. }
  333. static PySequenceMethods multidict_keysview_as_sequence = {
  334. (lenfunc)multidict_view_len, /* sq_length */
  335. 0, /* sq_concat */
  336. 0, /* sq_repeat */
  337. 0, /* sq_item */
  338. 0, /* sq_slice */
  339. 0, /* sq_ass_item */
  340. 0, /* sq_ass_slice */
  341. (objobjproc)multidict_keysview_contains, /* sq_contains */
  342. };
  343. static PyTypeObject multidict_keysview_type = {
  344. PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
  345. "_KeysView", /* tp_name */
  346. sizeof(_Multidict_ViewObject), /* tp_basicsize */
  347. 0, /* tp_itemsize */
  348. (destructor)multidict_view_dealloc, /* tp_dealloc */
  349. 0, /* tp_print */
  350. 0, /* tp_getattr */
  351. 0, /* tp_setattr */
  352. 0, /* tp_reserved */
  353. (reprfunc)multidict_keysview_repr, /* tp_repr */
  354. &multidict_view_as_number, /* tp_as_number */
  355. &multidict_keysview_as_sequence, /* tp_as_sequence */
  356. 0, /* tp_as_mapping */
  357. 0, /* tp_hash */
  358. 0, /* tp_call */
  359. 0, /* tp_str */
  360. PyObject_GenericGetAttr, /* tp_getattro */
  361. 0, /* tp_setattro */
  362. 0, /* tp_as_buffer */
  363. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  364. 0, /* tp_doc */
  365. (traverseproc)multidict_view_traverse, /* tp_traverse */
  366. (inquiry)multidict_view_clear, /* tp_clear */
  367. multidict_view_richcompare, /* tp_richcompare */
  368. 0, /* tp_weaklistoffset */
  369. (getiterfunc)multidict_keysview_iter, /* tp_iter */
  370. 0, /* tp_iternext */
  371. multidict_keysview_methods, /* tp_methods */
  372. };
  373. /********** Values **********/
  374. PyObject *
  375. multidict_valuesview_new(PyObject *md)
  376. {
  377. _Multidict_ViewObject *mv = PyObject_GC_New(
  378. _Multidict_ViewObject, &multidict_valuesview_type);
  379. if (mv == NULL) {
  380. return NULL;
  381. }
  382. _init_view(mv, md);
  383. return (PyObject *)mv;
  384. }
  385. static PyObject *
  386. multidict_valuesview_iter(_Multidict_ViewObject *self)
  387. {
  388. PyObject *iter;
  389. PyObject *impl = _PyObject_CallMethodId(self->md, &PyId_impl, NULL);
  390. if (impl == NULL) {
  391. return NULL;
  392. }
  393. iter = multidict_values_iter_new(impl);
  394. Py_DECREF(impl);
  395. return iter;
  396. }
  397. static PyObject *
  398. multidict_valuesview_repr(_Multidict_ViewObject *self)
  399. {
  400. return PyObject_CallFunctionObjArgs(
  401. valuesview_repr_func, self, NULL);
  402. }
  403. static PySequenceMethods multidict_valuesview_as_sequence = {
  404. (lenfunc)multidict_view_len, /* sq_length */
  405. 0, /* sq_concat */
  406. 0, /* sq_repeat */
  407. 0, /* sq_item */
  408. 0, /* sq_slice */
  409. 0, /* sq_ass_item */
  410. 0, /* sq_ass_slice */
  411. (objobjproc)0, /* sq_contains */
  412. };
  413. static PyTypeObject multidict_valuesview_type = {
  414. PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
  415. "_ValuesView", /* tp_name */
  416. sizeof(_Multidict_ViewObject), /* tp_basicsize */
  417. 0, /* tp_itemsize */
  418. (destructor)multidict_view_dealloc, /* tp_dealloc */
  419. 0, /* tp_print */
  420. 0, /* tp_getattr */
  421. 0, /* tp_setattr */
  422. 0, /* tp_reserved */
  423. (reprfunc)multidict_valuesview_repr, /* tp_repr */
  424. 0, /* tp_as_number */
  425. &multidict_valuesview_as_sequence, /* tp_as_sequence */
  426. 0, /* tp_as_mapping */
  427. 0, /* tp_hash */
  428. 0, /* tp_call */
  429. 0, /* tp_str */
  430. PyObject_GenericGetAttr, /* tp_getattro */
  431. 0, /* tp_setattro */
  432. 0, /* tp_as_buffer */
  433. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
  434. 0, /* tp_doc */
  435. (traverseproc)multidict_view_traverse, /* tp_traverse */
  436. (inquiry)multidict_view_clear, /* tp_clear */
  437. 0, /* tp_richcompare */
  438. 0, /* tp_weaklistoffset */
  439. (getiterfunc)multidict_valuesview_iter, /* tp_iter */
  440. 0, /* tp_iternext */
  441. };
  442. int
  443. multidict_views_init()
  444. {
  445. PyObject *reg_func_call_result = NULL;
  446. PyObject *module = PyImport_ImportModule("multidict._multidict_base");
  447. if (module == NULL) {
  448. goto fail;
  449. }
  450. #define GET_MOD_ATTR(VAR, NAME) \
  451. VAR = PyObject_GetAttrString(module, NAME); \
  452. if (VAR == NULL) { \
  453. goto fail; \
  454. }
  455. GET_MOD_ATTR(viewbaseset_richcmp_func, "_viewbaseset_richcmp");
  456. GET_MOD_ATTR(viewbaseset_and_func, "_viewbaseset_and");
  457. GET_MOD_ATTR(viewbaseset_or_func, "_viewbaseset_or");
  458. GET_MOD_ATTR(viewbaseset_sub_func, "_viewbaseset_sub");
  459. GET_MOD_ATTR(viewbaseset_xor_func, "_viewbaseset_xor");
  460. GET_MOD_ATTR(abc_itemsview_register_func, "_abc_itemsview_register");
  461. GET_MOD_ATTR(abc_keysview_register_func, "_abc_keysview_register");
  462. GET_MOD_ATTR(abc_valuesview_register_func, "_abc_valuesview_register");
  463. GET_MOD_ATTR(itemsview_repr_func, "_itemsview_isdisjoint");
  464. GET_MOD_ATTR(itemsview_repr_func, "_itemsview_repr");
  465. GET_MOD_ATTR(keysview_repr_func, "_keysview_repr");
  466. GET_MOD_ATTR(keysview_isdisjoint_func, "_keysview_isdisjoint");
  467. GET_MOD_ATTR(valuesview_repr_func, "_valuesview_repr");
  468. if (multidict_iter_init() < 0) {
  469. goto fail;
  470. }
  471. if (PyType_Ready(&multidict_itemsview_type) < 0 ||
  472. PyType_Ready(&multidict_valuesview_type) < 0 ||
  473. PyType_Ready(&multidict_keysview_type) < 0)
  474. {
  475. goto fail;
  476. }
  477. // abc.ItemsView.register(_ItemsView)
  478. reg_func_call_result = PyObject_CallFunctionObjArgs(
  479. abc_itemsview_register_func, (PyObject*)&multidict_itemsview_type, NULL);
  480. if (reg_func_call_result == NULL) {
  481. goto fail;
  482. }
  483. Py_DECREF(reg_func_call_result);
  484. // abc.KeysView.register(_KeysView)
  485. reg_func_call_result = PyObject_CallFunctionObjArgs(
  486. abc_keysview_register_func, (PyObject*)&multidict_keysview_type, NULL);
  487. if (reg_func_call_result == NULL) {
  488. goto fail;
  489. }
  490. Py_DECREF(reg_func_call_result);
  491. // abc.ValuesView.register(_KeysView)
  492. reg_func_call_result = PyObject_CallFunctionObjArgs(
  493. abc_valuesview_register_func, (PyObject*)&multidict_valuesview_type, NULL);
  494. if (reg_func_call_result == NULL) {
  495. goto fail;
  496. }
  497. Py_DECREF(reg_func_call_result);
  498. Py_DECREF(module);
  499. return 0;
  500. fail:
  501. Py_CLEAR(module);
  502. return -1;
  503. #undef GET_MOD_ATTR
  504. }