# Buggy code
def visit_image(self, node):
    olduri = node["uri"]
    # rewrite the URI if the environment knows about it
    if olduri in self.builder.images:
        node["uri"] = posixpath.join(self.builder.imgpath, self.builder.images[olduri])

    uri = node["uri"]
    if uri.lower().endswith("svg") or uri.lower().endswith("svgz"):
        atts = {"src": uri}
        if "width" in node:
            atts["width"] = node["width"]
        if "height" in node:
            atts["height"] = node["height"]
        atts["alt"] = node.get("alt", uri)
        if "align" in node:
            self.body.append(
                '<div align="%s" class="align-%s">' % (node["align"], node["align"])
            )
            self.context.append("</div>\n")
        else:
            self.context.append("")
        self.body.append(self.emptytag(node, "img", "", **atts))
        return

    if "scale" in node:
        # Try to figure out image height and width.  Docutils does that too,
        # but it tries the final file name, which does not necessarily exist
        # yet at the time the HTML file is written.
        if not ("width" in node and "height" in node):
            size = get_image_size(os.path.join(self.builder.srcdir, olduri))
            if size is None:
                self.builder.env.warn_node(
                    "Could not obtain image size. :scale: option is ignored.", node
                )
            else:
                if "width" not in node:
                    node["width"] = str(size[0])
                if "height" not in node:
                    node["height"] = str(size[1])
    BaseTranslator.visit_image(self, node)


# Traceback (AssertionError)
Traceback (most recent call last):
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/cmdline.py", line 296, in main
app.build(opts.force_all, filenames)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/application.py", line 333, in build
self.builder.build_update()
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 251, in build_update
'out of date' % len(to_build))
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 322, in build
self.write(docnames, list(updated_docnames), method)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 360, in write
self._write_serial(sorted(docnames), warnings)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/builders/__init__.py", line 368, in _write_serial
self.write_doc(docname, doctree)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/builders/html.py", line 452, in write_doc
self.docwriter.write(doctree, destination)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/docutils/writers/__init__.py", line 80, in write
self.translate()
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/sphinx/writers/html.py", line 48, in translate
self.document.walkabout(visitor)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/docutils/nodes.py", line 187, in walkabout
visitor.dispatch_departure(self)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/docutils/nodes.py", line 1895, in dispatch_departure
return method(node)
File "/Users/ptardy/dev/bb/buildbot/sandbox/lib/python2.7/site-packages/docutils/writers/_html_base.py", line 671, in depart_document
assert not self.context, 'len(context) = %s' % len(self.context)
AssertionError: len(context) = 4

# Fixed code
def visit_image(self, node):
    olduri = node["uri"]
    # rewrite the URI if the environment knows about it
    if olduri in self.builder.images:
        node["uri"] = posixpath.join(self.builder.imgpath, self.builder.images[olduri])

    uri = node["uri"]
    if uri.lower().endswith(("svg", "svgz")):
        atts = {"src": uri}
        if "width" in node:
            atts["width"] = node["width"]
        if "height" in node:
            atts["height"] = node["height"]
        atts["alt"] = node.get("alt", uri)
        if "align" in node:
            self.body.append(
                '<div align="%s" class="align-%s">' % (node["align"], node["align"])
            )
            self.context.append("</div>\n")
        else:
            self.context.append("")
        self.body.append(self.emptytag(node, "img", "", **atts))
        return

    if "scale" in node:
        # Try to figure out image height and width.  Docutils does that too,
        # but it tries the final file name, which does not necessarily exist
        # yet at the time the HTML file is written.
        if not ("width" in node and "height" in node):
            size = get_image_size(os.path.join(self.builder.srcdir, olduri))
            if size is None:
                self.builder.env.warn_node(
                    "Could not obtain image size. :scale: option is ignored.", node
                )
            else:
                if "width" not in node:
                    node["width"] = str(size[0])
                if "height" not in node:
                    node["height"] = str(size[1])
    BaseTranslator.visit_image(self, node)