Skip to content

Commit

Permalink
vlc: catch errors when instanciating vlc in is_available
Browse files Browse the repository at this point in the history
This would make the tests fail on a system without VLC installed.
Detecting it in is_available skips the tests.

For reference this is the error I get:
```
Traceback (most recent call last):
  File "/home/odrling/git/misc/dakara-player/src/dakara_player/media_player/vlc.py", line 94, in is_available
    return vlc is not None and vlc.Instance() is not None
                               ^^^^^^^^^^^^^^
  File "/home/odrling/git/misc/dakara-player/.direnv/python-3.12/lib/python3.12/site-packages/vlc.py", line 1838, in __new__
    return libvlc_new(len(args), args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/odrling/git/misc/dakara-player/.direnv/python-3.12/lib/python3.12/site-packages/vlc.py", line 5014, in libvlc_new
    _Cfunction('libvlc_new', ((1,), (1,),), class_result(Instance),
  File "/home/odrling/git/misc/dakara-player/.direnv/python-3.12/lib/python3.12/site-packages/vlc.py", line 302, in _Cfunction
    raise NameError('no function %r' % (name,))
NameError: no function 'libvlc_new'
```
  • Loading branch information
odrling committed Mar 26, 2024
1 parent 3ab2caf commit 5e303b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/dakara_player/media_player/vlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def is_available():
Returns:
bool: `True` if VLC is useable.
"""
return vlc is not None and vlc.Instance() is not None
try:
return vlc is not None and vlc.Instance() is not None
except NameError:
logger.exception("Failed to start VLC.")
return False

def init_player(self, config, tempdir):
"""Initialize the objects of VLC.
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_media_player_vlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ def test_set_vlc_callback(self):
vlc.EventType.MediaPlayerEndReached, callback
)

@skipIf(vlc is None, "VLC not installed")
def test_vlc_unavailable(self):
"""Test that is_available returns False when vlc.Instance raises a NameError."""
with patch.object(vlc, "Instance", side_effect=NameError()):
self.assertFalse(MediaPlayerVlc.is_available())

@patch("dakara_player.media_player.vlc.libvlc_get_version")
def test_get_version_long_4_digits(self, mocked_libvlc_get_version):
"""Test to get the VLC version when it is long and contains 4 digits."""
Expand Down

0 comments on commit 5e303b8

Please sign in to comment.