qbytearray.sip 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // qbytearray.sip generated by MetaSIP
  2. //
  3. // This file is part of the QtCore Python extension module.
  4. //
  5. // Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  6. //
  7. // This file is part of PyQt6.
  8. //
  9. // This file may be used under the terms of the GNU General Public License
  10. // version 3.0 as published by the Free Software Foundation and appearing in
  11. // the file LICENSE included in the packaging of this file. Please review the
  12. // following information to ensure the GNU General Public License version 3.0
  13. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  14. //
  15. // If you do not wish to use this file under the terms of the GPL version 3.0
  16. // then you may purchase a commercial license. For more information contact
  17. // info@riverbankcomputing.com.
  18. //
  19. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  20. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21. %ModuleCode
  22. #include <qbytearray.h>
  23. %End
  24. class QByteArray /TypeHintIn="Union[QByteArray, bytes, bytearray, memoryview]"/
  25. {
  26. %TypeHeaderCode
  27. #include <qbytearray.h>
  28. %End
  29. %TypeCode
  30. // This is needed by __hash__().
  31. #include <qhash.h>
  32. // Convenience function for converting a QByteArray to a Python str object.
  33. static PyObject *QByteArrayToPyStr(QByteArray *ba)
  34. {
  35. char *data = ba->data();
  36. if (data)
  37. // QByteArrays may have embedded '\0's so set the size explicitly.
  38. return PyBytes_FromStringAndSize(data, ba->size());
  39. return PyBytes_FromString("");
  40. }
  41. %End
  42. %ConvertToTypeCode
  43. // Accept anything that implements the buffer protocol (including QByteArray
  44. // itself).
  45. if (sipIsErr == NULL)
  46. return sipGetBufferInfo(sipPy, NULL);
  47. // If it is already a QByteArray then use it rather than make an unnecessary
  48. // copy of the data.
  49. if (sipCanConvertToType(sipPy, sipType_QByteArray, SIP_NO_CONVERTORS))
  50. {
  51. *sipCppPtr = reinterpret_cast<QByteArray *>(
  52. sipConvertToType(sipPy, sipType_QByteArray, sipTransferObj,
  53. SIP_NO_CONVERTORS, 0, sipIsErr));
  54. return 0;
  55. }
  56. // Create a QByteArray from the data.
  57. sipBufferInfoDef buffer_info;
  58. if (sipGetBufferInfo(sipPy, &buffer_info) > 0)
  59. {
  60. // Check that the buffer is compatible with one defined by
  61. // PyBuffer_FillInfo() as used by QByteArray and the standard Python byte
  62. // objects.
  63. if (buffer_info.bi_format == NULL || buffer_info.bi_format[0] == 'B')
  64. {
  65. *sipCppPtr = new QByteArray(
  66. reinterpret_cast<const char *>(buffer_info.bi_buf),
  67. (qsizetype)buffer_info.bi_len);
  68. sipReleaseBufferInfo(&buffer_info);
  69. // We don't support transfer of ownership.
  70. return SIP_TEMPORARY;
  71. }
  72. PyErr_Format(PyExc_TypeError, "unsupported buffer format '%s'",
  73. buffer_info.bi_format);
  74. sipReleaseBufferInfo(&buffer_info);
  75. }
  76. *sipIsErr = 1;
  77. return 0;
  78. %End
  79. %BIGetBufferCode
  80. // We may be building against a debug Python build.
  81. #if defined(Py_LIMITED_API)
  82. Q_UNUSED(sipSelf);
  83. sipBuffer->bd_buffer = sipCpp->data();
  84. sipBuffer->bd_length = sipCpp->size();
  85. sipBuffer->bd_readonly = 0;
  86. sipRes = 0;
  87. #else
  88. sipRes = PyBuffer_FillInfo(sipBuffer, sipSelf, sipCpp->data(),
  89. sipCpp->size(), 0, sipFlags);
  90. #endif
  91. %End
  92. %PickleCode
  93. sipRes = Py_BuildValue("(y#)", sipCpp->data(), static_cast<Py_ssize_t>(sipCpp->size()));
  94. %End
  95. public:
  96. QByteArray();
  97. QByteArray(qsizetype size, char c /Encoding="None"/);
  98. QByteArray(const QByteArray &a);
  99. ~QByteArray();
  100. void resize(qsizetype size);
  101. %If (Qt_6_4_0 -)
  102. void resize(qsizetype size, char c);
  103. %End
  104. QByteArray &fill(char c /Encoding="None"/, qsizetype size = -1);
  105. void clear();
  106. qsizetype indexOf(QByteArrayView bv, qsizetype from = 0) const;
  107. // In Qt v6.2 this was replaced by two separate overloads. However we need to keep the optional keyword argument.
  108. qsizetype lastIndexOf(QByteArrayView bv, qsizetype from = -1) const;
  109. qsizetype count(QByteArrayView bv) const;
  110. QByteArray left(qsizetype len) const;
  111. QByteArray right(qsizetype len) const;
  112. QByteArray mid(qsizetype index, qsizetype length = -1) const;
  113. QByteArray first(qsizetype n) const;
  114. QByteArray last(qsizetype n) const;
  115. bool startsWith(QByteArrayView bv) const;
  116. bool endsWith(QByteArrayView bv) const;
  117. void truncate(qsizetype pos);
  118. void chop(qsizetype n);
  119. QByteArray toLower() const;
  120. QByteArray toUpper() const;
  121. QByteArray trimmed() const;
  122. QByteArray simplified() const;
  123. QByteArray leftJustified(qsizetype width, char fill /Encoding="None"/ = ' ', bool truncate = false) const;
  124. QByteArray rightJustified(qsizetype width, char fill /Encoding="None"/ = ' ', bool truncate = false) const;
  125. QByteArray &prepend(QByteArrayView a);
  126. QByteArray &prepend(qsizetype count, char c /Encoding="None"/);
  127. QByteArray &append(QByteArrayView a);
  128. QByteArray &append(qsizetype count, char c /Encoding="None"/);
  129. QByteArray &insert(qsizetype i, QByteArrayView data);
  130. QByteArray &insert(qsizetype i, qsizetype count, char c /Encoding="None"/);
  131. QByteArray &remove(qsizetype index, qsizetype len);
  132. QByteArray &replace(QByteArrayView before, QByteArrayView after);
  133. QByteArray &replace(qsizetype index, qsizetype len, QByteArrayView s);
  134. QList<QByteArray> split(char sep /Encoding="None"/) const;
  135. QByteArray &operator+=(QByteArrayView a);
  136. bool operator==(const QString &s2) const;
  137. %MethodCode
  138. // Needed for Qt v6.8 and later.
  139. sipRes = (*sipCpp == *a0);
  140. %End
  141. bool operator!=(const QString &s2) const;
  142. %MethodCode
  143. // Needed for Qt v6.8 and later.
  144. sipRes = (*sipCpp != *a0);
  145. %End
  146. bool operator<(const QString &s2) const;
  147. %MethodCode
  148. // Needed for Qt v6.8 and later.
  149. sipRes = (*sipCpp < *a0);
  150. %End
  151. bool operator>(const QString &s2) const;
  152. %MethodCode
  153. // Needed for Qt v6.8 and later.
  154. sipRes = (*sipCpp > *a0);
  155. %End
  156. bool operator<=(const QString &s2) const;
  157. %MethodCode
  158. // Needed for Qt v6.8 and later.
  159. sipRes = (*sipCpp <= *a0);
  160. %End
  161. bool operator>=(const QString &s2) const;
  162. %MethodCode
  163. // Needed for Qt v6.8 and later.
  164. sipRes = (*sipCpp >= *a0);
  165. %End
  166. short toShort(bool *ok = 0, int base = 10) const;
  167. ushort toUShort(bool *ok = 0, int base = 10) const;
  168. int toInt(bool *ok = 0, int base = 10) const;
  169. uint toUInt(bool *ok = 0, int base = 10) const;
  170. long toLong(bool *ok = 0, int base = 10) const;
  171. ulong toULong(bool *ok = 0, int base = 10) const;
  172. qlonglong toLongLong(bool *ok = 0, int base = 10) const;
  173. qulonglong toULongLong(bool *ok = 0, int base = 10) const;
  174. float toFloat(bool *ok = 0) const;
  175. double toDouble(bool *ok = 0) const;
  176. QByteArray toBase64(QByteArray::Base64Options options = QByteArray::Base64Encoding) const;
  177. QByteArray &setNum(double n /Constrained/, char format = 'g', int precision = 6);
  178. QByteArray &setNum(SIP_PYOBJECT n /TypeHint="int"/, int base = 10);
  179. %MethodCode
  180. qlonglong val = sipLong_AsLongLong(a0);
  181. if (!PyErr_Occurred())
  182. {
  183. sipRes = &sipCpp->setNum(val, a1);
  184. }
  185. else
  186. {
  187. // If it is positive then it might fit an unsigned long long.
  188. qulonglong uval = sipLong_AsUnsignedLongLong(a0);
  189. if (!PyErr_Occurred())
  190. {
  191. sipRes = &sipCpp->setNum(uval, a1);
  192. }
  193. else
  194. {
  195. sipError = (PyErr_ExceptionMatches(PyExc_OverflowError)
  196. ? sipErrorFail : sipErrorContinue);
  197. }
  198. }
  199. %End
  200. static QByteArray number(double n /Constrained/, char format = 'g', int precision = 6);
  201. static QByteArray number(SIP_PYOBJECT n /TypeHint="int"/, int base = 10);
  202. %MethodCode
  203. qlonglong val = sipLong_AsLongLong(a0);
  204. if (!PyErr_Occurred())
  205. {
  206. sipRes = new QByteArray(QByteArray::number(val, a1));
  207. }
  208. else
  209. {
  210. // If it is positive then it might fit an unsigned long long.
  211. qulonglong uval = sipLong_AsUnsignedLongLong(a0);
  212. if (!PyErr_Occurred())
  213. {
  214. sipRes = new QByteArray(QByteArray::number(uval, a1));
  215. }
  216. else
  217. {
  218. sipError = (PyErr_ExceptionMatches(PyExc_OverflowError)
  219. ? sipErrorFail : sipErrorContinue);
  220. }
  221. }
  222. %End
  223. static QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options = QByteArray::Base64Encoding);
  224. static QByteArray fromHex(const QByteArray &hexEncoded);
  225. qsizetype count() const;
  226. qsizetype length() const;
  227. bool isNull() const;
  228. qsizetype size() const /__len__/;
  229. char at(qsizetype i) const /Encoding="None"/;
  230. char operator[](qsizetype i) const /Encoding="None"/;
  231. %MethodCode
  232. Py_ssize_t idx = sipConvertFromSequenceIndex(a0, sipCpp->size());
  233. if (idx < 0)
  234. sipIsErr = 1;
  235. else
  236. sipRes = sipCpp->operator[]((qsizetype)idx);
  237. %End
  238. QByteArray operator[](SIP_PYSLICE slice) const;
  239. %MethodCode
  240. Py_ssize_t start, stop, step, slicelength;
  241. if (sipConvertFromSliceObject(a0, sipCpp->size(), &start, &stop, &step, &slicelength) < 0)
  242. {
  243. sipIsErr = 1;
  244. }
  245. else
  246. {
  247. sipRes = new QByteArray();
  248. for (Py_ssize_t i = 0; i < slicelength; ++i)
  249. {
  250. sipRes->append(sipCpp->at((qsizetype)start));
  251. start += step;
  252. }
  253. }
  254. %End
  255. int __contains__(const QByteArrayView bv) const;
  256. %MethodCode
  257. sipRes = sipCpp->contains(*a0);
  258. %End
  259. Py_hash_t __hash__() const;
  260. %MethodCode
  261. sipRes = qHash(*sipCpp);
  262. %End
  263. SIP_PYOBJECT __str__() const /TypeHint="str"/;
  264. %MethodCode
  265. sipRes = QByteArrayToPyStr(sipCpp);
  266. PyObject *repr = PyObject_Repr(sipRes);
  267. if (repr)
  268. {
  269. Py_DECREF(sipRes);
  270. sipRes = repr;
  271. }
  272. %End
  273. SIP_PYOBJECT __repr__() const /TypeHint="str"/;
  274. %MethodCode
  275. if (sipCpp->isNull())
  276. {
  277. sipRes = PyUnicode_FromString("PyQt6.QtCore.QByteArray()");
  278. }
  279. else
  280. {
  281. PyObject *str = QByteArrayToPyStr(sipCpp);
  282. if (str)
  283. {
  284. sipRes = PyUnicode_FromFormat("PyQt6.QtCore.QByteArray(%R)", str);
  285. Py_DECREF(str);
  286. }
  287. }
  288. %End
  289. QByteArray operator*(int m) const;
  290. %MethodCode
  291. sipRes = new QByteArray();
  292. while (a0-- > 0)
  293. *sipRes += *sipCpp;
  294. %End
  295. QByteArray &operator*=(int m);
  296. %MethodCode
  297. QByteArray orig(*sipCpp);
  298. sipCpp->clear();
  299. while (a0-- > 0)
  300. *sipCpp += orig;
  301. %End
  302. bool isEmpty() const;
  303. SIP_PYOBJECT data() /TypeHint="bytes"/;
  304. %MethodCode
  305. // QByteArrays may contain embedded '\0's so set the size explicitly.
  306. char *res = sipCpp->data();
  307. qsizetype len = sipCpp->size();
  308. if (res)
  309. {
  310. if ((sipRes = PyBytes_FromStringAndSize(res, (Py_ssize_t)len)) == NULL)
  311. sipIsErr = 1;
  312. }
  313. else
  314. {
  315. Py_INCREF(Py_None);
  316. sipRes = Py_None;
  317. }
  318. %End
  319. qsizetype capacity() const;
  320. void reserve(qsizetype size);
  321. void squeeze();
  322. void push_back(QByteArrayView a);
  323. void push_front(QByteArrayView a);
  324. bool contains(QByteArrayView bv) const;
  325. QByteArray toHex(char separator /Encoding="None"/ = '\x00') const;
  326. QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(), const QByteArray &include = QByteArray(), char percent = '%') const;
  327. static QByteArray fromPercentEncoding(const QByteArray &input, char percent = '%');
  328. QByteArray repeated(qsizetype times) const;
  329. void swap(QByteArray &other /Constrained/);
  330. enum Base64Option /BaseType=Flag/
  331. {
  332. Base64Encoding,
  333. Base64UrlEncoding,
  334. KeepTrailingEquals,
  335. OmitTrailingEquals,
  336. IgnoreBase64DecodingErrors,
  337. AbortOnBase64DecodingErrors,
  338. };
  339. typedef QFlags<QByteArray::Base64Option> Base64Options;
  340. QByteArray chopped(qsizetype len) const;
  341. int compare(QByteArrayView a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
  342. bool isUpper() const;
  343. bool isLower() const;
  344. enum class Base64DecodingStatus
  345. {
  346. Ok,
  347. IllegalInputLength,
  348. IllegalCharacter,
  349. IllegalPadding,
  350. };
  351. static QByteArray::FromBase64Result fromBase64Encoding(const QByteArray &base64, QByteArray::Base64Options options = QByteArray::Base64Encoding);
  352. class FromBase64Result
  353. {
  354. %TypeHeaderCode
  355. #include <qbytearray.h>
  356. %End
  357. public:
  358. QByteArray decoded;
  359. QByteArray::Base64DecodingStatus decodingStatus;
  360. void swap(QByteArray::FromBase64Result &other /Constrained/);
  361. operator bool() const;
  362. %MethodCode
  363. // This is required because SIP doesn't handle operator bool() properly.
  364. sipRes = sipCpp->operator bool();
  365. %End
  366. Py_hash_t __hash__() const;
  367. %MethodCode
  368. sipRes = qHash(*sipCpp);
  369. %End
  370. };
  371. QByteArray sliced(qsizetype pos) const;
  372. QByteArray sliced(qsizetype pos, qsizetype n) const;
  373. %If (Qt_6_3_0 -)
  374. bool isValidUtf8() const;
  375. %End
  376. %If (Qt_6_4_0 -)
  377. QByteArray percentDecoded(char percent = '%') const;
  378. %End
  379. %If (Qt_6_5_0 -)
  380. QByteArray &removeAt(qsizetype pos);
  381. %End
  382. %If (Qt_6_5_0 -)
  383. QByteArray &removeFirst();
  384. %End
  385. %If (Qt_6_5_0 -)
  386. QByteArray &removeLast();
  387. %End
  388. %If (Qt_6_6_0 -)
  389. QByteArray &assign(QByteArrayView v);
  390. %End
  391. %If (Qt_6_8_0 -)
  392. QByteArray &slice(qsizetype pos, qsizetype n);
  393. %End
  394. %If (Qt_6_8_0 -)
  395. QByteArray &slice(qsizetype pos);
  396. %End
  397. %If (Qt_6_8_0 -)
  398. qsizetype max_size() const;
  399. %End
  400. %If (Qt_6_8_0 -)
  401. static qsizetype maxSize();
  402. %End
  403. %If (Qt_6_10_0 -)
  404. QByteArray nullTerminated() const;
  405. %End
  406. %If (Qt_6_10_0 -)
  407. QByteArray &nullTerminate();
  408. %End
  409. };
  410. bool operator==(const QByteArray &a1, const QByteArray &a2);
  411. bool operator!=(const QByteArray &a1, const QByteArray &a2);
  412. bool operator<(const QByteArray &a1, const QByteArray &a2);
  413. bool operator<=(const QByteArray &a1, const QByteArray &a2);
  414. bool operator>(const QByteArray &a1, const QByteArray &a2);
  415. bool operator>=(const QByteArray &a1, const QByteArray &a2);
  416. %If (Qt_6_5_0 -)
  417. QByteArray operator+(const QByteArray &a1, const QByteArray &a2);
  418. %End
  419. %If (- Qt_6_5_0)
  420. const QByteArray operator+(const QByteArray &a1, const QByteArray &a2);
  421. %End
  422. QDataStream &operator<<(QDataStream &, const QByteArray & /Constrained/) /ReleaseGIL/;
  423. QDataStream &operator>>(QDataStream &, QByteArray & /Constrained/) /ReleaseGIL/;
  424. QByteArray qCompress(const uchar *data /Array/, qsizetype nbytes /ArraySize/, int compressionLevel = -1);
  425. QByteArray qCompress(const QByteArray &data, int compressionLevel = -1);
  426. QByteArray qUncompress(const uchar *data /Array/, qsizetype nbytes /ArraySize/);
  427. QByteArray qUncompress(const QByteArray &data);
  428. %If (- Qt_6_2_0)
  429. quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard = Qt::ChecksumIso3309);
  430. %End
  431. bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs);
  432. bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs);