plumbum icon indicating copy to clipboard operation
plumbum copied to clipboard

cannot cast to string a StdinDataRedirection-bound LocalCommand

Open jesteria opened this issue 6 years ago • 0 comments

Having directed string input into the standard input of a LocalCommand (via << i.e. __lshift__), I am unable to cast the resulting StdinDataRedirection object to string. Attempting to do so raises an uncaught exception.

$ ipython
Python 3.6.3 (default, Dec 20 2017, 11:51:43) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import plumbum

In [2]: plumbum.__version__
Out[2]: (1, 6, 4)

In [3]: cat = plumbum.local['cat']

In [4]: stdin_bound_cat = cat << 'hello world'

In [5]: str(stdin_bound_cat)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-15af60a0e450> in <module>()
----> 1 str(stdin_bound_cat)

~/.pyenv/versions/3.6.3/envs/test/lib/python3.6/site-packages/plumbum/commands/base.py in __str__(self)
     49
     50     def __str__(self):
---> 51         return " ".join(self.formulate())
     52
     53     def __or__(self, other):

TypeError: sequence item 2: expected str instance, list found

Underlying this issue is that the formulate() method for this object returns a list which itself contains both strings and lists:

In [6]: stdin_bound_cat.formulate()
Out[6]: ["echo 'hello world'", '|', ['/bin/cat']]

which result cannot simply be joined by a string, as its __str__ attempts to do.

Needless to say, this issue prevents passing such objects to print(), and minorly complicates introspection.

jesteria avatar Aug 01 '18 16:08 jesteria