# Buggy code
def __exit__(self, exc_type, exc_val, exc_tb):
    # Flush any buffered output to the logger daemon.
    sys.stdout.flush()
    sys.stderr.flush()

    # restore previous output settings, either the low-level way or
    # the python way
    if self.use_fds:
        os.dup2(self._saved_stdout, sys.stdout.fileno())
        os.close(self._saved_stdout)

        os.dup2(self._saved_stderr, sys.stderr.fileno())
        os.close(self._saved_stderr)
    else:
        sys.stdout = self._saved_stdout
        sys.stderr = self._saved_stderr

    # print log contents in parent if needed.
    if self.write_log_in_parent:
        string = self.parent.recv()
        self.file_like.write(string)

    if self.close_log_in_parent:
        self.log_file.close()

    # recover and store echo settings from the child before it dies
    self.echo = self.parent.recv()

    # join the daemon process. The daemon will quit automatically
    # when the write pipe is closed; we just wait for it here.
    self.process.join()

    # restore old color and debug settings
    tty.color._force_color = self._saved_color
    tty._debug = self._saved_debug

    self._active = False  # safe to enter again


# Traceback (KeyError)
Traceback (most recent call last):
File "/u/sciteam/stewart1/spack/bin/spack", line 64, in <module>
sys.exit(spack.main.main())
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/main.py", line 770, in main
if spack.config.get('config:debug'):
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 671, in get
return config.get(path, default, scope)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 552, in __getattr__
return getattr(self.instance, name)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 548, in instance
self._instance = self.factory()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 653, in _config
_add_platform_scope(cfg, ConfigScope, name, path)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/config.py", line 606, in _add_platform_scope
platform = spack.architecture.platform().name
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/llnl/util/lang.py", line 178, in _memoized_function
func.cache[args] = func(*args)
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/architecture.py", line 516, in platform
return platform_cls()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/platforms/cray.py", line 63, in __init__
_target = self._default_target_from_env()
File "/mnt/a/u/sciteam/stewart1/spack/lib/spack/spack/platforms/cray.py", line 121, in _default_target_from_env
env={'TERM': os.environ['TERM']},
File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
raise KeyError(key)
KeyError: 'TERM'

# Fixed code
def __exit__(self, exc_type, exc_val, exc_tb):
    # Flush any buffered output to the logger daemon.
    sys.stdout.flush()
    sys.stderr.flush()

    # restore previous output settings, either the low-level way or
    # the python way
    if self.use_fds:
        os.dup2(self._saved_stdout, sys.stdout.fileno())
        os.close(self._saved_stdout)

        os.dup2(self._saved_stderr, sys.stderr.fileno())
        os.close(self._saved_stderr)
    else:
        sys.stdout = self._saved_stdout
        sys.stderr = self._saved_stderr

    # print log contents in parent if needed.
    if self.write_log_in_parent:
        string = self.parent_pipe.recv()
        self.file_like.write(string)

    if self.close_log_in_parent:
        self.log_file.close()

    # recover and store echo settings from the child before it dies
    self.echo = self.parent_pipe.recv()

    # join the daemon process. The daemon will quit automatically
    # when the write pipe is closed; we just wait for it here.
    self.process.join()

    # restore old color and debug settings
    tty.color._force_color = self._saved_color
    tty._debug = self._saved_debug

    self._active = False  # safe to enter again