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)
| 999 | return |
| 1000 | |
| 1001 | def _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 | |
| 1026 | def _find_store_names(co): |
| 1027 | """Find names of variables which are written in the code |
nothing calls this directly
no test coverage detected
searching dependent graphs…