Index: Lib/pydoc.py =================================================================== --- Lib/pydoc.py (revision 77420) +++ Lib/pydoc.py (working copy) @@ -53,6 +53,7 @@ # path will be displayed. import sys, imp, os, re, types, inspect, __builtin__, pkgutil +import locale from repr import Repr from string import expandtabs, find, join, lower, split, strip, rfind, rstrip from traceback import extract_tb @@ -1333,22 +1334,28 @@ if sys.platform == 'win32' or sys.platform.startswith('os2'): return lambda text: tempfilepager(plain(text), 'more <') if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0: - return lambda text: pipepager(text, 'less') + return lambda text: pipepager(encoded(text), 'less') import tempfile (fd, filename) = tempfile.mkstemp() os.close(fd) try: if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0: - return lambda text: pipepager(text, 'more') + return lambda text: pipepager(encoded(text), 'more') else: return ttypager finally: os.unlink(filename) +def encoded(text): + """Encode unicode text.""" + if isinstance(text, unicode): + return text.encode(locale.getpreferredencoding()) + return text + def plain(text): """Remove boldface formatting from text.""" - return re.sub('.\b', '', text) + return re.sub('.\b', '', encoded(text)) def pipepager(text, cmd): """Page through text by feeding it to another program.""" Index: Lib/test/test_pydoc.py =================================================================== --- Lib/test/test_pydoc.py (revision 77420) +++ Lib/test/test_pydoc.py (working copy) @@ -1,9 +1,8 @@ import sys import os -import os.path import difflib +import locale import subprocess -import re import pydoc import inspect import unittest @@ -15,7 +14,7 @@ from test import pydoc_mod expected_text_pattern = \ -""" +u""" NAME test.pydoc_mod - This is a test module for test_pydoc @@ -55,6 +54,7 @@ hunger lack of Python war + \xfcnicode\u2026 \x20\x20\x20\x20 nodoc_func() @@ -74,7 +74,7 @@ """.strip() expected_html_pattern = \ -""" +u"""
 
@@ -138,7 +138,8 @@
doc_func()
This function solves all of the world's problems:
hunger
lack of Python
-war
+war
+\xfcnicode\u2026
nodoc_func()

@@ -203,9 +204,10 @@ output = doc.docmodule(module) - # cleanup the extra text formatting that pydoc preforms - patt = re.compile('\b.') - output = patt.sub('', output) + # go through the pydoc pager + with test.test_support.captured_stdout() as stdout: + pydoc.pager(output) + output = stdout.getvalue() return output.strip(), loc def print_diffs(text1, text2): @@ -236,10 +238,34 @@ result, doc_loc = get_pydoc_text(pydoc_mod) expected_text = expected_text_pattern % \ (inspect.getabsfile(pydoc_mod), doc_loc) + expected_text = expected_text.encode(locale.getpreferredencoding()) if result != expected_text: print_diffs(expected_text, result) self.fail("outputs are not equal, see diff above") + def test_pager_unicode(self): + docstring = u"f\xfcr Elise" + with test.test_support.captured_stdout() as stdout: + pydoc.pager(docstring) + self.assertEqual(stdout.getvalue(), + docstring.encode(locale.getpreferredencoding())) + + def test_pager_bytes(self): + docstring = u"f\xfcr Elise" + for docbytes in docstring.encode('latin-1'), docstring.encode('utf-8'): + with test.test_support.captured_stdout() as stdout: + pydoc.pager(docbytes) + self.assertEqual(stdout.getvalue(), docbytes) + + def test_bogus_bytestring(self): + # This is preserved for backward compatibility. + # It should not be used, because it assumes that the + # docstring encoding is the same as the user preferred encoding. + def ludwig_van_b(): + """f\xfcr Elise""" + result, doc_loc = get_pydoc_text(ludwig_van_b) + self.assertIn(ludwig_van_b.__doc__, result) + def test_not_here(self): missing_module = "test.i_am_not_here" result = run_pydoc(missing_module) Index: Lib/test/pydoc_mod.py =================================================================== --- Lib/test/pydoc_mod.py (revision 77420) +++ Lib/test/pydoc_mod.py (working copy) @@ -16,11 +16,12 @@ pass def doc_func(): - """ + u""" This function solves all of the world's problems: hunger lack of Python war + \xfcnicode\u2026 """ def nodoc_func():