[PyKDE] SIP-20041231 enum issue

Phil Thompson phil at riverbankcomputing.co.uk
Wed Jan 5 07:26:48 GMT 2005


On Tuesday 04 January 2005 9:52 pm, Gerard Vermeulen wrote:
> Would it be to relax the type checking of enum parameters in SIP generated
> wrappers, so that both an int argument and an enum argument are accepted?
>
> I have to change two PyQwt examples to make them work with the current
> behaviour. Python pseudo code follows: ...
> FIXME = True # Set to True when explicit casts of a 'Python int' to a
> 'SIP-4.2 enum' are required. ...
>         # Example 1
>         # Translation of C++:
>         # for (int c=0; c<QColorGroup::NColorRoles; c++)
>         #     colorGroup.setColor(c, QColor());
>         for c in range(QColorGroup.NColorRoles):
>             if FIXME:
>                 c = QColorGroup.ColorRole(c) # SIP-4.2 needs this "cast"
> :-/ colorGroup.setColor(c, QColor())
> ...
>  # Example 2
>         # make sliders
>  # QwtSlider.BgSlot and QwtSlider.BgTrough are enums, and accepted as such
>         sldV1 = QwtSlider(self, "", Qt.Vertical, QwtSlider.Left,
>                           QwtSlider.BgSlot)
>         sldV2 = QwtSlider(self, "", Qt.Vertical, QwtSlider.None,
>                           QwtSlider.BgTrough)
>         if FIXME:
>             # Apparently QwtSlider.BgSlot | QwtSlider.BgTrough is not seen
> as an enum by SIP-4.2. # Therefore a cast is required :-(
>             sldV3 = QwtSlider(
>                 self, "", Qt.Vertical, QwtSlider.Right,
>                 QwtSlider.BGSTYLE(QwtSlider.BgSlot | QwtSlider.BgTrough))
>         else:
>             # SIP <= 4.1.1 is happy with the next statement
>             sldV3 = QwtSlider(self, "", Qt.Vertical, QwtSlider.Right,
>                               QwtSlider.BgSlot | QwtSlider.BgTrough)
>
> I would expect that one can pass a Python int as well as a SIP-4.2 enum
> argument into an enum parameter of a function wrapped by SIP (a C++
> function with a C++ enum parameter accepts C++ int arguments).

The C++ behaviour is due to automatically calling a ctor to create an 
appropriate type - SIP has never supported this (automatic) implicit type 
conversion. enums have different types to ints, Qt has at least one example 
where this is used in a function signature.

I'm not ruling out relaxing the check - I'd planned to release SIP 4.2rc1 to 
tease out some of these issues (eric needed a few changes). I do consider the 
above code to be buggy, but I don't want to break every PyQt program out 
there.

I confess I hadn't considered the idiom of oring two enum members. A 
compromise might be to return an object of the enum type when the operand 
types allow, and an int otherwise.

Phil




More information about the PyQt mailing list