summaryrefslogtreecommitdiffstats
path: root/Lib/pydoc.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-02-20 21:46:06 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-02-20 21:46:06 (GMT)
commit5e3d7a401d8568d3308b8928b146a9c306bc3ca8 (patch)
tree03ed582741171729bef1b65c37c406d6ac5e866e /Lib/pydoc.py
parente55181f517bbfc875065ce86ed3e05cf0e0246fa (diff)
downloadcpython-5e3d7a401d8568d3308b8928b146a9c306bc3ca8.zip
cpython-5e3d7a401d8568d3308b8928b146a9c306bc3ca8.tar.gz
cpython-5e3d7a401d8568d3308b8928b146a9c306bc3ca8.tar.bz2
Issue #23374: Fixed pydoc failure with non-ASCII files when stdout encoding
differs from file system encoding (e.g. on Mac OS).
Diffstat (limited to 'Lib/pydoc.py')
-rwxr-xr-xLib/pydoc.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index d37ebf1..8bbebc9 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1407,9 +1407,6 @@ 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)
@@ -1452,10 +1449,12 @@ def plain(text):
def pipepager(text, cmd):
"""Page through text by feeding it to another program."""
- pipe = os.popen(cmd, 'w')
+ import subprocess
+ proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
try:
- pipe.write(text)
- pipe.close()
+ with proc:
+ with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
+ pipe.write(text)
except OSError:
pass # Ignore broken pipes caused by quitting the pager program.
@@ -1463,16 +1462,21 @@ def tempfilepager(text, cmd):
"""Page through text by invoking a program on a temporary file."""
import tempfile
filename = tempfile.mktemp()
- with open(filename, 'w') as file:
+ with open(filename, 'w', errors='backslashreplace') as file:
file.write(text)
try:
os.system(cmd + ' "' + filename + '"')
finally:
os.unlink(filename)
+def _escape_stdout(text):
+ # Escape non-encodable characters to avoid encoding errors later
+ encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8'
+ return text.encode(encoding, 'backslashreplace').decode(encoding)
+
def ttypager(text):
"""Page through text on a text terminal."""
- lines = plain(text).split('\n')
+ lines = plain(_escape_stdout(text)).split('\n')
try:
import tty
fd = sys.stdin.fileno()
@@ -1516,7 +1520,7 @@ def ttypager(text):
def plainpager(text):
"""Simply print unformatted text. This is the ultimate fallback."""
- sys.stdout.write(plain(text))
+ sys.stdout.write(plain(_escape_stdout(text)))
def describe(thing):
"""Produce a short description of the given thing."""