Lambda alias in Xon.sh and pipes

115 Views Asked by At

Why this peaces of code dont't run the same.

samuel@corny ~ $ echo "pippo\npluto\nminnie\ntopolino" | @(lambda a,s=None: s.read())                                 
pippo
pluto
minnie
topolino

Second command

samuel@corny ~ $ echo "pippo\npluto\nminnie\ntopolino" | @(lambda a,s=None: s.readlines())                            
pippo
pluto

Is the python readlines uncorrect for read line by line the input Pipe.

My version of Xon.sh is 0.7.8

1

There are 1 best solutions below

0
On

So this is somewhat subtle, but callable aliases (which lambdas are one type of), have a few different kinds of objects that they can return. One of which is a tuple of (stdout, stderr, returncode).

The str.readlines() method returns a list, which in this case is ['pippo\n', 'pluto\n', 'minnie\n', 'topolino\n']. So, here,

  • stdout = "pippo\n"
  • stderr = "pluto\n"
  • returncode is never printed, but happens to be "minnie\n" here

This is probably not the intended behavior and is why the first example works.