Skip to content

Commit a9a852c

Browse files
committed
Modernize Python/makeopcodetargets.py
* Simply use "import opcode" to import the opcode module instead of tricks using the imp module * Use context manager for the output file * Move code into a new main() function * Replace assert with a regular if to check the number of arguments * Import modules at top level
1 parent e77c974 commit a9a852c

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

Python/makeopcodetargets.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,14 @@
33
(for compilers supporting computed gotos or "labels-as-values", such as gcc).
44
"""
55

6-
# This code should stay compatible with Python 2.3, at least while
7-
# some of the buildbots have Python 2.3 as their system Python.
8-
9-
import imp
6+
import opcode
107
import os
8+
import sys
119

1210

13-
def find_module(modname):
14-
"""Finds and returns a module in the local dist/checkout.
15-
"""
16-
modpath = os.path.join(
17-
os.path.dirname(os.path.dirname(__file__)), "Lib")
18-
return imp.load_module(modname, *imp.find_module(modname, [modpath]))
19-
2011
def write_contents(f):
2112
"""Write C code contents to the target file object.
2213
"""
23-
opcode = find_module("opcode")
2414
targets = ['_unknown_opcode'] * 256
2515
for opname, op in opcode.opmap.items():
2616
targets[op] = "TARGET_%s" % opname
@@ -29,15 +19,17 @@ def write_contents(f):
2919
f.write("\n};\n")
3020

3121

32-
if __name__ == "__main__":
33-
import sys
34-
assert len(sys.argv) < 3, "Too many arguments"
22+
def main():
23+
if len(sys.argv) >= 3:
24+
sys.exit("Too many arguments")
3525
if len(sys.argv) == 2:
3626
target = sys.argv[1]
3727
else:
3828
target = "Python/opcode_targets.h"
39-
f = open(target, "w")
40-
try:
29+
with open(target, "w") as f:
4130
write_contents(f)
42-
finally:
43-
f.close()
31+
print("Jump table written into %s" % target)
32+
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)