summaryrefslogtreecommitdiffstats
path: root/sip/qt/qstring.sip
diff options
context:
space:
mode:
authoraneejit1 <aneejit1@gmail.com>2022-04-19 13:21:52 +0000
committerSlávek Banko <slavek.banko@axis.cz>2022-07-28 00:56:02 +0200
commit52f8f8436dc2af136156ef95dbb8463481a78df8 (patch)
tree50e5f757a67774f98bfa931ef191dcc07683996d /sip/qt/qstring.sip
parent228b87ea89625d0783fdfe60114eba89e0f37942 (diff)
downloadpytqt-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 <aneejit1@gmail.com> Signed-off-by: Slávek Banko <slavek.banko@axis.cz> (cherry picked from commit 6be046642290c28c17949022fb66ae02ac21d544)
Diffstat (limited to 'sip/qt/qstring.sip')
-rw-r--r--sip/qt/qstring.sip525
1 files changed, 211 insertions, 314 deletions
diff --git a/sip/qt/qstring.sip b/sip/qt/qstring.sip
index ab30fa4..581bb0a 100644
--- a/sip/qt/qstring.sip
+++ b/sip/qt/qstring.sip
@@ -195,53 +195,8 @@ public:
static const TQChar replacement;
static const TQChar byteOrderMark;
static const TQChar byteOrderSwapped;
-%If (TQt_2_2_0 -)
static const TQChar nbsp;
-%End
-%If (- TQt_3_0_0)
- enum Category {
- NoCategory,
-
- Mark_NonSpacing,
- Mark_SpacingCombining,
- Mark_Enclosing,
-
- Number_DecimalDigit,
- Number_Letter,
- Number_Other,
-
- Separator_Space,
- Separator_Line,
- Separator_Paragraph,
-
- Other_Control,
- Other_Format,
- Other_Surrogate,
- Other_PrivateUse,
- Other_NotAssigned,
-
- Letter_Uppercase,
- Letter_Lowercase,
- Letter_Titlecase,
- Letter_Modifier,
- Letter_Other,
-
- Punctuation_Connector,
- Punctuation_Dask,
- Punctuation_Open,
- Punctuation_Close,
- Punctuation_InitialQuote,
- Punctuation_FinalQuote,
- Punctuation_Other,
-
- Symbol_Math,
- Symbol_Currency,
- Symbol_Modifier,
- Symbol_Other
- };
-%End
-%If (TQt_3_0_0 -)
enum Category {
NoCategory,
@@ -282,24 +237,7 @@ public:
Symbol_Modifier,
Symbol_Other
};
-%End
-%If (- TQt_3_0_0)
- enum Direction {
- DirL,
- DirR,
- DirEN,
- DirES,
- DirET,
- DirAN,
- DirCS,
- DirB,
- DirS,
- DirWS,
- DirON
- };
-%End
-%If (TQt_3_0_0 -)
enum Direction {
DirL,
DirR,
@@ -321,7 +259,6 @@ public:
DirNSM,
DirBN
};
-%End
enum Decomposition {
Single,
@@ -351,7 +288,6 @@ public:
Center
};
-%If (TQt_3_0_0 -)
enum CombiningClass {
Combining_BelowLeftAttached,
Combining_BelowAttached,
@@ -375,7 +311,6 @@ public:
Combining_DoubleAbove,
Combining_IotaSubscript
};
-%End
int digitValue() const;
TQChar lower() const;
@@ -385,19 +320,10 @@ public:
Direction direction() const;
Joining joining() const;
bool mirrored() const;
-%If (TQt_2_1_0 -)
TQChar mirroredChar() const;
-%End
-%If (- TQt_3_0_0)
- TQString decomposition() const;
-%End
-%If (TQt_3_0_0 -)
const TQString &decomposition() const;
-%End
Decomposition decompositionTag() const;
-%If (TQt_3_0_0 -)
unsigned char combiningClass() const;
-%End
char latin1() const;
ushort unicode() const;
@@ -409,22 +335,16 @@ public:
bool isMark() const;
bool isLetter() const;
bool isNumber() const;
-%If (TQt_2_1_0 -)
bool isLetterOrNumber() const;
-%End
bool isDigit() const;
-%If (TQt_3_0_0 -)
bool isSymbol() const;
-%End
// uchar& cell();
// uchar& row();
uchar cell() const;
uchar row() const;
-%If (TQt_3_0_0 -)
void setCell(uchar);
void setRow(uchar);
-%End
static bool networkOrdered();
};
@@ -447,6 +367,7 @@ class TQString
{
%TypeHeaderCode
#include <tqstring.h>
+#include <tqtextcodec.h>
%End
public:
@@ -457,18 +378,14 @@ public:
// TQString(const TQChar *,uint);
// TQString(const char *);
-%If (TQt_2_1_0 -)
// This is how we implement TQUrl::operator TQString() const.
TQString(const TQUrl &);
-%End
-%If (TQt_3_0_0 -)
// This is how we implement TQKeySequence::operator TQString() const.
TQString(const TQKeySequence &);
// This is how we implement TQUuid::operator TQString() const.
TQString(const TQUuid &);
-%End
static const TQString null;
@@ -476,20 +393,13 @@ public:
bool isEmpty() const;
uint length() const;
void truncate(uint);
-%If (- TQt_3_0_0)
- void fill(TQChar,int = -1);
-%End
-%If (TQt_3_0_0 -)
TQString &fill(TQChar,int = -1);
-%End
TQString copy() const;
TQString arg(int /Constrained/,int = 0,int = 10) const;
TQString arg(double /Constrained/,int = 0,char = 'g',int = -1) const;
-%If (TQt_3_2_0 -)
// TQString arg(TQ_LLONG,int = 0,int = 10) const;
// TQString arg(TQ_ULLONG,int = 0,int = 10) const;
-%End
TQString arg(long,int = 0,int = 10) const;
TQString arg(ulong,int = 0,int = 10) const;
// TQString arg(uint,int = 0,int = 10) const;
@@ -498,12 +408,10 @@ public:
TQString arg(char,int = 0) const;
TQString arg(TQChar,int = 0) const;
TQString arg(const TQString&,int = 0) const;
-%If (TQt_3_2_0 -)
TQString arg(const TQString &,const TQString &) const;
TQString arg(const TQString &,const TQString &,const TQString &) const;
TQString arg(const TQString &,const TQString &,const TQString &,
const TQString &) const;
-%End
// TQString &sprintf(const char *,...);
@@ -523,7 +431,6 @@ public:
int contains(const TQString &,bool = 1) const;
int contains(const TQRegExp &) const;
-%If (TQt_3_0_0 -)
enum SectionFlags {
SectionDefault,
SectionSkipEmpty,
@@ -540,7 +447,6 @@ public:
int = SectionDefault) const;
TQString section(const TQRegExp &,int,int = 0xffffffff,
int = SectionDefault) const;
-%End
TQString left(uint) const;
TQString right(uint) const;
@@ -553,56 +459,36 @@ public:
TQString simplifyWhiteSpace() const;
TQString &insert(uint,const TQString &);
-%If (TQt_3_2_0 -)
TQString &insert(uint,const TQByteArray &);
// TQString &insert(uint,const char *);
-%End
TQString &insert(uint,TQChar *,uint);
TQString &insert(uint,TQChar);
TQString &insert(uint,char);
TQString &append(char);
-%If (TQt_2_2_0 -)
TQString &append(TQChar);
-%End
TQString &append(const TQString &);
TQString &prepend(char);
-%If (TQt_2_2_0 -)
TQString &prepend(TQChar);
-%End
TQString &prepend(const TQString &);
TQString &remove(uint,uint);
-%If (TQt_3_1_0 -)
TQString &remove(const TQString &);
-%If (TQt_3_2_0 -)
TQString &remove(const TQString &,bool);
-%End
TQString &remove(TQChar);
TQString &remove(char);
TQString &remove(const TQRegExp &);
-%End
TQString &replace(uint,uint,const TQString &);
TQString &replace(uint,uint,const TQChar *,uint);
-%If (TQt_3_1_0 -)
TQString &replace(uint,uint,TQChar);
TQString &replace(uint,uint,char);
TQString &replace(TQChar,const TQString &);
-%If (TQt_3_2_0 -)
TQString &replace(TQChar,const TQString &,bool);
-%End
TQString &replace(char,const TQString &);
-%If (TQt_3_2_0 -)
TQString &replace(char,const TQString &,bool);
-%End
TQString &replace(const TQString &,const TQString &);
-%If (TQt_3_2_0 -)
TQString &replace(const TQString &,const TQString &,bool);
-%End
-%End
TQString &replace(const TQRegExp &,const TQString &);
-%If (TQt_3_1_0 -)
TQString &replace(TQChar,TQChar);
-%End
short toShort(bool * = 0,int = 10) const;
ushort toUShort(bool * = 0,int = 10) const;
@@ -610,10 +496,8 @@ public:
uint toUInt(bool * = 0,int = 10) const;
long toLong(bool * = 0,int = 10) const;
ulong toULong(bool * = 0,int = 10) const;
-%If (TQt_3_2_0 -)
// TQ_LLONG toLongLong(bool * = 0,int = 10) const;
// TQ_ULLONG toULongLong(bool * = 0,int = 10) const;
-%End
float toFloat(bool * = 0) const;
double toDouble(bool * = 0) const;
@@ -624,28 +508,22 @@ public:
// TQString &setNum(uint,int = 10);
TQString &setNum(long,int = 10);
TQString &setNum(ulong,int = 10);
-%If (TQt_3_2_0 -)
// TQString &setNum(TQ_LLONG,int = 10);
// TQString &setNum(TQ_ULLONG,int = 10);
-%End
// TQString &setNum(float,char = 'g',int = 6);
static TQString number(int /Constrained/,int = 10);
static TQString number(double /Constrained/,char = 'g',int = 6);
static TQString number(long,int = 10);
static TQString number(ulong,int = 10);
-%If (TQt_3_2_0 -)
// static TQString number(TQ_LLONG,int = 10);
// static TQString number(TQ_ULLONG,int = 10);
-%End
// static TQString number(uint,int = 10);
void setExpand(uint,TQChar);
TQString &operator+=(const TQString &);
-%If (TQt_3_3_0 -)
TQString &operator+=(const TQByteArray &);
-%End
TQString &operator+=(TQChar);
//TQString &operator+=(char);
@@ -673,9 +551,7 @@ public:
// TQChar &ref(uint);
// const TQChar *unicode() const;
const char *ascii() const;
-%If (TQt_3_1_0 -)
static TQString fromAscii(const char *,int = -1);
-%End
const char *latin1() const;
static TQString fromLatin1(const char *,int = -1);
TQCString utf8() const;
@@ -683,62 +559,34 @@ public:
TQCString local8Bit() const;
static TQString fromLocal8Bit(const char *,int = -1);
-%If (TQt_3_1_0 -)
// static TQString fromUcs2(const unsigned short *);
// const unsigned short *ucs2() const;
-%End
-%If (TQt_2_1_0 -)
// TQString &setUnicode(const TQChar *,uint);
// TQString &setUnicodeCodes(const ushort *,uint);
-%End
-%If (TQt_3_1_0 -)
TQString &setAscii(const char *,int = -1);
-%End
-%If (TQt_2_1_0 -)
TQString &setLatin1(const char *,int = -1);
-%End
int compare(const TQString &) const;
static int compare(const TQString &,const TQString &);
-%If (TQt_3_0_0 -)
int localeAwareCompare(const TQString &) const;
static int localeAwareCompare(const TQString &,const TQString &);
-%End
void compose();
-%If (- TQt_3_0_0)
- TQChar::Direction basicDirection();
- TQString visual(int = 0,int = -1);
-%End
-%If (TQt_2_2_0 -)
bool startsWith(const TQString &) const;
-%End
-%If (TQt_3_2_0 -)
bool startsWith(const TQString &,bool) const;
-%End
-%If (TQt_3_0_0 -)
bool endsWith(const TQString &) const;
-%End
-%If (TQt_3_2_0 -)
bool endsWith(const TQString &,bool) const;
-%End
-%If (TQt_3_0_0 -)
void setLength(uint);
-%End
-%If (TQt_3_2_0 -)
uint capacity() const;
void reserve(uint);
void squeeze();
-%End
-%If (TQt_3_0_0 -)
bool simpleText() const;
bool isRightToLeft() const;
-%End
// Force the numeric interpretation so that str + TQString gets handled
// as we want.
@@ -792,81 +640,12 @@ public:
SIP_PYOBJECT __unicode__();
%MethodCode
-#if PY_VERSION_HEX >= 0x01060000
- sipRes = TQStringToPyUnicode(sipCpp);
-#else
- Py_INCREF(Py_None);
- sipRes = Py_None;
-#endif
+ sipRes = PyTQt_qt_PyObject_FromTQString(sipCpp);
%End
SIP_PYOBJECT __str__();
%MethodCode
-#if PY_VERSION_HEX >= 0x01060000
- sipRes = TQStringToPyUnicode(sipCpp);
-#else
- const char *s;
-
- Py_BEGIN_ALLOW_THREADS
- s = *sipCpp;
- Py_END_ALLOW_THREADS
-
- if (s == NULL)
- s = "";
-
- sipRes = PyString_FromString(s);
-#endif
-%End
-
-%TypeCode
-#include <tqtextcodec.h>
-
-#if PY_VERSION_HEX >= 0x01060000
-// Convenience function for converting a TQString to a Python Unicode object.
-static PyObject *TQStringToPyUnicode(TQString *qs)
-{
- PyObject *uobj;
-
- if ((uobj = PyUnicode_FromUnicode(NULL,qs -> length())) == NULL)
- return NULL;
-
- Py_UNICODE *pyu = PyUnicode_AS_UNICODE(uobj);
-
- for (uint i = 0; i < qs -> length(); ++i)
- *pyu++ = (qs -> at(i)).unicode();
-
- return uobj;
-}
-#endif
-
-// Convenience function for converting a Python unicode or string object to a
-// TQString on the heap.
-static TQString *PyUnicodeStringToTQString(PyObject *py)
-{
-#if PY_VERSION_HEX >= 0x01060000
- if (PyUnicode_Check(py))
- {
- TQString *qs = new TQString;
-
-#if defined(Py_UNICODE_WIDE)
- PY_UNICODE_TYPE *ucode = PyUnicode_AS_UNICODE(py);
- int len = PyUnicode_GET_SIZE(py);
-
- for (int i = 0; i < len; ++i)
- qs->ref(i) = (uint)ucode[i];
-#else
- qs -> setUnicodeCodes((ushort *)PyUnicode_AS_UNICODE(py),PyUnicode_GET_SIZE(py));
-#endif
-
- return qs;
- }
-#endif
-
- if (PyString_Check(py))
- return new TQString(PyString_AS_STRING(py));
-
- return 0;
-}
+ sipRes = PyTQt_qt_PyObject_FromTQString(sipCpp);
%End
%ConvertToTypeCode
@@ -874,13 +653,11 @@ static TQString *PyUnicodeStringToTQString(PyObject *py)
// expected.
if (sipIsErr == NULL)
- return (PyString_Check(sipPy) ||
-#if PY_VERSION_HEX >= 0x01060000
+ return (SIPBytes_Check(sipPy) ||
PyUnicode_Check(sipPy) ||
-#endif
sipCanConvertToInstance(sipPy,sipClass_TQString,SIP_NO_CONVERTORS));
- *sipCppPtr = PyUnicodeStringToTQString(sipPy);
+ *sipCppPtr = PyTQt_qt_PyObject_AsTQString(sipPy);
if (*sipCppPtr)
return sipGetState(sipTransferObj);
@@ -942,7 +719,7 @@ static TQString *PyUnicodeStringToTQString(PyObject *py)
enc = codec->fromUnicode(*sipCpp);
- if ((encobj = PyString_FromString(enc.data())) != NULL)
+ if ((encobj = SIPBytes_FromString(enc.data())) != NULL)
{
*sipPtrPtr = (void *)PyString_AS_STRING(encobj);
sipRes = PyString_GET_SIZE(encobj);
@@ -973,107 +750,227 @@ bool operator>=(const TQString &,const TQString &);
%End
-%If (- TQt_2_00)
+%ModuleHeaderCode
+extern PyObject *PyTQt_qt_PyObject_FromTQString(const TQString *qstr);
+%End
-class TQString : TQByteArray
+%ModuleCode
+// Convert a QString to a Python Unicode object.
+PyObject *PyTQt_qt_PyObject_FromTQString(const TQString *qstr)
{
-%TypeHeaderCode
-#include <tqstring.h>
+ PyObject *obj;
+
+#if PY_VERSION_HEX >= 0x03030000
+ // We have to work out exactly which kind to use. We assume ASCII while we
+ // are checking so that we only go through the string once in the most
+ // common case. Note that we can't use PyUnicode_FromKindAndData() because
+ // it doesn't handle surrogates in UCS2 strings.
+
+ int qt_len = qstr->length();
+ Py_UCS4 maxchar = 0x007f;
+
+ for (int qt_i = 0; qt_i < qt_len && maxchar < 0xffff; ++qt_i)
+ {
+ Py_UCS4 uch = qstr->at(qt_i).unicode();
+
+ if (uch > 0x00ff)
+ {
+ maxchar = 0xffff;
+ }
+ else if (uch > 0x007f)
+ {
+ maxchar = 0x00ff;
+ }
+ }
+
+ // Create the correctly sized object.
+ if ((obj = PyUnicode_New(qt_len, maxchar)) == NULL)
+ return NULL;
+
+ int kind = PyUnicode_KIND(obj);
+ void *data = PyUnicode_DATA(obj);
+
+ for (int qt_i = 0; qt_i < qt_len; ++qt_i)
+ {
+ Py_UCS4 uch = qstr->at(qt_i).unicode();
+ PyUnicode_WRITE(kind, data, qt_i, uch);
+ }
+#elif defined(Py_UNICODE_WIDE)
+ // Note that this doesn't handle code points greater than 0xffff. It could
+ // but it's only an issue for old versions of Qt.
+
+ if ((obj = PyUnicode_FromUnicode(NULL, qstr->length())) == NULL)
+ return NULL;
+
+ Py_UNICODE *pyu = PyUnicode_AS_UNICODE(obj);
+
+ for (unsigned int i = 0; i < qstr->length(); ++i)
+ *pyu++ = (qstr->at(i)).unicode();
+#else
+ if ((obj = PyUnicode_FromUnicode(NULL, qstr->length())) == NULL)
+ return NULL;
+
+ memcpy(PyUnicode_AS_UNICODE(obj), qstr->ucs2(),
+ qstr->length() * sizeof (Py_UNICODE));
+#endif
+
+ return obj;
+}
%End
-public:
- TQString();
- TQString(int);
- TQString(const TQString &);
- TQString(const char *);
- TQString(const char *,uint);
- bool isNull() const;
- bool isEmpty() const;
- uint length() const;
- bool resize(uint);
- bool truncate(uint);
- bool fill(char,int = -1);
- TQString copy() const;
-// TQString &sprintf(const char *,...);
- int find(char,int = 0,bool = 1) const;
- int find(const char *,int = 0,bool = 1) const;
- int find(const TQRegExp &,int = 0) const;
- int findRev(char,int = -1,bool = 1) const;
- int findRev(const char *,int = -1,bool = 1) const;
- int findRev(const TQRegExp &,int = -1) const;
- int contains(char,bool = 1) const;
- int contains(const char *,bool = 1) const;
- int contains(const TQRegExp &) const;
- TQString left(uint) const;
- TQString right(uint) const;
- TQString mid(uint,uint) const;
- TQString leftJustify(uint,char = ' ',bool = 0) const;
- TQString rightJustify(uint,char = ' ',bool = 0) const;
- TQString lower() const;
- TQString upper() const;
- TQString stripWhiteSpace() const;
- TQString simplifyWhiteSpace() const;
- TQString &insert(uint,const char *);
- TQString &insert(uint,char);
- TQString &append(const char *);
- TQString &prepend(const char *);
- TQString &remove(uint,uint);
- TQString &replace(uint,uint,const char *);
- TQString &replace(const TQRegExp &,const char *);
-
- short toShort(bool * = 0) const;
- ushort toUShort(bool * = 0) const;
- int toInt(bool * = 0) const;
- uint toUInt(bool * = 0) const;
- long toLong(bool * = 0) const;
- ulong toULong(bool * = 0) const;
- float toFloat(bool * = 0) const;
- double toDouble(bool * = 0) const;
+%ModuleHeaderCode
+extern TQString *PyTQt_qt_PyObject_AsTQString(PyObject *obj);
+%End
- TQString &setStr(const char *);
-// TQString &setNum(short);
-// TQString &setNum(ushort);
- TQString &setNum(int /Constrained/);
-// TQString &setNum(uint);
-// TQString &setNum(long);
-// TQString &setNum(ulong);
-// TQString &setNum(float,char = 'g',int = 6);
- TQString &setNum(double,char = 'g',int = 6);
- bool setExpand(uint,char);
+%ModuleCode
+// Convert a Python Unicode object to a QString.
+TQString *PyTQt_qt_PyObject_AsTQString(PyObject *obj)
+{
+ if (PyUnicode_Check(obj))
+ {
+#if PY_VERSION_HEX >= 0x03030000
+ SIP_SSIZE_T len = PyUnicode_GET_LENGTH(obj);
- SIP_PYOBJECT __str__();
-%MethodCode
- const char *s;
+ switch (PyUnicode_KIND(obj))
+ {
+ case PyUnicode_1BYTE_KIND:
+ return new TQString(TQString::fromLatin1((char *)PyUnicode_1BYTE_DATA(obj), len));
- Py_BEGIN_ALLOW_THREADS
- s = *sipCpp;
- Py_END_ALLOW_THREADS
+ case PyUnicode_2BYTE_KIND:
+ // The (TQChar *) cast should be safe.
+ return new TQString((TQChar *)PyUnicode_2BYTE_DATA(obj), len);
- if (s == NULL)
- s = "";
+ case PyUnicode_4BYTE_KIND:
+ // Note that this doesn't handle code points greater than 0xffff. It
+ // could but it's only an issue for old versions of Qt.
- sipRes = PyString_FromString(s);
-%End
+ TQString *qstr = new TQString;
-%ConvertToTypeCode
- // Allow a Python string whenever a TQString is expected.
+ Py_UCS4 *ucode = PyUnicode_4BYTE_DATA(obj);
- if (sipIsErr == NULL)
- return (PyString_Check(sipPy) ||
- sipCanConvertToInstance(sipPy,sipClass_TQString,SIP_NO_CONVERTORS));
+ for (SIP_SSIZE_T i = 0; i < len; ++i)
+ qstr->append(TQChar((uint)ucode[i]));
- if (PyString_Check(sipPy))
- {
- *sipCppPtr = new TQString(PyString_AS_STRING(sipPy));
+ return qstr;
+ }
- return sipGetState(sipTransferObj);
- }
+ return NULL;
+#else
+ TQString *qstr = new TQString;
- *sipCppPtr = reinterpret_cast<TQString *>(sipConvertToInstance(sipPy,sipClass_TQString,sipTransferObj,SIP_NO_CONVERTORS,0,sipIsErr));
+# ifdef Py_UNICODE_WIDE
+ Py_UNICODE *ucode = PyUnicode_AS_UNICODE(obj);
+ SIP_SSIZE_T len = PyUnicode_GET_SIZE(obj);
- return 0;
+ for (SIP_SSIZE_T i = 0; i < len; ++i)
+ qstr->append(TQChar((uint)ucode[i]));
+# else
+ qstr->setUnicodeCodes((ushort *)PyUnicode_AS_UNICODE(obj),PyUnicode_GET_SIZE(obj));
+# endif
+
+ return qstr;
+#endif
+ }
+ else if (PyBytes_Check(obj))
+ {
+ return new TQString(SIPBytes_AS_STRING(obj));
+ }
+#if PY_MAJOR_VERSION < 3
+ else if (PyString_Check(obj))
+ {
+ return new TQString(PyString_AS_STRING(obj));
+ }
+#endif
+
+ return NULL;
+}
+%End
+
+
+%ModuleHeaderCode
+extern const char *PyTQt_qt_encode(PyObject **s, TQApplication::Encoding encoding);
%End
-};
+%ModuleCode
+// Convert a Python unicode/string/bytes object to a character string encoded
+// according to the given encoding. Update the object with a new reference to
+// the object that owns the data.
+const char *PyTQt_qt_encode(PyObject **s, TQApplication::Encoding encoding)
+{
+ PyObject *obj = *s;
+ const char *es = 0;
+ SIP_SSIZE_T sz;
+
+ if (PyUnicode_Check(obj))
+ {
+ if (encoding == TQApplication::UnicodeUTF8)
+ {
+ obj = PyUnicode_AsUTF8String(obj);
+ }
+ else
+ {
+ TQTextCodec *codec = TQTextCodec::codecForTr();
+
+ if (codec)
+ {
+ // Use the Qt codec to get to a byte string, and then to a
+ // Python object.
+ TQString *qstr = PyTQt_qt_PyObject_AsTQString(obj);
+ TQString qs = *qstr;
+ TQByteArray ba = codec->fromUnicode(qs);
+ delete qstr;
+
+#if PY_MAJOR_VERSION >= 3
+ obj = PyBytes_FromStringAndSize(ba.data(), ba.size());
+#else
+ obj = PyString_FromStringAndSize(ba.data(), ba.size());
+#endif
+ }
+ else
+ {
+ obj = PyUnicode_AsLatin1String(obj);
+ }
+ }
+
+ if (obj)
+ {
+#if PY_MAJOR_VERSION >= 3
+ es = PyBytes_AS_STRING(obj);
+#else
+ es = PyString_AS_STRING(obj);
+#endif
+ }
+ }
+#if PY_MAJOR_VERSION >= 3
+ else if (PyBytes_Check(obj))
+ {
+ es = PyBytes_AS_STRING(obj);
+ Py_INCREF(obj);
+ }
+#else
+ else if (PyString_Check(obj))
+ {
+ es = PyString_AS_STRING(obj);
+ Py_INCREF(obj);
+ }
+#endif
+ else if (PyObject_AsCharBuffer(obj, &es, &sz) >= 0)
+ {
+ Py_INCREF(obj);
+ }
+
+ if (es)
+ {
+ *s = obj;
+ }
+ else
+ {
+ PyErr_Format(PyExc_UnicodeEncodeError,
+ "unable to convert '%s' to requested encoding",
+ Py_TYPE(*s)->tp_name);
+ }
+
+ return es;
+}
%End