# Buggy code
def get_stack(self, name_or_stack_id):
    all_stacks = dict(self.deleted_stacks, **self.stacks)
    if name_or_stack_id in all_stacks:
        # Lookup by stack id - deleted stacks incldued
        return all_stacks[name_or_stack_id]
    else:
        # Lookup by stack name - undeleted stacks only
        for stack in self.stacks.values():
            if stack.name == name_or_stack_id:
                return stack


# Traceback (jinja2.exceptions.UndefinedError)
Traceback (most recent call last):
File "/home/user/git/modamod/inspiring-murdock/.venv/bin/inv", line 33, in <module>
sys.exit(load_entry_point('invoke==1.4.1', 'console_scripts', 'inv')())
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/invoke-1.4.1-py3.8.egg/invoke/program.py", line 384, in run
self.execute()
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/invoke-1.4.1-py3.8.egg/invoke/program.py", line 566, in execute
executor.execute(*self.tasks)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/invoke-1.4.1-py3.8.egg/invoke/executor.py", line 129, in execute
result = call.task(*args, **call.kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/invoke-1.4.1-py3.8.egg/invoke/tasks.py", line 127, in __call__
result = self.body(*args, **kwargs)
File "/home/user/git/modamod/inspiring-murdock/tasks.py", line 69, in wait
wait4cf(session(), 'test', 1, 10)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/core/models.py", line 102, in wrapper
result = func(*args, **kwargs)
File "/home/user/git/modamod/inspiring-murdock/inspiring_murdock/aws/cloudformation.py", line 86, in wait4cf
events = get_cf_events(session, stack_name, logger=logger)
File "/home/user/git/modamod/inspiring-murdock/inspiring_murdock/aws/cloudformation.py", line 76, in get_cf_events
events = itertools.chain(*map(lambda x: x.get("StackEvents"), pages)) if pages else iter(())
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/paginate.py", line 255, in __iter__
response = self._make_request(current_kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/paginate.py", line 332, in _make_request
return self._method(**current_kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/client.py", line 662, in _make_api_call
http, parsed_response = self._make_request(
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/client.py", line 682, in _make_request
return self._endpoint.make_request(operation_model, request_dict)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/endpoint.py", line 136, in _send_request
while self._needs_retry(attempts, operation_model, request_dict,
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/endpoint.py", line 253, in _needs_retry
responses = self._event_emitter.emit(
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 250, in __call__
should_retry = self._should_retry(attempt_number, response,
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 269, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 316, in __call__
checker_response = checker(attempt_number, response,
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 222, in __call__
return self._check_caught_exception(
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/retryhandler.py", line 359, in _check_caught_exception
raise caught_exception
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/endpoint.py", line 197, in _do_get_response
responses = self._event_emitter.emit(event_name, request=request)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/botocore-1.19.40-py3.8.egg/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/core/models.py", line 341, in __call__
status, headers, body = response_callback(
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/core/responses.py", line 202, in dispatch
return cls()._dispatch(*args, **kwargs)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/core/responses.py", line 312, in _dispatch
return self.call_action()
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/core/responses.py", line 397, in call_action
response = method()
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/moto/cloudformation/responses.py", line 254, in describe_stack_events
return template.render(stack=stack)
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/Jinja2-3.0.0a1-py3.8.egg/jinja2/environment.py", line 1045, in render
self.environment.handle_exception()
File "/home/user/git/modamod/inspiring-murdock/.venv/lib/python3.8/site-packages/Jinja2-3.0.0a1-py3.8.egg/jinja2/environment.py", line 787, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
jinja2.exceptions.UndefinedError: 'None' has no attribute 'events'

# Fixed code
def get_stack(self, name_or_stack_id):
    all_stacks = dict(self.deleted_stacks, **self.stacks)
    if name_or_stack_id in all_stacks:
        # Lookup by stack id - deleted stacks incldued
        return all_stacks[name_or_stack_id]
    else:
        # Lookup by stack name - undeleted stacks only
        for stack in self.stacks.values():
            if stack.name == name_or_stack_id:
                return stack
        raise ValidationError(name_or_stack_id)