Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(38907)

Delta Between Two Patch Sets: Python/makeopcodetargets.py

Issue 20021: "modernize" makeopcodetargets.py
Left Patch Set: Created 4 years, 11 months ago
Right Patch Set: Created 3 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 """Generate C code for the jump table of the threaded code interpreter 2 """Generate C code for the jump table of the threaded code interpreter
3 (for compilers supporting computed gotos or "labels-as-values", such as gcc). 3 (for compilers supporting computed gotos or "labels-as-values", such as gcc).
4 """ 4 """
5 5
6 # This code should stay compatible with Python 2.3, at least while 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. 7 # some of the buildbots have Python 2.3 as their system Python.
8 8
9 import importlib.machinery
10 import os 9 import os
11 10
12 11
13 def find_module(modname): 12 try:
14 """Finds and returns a module in the local dist/checkout. 13 from importlib.machinery import SourceFileLoader
15 """ 14 except ImportError:
16 modpath = os.path.join( 15 import imp
17 os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py") 16
18 return importlib.machinery.SourceFileLoader(modname, modpath).load_module() 17 def find_module(modname):
18 """Finds and returns a module in the local dist/checkout.
19 """
20 modpath = os.path.join(
21 os.path.dirname(os.path.dirname(__file__)), "Lib")
22 return imp.load_module(modname, *imp.find_module(modname, [modpath]))
23 else:
24 def find_module(modname):
25 """Finds and returns a module in the local dist/checkout.
26 """
27 modpath = os.path.join(
28 os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
29 return SourceFileLoader(modname, modpath).load_module()
30
19 31
20 def write_contents(f): 32 def write_contents(f):
21 """Write C code contents to the target file object. 33 """Write C code contents to the target file object.
22 """ 34 """
23 opcode = find_module("opcode") 35 opcode = find_module("opcode")
24 targets = ['_unknown_opcode'] * 256 36 targets = ['_unknown_opcode'] * 256
25 for opname, op in opcode.opmap.items(): 37 for opname, op in opcode.opmap.items():
26 targets[op] = "TARGET_%s" % opname 38 targets[op] = "TARGET_%s" % opname
27 f.write("static void *opcode_targets[256] = {\n") 39 f.write("static void *opcode_targets[256] = {\n")
28 f.write(",\n".join([" &&%s" % s for s in targets])) 40 f.write(",\n".join([" &&%s" % s for s in targets]))
29 f.write("\n};\n") 41 f.write("\n};\n")
30 42
31 43
32 if __name__ == "__main__": 44 if __name__ == "__main__":
33 import sys 45 import sys
34 assert len(sys.argv) < 3, "Too many arguments" 46 assert len(sys.argv) < 3, "Too many arguments"
35 if len(sys.argv) == 2: 47 if len(sys.argv) >= 2:
36 target = sys.argv[1] 48 target = sys.argv[1]
37 else: 49 else:
38 target = "Python/opcode_targets.h" 50 target = "Python/opcode_targets.h"
39 with open(target, "w") as f: 51 f = open(target, "w")
52 try:
40 write_contents(f) 53 write_contents(f)
54 finally:
55 f.close()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+