MCPcopy Index your code
hub / github.com/python/cpython / _find_imports

Function _find_imports

Lib/dis.py:1001–1024  ·  view source on GitHub ↗

Find import statements in the code Generate triplets (name, level, fromlist) where name is the imported module and level, fromlist are the corresponding args to __import__.

(co)

Source from the content-addressed store, hash-verified

999 return
1000
1001def _find_imports(co):
1002 """Find import statements in the code
1003
1004 Generate triplets (name, level, fromlist) where
1005 name is the imported module and level, fromlist are
1006 the corresponding args to __import__.
1007 """
1008 IMPORT_NAME = opmap['IMPORT_NAME']
1009
1010 consts = co.co_consts
1011 names = co.co_names
1012 opargs = [(op, arg) for _, _, op, arg in _unpack_opargs(co.co_code)
1013 if op != EXTENDED_ARG]
1014 for i, (op, oparg) in enumerate(opargs):
1015 if op == IMPORT_NAME and i >= 2:
1016 from_op = opargs[i-1]
1017 level_op = opargs[i-2]
1018 if (from_op[0] in hasconst and
1019 (level_op[0] in hasconst or level_op[0] == LOAD_SMALL_INT)):
1020 level = _get_const_value(level_op[0], level_op[1], consts)
1021 fromlist = _get_const_value(from_op[0], from_op[1], consts)
1022 # IMPORT_NAME encodes lazy/eager flags in bits 0-1,
1023 # name index in bits 2+.
1024 yield (names[oparg >> 2], level, fromlist)
1025
1026def _find_store_names(co):
1027 """Find names of variables which are written in the code

Callers

nothing calls this directly

Calls 3

_unpack_opargsFunction · 0.85
enumerateFunction · 0.85
_get_const_valueFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…