[New-bugs-announce] [issue33745] 3.7.0b5 changes the line number of empty functions with docstrings

Ned Batchelder report at bugs.python.org
Sun Jun 3 07:11:49 EDT 2018


New submission from Ned Batchelder <ned at nedbatchelder.com>:

I'm not sure if this is a regression or an intentional change.  I know that the behavior has changed.

If a function has a docstring but no other body, Python 3.7b5 assigns the line number of the docstring to the implicit "return None".  Previous versions (including 3.7b4) used the line number of the "def".

Demonstration:

$ cat /tmp/empty.py
def empty():
    pass

def empty_with_docstring():
    '''Docstring'''

def docstring():
    '''Docstring'''
    return 1

import dis, sys

print(sys.version)

for fn in [empty, empty_with_docstring, docstring]:
    print(fn.__name__)
    dis.dis(fn)

$ /usr/local/pythonz/pythons/CPython-2.7.14/bin/python2.7 /tmp/empty.py
2.7.14 (default, Oct  4 2017, 09:45:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]
empty
  2           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              3 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              3 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.6.4/bin/python3.6 /tmp/empty.py
3.6.4 (default, Dec 19 2017, 08:11:42)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.7.0b4/bin/python3.7 /tmp/empty.py
3.7.0b4 (default, May  2 2018, 21:07:21)
[Clang 9.0.0 (clang-900.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  4           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE

$ /usr/local/pythonz/pythons/CPython-3.7.0b5/bin/python3.7 /tmp/empty.py
3.7.0b5 (default, Jun  2 2018, 11:27:19)
[Clang 9.1.0 (clang-902.0.39.2)]
empty
  2           0 LOAD_CONST               0 (None)
              2 RETURN_VALUE
empty_with_docstring
  5           0 LOAD_CONST               1 (None)
              2 RETURN_VALUE
docstring
  9           0 LOAD_CONST               1 (1)
              2 RETURN_VALUE

----------
keywords: 3.7regression
messages: 318532
nosy: nedbat
priority: normal
severity: normal
status: open
title: 3.7.0b5 changes the line number of empty functions with docstrings
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33745>
_______________________________________


More information about the New-bugs-announce mailing list