Skip to content

Commit 54519d9

Browse files
committed
Address review comments
1 parent dae74dd commit 54519d9

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

Lib/pydoc.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -387,20 +387,17 @@ def ispackage(path):
387387
def source_synopsis(file):
388388
"""Return the one-line summary of a file object, if present"""
389389

390-
if hasattr(file, 'buffer'):
391-
file = file.buffer
392-
393390
try:
394-
source = file.read()
395-
tree = ast.parse(source)
396-
397-
if (tree.body and isinstance(tree.body[0], ast.Expr) and
398-
isinstance(tree.body[0].value, ast.Constant) and
399-
isinstance(tree.body[0].value.value, str)):
400-
docstring = tree.body[0].value.value
401-
return docstring.strip().split('\n')[0].strip()
402-
return None
403-
except (UnicodeDecodeError, SyntaxError, ValueError) as e:
391+
tokens = tokenize.generate_tokens(file.readline)
392+
for tok_type, tok_string, _, _, _ in tokens:
393+
if tok_type == tokenize.STRING:
394+
docstring = ast.literal_eval(tok_string)
395+
if isinstance(docstring, str):
396+
return docstring.strip().split('\n')[0].strip()
397+
return None
398+
elif tok_type not in (tokenize.COMMENT, tokenize.NL, tokenize.ENCODING):
399+
return None
400+
except (tokenize.TokenError, UnicodeDecodeError, SyntaxError, ValueError) as e:
404401
return None
405402

406403
def synopsis(filename, cache={}):

0 commit comments

Comments
 (0)