Skip to content

Commit 551b842

Browse files
authored
Deprecate support for source encodings other than UTF-8 (#13666)
1 parent 0767742 commit 551b842

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Deprecated
2323
Patch by Adam Turner.
2424
* #13644: Deprecate the :py:attr:`!Parser.config` and :py:attr:`!env` attributes.
2525
Patch by Adam Turner.
26+
* #13665: Deprecate support for non-UTF 8 source encodings,
27+
scheduled for removal in Sphinx 10.
28+
Patch by Adam Turner.
2629

2730
Features added
2831
--------------

doc/usage/configuration.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,9 @@ Options for source files
11571157
The recommended encoding is ``'utf-8-sig'``.
11581158

11591159
.. versionadded:: 0.5
1160+
.. deprecated:: 8.3
1161+
Support for source encodings other than UTF-8 is deprecated.
1162+
Sphinx 10 will only support UTF-8 files.
11601163

11611164
.. confval:: source_suffix
11621165
:type: :code-py:`dict[str, str] | Sequence[str] | str`

doc/usage/restructuredtext/basics.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,11 @@ configurations:
646646
Source encoding
647647
---------------
648648

649-
Since the easiest way to include special characters like em dashes or copyright
650-
signs in reStructuredText is to directly write them as Unicode characters, one has to
651-
specify an encoding. Sphinx assumes source files to be encoded in UTF-8 by
652-
default; you can change this with the :confval:`source_encoding` config value.
649+
Sphinx supports source files that are encoded in UTF-8.
650+
This means that the full range of Unicode__ characters may be used
651+
directly in reStructuredText.
652+
653+
__ https://www.unicode.org/
653654

654655

655656
Gotchas

doc/usage/restructuredtext/directives.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ __ https://pygments.org/docs/lexers
971971
:type: text
972972
973973
Explicitly specify the encoding of the file.
974-
This overwrites the default encoding (:confval:`source_encoding`).
974+
This overwrites the default encoding (UTF-8).
975975
For example:
976976
977977
.. code-block:: rst

sphinx/config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,21 @@ def check_master_doc(
895895
return changed
896896

897897

898+
def deprecate_source_encoding(_app: Sphinx, config: Config) -> None:
899+
"""Warn on non-UTF 8 source_encoding."""
900+
# RemovedInSphinx10Warning
901+
if config.source_encoding.lower() not in {'utf-8', 'utf-8-sig', 'utf8'}:
902+
msg = _(
903+
'Support for source encodings other than UTF-8 '
904+
'is deprecated and will be removed in Sphinx 10. '
905+
'Please comment at https://github.com/sphinx-doc/sphinx/issues/13665 '
906+
'if this causes a problem.'
907+
)
908+
logger.warning(msg)
909+
910+
898911
def setup(app: Sphinx) -> ExtensionMetadata:
912+
app.connect('config-inited', deprecate_source_encoding, priority=790)
899913
app.connect('config-inited', convert_source_suffix, priority=800)
900914
app.connect('config-inited', convert_highlight_options, priority=800)
901915
app.connect('config-inited', init_numfig_format, priority=800)

tests/test_config/test_config.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
)
2020
from sphinx.deprecation import RemovedInSphinx90Warning
2121
from sphinx.errors import ConfigError, ExtensionError, VersionRequirementError
22+
from sphinx.testing.util import SphinxTestApp
2223
from sphinx.util.tags import Tags
2324

2425
if TYPE_CHECKING:
2526
from collections.abc import Iterable
27+
from pathlib import Path
2628
from typing import TypeAlias
2729

28-
from sphinx.testing.util import SphinxTestApp
29-
3030
CircularList: TypeAlias = list[int | 'CircularList']
3131
CircularDict: TypeAlias = dict[str, int | 'CircularDict']
3232

@@ -811,3 +811,14 @@ def test_root_doc_and_master_doc_are_synchronized() -> None:
811811
c.root_doc = '1234'
812812
assert c.master_doc == '1234'
813813
assert c.root_doc == c.master_doc
814+
815+
816+
def test_source_encoding_deprecation(tmp_path: Path) -> None:
817+
(tmp_path / 'conf.py').touch()
818+
app = SphinxTestApp(
819+
buildername='dummy',
820+
srcdir=tmp_path,
821+
confoverrides={'source_encoding': 'latin-1'},
822+
)
823+
expected = 'Support for source encodings other than UTF-8 is deprecated and will be removed'
824+
assert expected in app.warning.getvalue()

0 commit comments

Comments
 (0)