From 063a9dd911ddc0f77309cb25b8ce0a6bd0b7584b Mon Sep 17 00:00:00 2001 From: Eitan Adler Date: Tue, 9 Oct 2018 15:53:03 -0700 Subject: [PATCH] Reduce noise in warnings from ansible (#40754) When python is compiled in debug mode, or certain command line flags are passed, python issues helpful warnings to let users know of files opened by not closed. This can be fairly spammy on an ansible run. This change reduces the number of such warnings by a factor of 10. --- lib/ansible/executor/module_common.py | 3 ++- lib/ansible/parsing/plugin_docs.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ansible/executor/module_common.py b/lib/ansible/executor/module_common.py index f384b639c8..0d19c005d6 100644 --- a/lib/ansible/executor/module_common.py +++ b/lib/ansible/executor/module_common.py @@ -684,7 +684,8 @@ def _find_module_utils(module_name, b_module_data, module_path, module_args, tas # Optimization -- don't lock if the module has already been cached if os.path.exists(cached_module_filename): display.debug('ANSIBALLZ: using cached module: %s' % cached_module_filename) - zipdata = open(cached_module_filename, 'rb').read() + with open(cached_module_filename, 'rb') as module_data: + zipdata = module_data.read() else: if module_name in action_write_locks.action_write_locks: display.debug('ANSIBALLZ: Using lock for %s' % module_name) diff --git a/lib/ansible/parsing/plugin_docs.py b/lib/ansible/parsing/plugin_docs.py index 11f3f5c987..dd1e038d9c 100644 --- a/lib/ansible/parsing/plugin_docs.py +++ b/lib/ansible/parsing/plugin_docs.py @@ -39,8 +39,8 @@ def read_docstring(filename, verbose=True, ignore_errors=True): } try: - b_module_data = open(filename, 'rb').read() - M = ast.parse(b_module_data) + with open(filename, 'rb') as b_module_data: + M = ast.parse(b_module_data.read()) for child in M.body: if isinstance(child, ast.Assign):