Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QtMultimedia availability check added #1203

Closed
wants to merge 1 commit into from

Conversation

timur-enikeev
Copy link
Contributor

Summary

  • This is a…
    • Bug fix
    • Feature addition
    • Refactoring
    • Minor / simple change (like a typo)
    • Other
  • Describe this change in 1-2 sentences:

Problem

After internal music player has been added, picard crashes if PyQt5.QtMultimedia module is not available. I have added availability check for QtMultimedia module.

Solution

I have added handling of ImportError exception when we try to import PyQt5.QtMultimedia. If the import fails, qt_multimedia_available variable will be False. If the import was successful, this variable will be True.

Action

@timur-enikeev
Copy link
Contributor Author

@samj1912

This should be an except ImportError instead of a broad except.

But what if there will be another exception type? I don't know entrails of pyqt. Maybe it can raise another exception type.

from PyQt5 import QtMultimedia
except Exception as e:
qt_multimedia_available = False
log.warning("{} - Internal player disabled".format(e.msg))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to pre-format with log.warning() and we usually place the detailed reason after the main message.
Also import will raise ImportError

try:
    from PyQt5 import QtMultimedia
except ImportError as e:
    qt_multimedia_available = False
    log.warning('Internal player disabled: %s', e.msg)
else:
    qt_multimedia_available = True

@@ -113,8 +121,9 @@ def __init__(self, parent=None):
self.selected_objects = []
self.ignore_selection_changes = False
self.toolbar = None
self.player = QtMultimedia.QMediaPlayer(self)
self.player.error.connect(self._on_player_error)
if qt_multimedia_available:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also try to import directly here and only set player property if a player is actually available.

self.player = None
try:
    from PyQt5 import QtMultimedia
    player = QtMultimedia.QMediaPlayer(self)
    if player.availability() != QtMultimedia.QMultimedia.ServiceMissing:
        self.player = player
        self.player.error.connect(self._on_player_error)
except ImportError as e:
    log.warning('Internal player disabled: %s', e.msg)

It would simplify tests:

if self.player:
   do_something_with(self.player)

And ensure an exception is raised if self.player is used without being really available (like using self.player.volume()).

@timur-enikeev @phw : what do you think ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of importing inline, I think this is clearer if done at the top. Apart from that I agree, so I would change your example to:

self.player = None
player = QtMultimedia.QMediaPlayer(self)
if qt_multimedia_available and player.availability() != QtMultimedia.QMultimedia.ServiceMissing:
    self.player = player
    self.player.error.connect(self._on_player_error)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented this, with extra error logging in #1207

@zas
Copy link
Collaborator

zas commented Jun 20, 2019

But what if there will be another exception type? I don't know entrails of pyqt. Maybe it can raise another exception type.

If another exception is raised, then it shouldn't be converted to a warning, because it'd be unexpected.
@samj1912 is right here, we should only catch ImportError.

But testing lack of exception on module import isn't sufficient to ensure the player is available, see #1203 (comment)

We have 3 cases:

  • module cannot be imported at all
  • module can be imported but player isn't available because of missing dependencies (typically gstreamer on linux)
  • module can be imported and player is available

@phw phw mentioned this pull request Jun 26, 2019
5 tasks
@zas
Copy link
Collaborator

zas commented Jun 26, 2019

Closing this pull request as #1207 includes it.

@zas zas closed this Jun 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants