Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 213cc38

Browse files
committed
Reintroduce Python2 support in generate_opcode_h.py
Issue #28821. Add also a message to show that the command did something :-)
1 parent 50c13f2 commit 213cc38

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

Tools/scripts/generate_opcode_h.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This script generates the opcode.h header file.
22

33
import sys
4+
import tokenize
5+
46
header = """/* Auto-generated by Tools/scripts/generate_opcode_h.py */
57
#ifndef Py_OPCODE_H
68
#define Py_OPCODE_H
@@ -32,12 +34,14 @@
3234
#endif /* !Py_OPCODE_H */
3335
"""
3436

35-
import tokenize
36-
3737

3838
def main(opcode_py, outfile='Include/opcode.h'):
3939
opcode = {}
40-
with tokenize.open(opcode_py) as fp:
40+
if hasattr(tokenize, 'open'):
41+
fp = tokenize.open(opcode_py) # Python 3.2+
42+
else:
43+
fp = open(opcode_py) # Python 2.7
44+
with fp:
4145
code = fp.read()
4246
exec(code, opcode)
4347
opmap = opcode['opmap']
@@ -51,6 +55,8 @@ def main(opcode_py, outfile='Include/opcode.h'):
5155
('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT']))
5256
fobj.write(footer)
5357

58+
print("%s regenerated from %s" % (outfile, opcode_py))
59+
5460

5561
if __name__ == '__main__':
5662
main(sys.argv[1], sys.argv[2])

0 commit comments

Comments
 (0)