Skip to content

Commit ca9dbc7

Browse files
committed
makeopcodetargets.py: we need to import Lib/opcode.py
Issue python#20021: use importlib.machinery to import Lib/opcode.py and not an opcode module coming from somewhere else. makeopcodetargets.py is part of the Python build process and it is run by an external Python program, not the built Python program. Patch written by Serhiy Storchaka.
1 parent 92c8dce commit ca9dbc7

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

Python/makeopcodetargets.py

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

6-
import opcode
76
import os
87
import sys
98

109

10+
try:
11+
from importlib.machinery import SourceFileLoader
12+
except ImportError:
13+
import imp
14+
15+
def find_module(modname):
16+
"""Finds and returns a module in the local dist/checkout.
17+
"""
18+
modpath = os.path.join(
19+
os.path.dirname(os.path.dirname(__file__)), "Lib")
20+
return imp.load_module(modname, *imp.find_module(modname, [modpath]))
21+
else:
22+
def find_module(modname):
23+
"""Finds and returns a module in the local dist/checkout.
24+
"""
25+
modpath = os.path.join(
26+
os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
27+
return SourceFileLoader(modname, modpath).load_module()
28+
29+
1130
def write_contents(f):
1231
"""Write C code contents to the target file object.
1332
"""
33+
opcode = find_module('opcode')
1434
targets = ['_unknown_opcode'] * 256
1535
for opname, op in opcode.opmap.items():
1636
targets[op] = "TARGET_%s" % opname

0 commit comments

Comments
 (0)