qpynetwork_qhash.sip 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // This is the SIP interface definition for the QHash based mapped types
  2. // specific to the QtNetwork module.
  3. //
  4. // Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  5. //
  6. // This file is part of PyQt6.
  7. //
  8. // This file may be used under the terms of the GNU General Public License
  9. // version 3.0 as published by the Free Software Foundation and appearing in
  10. // the file LICENSE included in the packaging of this file. Please review the
  11. // following information to ensure the GNU General Public License version 3.0
  12. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  13. //
  14. // If you do not wish to use this file under the terms of the GPL version 3.0
  15. // then you may purchase a commercial license. For more information contact
  16. // info@riverbankcomputing.com.
  17. //
  18. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. %MappedType QHash<QNetworkRequest::Attribute, QVariant>
  21. /TypeHint="Dict[QNetworkRequest.Attribute, QVariant]",
  22. TypeHintValue="{}"/
  23. {
  24. %TypeHeaderCode
  25. #include <qhash.h>
  26. #include <qnetworkrequest.h>
  27. #include <qvariant.h>
  28. %End
  29. %ConvertFromTypeCode
  30. PyObject *d = PyDict_New();
  31. if (!d)
  32. return 0;
  33. QHash<QNetworkRequest::Attribute, QVariant>::const_iterator it = sipCpp->constBegin();
  34. QHash<QNetworkRequest::Attribute, QVariant>::const_iterator end = sipCpp->constEnd();
  35. while (it != end)
  36. {
  37. PyObject *kobj = sipConvertFromEnum(it.key(),
  38. sipType_QNetworkRequest_Attribute);
  39. if (!kobj)
  40. {
  41. Py_DECREF(d);
  42. return 0;
  43. }
  44. QVariant *v = new QVariant(it.value());
  45. PyObject *vobj = sipConvertFromNewType(v, sipType_QVariant,
  46. sipTransferObj);
  47. if (!vobj)
  48. {
  49. delete v;
  50. Py_DECREF(kobj);
  51. Py_DECREF(d);
  52. return 0;
  53. }
  54. int rc = PyDict_SetItem(d, kobj, vobj);
  55. Py_DECREF(vobj);
  56. Py_DECREF(kobj);
  57. if (rc < 0)
  58. {
  59. Py_DECREF(d);
  60. return 0;
  61. }
  62. ++it;
  63. }
  64. return d;
  65. %End
  66. %ConvertToTypeCode
  67. if (!sipIsErr)
  68. return PyDict_Check(sipPy);
  69. QHash<QNetworkRequest::Attribute, QVariant> *qh = new QHash<QNetworkRequest::Attribute, QVariant>;
  70. Py_ssize_t pos = 0;
  71. PyObject *kobj, *vobj;
  72. while (PyDict_Next(sipPy, &pos, &kobj, &vobj))
  73. {
  74. int k = sipConvertToEnum(kobj, sipType_QNetworkRequest_Attribute);
  75. if (PyErr_Occurred())
  76. {
  77. PyErr_Format(PyExc_TypeError,
  78. "a key has type '%s' but 'QNetworkRequest.Attribute' is expected",
  79. sipPyTypeName(Py_TYPE(kobj)));
  80. delete qh;
  81. *sipIsErr = 1;
  82. return 0;
  83. }
  84. int vstate;
  85. QVariant *v = reinterpret_cast<QVariant *>(
  86. sipForceConvertToType(vobj, sipType_QVariant, sipTransferObj,
  87. SIP_NOT_NONE, &vstate, sipIsErr));
  88. if (*sipIsErr)
  89. {
  90. // Any error must be internal, so leave the exception as it is.
  91. delete qh;
  92. return 0;
  93. }
  94. qh->insert(static_cast<QNetworkRequest::Attribute>(k), *v);
  95. sipReleaseType(v, sipType_QVariant, vstate);
  96. }
  97. *sipCppPtr = qh;
  98. return sipGetState(sipTransferObj);
  99. %End
  100. };