diff --git a/sphinx/builder.py b/sphinx/builder.py index 4c9a35a1e..8ee11edde 100644 --- a/sphinx/builder.py +++ b/sphinx/builder.py @@ -128,20 +128,19 @@ class Builder(object): Pick the best candidate for all image URIs. """ for node in doctree.traverse(nodes.image): - uri = node['candidates'].get('*', None) - if not uri: + if '*' not in node['candidates']: for imgtype in self.supported_image_types: - uri = node['candidates'].get(imgtype, None) - if uri: - node['uri'] = uri + candidate = node['candidates'].get(imgtype, None) + if candidate: break else: - self.warn('%s:%s: %s' % - (node.source, node.lineno, - 'No matching candidate for uri: %(uri)s' % node)) + self.warn('%s:%s: no matching candidate for image URI %r' % + (node.source, node.lineno, node['uri'])) continue - if uri in self.env.images: - self.images[uri] = self.env.images[uri][1] + node['uri'] = candidate + else: + candidate = node['uri'] + self.images[candidate] = self.env.images[candidate][1] # build methods diff --git a/sphinx/environment.py b/sphinx/environment.py index 036d44cfc..749f92c0d 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -63,7 +63,7 @@ default_settings = { # This is increased every time an environment attribute is added # or changed to properly invalidate pickle files. -ENV_VERSION = 23 +ENV_VERSION = 24 default_substitutions = set([ @@ -535,6 +535,7 @@ class BuildEnvironment: candidates['*'] = imguri continue imgpath = path.normpath(path.join(docdir, imguri)) + node['uri'] = imgpath if imgpath.endswith(os.extsep + '*'): for filename in glob(imgpath): basename, ext = os.path.splitext(filename) @@ -543,7 +544,11 @@ class BuildEnvironment: elif ext == '.svg': candidates['image/svg+xml'] = filename else: - imgtype = imghdr.what(filename) + f = open(filename, 'rb') + try: + imgtype = imghdr.what(f) + finally: + f.close() if imgtype: candidates['image/' + imgtype] = filename else: