Skip to content

Conversation

@SilverRainZ
Copy link
Contributor

Purpose

The previous code that manually parsed data URIs had a bug: an Exception is raised when parsing non-base64 encoded data URIs. Using urllib completely avoids these problems and
simplifies the project.

Reproduce

index.rst:

=====
Index
=====

.. image:: data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%20100%20100%22%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22%23eff2f5%22/%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22%23116329%22/%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2225.0%22%20fill%3D%22%23ffffff%22/%3E%3C/svg%3E

Run make dummy we got:

Traceback
=========

    Traceback (most recent call last):
      File "/usr/lib/python3.13/site-packages/sphinx/cmd/build.py", line 432, in build_main
        app.build(args.force_all, args.filenames)
        ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/application.py", line 426, in build
        self.builder.build_update()
        ~~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/usr/lib/python3.13/site-packages/sphinx/builders/__init__.py", line 375, in build_update
        self.build(
        ~~~~~~~~~~^
            to_build,
            ^^^^^^^^^
        ...<2 lines>...
            method='update',
            ^^^^^^^^^^^^^^^^
        )
        ^
      File "/usr/lib/python3.13/site-packages/sphinx/builders/__init__.py", line 454, in build
        self.write(docnames, updated_docnames, method)
        ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/builders/__init__.py", line 735, in write
        self.write_documents(docnames)
        ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/builders/__init__.py", line 749, in write_documents
        self._write_serial(sorted_docnames)
        ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/builders/__init__.py", line 765, in _write_serial
        doctree = self.env.get_and_resolve_doctree(docname, self)
      File "/usr/lib/python3.13/site-packages/sphinx/environment/__init__.py", line 701, in get_and_resolve_doctree
        self.apply_post_transforms(doctree, docname)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/environment/__init__.py", line 773, in apply_post_transforms
        transformer.apply_transforms()
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/usr/lib/python3.13/site-packages/sphinx/transforms/__init__.py", line 92, in apply_transforms
        super().apply_transforms()  # type: ignore[misc]
        ~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/usr/lib/python3.13/site-packages/docutils/transforms/__init__.py", line 182, in apply_transforms
        transform.apply(**kwargs)
        ~~~~~~~~~~~~~~~^^^^^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/transforms/post_transforms/images.py", line 38, in apply
        self.handle(node)
        ~~~~~~~~~~~^^^^^^
      File "/usr/lib/python3.13/site-packages/sphinx/transforms/post_transforms/images.py", line 138, in handle
        image = parse_data_uri(node['uri'])
      File "/usr/lib/python3.13/site-packages/sphinx/util/images.py", line 107, in parse_data_uri
        image_data = base64.b64decode(data)
      File "/usr/lib/python3.13/base64.py", line 88, in b64decode
        return binascii.a2b_base64(s, strict_mode=validate)
               ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
    binascii.Error: Invalid base64-encoded string: number of data characters (281) cannot be 1 more than a multiple of 4

References

SilverRainZ and others added 6 commits October 29, 2025 16:43
The previous code that manually parsed data URIs had a bug:
Exception is raised when parsing non-base64 encoded data URIs.
Using urllib completely avoids these problems and
simplifies the project.
@AA-Turner AA-Turner changed the title fix: Use urllib.request.DataHandler in parse_data_uri Support images with non-base64 data URIs Nov 25, 2025
@AA-Turner AA-Turner merged commit 9ca942b into sphinx-doc:master Nov 25, 2025
30 checks passed
@AA-Turner AA-Turner added this to the 9.0.0 milestone Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants