def parse_function(function): """ Tries to guess which modules are used by *function*. Returns a list of module names. This function is used by :func:`find_modules` to parse modules used by a function. You can use it to check that all modules used by your functions are detected correctly. The current heuristic requires that modules are accessed using the dot notation directly, e.g. ``random.uniform(1, 10)``. For instance, required modules are not detected correctly in the following snippet:: a = random a.uniform(1, 10) Also, modules used in generator expressions, like here:: return ((k, base64.encodestring(v)) for k, v in d.iteritems()) are not detected correctly. """ if isinstance(function, functools.partial): return parse_function(function.func) code = function.func_code mod = re.compile(r'\x%.2x(..)\x%.2x' % (opname.index('LOAD_GLOBAL'), opname.index('LOAD_ATTR')), re.DOTALL) return [code.co_names[struct.unpack('<H', x)[0]] for x in mod.findall(code.co_code)]
def parse_function(function): if isinstance(function, functools.partial): return parse_function(function.func) code = function.func_code mod = re.compile(r'\x%.2x(..)\x%.2x' % (opname.index('LOAD_GLOBAL'), opname.index('LOAD_ATTR')), re.DOTALL) return [code.co_names[struct.unpack('<H', x)[0]] for x in mod.findall(code.co_code)]
def parse_function(function): """ Tries to guess which modules are used by *function*. Returns a list of module names. This function is used by :func:`find_modules` to parse modules used by a function. You can use it to check that all modules used by your functions are detected correctly. The current heuristic requires that modules are accessed using the dot notation directly, e.g. ``random.uniform(1, 10)``. For instance, required modules are not detected correctly in the following snippet:: a = random a.uniform(1, 10) Also, modules used in generator expressions, like here:: return ((k, base64.encodestring(v)) for k, v in d.iteritems()) are not detected correctly. """ if isinstance(function, functools.partial): return parse_function(function.func) code = function.func_code mod = re.compile( r'\x%.2x(..)\x%.2x' % (opname.index('LOAD_GLOBAL'), opname.index('LOAD_ATTR')), re.DOTALL) return [ code.co_names[struct.unpack('<H', x)[0]] for x in mod.findall(code.co_code) ]
def parse_function(fun): code = fun.func_code mod = re.compile("\\x%.2x(..)\\x%.2x" % (opname.index("LOAD_GLOBAL"),\ opname.index("LOAD_ATTR")), re.DOTALL) modules = [] for x in mod.findall(code.co_code): modules.append(code.co_names[struct.unpack("<H", x)[0]]) return modules