Browse Source

Close descriptor after reading file.

This change has been motivated by below ResourceWarning Python3
is emitting when using this library:

  ResourceWarning: unclosed file <_io.TextIOWrapper name='.../site-packages/os_service_types/data/service-types.json' mode='r' encoding='UTF-8'>
    return json.load(open(os.path.join(DATA_DIR, filename), 'r'))

Change-Id: Id86cdedab44bf4096185ced2ed6b9379a71bc3e1
Federico Ressi 8 months ago
parent
commit
a372532fd9
1 changed files with 7 additions and 2 deletions
  1. 7
    2
      os_service_types/data/__init__.py

+ 7
- 2
os_service_types/data/__init__.py View File

@@ -21,5 +21,10 @@ DATA_DIR = os.path.dirname(__file__)
21 21
 
22 22
 
23 23
 def read_data(filename):
24
-    """Return data that is shipped inside the python package."""
25
-    return json.load(open(os.path.join(DATA_DIR, filename), 'r'))
24
+    """Return data that is shipped inside the Python package.
25
+
26
+    """
27
+
28
+    filepath = os.path.join(DATA_DIR, filename)
29
+    with open(filepath, 'r') as fd:
30
+        return json.load(fd)

Loading…
Cancel
Save