Skip to content

Fix @$ subprocess operator#3156

Closed
laloch wants to merge 5 commits into
xonsh:masterfrom
laloch:fix-captured-inject
Closed

Fix @$ subprocess operator#3156
laloch wants to merge 5 commits into
xonsh:masterfrom
laloch:fix-captured-inject

Conversation

@laloch

@laloch laloch commented Jun 6, 2019

Copy link
Copy Markdown
Member

@$(cmd) operator now returns a list of tokens created by splitting the command's output at the newline character.

Closes #2328

@laloch

laloch commented Jun 6, 2019

Copy link
Copy Markdown
Member Author

The implementation is currently wrong. It breaks the @$(which ls) example from the tutorial and it is not consistent with the bash's behavior either.

@scopatz

scopatz commented Jun 6, 2019

Copy link
Copy Markdown
Member

Thanks for pushing on this

@laloch laloch force-pushed the fix-captured-inject branch from 916c05c to a6e30f1 Compare June 6, 2019 15:05
@laloch

laloch commented Jun 6, 2019

Copy link
Copy Markdown
Member Author

OK, the @$(cmd) operator's behavior is now consistent with $(cmd) in bash.
It still can't do basic ls /lib/modules/@$(uname -r)/kernel though. Parser adjustment required...

Edit: I don't really understand the failing test case.

@laloch

laloch commented Jun 6, 2019

Copy link
Copy Markdown
Member Author

The @$ operator does not require the leading white space character any more. Now I can do

$ echo %%%%@$(which ls)
%%%% ls --color=auto -v --group-directories-first

There is, however still an extra space between the literal argument and the output of the subprocess. @scopatz, do you have a clue about where this extra space comes from?

@scopatz

scopatz commented Jun 6, 2019

Copy link
Copy Markdown
Member

@laloch

laloch commented Jun 7, 2019

Copy link
Copy Markdown
Member Author

@scopatz, thanks for the suggestion, but no, the toks list does not contain the spaces.

@laloch

laloch commented Jun 7, 2019

Copy link
Copy Markdown
Member Author

There is something that's ensuring the subproc_arg tokens to get space-separated.

$ echo a@$(which cp)@$(which cp)
a /usr/bin/cp /usr/bin/cp

@laloch laloch force-pushed the fix-captured-inject branch 2 times, most recently from 3e5d062 to cca3d4d Compare June 7, 2019 11:25
@scopatz

scopatz commented Jun 18, 2019

Copy link
Copy Markdown
Member

Yeah, I wonder if the lexer itself is adding this token (WS)?

@scopatz

scopatz commented Jun 18, 2019

Copy link
Copy Markdown
Member

Yeah, maybe it is this line: https://github.com/xonsh/xonsh/blob/master/xonsh/lexer.py#L349

@laloch

laloch commented Mar 26, 2020

Copy link
Copy Markdown
Member Author

So, I'm back to this PR after several months.
I still can't understand where the surrounding space characters come from. The @$ operator is translated internally by the parser to a call to __xonsh__.subproc_captured_inject which by itself does not return any extra spaces with this patchset applied. Yet, as can be seen below, the @$ operator yields both extra leading and trailing space.

$ echo _@(1)_
_1_
$ echo _@($(pidof init).rstrip())_
_1_
$ echo _@$(pidof init)_
_ 1 _
$ __xonsh__.subproc_captured_inject(["pidof", "init"])
['1']
$ echo _@(__xonsh__.subproc_captured_inject(["pidof", "init"]))_
_1_

@laloch

laloch commented Mar 26, 2020

Copy link
Copy Markdown
Member Author

Never mind. I know what the problem is - it's that I'm parsing the @$ operator without leading whitespace as a separate subprocess argument, which is obviously wrong. I need to concatenate the resulting expression with the previous argument instead.

@laloch laloch force-pushed the fix-captured-inject branch from 05fd575 to 58c0982 Compare March 27, 2020 01:30
@laloch laloch changed the title [WIP] Fix @$ subprocess operator Fix @$ subprocess operator Mar 27, 2020
@laloch

laloch commented Mar 27, 2020

Copy link
Copy Markdown
Member Author

This is now ready to land.
The @$() operator now behaves accordingly to the tutorial and consistently with Bash $() operator. It doesn't require leading and trailing whitespace anymore, so cd /lib/modules/@$(uname -r)/kernel and similar are now possible.

@laloch

laloch commented Mar 27, 2020

Copy link
Copy Markdown
Member Author

Closes #2755
Closes #3457

@laloch laloch force-pushed the fix-captured-inject branch 2 times, most recently from c7ba3b0 to bb98643 Compare March 27, 2020 11:25
@melund

melund commented Mar 27, 2020

Copy link
Copy Markdown
Member

@laloch Any idea some test fail. Are they related to this?

@laloch

laloch commented Mar 27, 2020

Copy link
Copy Markdown
Member Author

@laloch Any idea some test fail. Are they related to this?

Yeah, I would like to know too, but I don't think they might be related.

@laloch

laloch commented Mar 31, 2020

Copy link
Copy Markdown
Member Author

Please do not merge. This is wrong once again. It breaks @() outer product expansion.

@laloch laloch changed the title Fix @$ subprocess operator [WIP] Fix @$ subprocess operator Mar 31, 2020
@laloch laloch force-pushed the fix-captured-inject branch from e3f7f5e to dc1a63e Compare March 31, 2020 22:57
@laloch

laloch commented Mar 31, 2020

Copy link
Copy Markdown
Member Author

Now the @$() operator behaves like @() including the outer product expansion, so now _@$(ls)_ yields

$ cd @$(mktemp -d) and touch a b c d
$ echo _@$(ls)_
_a_ _b_ _c_ _d_

, which is not desirable, since I aim for consistency with $() operator in Bash.

Edit: maybe it is more important to be consistent internally. Maybe I should forget about Bash and keep this consistent with our @() operator instead. Any thoughts?

@scopatz

scopatz commented Apr 1, 2020

Copy link
Copy Markdown
Member

Thanks so much for this @laloch! Can you add a couple of cases to test_parser.py?

@laloch

laloch commented Apr 1, 2020

Copy link
Copy Markdown
Member Author

I'm pretty sure this is now ready to land - this time for real.

@scopatz

scopatz commented Apr 1, 2020

Copy link
Copy Markdown
Member

Windows on Python 3.8 keeps failing. Can you look into it please?

@laloch

laloch commented Apr 1, 2020

Copy link
Copy Markdown
Member Author

The failures seem all to be of similar nature - either builtins or __xonsh__ attribute doesn't exist without any previous error. That happens quite often and I don't have the slightest clue about how that can be possible.

Edit: I mean

        builtins.__xonsh__.path_literal = path_literal
>       builtins.__xonsh__.builtins = _BuiltIns(execer=execer)
E       AttributeError: module 'builtins' has no attribute '__xonsh__

Well, really?

@laloch laloch force-pushed the fix-captured-inject branch 3 times, most recently from 198bd53 to a5ee1d0 Compare April 8, 2020 15:01
laloch added 2 commits April 8, 2020 17:10
@$() operator now removes trailing newline characters from
multiline command output.
@scopatz

scopatz commented Apr 8, 2020

Copy link
Copy Markdown
Member

Hey @laloch - maybe we can get these tests to pass by merging from master. Not sure if you did that just now?

@laloch

laloch commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

Yeah, I just did a rebase on master, that was supposed to trigger the GitHub CI. It didn't.

@scopatz

scopatz commented Apr 8, 2020

Copy link
Copy Markdown
Member

Yeah. Weird. Maybe push an empty commit to try to trigger it?

@laloch laloch force-pushed the fix-captured-inject branch from a5ee1d0 to ed64cdc Compare April 8, 2020 15:29
@laloch laloch force-pushed the fix-captured-inject branch from ed64cdc to 18d199b Compare April 8, 2020 15:31
@laloch

laloch commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

I did some interactive rebasing, rewording and squashing - still the same. Let's try a dummy commit now...

@laloch

laloch commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

Nope. Still the same :(

@scopatz

scopatz commented Apr 8, 2020

Copy link
Copy Markdown
Member

OK, I am going to try to close and reopen

@scopatz scopatz closed this Apr 8, 2020
@scopatz scopatz reopened this Apr 8, 2020
@scopatz

scopatz commented Apr 8, 2020

Copy link
Copy Markdown
Member

Yeah, didn't work. I'll just merge once this passes

@laloch

laloch commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

I can try to open a new PR for the same branch...

@scopatz

scopatz commented Apr 8, 2020

Copy link
Copy Markdown
Member

Yeah, if you want that would be nice

@laloch

laloch commented Apr 8, 2020

Copy link
Copy Markdown
Member Author

No problem. Let's try it.
Closing to open a new one with the same contents.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Syntax for *just* stripping last newline

3 participants