[Mesa-dev] [PATCH 11/14] util/gen_xmlpool: use with statement to open file

Dylan Baker dylan at pnwbakers.com
Fri Oct 26 17:23:40 UTC 2018


Which ensures it is closed at the end of the scope.
---
 src/util/xmlpool/gen_xmlpool.py | 66 ++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/src/util/xmlpool/gen_xmlpool.py b/src/util/xmlpool/gen_xmlpool.py
index a39f9e9e2fa..6a5dcee0a87 100644
--- a/src/util/xmlpool/gen_xmlpool.py
+++ b/src/util/xmlpool/gen_xmlpool.py
@@ -189,42 +189,40 @@ def main():
 
     # Process the options template and generate options.h with all
     # translations.
-    template = io.open(args.template, mode="rt", encoding='utf-8')
-    descMatches = []
-    for line in template:
-        if len(descMatches) > 0:
-            matchENUM = reENUM.match(line)
-            matchDESC_END = reDESC_END.match(line)
-            if matchENUM:
-                descMatches.append(matchENUM)
-            elif matchDESC_END:
-                expandMatches(descMatches, translations, line)
-                descMatches = []
+    with io.open(args.template, mode="rt", encoding='utf-8') as template:
+        descMatches = []
+        for line in template:
+            if len(descMatches) > 0:
+                matchENUM = reENUM.match(line)
+                matchDESC_END = reDESC_END.match(line)
+                if matchENUM:
+                    descMatches.append(matchENUM)
+                elif matchDESC_END:
+                    expandMatches(descMatches, translations, line)
+                    descMatches = []
+                else:
+                    print("Warning: unexpected line inside description dropped:\n",
+                          line, file=sys.stderr)
+                continue
+            if reLibintl_h.search(line):
+                # Ignore (comment out) #include <libintl.h>
+                print("/* %s * commented out by gen_xmlpool.py */" % line)
+                continue
+            matchDESC = reDESC.match(line)
+            matchDESC_BEGIN = reDESC_BEGIN.match(line)
+            if matchDESC:
+                assert len(descMatches) == 0
+                expandMatches([matchDESC], translations)
+            elif matchDESC_BEGIN:
+                assert len(descMatches) == 0
+                descMatches = [matchDESC_BEGIN]
             else:
-                print("Warning: unexpected line inside description dropped:\n", line,
-                      file=sys.stderr)
-            continue
-        if reLibintl_h.search(line):
-            # Ignore (comment out) #include <libintl.h>
-            print("/* %s * commented out by gen_xmlpool.py */" % line)
-            continue
-        matchDESC = reDESC.match(line)
-        matchDESC_BEGIN = reDESC_BEGIN.match(line)
-        if matchDESC:
-            assert len(descMatches) == 0
-            expandMatches([matchDESC], translations)
-        elif matchDESC_BEGIN:
-            assert len(descMatches) == 0
-            descMatches = [matchDESC_BEGIN]
-        else:
-            # In Python 2, stdout expects encoded byte strings, or else it will
-            # encode them with the ascii 'codec'
-            if sys.version_info.major == 2:
-               line = line.encode('utf-8')
-
-            print(line, end='')
+                # In Python 2, stdout expects encoded byte strings, or else it will
+                # encode them with the ascii 'codec'
+                if sys.version_info.major == 2:
+                   line = line.encode('utf-8')
 
-    template.close()
+                print(line, end='')
 
     if len(descMatches) > 0:
         print("Warning: unterminated description at end of file.", file=sys.stderr)
-- 
2.19.1



More information about the mesa-dev mailing list