From 1acb2ddd2f7b671816f83790f5b2a5c7231c38d2 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Wed, 26 May 2021 15:54:36 +0530 Subject: [PATCH] fix: Use context managers to stream data While executing git commands in the shell via Frappe processes, use context managers to ensure files get closed after usage. This fixes the ResourceWarning errors due to unclosed files. --- frappe/utils/change_log.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/frappe/utils/change_log.py b/frappe/utils/change_log.py index 6cb71c6ac5..ddd11265eb 100644 --- a/frappe/utils/change_log.py +++ b/frappe/utils/change_log.py @@ -118,9 +118,9 @@ def get_versions(): def get_app_branch(app): '''Returns branch of an app''' try: - null_stream = open(os.devnull, 'wb') - result = subprocess.check_output('cd ../apps/{0} && git rev-parse --abbrev-ref HEAD'.format(app), - shell=True, stdin=null_stream, stderr=null_stream) + with open(os.devnull, 'wb') as null_stream: + result = subprocess.check_output(f'cd ../apps/{app} && git rev-parse --abbrev-ref HEAD', + shell=True, stdin=null_stream, stderr=null_stream) result = safe_decode(result) result = result.strip() return result @@ -129,9 +129,9 @@ def get_app_branch(app): def get_app_last_commit_ref(app): try: - null_stream = open(os.devnull, 'wb') - result = subprocess.check_output('cd ../apps/{0} && git rev-parse HEAD --short 7'.format(app), - shell=True, stdin=null_stream, stderr=null_stream) + with open(os.devnull, 'wb') as null_stream: + result = subprocess.check_output(f'cd ../apps/{app} && git rev-parse HEAD --short 7', + shell=True, stdin=null_stream, stderr=null_stream) result = safe_decode(result) result = result.strip() return result