qjsonarray.sip 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // This is the SIP interface definition for the QJsonArray mapped type.
  2. //
  3. // Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of PyQt6.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. // Note that we assume any iterable can be converted to a QJsonArray. However,
  20. // because QJsonValue is an iterable and QJsonObject is implemented as a dict
  21. // (which is also an iterable), then any overloads that handle one or more of
  22. // them must be ordered so that QJsonArray is checked last.
  23. %MappedType QJsonArray
  24. /TypeHintIn="Iterable[QJsonValue]", TypeHintOut="List[QJsonValue]",
  25. TypeHintValue="[]"/
  26. {
  27. %TypeHeaderCode
  28. #include <qjsonarray.h>
  29. %End
  30. %ConvertFromTypeCode
  31. PyObject *l = PyList_New(sipCpp->size());
  32. if (!l)
  33. return 0;
  34. for (int i = 0; i < sipCpp->size(); ++i)
  35. {
  36. QJsonValue *t = new QJsonValue(sipCpp->at(i));
  37. PyObject *tobj = sipConvertFromNewType(t, sipType_QJsonValue,
  38. sipTransferObj);
  39. if (!tobj)
  40. {
  41. delete t;
  42. Py_DECREF(l);
  43. return 0;
  44. }
  45. PyList_SetItem(l, i, tobj);
  46. }
  47. return l;
  48. %End
  49. %ConvertToTypeCode
  50. PyObject *iter = PyObject_GetIter(sipPy);
  51. if (!sipIsErr)
  52. {
  53. PyErr_Clear();
  54. Py_XDECREF(iter);
  55. return (iter && !PyUnicode_Check(sipPy));
  56. }
  57. if (!iter)
  58. {
  59. *sipIsErr = 1;
  60. return 0;
  61. }
  62. QJsonArray *ql = new QJsonArray;
  63. for (Py_ssize_t i = 0; ; ++i)
  64. {
  65. PyErr_Clear();
  66. PyObject *itm = PyIter_Next(iter);
  67. if (!itm)
  68. {
  69. if (PyErr_Occurred())
  70. {
  71. delete ql;
  72. Py_DECREF(iter);
  73. *sipIsErr = 1;
  74. return 0;
  75. }
  76. break;
  77. }
  78. int state;
  79. QJsonValue *t = reinterpret_cast<QJsonValue *>(
  80. sipForceConvertToType(itm, sipType_QJsonValue, sipTransferObj,
  81. SIP_NOT_NONE, &state, sipIsErr));
  82. if (*sipIsErr)
  83. {
  84. PyErr_Format(PyExc_TypeError,
  85. "index %zd has type '%s' but 'QJsonValue' is expected", i,
  86. sipPyTypeName(Py_TYPE(itm)));
  87. Py_DECREF(itm);
  88. delete ql;
  89. Py_DECREF(iter);
  90. return 0;
  91. }
  92. ql->append(*t);
  93. sipReleaseType(t, sipType_QJsonValue, state);
  94. Py_DECREF(itm);
  95. }
  96. Py_DECREF(iter);
  97. *sipCppPtr = ql;
  98. return sipGetState(sipTransferObj);
  99. %End
  100. };