Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't copy expansion state for subshells #11

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions sh_expand/bash_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sys
import tempfile
from collections.abc import Callable
from contextlib import contextmanager
from typing import TextIO

import pexpect
Expand Down Expand Up @@ -227,14 +226,6 @@ def close(self):
# TODO: Fails sometimes
pass

@contextmanager
def subshell(self):
try:
self.run_command(STR_COMMAND)
yield
finally:
self.run_command("exit")

def expand_word(self, word: str) -> list[str]:
assert self.is_open
self.log("To expand with bash:", word)
Expand Down Expand Up @@ -335,9 +326,7 @@ def compile_node(ast_object: AstNode, exp_state: BashExpansionState) -> AstNode:


def compile_node_pipe(ast_node: PipeNode, exp_state: BashExpansionState):
# TODO: Handle shopt -s lastpipe
with exp_state.subshell():
ast_node.items = [compile_node(item, exp_state) for item in ast_node.items]
ast_node.items = [compile_node(item, exp_state) for item in ast_node.items]
return ast_node


Expand All @@ -355,8 +344,7 @@ def compile_node_command(ast_node: CommandNode, exp_state: BashExpansionState):


def compile_node_subshell(ast_node: SubshellNode, exp_state: BashExpansionState):
with exp_state.subshell():
ast_node.body = compile_node(ast_node.body, exp_state)
ast_node.body = compile_node(ast_node.body, exp_state)
return ast_node


Expand Down Expand Up @@ -391,8 +379,7 @@ def compile_node_redir(ast_node: RedirNode, exp_state: BashExpansionState):

def compile_node_background(ast_node: BackgroundNode, exp_state: BashExpansionState):
ast_node.redir_list = compile_redirections(ast_node.redir_list, exp_state)
with exp_state.subshell():
ast_node.node = compile_node(ast_node.node, exp_state)
ast_node.node = compile_node(ast_node.node, exp_state)
return ast_node


Expand Down
16 changes: 4 additions & 12 deletions sh_expand/expand.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import copy

from shasta.ast_node import *

from sh_expand.util import log
Expand Down Expand Up @@ -442,8 +440,7 @@ def expand_command(command, exp_state: ExpansionState):

def expand_pipe(node, exp_state):
for i, n in enumerate(node.items):
# copy environment to simulate subshell (no outer effect)
node.items[i] = expand_command(n, copy.deepcopy(exp_state))
node.items[i] = expand_command(n, exp_state)

return node

Expand Down Expand Up @@ -477,21 +474,16 @@ def expand_and_or_semi(node, exp_state):
return node

def expand_redir_subshell(node, exp_state):
# copy environment to simulate subshell (no outer effect)
node.node = expand_command(node.node, copy.deepcopy(exp_state))

node.node = expand_command(node.node, exp_state)
return node

def expand_background(node, exp_state):
# copy environment to simulate subshell (no outer effect)
node.node = expand_command(node.node, copy.deepcopy(exp_state))

node.node = expand_command(node.node, exp_state)
return node

def expand_defun(node, exp_state):
# TODO 2020-11-24 MMG invalidate postional args
node.body = expand_command(node.body, copy.deepcopy(exp_state))

node.body = expand_command(node.body, exp_state)
return node

def expand_for(node, exp_state):
Expand Down