From 2c2930c663d4855f678b384e0c194be80cbaf9fb Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 29 Jan 2019 13:57:24 +0100 Subject: [PATCH] utils: fix unimporting partially-imported modules Previously, delattr from the parent package might throw KeyError. --- src/trezor/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/trezor/utils.py b/src/trezor/utils.py index bdc16cbf5..fd75f6eea 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -28,8 +28,12 @@ def unimport_end(mods): continue path = mod[:i] name = mod[i + 1 :] - if path in sys.modules: + try: delattr(sys.modules[path], name) + except KeyError: + # either path is not present in sys.modules, or module is not + # referenced from the parent package. both is fine. + pass # collect removed modules gc.collect()