diff options
author | aneejit1 <[email protected]> | 2022-04-19 13:21:52 +0000 |
---|---|---|
committer | Slávek Banko <[email protected]> | 2022-07-28 00:56:02 +0200 |
commit | 52f8f8436dc2af136156ef95dbb8463481a78df8 (patch) | |
tree | 50e5f757a67774f98bfa931ef191dcc07683996d /sip/qt/qbytearray.sip | |
parent | 228b87ea89625d0783fdfe60114eba89e0f37942 (diff) | |
download | pytqt-52f8f8436dc2af136156ef95dbb8463481a78df8.tar.gz pytqt-52f8f8436dc2af136156ef95dbb8463481a78df8.zip |
Updates to support Python version 3
Amendments to the sip source and configuration/build scripts to allow
for support under Python version 3. The examples have been updated
using "2to3" along with some manual changes to sort out intentation
and casting to integer from float.
Signed-off-by: aneejit1 <[email protected]>
Signed-off-by: Slávek Banko <[email protected]>
(cherry picked from commit 6be046642290c28c17949022fb66ae02ac21d544)
Diffstat (limited to 'sip/qt/qbytearray.sip')
-rw-r--r-- | sip/qt/qbytearray.sip | 115 |
1 files changed, 81 insertions, 34 deletions
diff --git a/sip/qt/qbytearray.sip b/sip/qt/qbytearray.sip index db5d266..d975472 100644 --- a/sip/qt/qbytearray.sip +++ b/sip/qt/qbytearray.sip @@ -98,6 +98,36 @@ class TQByteArray #include <tqstring.h> %End +%TypeCode +// Convert a TQByteArray to a Python string or Py_None if there's +// no data +static PyObject* TQByteArray_To_String(TQByteArray *ba) +{ + // TQByteArrays aren't '\0' terminated so set the size + // explicitly. + char *data; + uint len; + + Py_BEGIN_ALLOW_THREADS + data = ba->data(); + len = ba->size(); + Py_END_ALLOW_THREADS + + if (data) + { +#if PY_MAJOR_VERSION >= 3 + return PyUnicode_FromStringAndSize(data, len); +#else + return SIPBytes_FromStringAndSize(data, len); +#endif + } + else + { + return Py_None; + } +} +%End + public: TQByteArray(); TQByteArray(int); @@ -105,27 +135,20 @@ public: SIP_PYOBJECT data() const; %MethodCode - // TQByteArrays aren't '\0' terminated so set the size - // explicitly. - - char *res; - uint len; - - Py_BEGIN_ALLOW_THREADS - res = sipCpp -> data(); - len = sipCpp -> size(); - Py_END_ALLOW_THREADS + PyObject* res = TQByteArray_To_String(sipCpp); - if (res) + if (res == Py_None) { - if ((sipRes = PyString_FromStringAndSize(res,len)) == NULL) - sipIsErr = 1; + Py_INCREF(Py_None); } - else + else if (res == NULL) { - Py_INCREF(Py_None); - sipRes = Py_None; + sipIsErr = 1; } + else + { + sipRes = res; + } %End // These are actually in TQMemArray, which isn't implemented so pretend @@ -151,36 +174,62 @@ public: SIP_PYOBJECT __str__(); %MethodCode - // TQByteArrays aren't '\0' terminated so set the size - // explicitly. + PyObject* res = TQByteArray_To_String(sipCpp); - char *data; - uint len; - - Py_BEGIN_ALLOW_THREADS - data = sipCpp -> data(); - len = sipCpp -> size(); - Py_END_ALLOW_THREADS - - if (data == NULL) - sipRes = PyString_FromString(""); + if (res == Py_None) + { + sipRes = SIPBytes_FromString(""); + } else - sipRes = PyString_FromStringAndSize(data,len); + { + sipRes = res; + } %End + %ConvertToTypeCode // Allow a Python string whenever a TQByteArray is expected. if (sipIsErr == NULL) - return (PyString_Check(sipPy) || + return (SIPBytes_Check(sipPy) || + PyUnicode_Check(sipPy) || sipCanConvertToInstance(sipPy,sipClass_TQByteArray,SIP_NO_CONVERTORS)); - if (PyString_Check(sipPy)) + if (PyUnicode_Check(sipPy)) + { + Py_BEGIN_ALLOW_THREADS + TQByteArray *ba = new TQByteArray(); + +#if PY_VERSION_HEX >= 0x03030000 + ba -> duplicate((char *)PyUnicode_1BYTE_DATA(sipPy),PyUnicode_GET_SIZE(sipPy)); +#else + ba -> duplicate((char *)PyUnicode_AS_DATA(sipPy),PyUnicode_GET_SIZE(sipPy)); +#endif + *sipCppPtr = ba; + Py_END_ALLOW_THREADS + + return sipGetState(sipTransferObj); + } +#if PY_VERSION_HEX >= 0x02060000 + else if (PyByteArray_Check(sipPy)) { Py_BEGIN_ALLOW_THREADS TQByteArray *ba = new TQByteArray(); - ba -> duplicate(PyString_AS_STRING(sipPy),PyString_GET_SIZE(sipPy)); + ba -> duplicate(PyByteArray_AS_STRING(sipPy),PyByteArray_GET_SIZE(sipPy)); + + *sipCppPtr = ba; + Py_END_ALLOW_THREADS + + return sipGetState(sipTransferObj); + } +#endif + else if (SIPBytes_Check(sipPy)) + { + Py_BEGIN_ALLOW_THREADS + TQByteArray *ba = new TQByteArray(); + + ba -> duplicate(SIPBytes_AS_STRING(sipPy),SIPBytes_GET_SIZE(sipPy)); *sipCppPtr = ba; Py_END_ALLOW_THREADS @@ -195,9 +244,7 @@ public: }; -%If (TQt_3_1_0 -) TQByteArray tqCompress(const uchar * /Array/,int /ArraySize/); TQByteArray tqCompress(const TQByteArray &); TQByteArray tqUncompress(const uchar * /Array/,int /ArraySize/); TQByteArray tqUncompress(const TQByteArray &); -%End |