Permalink
Browse files

Adds error handling to the pushd and popd builtins. (#52)

Add error handling to the pushd and popd builtins.

This makes them approximately match the behavior of bash,

- Exit `pushd` when too many arguments are passed (instead of simply ignoring extraneous input).
- Fix a bug in `pushd` behavior where the current working directory is pushed onto the stack even if the argument is invalid.
  • Loading branch information...
timetoplatypus authored and andychu committed Nov 19, 2017
1 parent cc1b515 commit 9a81dcbb07003dba19131b65c941303a3a47a46a
Showing with 23 additions and 3 deletions.
  1. +23 −3 core/builtin.py
View
@@ -484,9 +484,23 @@ def Cd(argv, mem):
def Pushd(argv, dir_stack):
dir_stack.append(os.getcwd())
num_args = len(argv)
if num_args <= 0:
util.error('pushd: no other directory')
return 1
elif num_args > 1:
util.error('pushd: too many arguments')
return 1
dest_dir = argv[0]
os.chdir(dest_dir) # TODO: error checking
try:
os.chdir(dest_dir)
except OSError as e:
util.error("cd: %r: %s", dest_dir, os.strerror(e.errno))
return 1
dir_stack.append(os.getcwd())
return 0
@@ -496,7 +510,13 @@ def Popd(argv, dir_stack):
except IndexError:
log('popd: directory stack is empty')
return 1
os.chdir(dest_dir) # TODO: error checking
try:
os.chdir(dest_dir)
except OSError as e:
util.error("cd: %r: %s", dest_dir, os.strerror(e.errno))
return 1
return 0

0 comments on commit 9a81dcb

Please sign in to comment.