Skip to content

Commit ce17d7b

Browse files
author
florianlink
committed
return unicode instead on Py3
git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@276 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent cc2e43d commit ce17d7b

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/PythonQtInstanceWrapper.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,11 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
482482
}
483483

484484
// look for the internal methods (className(), help())
485+
#ifdef PY3K
486+
PyObject* internalMethod = PyObject_GenericGetAttr(obj, name);
487+
#else
485488
PyObject* internalMethod = Py_FindMethod( PythonQtInstanceWrapper_methods, obj, (char*)attributeName);
489+
#endif
486490
if (internalMethod) {
487491
return internalMethod;
488492
}
@@ -646,11 +650,13 @@ static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
646650
if (wrapper->classInfo()->metaTypeId()==QVariant::ByteArray) {
647651
QByteArray* b = (QByteArray*) wrapper->_wrappedPtr;
648652
#ifdef PY3K
649-
// TODO: check how Python 3 likes str() returning bytes...
653+
// Note: In Python 2, this was used to access the data() of a byte array.
654+
// Since in Python 3 str() will return a unicode, this is no longer possible.
655+
// The user needs to call .data() to get access to the data as bytes.
650656
if (b->data()) {
651-
return PyBytes_FromStringAndSize(b->data(), b->size());
657+
return PyUnicode_FromStringAndSize(b->data(), b->size());
652658
} else {
653-
return PyBytes_FromString("");
659+
return PyUnicode_New(0, 0);
654660
}
655661
#else
656662
if (b->data()) {
@@ -727,30 +733,38 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = {
727733
0, /* nb_add */
728734
0, /* nb_subtract */
729735
0, /* nb_multiply */
736+
#ifndef PY3K
730737
0, /* nb_divide */
738+
#endif
731739
0, /* nb_remainder */
732740
0, /* nb_divmod */
733741
0, /* nb_power */
734742
0, /* nb_negative */
735743
0, /* nb_positive */
736744
0, /* nb_absolute */
737-
PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero */
745+
PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero / nb_bool in Py3K */
738746
0, /* nb_invert */
739747
0, /* nb_lshift */
740748
0, /* nb_rshift */
741749
0, /* nb_and */
742750
0, /* nb_xor */
743751
0, /* nb_or */
752+
#ifndef PY3K
744753
0, /* nb_coerce */
754+
#endif
745755
0, /* nb_int */
746-
0, /* nb_long */
756+
0, /* nb_long / nb_reserved in Py3K */
747757
0, /* nb_float */
758+
#ifndef PY3K
748759
0, /* nb_oct */
749760
0, /* nb_hex */
761+
#endif
750762
0, /* nb_inplace_add */
751763
0, /* nb_inplace_subtract */
752764
0, /* nb_inplace_multiply */
765+
#ifndef PY3K
753766
0, /* nb_inplace_divide */
767+
#endif
754768
0, /* nb_inplace_remainder */
755769
0, /* nb_inplace_power */
756770
0, /* nb_inplace_lshift */
@@ -762,10 +776,13 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = {
762776
0, /* nb_true_divide */
763777
0, /* nb_inplace_floor_divide */
764778
0, /* nb_inplace_true_divide */
779+
#ifdef PY3K
780+
0, /* nb_index in Py3K */
781+
#endif
765782
};
766783

767784
PyTypeObject PythonQtInstanceWrapper_Type = {
768-
PyVarObject_HEAD_INIT(&PythonQtClassWrapper_Type, 0),
785+
PyVarObject_HEAD_INIT(&PythonQtClassWrapper_Type, 0)
769786
"PythonQt.PythonQtInstanceWrapper", /*tp_name*/
770787
sizeof(PythonQtInstanceWrapper), /*tp_basicsize*/
771788
0, /*tp_itemsize*/
@@ -784,15 +801,23 @@ PyTypeObject PythonQtInstanceWrapper_Type = {
784801
PythonQtInstanceWrapper_getattro, /*tp_getattro*/
785802
PythonQtInstanceWrapper_setattro, /*tp_setattro*/
786803
0, /*tp_as_buffer*/
787-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
804+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
805+
#ifndef PY3K
806+
| Py_TPFLAGS_CHECKTYPES
807+
#endif
808+
, /*tp_flags*/
788809
"PythonQtInstanceWrapper object", /* tp_doc */
789810
0, /* tp_traverse */
790811
0, /* tp_clear */
791812
(richcmpfunc)PythonQtInstanceWrapper_richcompare, /* tp_richcompare */
792813
0, /* tp_weaklistoffset */
793814
0, /* tp_iter */
794815
0, /* tp_iternext */
816+
#ifdef PY3K
817+
PythonQtInstanceWrapper_methods,
818+
#else
795819
0, /* tp_methods */
820+
#endif
796821
0, /* tp_members */
797822
0, /* tp_getset */
798823
0, /* tp_base */

0 commit comments

Comments
 (0)