diff -r 4419e2941205 Lib/pydoc.py --- a/Lib/pydoc.py Sun Sep 22 11:46:51 2013 +0200 +++ b/Lib/pydoc.py Sun Sep 22 21:44:09 2013 +0530 @@ -64,6 +64,8 @@ import time import tokenize import warnings +import ast +from token import tok_name from collections import deque from reprlib import Repr from traceback import extract_tb, format_exception_only @@ -202,23 +204,28 @@ return True return False -def source_synopsis(file): - line = file.readline() - while line[:1] == '#' or not line.strip(): - line = file.readline() - if not line: break - line = line.strip() - if line[:4] == 'r"""': line = line[1:] - if line[:3] == '"""': - line = line[3:] - if line[-1:] == '\\': line = line[:-1] - while not line.strip(): - line = file.readline() - if not line: break - result = line.split('"""')[0].strip() - else: result = None - return result - +def source_synopsis(file_): + if hasattr(file_, 'buffer'): + file_ = file_.buffer + elif isinstance(file_, io.StringIO): + try: + file_ = io.BytesIO(bytes(file_.read(), 'UTF-8')) + except: + pass + try: + tokens = tokenize.tokenize(file_.readline) + except TypeError: + return None + while True: + token = next(tokens) + token.name = tok_name[token.type] + if token.name not in ['COMMENT', 'NL', 'ENCODING']: + break + if token.name == 'STRING': + return ast.literal_eval(token.string).split('\n')[0].strip() + else: + return None + def synopsis(filename, cache={}): """Get the one-line summary out of a module file.""" mtime = os.stat(filename).st_mtime