Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 916298 - dev-python/QtPy: backend "commenting out" code incompatible with implicit backend selection via prior import
Summary: dev-python/QtPy: backend "commenting out" code incompatible with implicit bac...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Python Gentoo Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-10-26 13:11 UTC by Michał Górny
Modified: 2023-10-26 15:21 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2023-10-26 13:11:59 UTC
We currently force ImportErrors to prevent dev-python/QtPy from using a particular backend if it wasn't enabled via USE flags (I suppose to prevent flag mismatches).  However, QtPy has this fancy logic that if some backend is in sys.modules already, it implicitly forces that backend.  If this backend wasn't enabled via USE flags, it effectively fails to load.

For example:

In [1]: import qtpy

In [2]: qtpy.API_NAME
Out[2]: 'PyQt5'

but:

In [1]: import PyQt6

In [2]: import qtpy
---------------------------------------------------------------------------
QtBindingsNotFoundError                   Traceback (most recent call last)
Cell In[2], line 1
----> 1 import qtpy

File /usr/lib/python3.10/site-packages/qtpy/__init__.py:283
    280     QT6 = PYSIDE6 = True
    282 except ImportError:
--> 283     raise QtBindingsNotFoundError from None
    284 else:
    285     os.environ[QT_API] = API

QtBindingsNotFoundError: No Qt bindings could be found
Comment 1 Andrew Nowa Ammerlaan gentoo-dev 2023-10-26 13:20:16 UTC
There is the FORCE_QT_API env var which should prevent QtPy from doing this[1]:

'''
Add the FORCE_QT_API environment variable to keep using the Qt bindings selected with the QT_API variable and avoid switching to the currently imported bindings. This allows to have applications that import PySide and PyQt bindings at the same time (which is possible if both bindings are compiled for the same Qt version).
'''

I wonder what the best solution is here, force set this variable somehow, or just patch out line 188 to 197 of __init__.py.

[1] https://github.com/spyder-ide/qtpy/blob/master/CHANGELOG.md#version-190-2019-07-23
Comment 2 Ionen Wolkens gentoo-dev 2023-10-26 13:30:47 UTC
>    elog "the first enabled target in this order: PyQt5 PySide2 PyQt6 PySide6."
Maybe an option could be to change the priority rather than raise errors, aka if PyQt5 is disabled then move it to the end of the list and it'll prevent loading a mismatching one by default.

Albeit I guess it'd be kind of ugly to change the if/elif block from the ebuild.
Comment 3 Larry the Git Cow gentoo-dev 2023-10-26 15:18:51 UTC
The bug has been closed via the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=ad167a488a979260256cb3513dbe7594b9276fc9

commit ad167a488a979260256cb3513dbe7594b9276fc9
Author:     Andrew Ammerlaan <andrewammerlaan@gentoo.org>
AuthorDate: 2023-10-26 15:15:11 +0000
Commit:     Andrew Ammerlaan <andrewammerlaan@gentoo.org>
CommitDate: 2023-10-26 15:18:31 +0000

    dev-python/QtPy: properly handle implicit QT_API behaviour
    
    If a Qt4Python implementation has already been imported QtPy tries to use
    that implementation by default, even if this implementation is disabled by
    USE flag configuration. Here we add some extra sed's to remove the sys.modules
    check for implementations that we have disabled, taking care that the first
    entry in the if block always starts with if and not elif.
    
    Closes: https://bugs.gentoo.org/916298
    Signed-off-by: Andrew Ammerlaan <andrewammerlaan@gentoo.org>

 .../{QtPy-2.4.1.ebuild => QtPy-2.4.1-r1.ebuild}    | 43 ++++++++++++++++++----
 1 file changed, 35 insertions(+), 8 deletions(-)
Comment 4 Andrew Nowa Ammerlaan gentoo-dev 2023-10-26 15:21:02 UTC
(In reply to Andrew Ammerlaan from comment #1)
> I wonder what the best solution is here, force set this variable somehow, or
> just patch out line 188 to 197 of __init__.py.

Ended up going with neither of these options, but with sed instead. This way we retain the behaviour that implicitly selects an already imported Qt4Python implementation, but prevent this from selecting an implementation that we have disabled.

It is not pretty, especially because we have to ensure this if block starts with an if, not an elif, but it works.