summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorTian Gao <gaogaotiantian@hotmail.com>2024-03-21 10:30:10 (GMT)
committerGitHub <noreply@github.com>2024-03-21 10:30:10 (GMT)
commitd16c9d1278164f04778861814ebc87ed087511fc (patch)
treef75015b09200282f310af828f79edaa650c3c908 /Lib/inspect.py
parent6547330f4e896c6748da23704b617e060e6cc68e (diff)
downloadcpython-d16c9d1278164f04778861814ebc87ed087511fc.zip
cpython-d16c9d1278164f04778861814ebc87ed087511fc.tar.gz
cpython-d16c9d1278164f04778861814ebc87ed087511fc.tar.bz2
gh-116987: Support class code objects in inspect.findsource() (GH-117025)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py11
1 files changed, 2 insertions, 9 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 7336cea..422c09a 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1157,15 +1157,8 @@ def findsource(object):
if not hasattr(object, 'co_firstlineno'):
raise OSError('could not find function definition')
lnum = object.co_firstlineno - 1
- pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
- while lnum > 0:
- try:
- line = lines[lnum]
- except IndexError:
- raise OSError('lineno is out of bounds')
- if pat.match(line):
- break
- lnum = lnum - 1
+ if lnum >= len(lines):
+ raise OSError('lineno is out of bounds')
return lines, lnum
raise OSError('could not find code object')