Permalink
Browse files

Use an object-oriented style for the dir stack so we maintain its inv…

…ariants.

Also reset allowed failures for 'builtins2', now that 'dirs' works.
  • Loading branch information...
Andy Chu
Andy Chu committed Dec 19, 2017
1 parent 87f1f86 commit f17ff5298db23f6775ba6946c8639edca3606533
Showing with 39 additions and 20 deletions.
  1. +14 −18 core/builtin.py
  2. +1 −1 core/cmd_exec.py
  3. +23 −0 core/state.py
  4. +1 −1 test/spec.sh
View
@@ -480,11 +480,8 @@ def Cd(argv, mem, dir_stack):
util.error("cd %r: %s", dest_dir, os.strerror(e.errno))
return 1
# Set $PWD.
state.SetGlobalString(mem, 'PWD', dest_dir)
# Truncate the directory stack.
dir_stack[:] = [dest_dir]
state.SetGlobalString(mem, 'PWD', dest_dir) # Set $PWD.
dir_stack.Reset() # for pushd/popd/dirs
return 0
@@ -493,19 +490,19 @@ def Cd(argv, mem, dir_stack):
WITHOUT_PREFIX = 2
SINGLE_LINE = 3
def _PrintDirStack(dir_stack, mode):
to_print = list(reversed(dir_stack))
def _PrintDirStack(dir_stack, style):
"""Helper for 'dirs'."""
if mode == WITH_PREFIX:
for i, entry in enumerate(to_print):
if style == WITH_PREFIX:
for i, entry in enumerate(dir_stack.Iter()):
print('%2d %s' % (i, entry))
elif mode == WITHOUT_PREFIX:
for entry in to_print:
elif style == WITHOUT_PREFIX:
for entry in dir_stack.Iter():
print(entry)
elif mode == SINGLE_LINE:
print(' '.join(to_print))
elif style == SINGLE_LINE:
print(' '.join(dir_stack.Iter()))
sys.stdout.flush()
@@ -526,17 +523,17 @@ def Pushd(argv, dir_stack):
util.error("pushd: %r: %s", dest_dir, os.strerror(e.errno))
return 1
dir_stack.append(dest_dir)
dir_stack.Push(dest_dir)
_PrintDirStack(dir_stack, SINGLE_LINE)
return 0
def Popd(argv, dir_stack):
if len(dir_stack) <= 1:
dest_dir = dir_stack.Pop()
if dest_dir is None:
util.error('popd: directory stack is empty')
return 1
dest_dir = dir_stack.pop()
try:
os.chdir(dest_dir)
except OSError as e:
@@ -559,8 +556,7 @@ def Dirs(argv, dir_stack):
util.warn('*** dirs -l not implemented ***')
# Following bash behavior
if arg.c:
# This initialization is also in the executor.
dir_stack[:] = [os.getcwd()]
dir_stack.Reset()
elif arg.v:
_PrintDirStack(dir_stack, WITH_PREFIX)
elif arg.p:
View
@@ -124,7 +124,7 @@ def __init__(self, mem, status_lines, funcs, completion, comp_lookup,
self.traps = {}
self.fd_state = process.FdState()
self.dir_stack = [os.getcwd()]
self.dir_stack = state.DirStack()
# TODO: Pass these in from main()
self.aliases = {} # alias name -> string
View
@@ -194,6 +194,29 @@ def SetArgv(self, argv):
self.num_shifted = 0
class DirStack(object):
"""For pushd/popd/dirs."""
def __init__(self):
self.stack = []
self.Reset()
def Reset(self):
self.stack[:] = [os.getcwd()]
def Push(self, entry):
self.stack.append(entry)
def Pop(self):
if len(self.stack) <= 1:
return None
return self.stack.pop()
def Iter(self):
"""Iterate in reverse order."""
return reversed(self.stack)
class Mem(object):
"""For storing variables.
View
@@ -258,7 +258,7 @@ builtins() {
builtins2() {
# 4 tests for 'dirs' fail.
sh-spec spec/builtins2.test.sh --osh-failures-allowed 4 \
sh-spec spec/builtins2.test.sh \
${REF_SHELLS[@]} $ZSH $OSH "$@"
}

0 comments on commit f17ff52

Please sign in to comment.