Error/crash/hang when comparing enum value to uncomparable type
Phil Thompson
phil at riverbankcomputing.com
Mon Feb 27 18:19:46 GMT 2023
On 22/02/2023 23:47, pyqtbugs at toeai.com wrote:
> Le 24/10/2022 à 03:16, Phil Thompson a écrit :
>> PyQt5 enums don't support comparisons with strings.
>
> Jean Abou Samra went on to ask whether comparing such uncomparable
> types ever "worked" and got the reply:
>
>> I don't think so.
>>
>> Phil
>
> But what I'm seeing is that they still kind of "work", even if they're
> not supposed to.
>
> Within the Python 3 REPL, I can do:
>>>> from PyQt5.QtGui import QKeySequence
>>>> not QKeySequence.InsertLineSeparator == "string"
> True
>>>> assert not QKeySequence.InsertLineSeparator == "string"
> TypeError: a member of enum 'StandardKey' is expected not 'str'
>
> The above exception was the direct cause of the following exception:
>
> SystemError: _PyEval_EvalFrameDefault returned a result with an
> exception set
>>>>
>
> I'm guessing that these comparisons kind of sort of working (not
> really, but seeming like they do) is what led to these bugs staying in
> the Frescobaldi app for so many years unnoticed.
>
> Three questions:
>
> 1) Can you explain why there isn't an error when the expression
> containing the '==' comparison is dumped directly to the REPL but it
> does raise an error when you assign it to a variable name, pass it to
> a function, use it in an assert statement, etc.? (NB: The error does
> not actually prevent the expression from being assigned to the given
> variable name, and the expression can be made fairly complex, e.g.
> putting it in a list and multiplying it by 5. Nor does the error
> prevent the assertion from being evaluated. You can see that if you
> remove the "not" in the example above. The error does seem to prevent
> a function call.)
>
> 2) Can you explain why the result is still assigned to variable names
> and used in assert statements when an error occurs, unlike what we're
> all used to, e.g.
> one_over_zero = 1/0
> of course does NOT set any value to 'one_over_zero'. Nor does:
> assert 1/0 and False
> raise an assertion error.
>
> 3) Can you explain why '!=' expressions evaluate without error, e.g.
>>>> QKeySequence.InsertLineSeparator != "string"
> True
>>>> assert QKeySequence.InsertLineSeparator != "string"
>>>> print("Not equal:", QKeySequence.InsertLineSeparator != "string")
> Not equal: True
>>>>
> (Very counterintuitive behavior, given the examples above.)
>
>
> Thanks.
The exception affects QKeySequence in particular rather than enums in
general. It should be fixed in the next SIP snapshot but is a code
generation bug so will require rebuilding PyQt5 (and PyQt6).
Thanks,
Phil
More information about the PyQt
mailing list