Skip to content

inspect.getsourcelines broken on commented decorators in 3.13.8 #140268

@dmadisetti

Description

@dmadisetti

Bug report

Bug description:

Off by one error in extracting the source with inspect.getsourcelines When there is a more than one line break between the decorator and the function definition.

import inspect

decorator = lambda fn: fn

@decorator
# Hello World
def decorated(a, b):
    return a + b

code, _lnum = inspect.getsourcelines(decorated)
print("".join(code))

Running with 3.13.7 gives

@decorator
# Hello World
def decorated(a, b):
    return a + b

Running with 3.13.8 gives:

@decorator
# Hello World
def decorated(a, b):

Doesn't have to be a comment

@decorator

def decorated(a, b):
    ...

produces the behavior and multiple line breaks still give the same off by one error.

CPython versions tested on:

3.13

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions