]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix a ResourceWarning in generate_opcode_h.py
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 25 Nov 2016 10:59:52 +0000 (11:59 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 25 Nov 2016 10:59:52 +0000 (11:59 +0100)
Use a context manager to close the Python file. Replace also open() with
tokenize.open() to handle coding cookie if any in Lib/opcode.py.

Tools/scripts/generate_opcode_h.py

index 948b56f9007e93a55a3296d7d584abcef4bd6b9a..bbc3aab5535419be7ecb84d586db2c339d5b5e47 100644 (file)
@@ -32,10 +32,14 @@ enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE,
 #endif /* !Py_OPCODE_H */
 """
 
+import tokenize
+
 
 def main(opcode_py, outfile='Include/opcode.h'):
     opcode = {}
-    exec(open(opcode_py).read(), opcode)
+    with tokenize.open(opcode_py) as fp:
+        code = fp.read()
+    exec(code, opcode)
     opmap = opcode['opmap']
     with open(outfile, 'w') as fobj:
         fobj.write(header)