Skip to content

Commit d430842

Browse files
committed
Revert the removal of sphinx.util:force_decode()
After the release of 4.0.0, some 3rd party extensions have became not working with the latest Sphinx because `force_decode()` function was removed. It was deprecated since Sphinx-2.0 and warned for the removal since 3.0. This reverts the removal and extends its deprecation period to 5.0.0. I hope it helps users of these extensions.
1 parent de64bfd commit d430842

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* Revert the removal of ``sphinx.util:force_decode()`` to become some 3rd party
17+
extensions available again during 5.0
18+
1619
Bugs fixed
1720
----------
1821

doc/extdev/deprecated.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ The following is a list of deprecated interfaces.
10151015

10161016
* - ``sphinx.util.force_decode()``
10171017
- 2.0
1018-
- 4.0
1018+
- 5.0
10191019
- N/A
10201020

10211021
* - ``sphinx.util.get_matching_docs()``

sphinx/util/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,23 @@ def parselinenos(spec: str, total: int) -> List[int]:
337337
return items
338338

339339

340+
def force_decode(string: str, encoding: str) -> str:
341+
"""Forcibly get a unicode string out of a bytestring."""
342+
warnings.warn('force_decode() is deprecated.',
343+
RemovedInSphinx50Warning, stacklevel=2)
344+
if isinstance(string, bytes):
345+
try:
346+
if encoding:
347+
string = string.decode(encoding)
348+
else:
349+
# try decoding with utf-8, should only work for real UTF-8
350+
string = string.decode()
351+
except UnicodeError:
352+
# last resort -- can't fail
353+
string = string.decode('latin1')
354+
return string
355+
356+
340357
def rpartition(s: str, t: str) -> Tuple[str, str]:
341358
"""Similar to str.rpartition from 2.5, but doesn't return the separator."""
342359
warnings.warn('rpartition() is now deprecated.', RemovedInSphinx50Warning, stacklevel=2)

0 commit comments

Comments
 (0)