Index: Lib/pydoc.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/pydoc.py,v retrieving revision 1.99 diff -u -r1.99 pydoc.py --- Lib/pydoc.py 7 Nov 2004 19:16:05 -0000 1.99 +++ Lib/pydoc.py 14 Nov 2004 01:11:39 -0000 @@ -52,7 +52,7 @@ # the current directory is changed with os.chdir(), an incorrect # path will be displayed. -import sys, imp, os, re, types, inspect, __builtin__ +import sys, imp, os, locale, re, types, inspect, __builtin__ from repr import Repr from string import expandtabs, find, join, lower, split, strip, rfind, rstrip from collections import deque @@ -164,6 +164,21 @@ else: return not name.startswith('_') +# ----------------------------------------------------- Unicode support helpers + +def _ustr(obj): + """Return str() if possible, unicode() if required.""" + try: + return str(obj) + except UnicodeError: + return unicode(obj) + +_encoding = locale.getpreferredencoding() + +def _encstr(s): + """Encode string to the locale's preferred encoding.""" + return s.encode(_encoding, 'replace') + # ----------------------------------------------------- module manipulation def ispackage(path): @@ -394,6 +409,7 @@ return ''' Python: %s + %s ''' % (title, contents) @@ -572,12 +588,12 @@ filelink = '(built-in)' info = [] if hasattr(object, '__version__'): - version = str(object.__version__) + version = _ustr(object.__version__) if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': version = strip(version[11:-1]) info.append('version %s' % self.escape(version)) if hasattr(object, '__date__'): - info.append(self.escape(str(object.__date__))) + info.append(self.escape(_ustr(object.__date__))) if info: head = head + ' (%s)' % join(info, ', ') docloc = self.getdocloc(object) @@ -668,11 +684,11 @@ result = result + self.bigsection( 'Data', '#ffffff', '#55aa55', join(contents, '
\n')) if hasattr(object, '__author__'): - contents = self.markup(str(object.__author__), self.preformat) + contents = self.markup(_ustr(object.__author__), self.preformat) result = result + self.bigsection( 'Author', '#ffffff', '#7799ee', contents) if hasattr(object, '__credits__'): - contents = self.markup(str(object.__credits__), self.preformat) + contents = self.markup(_ustr(object.__credits__), self.preformat) result = result + self.bigsection( 'Credits', '#ffffff', '#7799ee', contents) @@ -1083,16 +1099,16 @@ result = result + self.section('DATA', join(contents, '\n')) if hasattr(object, '__version__'): - version = str(object.__version__) + version = _ustr(object.__version__) if version[:11] == '$' + 'Revision: ' and version[-1:] == '$': version = strip(version[11:-1]) result = result + self.section('VERSION', version) if hasattr(object, '__date__'): - result = result + self.section('DATE', str(object.__date__)) + result = result + self.section('DATE', _ustr(object.__date__)) if hasattr(object, '__author__'): - result = result + self.section('AUTHOR', str(object.__author__)) + result = result + self.section('AUTHOR', _ustr(object.__author__)) if hasattr(object, '__credits__'): - result = result + self.section('CREDITS', str(object.__credits__)) + result = result + self.section('CREDITS', _ustr(object.__credits__)) return result def docclass(self, object, name=None, mod=None): @@ -1292,7 +1308,7 @@ if chop < 0: repr = repr[:chop] + '...' line = (name and self.bold(name) + ' = ' or '') + repr if doc is not None: - line += '\n' + self.indent(str(doc)) + line += '\n' + self.indent(_ustr(doc)) return line # --------------------------------------------------------- user interfaces @@ -1342,7 +1358,7 @@ """Page through text by feeding it to another program.""" pipe = os.popen(cmd, 'w') try: - pipe.write(text) + pipe.write(_encstr(text)) pipe.close() except IOError: pass # Ignore broken pipes caused by quitting the pager program. @@ -1352,7 +1368,7 @@ import tempfile filename = tempfile.mktemp() file = open(filename, 'w') - file.write(text) + file.write(_encstr(text)) file.close() try: os.system(cmd + ' ' + filename) @@ -1361,7 +1377,7 @@ def ttypager(text): """Page through text on a text terminal.""" - lines = split(plain(text), '\n') + lines = split(plain(_encstr(text)), '\n') try: import tty fd = sys.stdin.fileno() @@ -1399,7 +1415,7 @@ def plainpager(text): """Simply print unformatted text. This is the ultimate fallback.""" - sys.stdout.write(plain(text)) + sys.stdout.write(plain(_encstr(text))) def describe(thing): """Produce a short description of the given thing.""" @@ -1475,7 +1491,7 @@ object, name = resolve(thing, forceload) page = html.page(describe(object), html.document(object, name)) file = open(name + '.html', 'w') - file.write(page) + file.write(page.encode('utf-8')) file.close() print 'wrote', name + '.html' except (ImportError, ErrorDuringImport), value: @@ -1911,9 +1927,9 @@ def send_document(self, title, contents): try: self.send_response(200) - self.send_header('Content-Type', 'text/html') + self.send_header('Content-Type', 'text/html; charset=utf-8') self.end_headers() - self.wfile.write(html.page(title, contents)) + self.wfile.write(html.page(title, contents).encode('utf-8')) except IOError: pass def do_GET(self): @@ -1924,7 +1940,7 @@ try: obj = locate(path, forceload=1) except ErrorDuringImport, value: - self.send_document(path, html.escape(str(value))) + self.send_document(path, html.escape(value)) return if obj: self.send_document(describe(obj), html.document(obj, path))