1

In Bash, if I create a function, I can use the type command to show me that function definition like so:

# define a function
foo {
    echo "bar"
}

# now show me that definition
$ type foo
foo is a function
foo ()
{
    echo "bar"
}

Zsh and Fish both have something similar with their functions commands. I checked the bash-to-xsh page here, but could not find an equivalent. I tried using Python's inspect, but that didn't work either:

def foo():
  echo "bar"

$ foo
<function __main__.foo>

$ import inspect as i
$ i.getsource(foo)
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
OSError: could not get source code

How do you show method definitions in Xonsh?

mattmc3
  • 17,595
  • 7
  • 83
  • 103

2 Answers2

0

It appears that this is a limitation of Python, and not really a problem with Xonsh, according to this thread.

If the Xonsh function is defined in a file, inspect works. If the function is defined only in memory, it is not available.

For example, this code works:

import inspect
echo @(inspect.getsource($PROMPT_FIELDS['cwd']))

So does this:

import inspect
echo "def foo():\n\techo 'bar'\n" > foo.xsh
source foo.xsh
echo @(inspect.getsource(foo))
mattmc3
  • 17,595
  • 7
  • 83
  • 103
0

Partial answer: Current versions of xonsh (e.g. 0.13.4) contain superhelp where affixing ?? outputs information about the object. It is an easier way to perform the inspection than using inspect module:

$ foo??

Type:         function
String form:  <function foo at 0x1084e58a0>
File:         /path/to/foo.xsh
Definition:   foo()
Source:
def foo():
    echo "bar"

Unfortunately, it suffers from the same problem you describe in your own answer, that is that the source code would be displayed only for a function saved in a file.

tmt
  • 7,611
  • 4
  • 32
  • 46