tonybaloney / instaviz

Instant visualization of Python AST and Code Objects
Apache License 2.0
129 stars 18 forks source link

Unable to run instaviz example from cpython internals book with ipython #18

Open esc opened 3 years ago

esc commented 3 years ago

I tried to execute the code as listed and i receive the following error:

Python 3.8.2 (default, Mar 26 2020, 10:43:30)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import instaviz

In [2]: def example():
   ...:     a = 1
   ...:     b = a + 1
   ...:     return b
   ...:

In [3]: instaviz.show(example)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-3-d38f03221afb> in <module>
----> 1 instaviz.show(example)

~/miniconda3/envs/numba_3.8/lib/python3.8/site-packages/instaviz/__init__.py in show(obj)
     16     if hasattr(obj, "__code__"):
     17         instructions = get_instructions(obj.__code__)
---> 18         show_code_object(obj, instructions)
     19     else:
     20         print("{0} is not compiled, could not locate __code__ attribute.".format(type(obj)))

~/miniconda3/envs/numba_3.8/lib/python3.8/site-packages/instaviz/web.py in show_code_object(obj, instructions)
    109     data["ins"] = list(instructions)
    110
--> 111     (lines, start_line) = source.getsourcelines(obj)
    112     src = "".join(lines)
    113     tree = ast.parse(src, cobj.co_filename)

~/miniconda3/envs/numba_3.8/lib/python3.8/site-packages/dill/source.py in getsourcelines(object, lstrip, enclosing)
    323     If lstrip=True, ensure there is no indentation in the first line of code.
    324     If enclosing=True, then also return any enclosing code."""
--> 325     code, n = getblocks(object, lstrip=lstrip, enclosing=enclosing, locate=True)
    326     return code[-1], n[-1]
    327

~/miniconda3/envs/numba_3.8/lib/python3.8/site-packages/dill/source.py in getblocks(object, lstrip, enclosing, locate)
    249     DEPRECATED: use 'getsourcelines' instead
    250     """
--> 251     lines, lnum = findsource(object)
    252
    253     if ismodule(object):

~/miniconda3/envs/numba_3.8/lib/python3.8/site-packages/dill/source.py in findsource(object)
    152
    153     if not lines:
--> 154         raise IOError('could not extract source code')
    155
    156     #FIXME: all below may fail if exec used (i.e. exec('f = lambda x:x') )

OSError: could not extract source code
esc commented 3 years ago

I thought it may be a 3.9 issue, but I get the same on Python 3.9

esc commented 3 years ago

Right, it's an ipython issue, works with a regular python.

esc commented 3 years ago

I updated the issue title to more closely reflect the discovery.

zzhengnan commented 2 years ago

+1 that instaviz works in regular Python REPL but fails in IPython (with the same error as in the issue description).

Versions

Thanks for creating this great tool!