Skip to content

Commit

Permalink
Scan class definitions for import statements. Resolves mitogen-hq#682
Browse files Browse the repository at this point in the history
  • Loading branch information
opoplawski committed Oct 6, 2020
1 parent a60c6c1 commit d0a8a91
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions mitogen/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def _get_core_source():
mitogen.parent._get_core_source = _get_core_source


BUILD_TUPLE = dis.opname.index('BUILD_TUPLE')
LOAD_CONST = dis.opname.index('LOAD_CONST')
LOAD_NAME = dis.opname.index('LOAD_NAME')
IMPORT_NAME = dis.opname.index('IMPORT_NAME')


Expand Down Expand Up @@ -237,7 +239,8 @@ def scan_code_imports(co):
"""
Given a code object `co`, scan its bytecode yielding any ``IMPORT_NAME``
and associated prior ``LOAD_CONST`` instructions representing an `Import`
statement or `ImportFrom` statement.
statement or `ImportFrom` statement. Also scan class definitions for
imports.
:return:
Generator producing `(level, modname, namelist)` tuples, where:
Expand All @@ -260,11 +263,12 @@ def scan_code_imports(co):
return

if sys.version_info >= (2, 5):
for oparg1, oparg2, (op3, arg3) in izip(opit, opit2, opit3):
if op3 == IMPORT_NAME:
op2, arg2 = oparg2
op1, arg1 = oparg1
if op1 == op2 == LOAD_CONST:
for (op1, arg1), (op2, arg2), (op3, arg3) in izip(opit, opit2, opit3):
# Scan defined classes for imports
if op1 == LOAD_NAME and op2 == BUILD_TUPLE and op3 == LOAD_CONST and isinstance(co.co_consts[arg3],types.CodeType):
for level, modname, namelist in scan_code_imports(co.co_consts[arg3]):
yield (level, modname, namelist)
if op3 == IMPORT_NAME and op1 == op2 == LOAD_CONST:
yield (co.co_consts[arg1],
co.co_names[arg3],
co.co_consts[arg2] or ())
Expand Down

0 comments on commit d0a8a91

Please sign in to comment.