def accessed_attributes_of_local(f, local_name):
"""
Get a list of attributes of ``local_name`` accessed by ``f``.
The analysis performed by this function is conservative, meaning that
it's not guaranteed to find **all** attributes used.
"""
used = set()
# Find sequences of the form: LOAD_FAST(local_name), LOAD_ATTR(<name>).
# This will find all usages of the form ``local_name.<name>``.
#
# It will **NOT** find usages in which ``local_name`` is aliased to
# another name.
for first, second in sliding_window(dis.get_instructions(f), 2):
if first.opname == 'LOAD_FAST' and first.argval == local_name:
if second.opname in ('LOAD_ATTR', 'STORE_ATTR'):
used.add(second.argval)
return used
评论列表
文章目录