[Bf-blender-cvs] [d636152] master: Use context manager for reading addon headers

Campbell Barton noreply at git.blender.org
Mon Jun 8 13:23:27 CEST 2015


Commit: d63615272cc667d9e64fa1fe86a8be3d60519f2c
Author: Campbell Barton
Date:   Mon Jun 8 21:21:54 2015 +1000
Branches: master
https://developer.blender.org/rBd63615272cc667d9e64fa1fe86a8be3d60519f2c

Use context manager for reading addon headers

===================================================================

M	release/scripts/modules/addon_utils.py

===================================================================

diff --git a/release/scripts/modules/addon_utils.py b/release/scripts/modules/addon_utils.py
index 68efe9c..63b01a4 100644
--- a/release/scripts/modules/addon_utils.py
+++ b/release/scripts/modules/addon_utils.py
@@ -80,39 +80,37 @@ def modules_refresh(module_cache=addons_fake_modules):
             print("Error opening file %r: %s" % (mod_path, e))
             return None
 
-        if speedy:
-            lines = []
-            line_iter = iter(file_mod)
-            l = ""
-            while not l.startswith("bl_info"):
-                try:
-                    l = line_iter.readline()
-                except UnicodeDecodeError as e:
-                    if not error_encoding:
-                        error_encoding = True
-                        print("Error reading file as UTF-8:", mod_path, e)
-                    file_mod.close()
-                    return None
-
-                if len(l) == 0:
-                    break
-            while l.rstrip():
-                lines.append(l)
-                try:
-                    l = line_iter.readline()
-                except UnicodeDecodeError as e:
-                    if not error_encoding:
-                        error_encoding = True
-                        print("Error reading file as UTF-8:", mod_path, e)
-                    file_mod.close()
-                    return None
-
-            data = "".join(lines)
+        with file_mod:
+            if speedy:
+                lines = []
+                line_iter = iter(file_mod)
+                l = ""
+                while not l.startswith("bl_info"):
+                    try:
+                        l = line_iter.readline()
+                    except UnicodeDecodeError as e:
+                        if not error_encoding:
+                            error_encoding = True
+                            print("Error reading file as UTF-8:", mod_path, e)
+                        return None
+
+                    if len(l) == 0:
+                        break
+                while l.rstrip():
+                    lines.append(l)
+                    try:
+                        l = line_iter.readline()
+                    except UnicodeDecodeError as e:
+                        if not error_encoding:
+                            error_encoding = True
+                            print("Error reading file as UTF-8:", mod_path, e)
+                        return None
+
+                data = "".join(lines)
 
-        else:
-            data = file_mod.read()
-
-        file_mod.close()
+            else:
+                data = file_mod.read()
+        del file_mod
 
         try:
             ast_data = ast.parse(data, filename=mod_path)




More information about the Bf-blender-cvs mailing list