summaryrefslogtreecommitdiffstats
path: root/Lib/dis.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2021-09-09 13:04:12 (GMT)
committerGitHub <noreply@github.com>2021-09-09 13:04:12 (GMT)
commit04676b69466d2e6d2903f1c6879d2cb292721455 (patch)
tree9e9901e4c25cb84800949d990a9cbaef851d32be /Lib/dis.py
parent49acac00c08838d8080ce00d02c05284b94f8fb2 (diff)
downloadcpython-04676b69466d2e6d2903f1c6879d2cb292721455.zip
cpython-04676b69466d2e6d2903f1c6879d2cb292721455.tar.gz
cpython-04676b69466d2e6d2903f1c6879d2cb292721455.tar.bz2
bpo-45017: move opcode-related logic from modulefinder to dis (GH-28246)
Diffstat (limited to 'Lib/dis.py')
-rw-r--r--Lib/dis.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/dis.py b/Lib/dis.py
index 66487dc..a073572 100644
--- a/Lib/dis.py
+++ b/Lib/dis.py
@@ -535,6 +535,42 @@ def findlinestarts(code):
yield start, line
return
+def _find_imports(co):
+ """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__.
+ """
+ IMPORT_NAME = opmap['IMPORT_NAME']
+ LOAD_CONST = opmap['LOAD_CONST']
+
+ consts = co.co_consts
+ names = co.co_names
+ opargs = [(op, arg) for _, op, arg in _unpack_opargs(co.co_code)
+ if op != EXTENDED_ARG]
+ for i, (op, oparg) in enumerate(opargs):
+ if (op == IMPORT_NAME and i >= 2
+ and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST):
+ level = consts[opargs[i-2][1]]
+ fromlist = consts[opargs[i-1][1]]
+ yield (names[oparg], level, fromlist)
+
+def _find_store_names(co):
+ """Find names of variables which are written in the code
+
+ Generate sequence of strings
+ """
+ STORE_OPS = {
+ opmap['STORE_NAME'],
+ opmap['STORE_GLOBAL']
+ }
+
+ names = co.co_names
+ for _, op, arg in _unpack_opargs(co.co_code):
+ if op in STORE_OPS:
+ yield names[arg]
+
class Bytecode:
"""The bytecode operations of a piece of code