New submission from RalfM <ral...@t-online.de>:

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> def func1():
...     """This is func1.
...     """
...     pass
...
>>> inspect.getdoc(func1)
'This is func1.\n    '
>>>
>>> def func2():
...     """Line1
...        Line2 
...            
...            """
...
>>> inspect.getdoc(func2)
'Line1\nLine2 \n    \n    '

Note: The blank line between "Line2 " and the closing """ contains 11 spaces.

The algorithm given in PEP 257 returns what I would expect, i.e. 
'This is func1.'
and
'Line1\nLine2'
respectively.

Strictly speaking, inspect.cleandoc doesn't claim to implement PEP 257.
However, there is a comment "# Remove any trailing or leading blank lines." in 
the code of inspect.cleandoc, and this is obviously not done.

Looking at the code, the reason seems to be twofold:

1. When removing the indentation, PEP 257 also does a .rstrip() on the lines, 
inspect.cleandoc doesn't.
As a consequence, in inspect.cleandoc trailing lines with many spaces will 
still contain spaces after the indentation has been removed, thus are not empty 
and the "while lines and not lines[-1]" doesn't remove them.
That explains func2 above.

2. If all lines but the first are blank (as in func1 above), indent / margin 
will be sys.maxint / sys.maxsize and no indentation will be removed.
PEP 257 copies dedented lines to a new list. If no indentation needs to be 
removed, nothing but the first line will be copied, and so the trailing lines 
are gone.
inspect.cleandoc dedents lines inplace. If no indentation needs to be removed 
the trailing lines with spaces remain and, as they contain spaces, the "while 
lines and not lines[-1]" doesn't remove them.

There is another difference between PEP 257 and inspect.cleandoc: PEP 257 
removes trailing whitespace on every line, inspect.cleandoc preserves it.
I don't know whether that's intentional.

I see this behaviour in 3.7 and 3.8, and the inspect.cleandoc code is unchanged 
in 3.9.0rc1.

----------
components: Library (Lib)
messages: 376136
nosy: RalfM
priority: normal
severity: normal
status: open
title: inspect.getdoc/.cleandoc doesn't always remove trailing blank lines
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41671>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to