-1

I am not able to get the source code of any function in python. Do I need to reinstall python? Does python looks OK in this case?

# python
Python 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 22:25:07)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(arg1,arg2):
...     #do something with args
...     a = arg1 + arg2
...     return a
...
>>> import inspect
>>> lines = inspect.getsource(foo)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/miniforge3/lib/python3.7/inspect.py", line 973, in getsource
    lines, lnum = getsourcelines(object)
  File "/root/miniforge3/lib/python3.7/inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "/root/miniforge3/lib/python3.7/inspect.py", line 786, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

Update:

Here is the module that I am trying to use and getting the above error:

>>> from marvin import ai_fn
>>> @ai_fn
... @ai_fn
... def rhyme(word: str) -> str:
...     "Returns a word that rhymes with the input word."
...
>>> rhyme("blue")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/marvin/bots/ai_functions.py", line 128, in ai_fn_wrapper
    return_value = fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/marvin/bots/ai_functions.py", line 141, in ai_fn_wrapper
    function_def = inspect.cleandoc(inspect.getsource(fn))
  File "/usr/local/lib/python3.10/inspect.py", line 1139, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/local/lib/python3.10/inspect.py", line 1121, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/local/lib/python3.10/inspect.py", line 958, in findsource
    raise OSError('could not get source code')
OSError: could not get source code
shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • 2
    This is expected in interactive mode. If you put that code into a file, it should work. – Unmitigated Apr 01 '23 at 04:56
  • 1
    It seems like you are expecting "get source" to mean "look at the compiled function, figure out what the source code would have to be in order to get that compiled result, and return that." It does not mean that, and that would be impossible anyway - there are multiple different possible input sources that could give the same result. You cannot get the cow back from the hamburgers. Instead, "get source" means "look at the function's metadata to figure out what **file on disk** it was compiled from, and copy-paste the corresponding lines". This can only happen if there **is** such a file. – Karl Knechtel Apr 01 '23 at 04:59
  • I closed this as a duplicate because the top answers there explicitly mention the limitation. – Karl Knechtel Apr 01 '23 at 04:59
  • @KarlKnechtel Personally, I think this would be a better target: https://stackoverflow.com/questions/57035364/inspect-getsourcelinesobject-shows-oserror-if-run-from-python-shell-but-gives – Unmitigated Apr 01 '23 at 05:01
  • "Here is the module that I am trying to use and getting the above error:" It **does not matter** what module you are using. The problem is that the code is **not physically stored in a file on your hard drive (or SSD) and directly loaded from there**. – Karl Knechtel Apr 01 '23 at 05:02
  • @Unmitigated much better; updated. – Karl Knechtel Apr 01 '23 at 05:03
  • If I import the function from a module, Isn't it obvious that it is physically stored in a file? It is from "getting started" page. – shantanuo Apr 01 '23 at 05:04
  • 1
    `rhyme` isn’t imported from a file. – MisterMiyagi Apr 01 '23 at 05:21

2 Answers2

0

inspect.getsource just doesn't work for functions defined interactively. It needs to be able to open the file a function was defined in, but your function wasn't defined in a file in the first place.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • That was just an example. I am getting the same os-error here... https://www.askmarvin.ai/getting_started/ai_functions_quickstart/ – shantanuo Apr 01 '23 at 04:57
  • 1
    @shantanuo Then update your post please. Don't wait for someone to write an answer before you put [all the details](/help/how-to-ask) in your post. – Mike 'Pomax' Kamermans Apr 01 '23 at 05:00
  • Yes, the problem is the same. If you use some magic function decorator to fill in the body of a function by, e.g., making a web request to some API that will fill in an AI code completion result, then there is still no **physical file on disk** from which the source can be extracted. – Karl Knechtel Apr 01 '23 at 05:01
  • The module looks very popular. Are you suggesting it does not work as advertised? – shantanuo Apr 01 '23 at 05:07
0

inspect.getsource will raise OS Error, if python cannot able to retrieve the source code. getsource function needs to retrieve the source code file, instead of executing in IDLE interactive shell try executing in python file, it works for meExecuted in source File

Executed in Interactive Shell

Mahira
  • 1
  • 3