Skip to content

Commit 272ca67

Browse files
committed
Change the logic
1 parent 4bb399d commit 272ca67

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

Lib/pydoc.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class or function within a module or module in a package. If the
7676
from collections import deque
7777
from reprlib import Repr
7878
from traceback import format_exception_only
79-
from token import tok_name
8079

8180
from _pyrepl.pager import (get_pager, pipe_pager,
8281
plain_pager, tempfile_pager, tty_pager)
@@ -387,28 +386,33 @@ def ispackage(path):
387386

388387
def source_synopsis(file):
389388
"""Return the one-line summary of a file object, if present"""
389+
390390
if hasattr(file, 'buffer'):
391391
file = file.buffer
392392
if isinstance(file, io.TextIOBase):
393393
try:
394-
file = io.BytesIO(bytes(file.read(), 'utf-8'))
394+
source = file.read()
395+
if isinstance(source, bytes):
396+
source = source.decode('utf-8')
395397
except UnicodeEncodeError:
396-
# an exception will be raised if both utf-8 and latin-1 don't work
397-
file = io.BytesIO(bytes(file.read(), 'latin-1'))
398-
399-
tokens = tokenize.tokenize(file.readline)
400-
401-
# tokenize always returns at least ENCODING and ENDMARKER
402-
for token in tokens:
403-
token_name = tok_name[token.type]
404-
if token.name not in {'COMMENT', 'NL', 'ENCODING'}:
405-
break
406-
407-
# xxx may not be set
408-
if token_name == 'STRING':
409-
return ast.literal_eval(token.string).strip().split('\n')[0].strip()
398+
source = file.read().decode('latin-1')
399+
else:
400+
# Binary file
401+
try:
402+
source = tokenize.untokenize(tokenize.tokenize(file.readline))
403+
except (SyntaxError, tokenize.TokenError, UnicodeDecodeError, ValueError):
404+
return None
410405

411-
return None
406+
try:
407+
tree = ast.parse(source)
408+
if (tree.body and isinstance(tree.body[0], ast.Expr) and
409+
isinstance(tree.body[0].value, ast.Constant) and
410+
isinstance(tree.body[0].value.vlaue, str)):
411+
docstring = tree.body[0].value.value
412+
return docstring.strip().split('\n')[0].strip()
413+
return None
414+
except (SyntaxError, ValueError) as e:
415+
return None
412416

413417
def synopsis(filename, cache={}):
414418
"""Get the one-line summary out of a module file."""

0 commit comments

Comments
 (0)