From 9f1f2059d917dc55cd62ce2b98a81e6195175c12 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sat, 23 Feb 2019 15:42:29 -0500 Subject: [PATCH] handle ValueError Exception via Dbus import; refs #75 --- apprise/plugins/NotifyDBus.py | 7 +++++-- test/test_glib_plugin.py | 30 ++++++++++++++++++++++++++++++ test/test_gnome_plugin.py | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/apprise/plugins/NotifyDBus.py b/apprise/plugins/NotifyDBus.py index a9740d24..af3ede5f 100644 --- a/apprise/plugins/NotifyDBus.py +++ b/apprise/plugins/NotifyDBus.py @@ -86,9 +86,12 @@ try: from gi.repository import GdkPixbuf NOTIFY_DBUS_IMAGE_SUPPORT = True - except ImportError: + except (ImportError, ValueError): # No problem; this will get caught in outer try/catch - raise + + # A ValueError will get thrown upon calling gi.require_version() if + # GDK/GTK isn't installed on the system but gi is. + pass except ImportError: # No problem; we just simply can't support this plugin; we could diff --git a/test/test_glib_plugin.py b/test/test_glib_plugin.py index c2650f1a..fbe1416a 100644 --- a/test/test_glib_plugin.py +++ b/test/test_glib_plugin.py @@ -230,3 +230,33 @@ def test_dbus_plugin(mock_mainloop, mock_byte, mock_bytearray, # Our notification succeeds even though the gi library was not loaded assert(obj.notify(title='title', body='body', notify_type=apprise.NotifyType.INFO) is True) + + # Verify this all works in the event a ValueError is also thronw + # out of the call to gi.require_version() + + # Emulate require_version function: + gi.require_version.side_effect = ValueError() + + # The following libraries need to be reloaded to prevent + # TypeError: super(type, obj): obj must be an instance or subtype of type + # This is better explained in this StackOverflow post: + # https://stackoverflow.com/questions/31363311/\ + # any-way-to-manually-fix-operation-of-\ + # super-after-ipython-reload-avoiding-ty + # + reload(sys.modules['apprise.plugins.NotifyDBus']) + reload(sys.modules['apprise.plugins']) + reload(sys.modules['apprise.Apprise']) + reload(sys.modules['apprise']) + + # Create our instance + obj = apprise.Apprise.instantiate('glib://', suppress_exceptions=False) + assert(isinstance(obj, apprise.plugins.NotifyDBus) is True) + obj.duration = 0 + + # Test url() call + assert(compat_is_basestring(obj.url()) is True) + + # Our notification succeeds even though the gi library was not loaded + assert(obj.notify(title='title', body='body', + notify_type=apprise.NotifyType.INFO) is True) diff --git a/test/test_gnome_plugin.py b/test/test_gnome_plugin.py index ef66e8d2..a584f00a 100644 --- a/test/test_gnome_plugin.py +++ b/test/test_gnome_plugin.py @@ -77,7 +77,7 @@ def test_gnome_plugin(): gi.repository.Notify.init.return_value = True gi.repository.Notify.Notification = mock_notify - # Emulate require_version function1k: + # Emulate require_version function: gi.require_version = mock.Mock( name=gi_name + '.require_version')