diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 5f128320d25..42f48e1d630 100755 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1404,6 +1404,9 @@ class _PlainTextDoc(TextDoc): def pager(text): """The first time this is called, determine what kind of pager to use.""" global pager + # Escape non-encodable characters to avoid encoding errors later + encoding = sys.getfilesystemencoding() + text = text.encode(encoding, 'backslashreplace').decode(encoding) pager = getpager() pager(text) diff --git a/Misc/NEWS b/Misc/NEWS index 7366cd84531..57460745636 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -84,6 +84,9 @@ Core and Builtins Library ------- +- Issue #21398: Fix an unicode error in the pydoc pager when the documentation + contains characters not encodable to the stdout encoding. + - Issue #16531: ipaddress.IPv4Network and ipaddress.IPv6Network now accept an (address, netmask) tuple argument, so as to easily construct network objects from existing addresses.