132 This is basically just the default opcode scanner from ModuleFinder, but extended to also
133 look for "process.load(<module>)' commands. Since the Process object might not necassarily
134 be called "process", it scans for a call to a "load" method with a single parameter on
135 *any* object. If one is found it checks if the parameter is a string that refers to a valid
136 python module in the local or global area. If it does, the scanner assumes this was a call
137 to a Process object and yields the module name.
138 It's not possible to scan first for Process object declarations to get the name of the
139 objects since often (e.g. for customisation functions) the object is passed to a function
142 The ModuleFinder.scan_opcodes_25 implementation this is based was taken from
143 https://hg.python.org/cpython/file/2.7/Lib/modulefinder.py#l364
149 consts = co.co_consts
150 LOAD_CONST = modulefinder.LOAD_CONST
151 IMPORT_NAME = modulefinder.IMPORT_NAME
152 STORE_OPS = modulefinder.STORE_OPS
153 HAVE_ARGUMENT = modulefinder.HAVE_ARGUMENT
154 LOAD_ATTR = chr(dis.opname.index(
'LOAD_ATTR'))
155 LOAD_NAME = chr(dis.opname.index(
'LOAD_NAME'))
156 CALL_FUNCTION = chr(dis.opname.index(
'CALL_FUNCTION'))
157 LOAD_LOAD_AND_IMPORT = LOAD_CONST + LOAD_CONST + IMPORT_NAME
160 indexOfLoadConst = names.index(
"load")
163 loadMethodOpcodes = LOAD_ATTR+struct.pack(
'<H',indexOfLoadConst)
166 loadMethodOpcodes=
None
172 if loadMethodOpcodes!=
None and len(code)>=9 :
173 if code[:3]==loadMethodOpcodes :
178 if code[6]==CALL_FUNCTION :
182 indexInTable=
unpack(
'<H',code[4:6])[0]
183 if code[3]==LOAD_CONST :
185 loadMethodArgument=consts[indexInTable]
190 loadMethodArgument = loadMethodArgument.replace(
"/",
".")
198 for subModule
in loadMethodArgument.split(
".") :
199 moduleInfo=imp.find_module( subModule, parentFilename )
200 parentFilename=[moduleInfo[1]]
202 yield "import", (
None, loadMethodArgument)
206 for subModule
in loadMethodArgument.split(
".") :
207 moduleInfo=imp.find_module( subModule, parentFilename )
208 parentFilename=[moduleInfo[1]]
210 yield "import", (
None, loadMethodArgument)
211 except Exception
as error:
217 elif code[3]==LOAD_NAME :
221 print "Unable to determine the value of variable '"+names[indexInTable]+
"' to see if it is a proces.load(...) statement in file "+co.co_filename
227 oparg, =
unpack(
'<H', code[1:3])
228 yield "store", (names[oparg],)
231 if code[:9:3] == LOAD_LOAD_AND_IMPORT:
232 oparg_1, oparg_2, oparg_3 =
unpack(
'<xHxHxH', code[:9])
233 level = consts[oparg_1]
235 yield "import", (consts[oparg_2], names[oparg_3])
237 yield "absolute_import", (consts[oparg_2], names[oparg_3])
239 yield "relative_import", (level, consts[oparg_2], names[oparg_3])
242 if c >= HAVE_ARGUMENT: