Menu

Segfault when creating QVariant from Enum based Python class

2018-11-21
2019-06-26
  • Erik Lundin

    Erik Lundin - 2018-11-21

    Hi,

    Background

    When running some Python code from PythonQt the other day, I got a segfault in PythonQtConversion.cpp. It turned out that when an Enum based class is processed by PyObjToQVariant, it's recognised as a sequence (since it's possible to iterate over an Enum in Python), but PySequence_GetItem returns nullptr when the loop gets the value to add to the QVariantList, causing the segfault when calling PyObjToVariant with a null pointer. Unless Enums should be treated in a special way, I assume that the best is to handle them as "normal" classes. In order to do that I created a small patch that checks if val->ob_type->tp_name is "EnumMeta". I'm not sure if this is an acceptable solution, so I'm open for better ways to fix it.

    Python 2.7 (with enum34 installed) and 3.6 behave the same.

    Files

    Supplied files:

    • pythonqt_run_enum_test.cpp: Minimal application that shows the problem
    • enumtest.py: Python module with an Enum for use with the test program
    • handle-enum-when-making-qvariant.diff: My patch

    Minimal example

    Building

    Building the test application (very manual – I ran this on Xubuntu 18.10):

    g++ -fPIC -c `pkg-config --cflags Qt5Core python-3.6 PythonQt-Qt5-Python3` -o pythonqt_run_enum_test.o pythonqt_run_enum_test.cpp
    g++ -o pythonqt_run_enum_test pythonqt_run_enum_test.o `pkg-config --libs Qt5Core python-3.6 PythonQt-Qt5-Python3`
    ./pythonqt_run_enum_test
    

    Expected output

    Without the patch, there is a segfault when running the minimal example. With the patch applied, it outputs this:

    QVariant(QVariantList, (QVariant(QVariantList, (QVariant(QString, "Color"), QVariant(PythonQtObjectPtr, ))), QVariant(QVariantList, (QVariant(QString, "Enum"), QVariant(PythonQtObjectPtr, )))))
    

    Links regarding Enum

     

    Last edit: Erik Lundin 2018-11-21
  • Florian Link

    Florian Link - 2018-11-22

    I don't have time to fix this right now, but I added a bug report in our internal bug tracker.

     
  • Erik Lundin

    Erik Lundin - 2019-06-26
     

Log in to post a comment.