Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1913

QByteArray bool() result is wrong for empty string

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • 7.0
    • 6.3.0
    • PySide
    • None
    • faebfdad1f (pyside/pyside-setup/dev) faebfdad1f (pyside/tqtc-pyside-setup/dev)

    Description

      bool(QByteArray(b"")) is True, but should be false (like bool(QByteArray())):

      >>> from PySide6.QtCore import QByteArray
      >>> ba1 = QByteArray(b"test")
      >>> ba2 = QByteArray(b"")
      >>> ba3 = QByteArray()
      >>> ba1.__bool__
      <method-wrapper '__bool__' of PySide6.QtCore.QByteArray object at 0x7f4544f35800>
      >>> len(ba1)
      4
      >>> len(ba2)
      0
      >>> len(ba3)
      0
      >>> bool(ba1)
      True
      >>> bool(ba2)
      True   # !!!
      >>> bool(ba3)
      False
      

      Note especially that:

      >>> ba2 == ba3
      True
      

      so we have two objects which are equal, yet produce different results with bool().

      I believe the fix would be to just not define a __bool__ on it. According to the Python data model: When __bool__ is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero.

      I tried to find where this is actually implemented in
      sources/pyside6/PySide6/QtCore/typesystem_core_common.xml or sources/pyside6/PySide6/glue/qtcore.cpp but failed to find it.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            the compiler Florian Bruhin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes