Fix a ResourceWarning in generate_opcode_h.py

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.
This commit is contained in:
Victor Stinner 2016-11-25 11:59:52 +01:00
parent 1018fad6a4
commit 6193ecd779
1 changed files with 5 additions and 1 deletions

View 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)