Skip to content

inspect.getsourcelines() mishandles multi-line generator expressions #137477

@George-Ogden

Description

@George-Ogden

Bug report

Bug description:

import inspect

def source_error(*args):
    frame = inspect.currentframe()
    if frame is None:
        return
    frame = frame.f_back
    if frame is None:
        return
    print(inspect.getsource(frame))

def demo():
    loops = (
        None for _ in [0] if source_error(True)
    )
    list(loops)

demo()

Expected output:

def demo():
    loops = (
        None for _ in [0] if source_error(True)
    )
    list(loops)

Actual output:

    loops = (

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Labels

3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions