From 4f33788404f9628b30c5ca3a0902f7a72aa89906 Mon Sep 17 00:00:00 2001 From: Hilal Alsibai Date: Thu, 3 Feb 2022 10:56:12 -0800 Subject: [PATCH] Fix dangling file handles Summary: Not sure why these weren't closed properly before Differential Revision: D33961678 fbshipit-source-id: d110cc971adc2f2fc9744fb48fa474862fd10e6b --- plugin/src/py/android_screenshot_tests/pull_screenshots.py | 6 ++++-- plugin/src/py/android_screenshot_tests/recorder.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/plugin/src/py/android_screenshot_tests/pull_screenshots.py b/plugin/src/py/android_screenshot_tests/pull_screenshots.py index b642cd33..20b1f1ec 100755 --- a/plugin/src/py/android_screenshot_tests/pull_screenshots.py +++ b/plugin/src/py/android_screenshot_tests/pull_screenshots.py @@ -113,7 +113,8 @@ def generate_html( # and returns a url to an image from a previous run of the test, # old_imgs_data a dict that will be used in the test_img_api url. # Creates the html for showing a before and after comparison of the images. - screenshots = json.load(open(join(output_dir, "metadata.json"))) + with open(join(output_dir, "metadata.json")) as m: + screenshots = json.load(m) alternate = False index_html = abspath(join(output_dir, "index.html")) with codecs.open(index_html, mode="w", encoding="utf-8") as html: @@ -530,7 +531,8 @@ def move_all_files_to_different_directory(source_dir, target_dir): def _summary(dir): - metadataJson = json.load(open(join(dir, "metadata.json"))) + with open(join(dir, "metadata.json")) as f: + metadataJson = json.load(f) count = len(metadataJson) print("Found %d screenshots" % count) diff --git a/plugin/src/py/android_screenshot_tests/recorder.py b/plugin/src/py/android_screenshot_tests/recorder.py index e990c454..d7e18bee 100644 --- a/plugin/src/py/android_screenshot_tests/recorder.py +++ b/plugin/src/py/android_screenshot_tests/recorder.py @@ -71,8 +71,8 @@ def _copy(self, name, w, h): im.close() def _get_metadata_json(self): - file = open(join(self._input, "metadata.json"), "r") - return json.load(file) + with open(join(self._input, "metadata.json"), "r") as f: + return json.load(f) def _record(self): metadata = self._get_metadata_json()