From 9a520b26e39b3483607d9ff0b9690aa5eb8e9005 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 8 Jan 2019 09:03:49 -0600 Subject: [PATCH] Make sure to clean up even if an exception occurs (#50620) --- test/sanity/validate-modules/module_args.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/test/sanity/validate-modules/module_args.py b/test/sanity/validate-modules/module_args.py index 2031017ef3..4a7644c209 100644 --- a/test/sanity/validate-modules/module_args.py +++ b/test/sanity/validate-modules/module_args.py @@ -61,17 +61,18 @@ def setup_env(filename): setattr(module.AnsibleModule, '__init__', fake) setattr(module, '_load_params', _fake_load_params) - yield fake + try: + yield fake + finally: + setattr(module.AnsibleModule, '__init__', _original_init) + setattr(module, '_load_params', _original_load_params) - setattr(module.AnsibleModule, '__init__', _original_init) - setattr(module, '_load_params', _original_load_params) - - # Clean up imports to prevent issues with mutable data being used in modules - for k in list(sys.modules.keys()): - # It's faster if we limit to items in ansible.module_utils - # But if this causes problems later, we should remove it - if k not in pre_sys_modules and k.startswith('ansible.module_utils.'): - del sys.modules[k] + # Clean up imports to prevent issues with mutable data being used in modules + for k in list(sys.modules.keys()): + # It's faster if we limit to items in ansible.module_utils + # But if this causes problems later, we should remove it + if k not in pre_sys_modules and k.startswith('ansible.module_utils.'): + del sys.modules[k] def get_argument_spec(filename):