Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

inspect.getsource does not work for a class-defined code object #116987

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@blhsing

Description

@blhsing

Bug report

Bug description:

In the following MRE,code.co_firstlineno correctly returns 4, the first line number of the class definition, butgetsource(code) returns the content of the entire file:

importsysfrominspectimportgetsourceclassA:code=sys._getframe(0).f_codeprint(code.co_firstlineno)print(getsource(code))

This is becauseinspect.findsource uses a regex pattern that does not matchclass:

pat=re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp