# Buggy code
def handle_signature(self, sig, signode):
    parser = DefinitionParser(sig)
    try:
        ast = self.parse_definition(parser)
        parser.assert_end()
    except DefinitionError as e:
        self.state_machine.reporter.warning(e.description, line=self.lineno)
        raise ValueError
    self.describe_signature(signode, ast)

    parent = self.env.ref_context.get("cpp:parent")
    if parent and len(parent) > 0:
        ast = ast.clone()
        ast.prefixedName = ast.name.prefix_nested_name(parent[-1])
    else:
        ast.prefixedName = ast.name
    return ast


# Traceback (KeyError)
# Sphinx version: 1.3b3
# Python version: 2.7.6
# Docutils version: 0.11 release
# Jinja2 version: 2.7.2
# Last messages:
#   Running Sphinx v1.3b3
#   loading pickled environment...
#   done
#   WARNING: 'default' html theme has been renamed to 'classic'. Please rename it to 'alabaster' new theme or 'classic'.
#   building [mo]: targets for 0 po files that are out of date
#   building [html]: targets for 1 source files that are out of date
#   updating environment:
#   [extensions changed] 1 added, 0 changed, 0 removed
#   reading sources... [100%] index
# Loaded extensions:
#   alabaster (unknown version) from /usr/local/lib/python2.7/dist-packages/alabaster-0.6.3-py2.7.egg/alabaster/__init__.pyc
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/cmdline.py", line 246, in main
app.build(opts.force_all, filenames)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/application.py", line 263, in build
self.builder.build_update()
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/builders/__init__.py", line 237, in build_update
'out of date' % len(to_build))
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/builders/__init__.py", line 251, in build
self.doctreedir, self.app))
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/environment.py", line 586, in update
self._read_serial(docnames, app)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/environment.py", line 606, in _read_serial
self.read_doc(docname, app)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/environment.py", line 758, in read_doc
pub.publish()
File "/usr/lib/python2.7/dist-packages/docutils/core.py", line 217, in publish
self.settings)
File "/usr/lib/python2.7/dist-packages/docutils/readers/__init__.py", line 72, in read
self.parse()
File "/usr/lib/python2.7/dist-packages/docutils/readers/__init__.py", line 78, in parse
self.parser.parse(self.input, document)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/__init__.py", line 172, in parse
self.statemachine.run(inputlines, document, inliner=self.inliner)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 171, in run
input_source=document['source'])
File "/usr/lib/python2.7/dist-packages/docutils/statemachine.py", line 239, in run
context, state, transitions)
File "/usr/lib/python2.7/dist-packages/docutils/statemachine.py", line 460, in check_line
return method(match, context, next_state)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 2300, in explicit_markup
nodelist, blank_finish = self.explicit_construct(match)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 2312, in explicit_construct
return method(self, expmatch)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 2055, in directive
directive_class, match, type_name, option_presets)
File "/usr/lib/python2.7/dist-packages/docutils/parsers/rst/states.py", line 2104, in run_directive
result = directive_instance.run()
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/domains/__init__.py", line 195, in run
return BaseDirective.run(self)
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/directives/__init__.py", line 159, in run
self.after_content()
File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.3b3dev_20150223-py2.7.egg/sphinx/domains/cpp.py", line 2228, in after_content
self.env.ref_context['cpp:parent'].pop()
KeyError: 'cpp:parent'

# Fixed code
def handle_signature(self, sig, signode):
    def set_lastname(name):
        parent = self.env.ref_context.get("cpp:parent")
        if parent and len(parent) > 0:
            res = name.prefix_nested_name(parent[-1])
        else:
            res = name
        assert res
        self.env.ref_context["cpp:lastname"] = res
        return res

    parser = DefinitionParser(sig)
    try:
        ast = self.parse_definition(parser)
        parser.assert_end()
    except DefinitionError as e:
        self.state_machine.reporter.warning(e.description, line=self.lineno)
        # It is easier to assume some phony name than handling the error in
        # the possibly inner declarations.
        name = ASTNestedName([ASTNestedNameElement("PhonyNameDueToError", None)])
        set_lastname(name)
        raise ValueError
    self.describe_signature(signode, ast)

    ast.prefixedName = set_lastname(ast.name)
    assert ast.prefixedName
    return ast