Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dill.source.getsource fails inside of ipython #346

Closed
mmckerns opened this issue Oct 23, 2019 · 4 comments · Fixed by #531
Closed

dill.source.getsource fails inside of ipython #346

mmckerns opened this issue Oct 23, 2019 · 4 comments · Fixed by #531

Comments

@mmckerns
Copy link
Member

tested in python 3.6.9. dill.source.getsource fails to recognize a function defined in the current session. (using most current bleeding edge build of dill).

Works in python:

Python 3.6.9 (default, Sep  7 2019, 21:14:28) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> 
>>> def squared(x):
...   return x*x
... 
>>> dill.source.getsource(squared)
'def squared(x):\n  return x*x\n'
>>> 

Fails in ipython:

Python 3.6.9 (default, Sep  7 2019, 21:14:28) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import dill                                                          

In [2]: def squared(x): 
   ...:     return x*x 
   ...:                                                                      

In [3]: dill.source.getsource(squared)[:20]                                                                  
Out[3]: '_HiStOrY_V2_\nimport\\'
@satra
Copy link

satra commented May 16, 2020

just an fyi that while dill.source.getsource generates an error, using it on codeobj works fine.

In [21]: dill.source.getsource(dill.detect.code(test))                                                                      
Out[21]: 'def test():\n    return np.random.random(3)\n'

this returns error

In [20]: dill.source.getsource(test)                                                                                        
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-20-671fbf4a40a5> in <module>
----> 1 dill.source.getsource(test)

~/software/miniconda3/envs/mee-voice/lib/python3.8/site-packages/dill/source.py in getsource(object, alias, lstrip, enclosing, force, builtin)
    352     # get source lines; if fail, try to 'force' an import
    353     try: # fails for builtins, and other assorted object types
--> 354         lines, lnum = getsourcelines(object, enclosing=enclosing)
    355     except (TypeError, IOError): # failed to get source, resort to import hooks
    356         if not force: # don't try to get types that findsource can't get

~/software/miniconda3/envs/mee-voice/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 

~/software/miniconda3/envs/mee-voice/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):

~/software/miniconda3/envs/mee-voice/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

@mmckerns
Copy link
Member Author

@satra: thanks for the information.

@mmckerns mmckerns added this to the dill-0.3.6 milestone Jun 9, 2022
@mmckerns
Copy link
Member Author

See also: #359, #464, #516

mmckerns pushed a commit that referenced this issue Jul 29, 2022
… (#531)

* Temporary quick fix getsource() on IPython interpreter (fixes #346)

* minor changes
@fire17
Copy link

fire17 commented Aug 28, 2022

@satra 's answer simply works! THANKS!! 💛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants