From 6d64b12b4117b4b85af92f7aeca8a78e49fa9bc8 Mon Sep 17 00:00:00 2001 From: rossjones Date: Tue, 4 Feb 2020 10:02:43 +0000 Subject: [PATCH] Remove ResourceWarnings when loading AMIS and INSTANCE_TYPES When loading AMIS and INSTANCE_TYPES in moto.ec2.models a file handle is potentially leaked when loading the JSON. This results in a ResourceWarning which is a bit of unnecessary noise. Rather than pass a call to open() to json.load() this instead uses a context-manager in a small private helper function. This fixes https://github.com/spulec/moto/issues/2620 --- moto/ec2/models.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 93a350914..a0c886087 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -139,17 +139,22 @@ from .utils import ( rsa_public_key_fingerprint, ) -INSTANCE_TYPES = json.load( - open(resource_filename(__name__, "resources/instance_types.json"), "r") + +def _load_resource(filename): + with open(filename, "r") as f: + return json.load(f) + + +INSTANCE_TYPES = _load_resource( + resource_filename(__name__, "resources/instance_types.json") ) -AMIS = json.load( - open( - os.environ.get("MOTO_AMIS_PATH") - or resource_filename(__name__, "resources/amis.json"), - "r", - ) + +AMIS = _load_resource( + os.environ.get("MOTO_AMIS_PATH") + or resource_filename(__name__, "resources/amis.json"), ) + OWNER_ID = "111122223333"