@@ -482,7 +482,11 @@ static PyObject *PythonQtInstanceWrapper_getattro(PyObject *obj,PyObject *name)
482
482
}
483
483
484
484
// look for the internal methods (className(), help())
485
+ #ifdef PY3K
486
+ PyObject* internalMethod = PyObject_GenericGetAttr (obj, name);
487
+ #else
485
488
PyObject* internalMethod = Py_FindMethod ( PythonQtInstanceWrapper_methods, obj, (char *)attributeName);
489
+ #endif
486
490
if (internalMethod) {
487
491
return internalMethod;
488
492
}
@@ -646,11 +650,13 @@ static PyObject * PythonQtInstanceWrapper_str(PyObject * obj)
646
650
if (wrapper->classInfo ()->metaTypeId ()==QVariant::ByteArray) {
647
651
QByteArray* b = (QByteArray*) wrapper->_wrappedPtr ;
648
652
#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.
650
656
if (b->data ()) {
651
- return PyBytes_FromStringAndSize (b->data (), b->size ());
657
+ return PyUnicode_FromStringAndSize (b->data (), b->size ());
652
658
} else {
653
- return PyBytes_FromString ( " " );
659
+ return PyUnicode_New ( 0 , 0 );
654
660
}
655
661
#else
656
662
if (b->data ()) {
@@ -727,30 +733,38 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = {
727
733
0 , /* nb_add */
728
734
0 , /* nb_subtract */
729
735
0 , /* nb_multiply */
736
+ #ifndef PY3K
730
737
0 , /* nb_divide */
738
+ #endif
731
739
0 , /* nb_remainder */
732
740
0 , /* nb_divmod */
733
741
0 , /* nb_power */
734
742
0 , /* nb_negative */
735
743
0 , /* nb_positive */
736
744
0 , /* nb_absolute */
737
- PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero */
745
+ PythonQtInstanceWrapper_builtin_nonzero, /* nb_nonzero / nb_bool in Py3K */
738
746
0 , /* nb_invert */
739
747
0 , /* nb_lshift */
740
748
0 , /* nb_rshift */
741
749
0 , /* nb_and */
742
750
0 , /* nb_xor */
743
751
0 , /* nb_or */
752
+ #ifndef PY3K
744
753
0 , /* nb_coerce */
754
+ #endif
745
755
0 , /* nb_int */
746
- 0 , /* nb_long */
756
+ 0 , /* nb_long / nb_reserved in Py3K */
747
757
0 , /* nb_float */
758
+ #ifndef PY3K
748
759
0 , /* nb_oct */
749
760
0 , /* nb_hex */
761
+ #endif
750
762
0 , /* nb_inplace_add */
751
763
0 , /* nb_inplace_subtract */
752
764
0 , /* nb_inplace_multiply */
765
+ #ifndef PY3K
753
766
0 , /* nb_inplace_divide */
767
+ #endif
754
768
0 , /* nb_inplace_remainder */
755
769
0 , /* nb_inplace_power */
756
770
0 , /* nb_inplace_lshift */
@@ -762,10 +776,13 @@ static PyNumberMethods PythonQtInstanceWrapper_as_number = {
762
776
0 , /* nb_true_divide */
763
777
0 , /* nb_inplace_floor_divide */
764
778
0 , /* nb_inplace_true_divide */
779
+ #ifdef PY3K
780
+ 0 , /* nb_index in Py3K */
781
+ #endif
765
782
};
766
783
767
784
PyTypeObject PythonQtInstanceWrapper_Type = {
768
- PyVarObject_HEAD_INIT (&PythonQtClassWrapper_Type, 0 ),
785
+ PyVarObject_HEAD_INIT (&PythonQtClassWrapper_Type, 0 )
769
786
" PythonQt.PythonQtInstanceWrapper" , /* tp_name*/
770
787
sizeof (PythonQtInstanceWrapper), /* tp_basicsize*/
771
788
0 , /* tp_itemsize*/
@@ -784,15 +801,23 @@ PyTypeObject PythonQtInstanceWrapper_Type = {
784
801
PythonQtInstanceWrapper_getattro, /* tp_getattro*/
785
802
PythonQtInstanceWrapper_setattro, /* tp_setattro*/
786
803
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*/
788
809
" PythonQtInstanceWrapper object" , /* tp_doc */
789
810
0 , /* tp_traverse */
790
811
0 , /* tp_clear */
791
812
(richcmpfunc)PythonQtInstanceWrapper_richcompare, /* tp_richcompare */
792
813
0 , /* tp_weaklistoffset */
793
814
0 , /* tp_iter */
794
815
0 , /* tp_iternext */
816
+ #ifdef PY3K
817
+ PythonQtInstanceWrapper_methods,
818
+ #else
795
819
0 , /* tp_methods */
820
+ #endif
796
821
0 , /* tp_members */
797
822
0 , /* tp_getset */
798
823
0 , /* tp_base */
0 commit comments