Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 237b6e1

Browse filesBrowse files
authored
Handle optional import where imported module raises exception on import (plotly#1192)
* Handle optional import where imported module raises exception on import. Log exception but don't raise
1 parent 79cdc1e commit 237b6e1
Copy full SHA for 237b6e1

File tree

Expand file treeCollapse file tree

3 files changed

+39
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+39
-2
lines changed

‎plotly/optional_imports.py

Copy file name to clipboardExpand all lines: plotly/optional_imports.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from __future__ import absolute_import
66

77
from importlib import import_module
8+
import logging
89

10+
logger = logging.getLogger(__name__)
911
_not_importable = set()
1012

1113

@@ -23,3 +25,7 @@ def get_module(name):
2325
return import_module(name)
2426
except ImportError:
2527
_not_importable.add(name)
28+
except Exception as e:
29+
_not_importable.add(name)
30+
msg = "Error importing optional module {}".format(name)
31+
logger.exception(msg)
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# A module that raises and exception on import
2+
raise Exception("Boom!")

‎plotly/tests/test_core/test_optional_imports/test_optional_imports.py

Copy file name to clipboardExpand all lines: plotly/tests/test_core/test_optional_imports/test_optional_imports.py
+31-2Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import
2-
2+
import sys
33
from unittest import TestCase
4-
54
from plotly.optional_imports import get_module
65

76

@@ -22,3 +21,33 @@ def test_get_module_exists_submodule(self):
2221
def test_get_module_does_not_exist(self):
2322
module = get_module('hoopla')
2423
self.assertIsNone(module)
24+
25+
def test_get_module_import_exception(self):
26+
# Get module that raises an exception on import
27+
module_str = ('plotly.tests.test_core.'
28+
'test_optional_imports.exploding_module')
29+
30+
if sys.version_info.major == 3 and sys.version_info.minor >= 4:
31+
with self.assertLogs('plotly.optional_imports', level='ERROR') as cm:
32+
module = get_module(module_str)
33+
34+
# No exception should be raised and None should be returned
35+
self.assertIsNone(module)
36+
37+
# Check logging level and log message
38+
expected_start = ('ERROR:plotly.optional_imports:'
39+
'Error importing optional module ' + module_str)
40+
41+
self.assertEqual(
42+
cm.output[0][:len(expected_start)], expected_start)
43+
44+
# Check that exception message is included after log message
45+
expected_end = 'Boom!'
46+
self.assertEqual(
47+
cm.output[0][-len(expected_end):], expected_end)
48+
else:
49+
# Don't check logging
50+
module = get_module(module_str)
51+
52+
# No exception should be raised and None should be returned
53+
self.assertIsNone(module)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.