Show More
@@ -0,0 +1,12 b'' | |||||
|
1 | from IPython.utils.shimmodule import ShimModule | |||
|
2 | import IPython | |||
|
3 | ||||
|
4 | ||||
|
5 | def test_shimmodule_repr_does_not_fail_on_import_error(): | |||
|
6 | shim_module = ShimModule("shim_module", mirror="mirrored_module_does_not_exist") | |||
|
7 | repr(shim_module) | |||
|
8 | ||||
|
9 | ||||
|
10 | def test_shimmodule_repr_forwards_to_module(): | |||
|
11 | shim_module = ShimModule("shim_module", mirror="IPython") | |||
|
12 | assert repr(shim_module) == repr(IPython) |
@@ -79,3 +79,11 b' class ShimModule(types.ModuleType):' | |||||
79 | return import_item(name) |
|
79 | return import_item(name) | |
80 | except ImportError as e: |
|
80 | except ImportError as e: | |
81 | raise AttributeError(key) from e |
|
81 | raise AttributeError(key) from e | |
|
82 | ||||
|
83 | def __repr__(self): | |||
|
84 | # repr on a module can be called during error handling; make sure | |||
|
85 | # it does not fail, even if the import fails | |||
|
86 | try: | |||
|
87 | return self.__getattr__("__repr__")() | |||
|
88 | except AttributeError: | |||
|
89 | return f"<ShimModule for {self._mirror!r}>" |
General Comments 0
You need to be logged in to leave comments.
Login now