Error/crash/hang when comparing enum value to uncomparable type

Phil Thompson phil at riverbankcomputing.com
Mon Oct 24 01:37:54 BST 2022


On 23/10/2022 20:09, Jean Abou Samra wrote:
> Hi,
> 
> I'm new to Qt and PyQt. I'm trying to unbreak the Frescobaldi 
> application,
> which is written in Python with PyQt5. If you want more context, the
> upstream issue is
> 
> https://github.com/frescobaldi/frescobaldi/issues/1453
> 
> This issue appears to be caused by a problem with the QKeySequence
> enum, more specifically its behavior when comparing an enum member
> with an object of an unrelated type. This works:
> 
>>>> from PyQt5.Qt import QKeySequence
>>>> QKeySequence.InsertLineSeparator == "string"
> False
> 
> 
> As expected in Python, two objects of different types without
> any special relationship compare unequal. However, take this
> script:
> 
> 
> import sys
> 
> from PyQt5.QtWidgets import QApplication, QPlainTextEdit, QVBoxLayout, 
> QWidget
> from PyQt5.Qt import QKeySequence
> 
> class QPlainTextEditSubclass(QPlainTextEdit):
>     def event(self, ev):
>         print(QKeySequence.InsertLineSeparator == "string")
> 
> class Window(QWidget):
>     def __init__(self):
>         super().__init__()
>         vbox = QVBoxLayout()
>         plainText = QPlainTextEditSubclass()
>         plainText.appendPlainText("Hi there")
>         vbox.addWidget(plainText)
>         self.setLayout(vbox)
>         self.show()
> 
> App = QApplication(sys.argv)
> window = Window()
> sys.exit(App.exec())
> 
> 
> 
> 
> For me, under Fedora 37 (beta), with both Python 3.10 and Python 3.11,
> this doesn't work, which seems strange to me. The comparison is just
> embedded in an event handler. In Python 3.11.0rc2, it yields
> 
> QSocketNotifier: Can only be used with threads started with QThread
> Traceback (most recent call last):
>   File "/home/jean/repos/frescobaldi/foo.py", line 8, in event
>     print(QKeySequence.InsertLineSeparator == "string")
> TypeError: a member of enum 'StandardKey' is expected not 'str'
> Traceback (most recent call last):
>   File "/home/jean/repos/frescobaldi/foo.py", line 8, in event
>     print(QKeySequence.InsertLineSeparator == "string")
> TypeError: a member of enum 'StandardKey' is expected not 'str'
> 
> 
> Then the process seems to freeze indefinitely. It doesn't
> respond to signals anymore: instead of "kill <pid>", I have
> to use "kill -9 <pid>".
> 
> In Python 3.10, I get an immediate crash:
> 
> Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use
> QT_QPA_PLATFORM=wayland to run on Wayland anyway.
> Traceback (most recent call last):
>   File "/home/jean/repos/frescobaldi/foo.py", line 8, in event
>     print(QKeySequence.InsertLineSeparator == "string")
> TypeError: a member of enum 'StandardKey' is expected not 'str'
> Abandon (core dumped)
> 
> 
> 
> Am I right to believe this is a bug in PyQt?

No. A Python exception has been raised in a Python re-implementation of 
a C++ virtual. See...

https://www.riverbankcomputing.com/static/Docs/PyQt5/incompatibilities.html#unhandled-python-exceptions

Phil


More information about the PyQt mailing list