getdoc():

Remove leading whitespace from first line; remove leading and
    trailing blank lines from docstrings.  (Patch 645938 submitted
    by David Goodger.)
This commit is contained in:
Ka-Ping Yee 2002-11-30 03:53:15 +00:00
parent 362c7cd07b
commit a59ef7bbe0
2 changed files with 15 additions and 7 deletions

View File

@ -275,15 +275,23 @@ def getdoc(object):
except UnicodeError: except UnicodeError:
return None return None
else: else:
margin = None # Find minimum indentation of any non-blank lines after first line.
margin = sys.maxint
for line in lines[1:]: for line in lines[1:]:
content = len(string.lstrip(line)) content = len(string.lstrip(line))
if not content: continue if content:
indent = len(line) - content indent = len(line) - content
if margin is None: margin = indent margin = min(margin, indent)
else: margin = min(margin, indent) # Remove indentation.
if margin is not None: if lines:
lines[0] = lines[0].lstrip()
if margin < sys.maxint:
for i in range(1, len(lines)): lines[i] = lines[i][margin:] for i in range(1, len(lines)): lines[i] = lines[i][margin:]
# Remove any trailing or leading blank lines.
while lines and not lines[-1]:
lines.pop()
while lines and not lines[0]:
lines.pop(0)
return string.join(lines, '\n') return string.join(lines, '\n')
def getfile(object): def getfile(object):

View File

@ -144,7 +144,7 @@ test(inspect.getsource(mod.StupidGit) == sourcerange(21, 46),
test(inspect.getdoc(mod.StupidGit) == test(inspect.getdoc(mod.StupidGit) ==
'A longer,\n\nindented\n\ndocstring.', 'getdoc(mod.StupidGit)') 'A longer,\n\nindented\n\ndocstring.', 'getdoc(mod.StupidGit)')
test(inspect.getdoc(git.abuse) == test(inspect.getdoc(git.abuse) ==
'Another\n\ndocstring\n\ncontaining\n\ntabs\n\n', 'getdoc(git.abuse)') 'Another\n\ndocstring\n\ncontaining\n\ntabs', 'getdoc(git.abuse)')
test(inspect.getcomments(mod.StupidGit) == '# line 20\n', test(inspect.getcomments(mod.StupidGit) == '# line 20\n',
'getcomments(mod.StupidGit)') 'getcomments(mod.StupidGit)')