Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6e5971

Browse files
committed
Fix findsource() to work for derived classes.
1 parent 63085d4 commit a6e5971

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Lib/inspect.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
__author__ = 'Ka-Ping Yee <[email protected]>'
2828
__date__ = '1 Jan 2001'
2929

30-
import sys, os, types, string, dis, imp, tokenize
30+
import sys, os, types, string, re, dis, imp, tokenize
3131

3232
# ----------------------------------------------------------- type-checking
3333
def ismodule(object):
@@ -259,10 +259,9 @@ def findsource(object):
259259

260260
if isclass(object):
261261
name = object.__name__
262-
matches = (['class', name], ['class', name + ':'])
262+
pat = re.compile(r'^\s*class\s*' + name + r'\b')
263263
for i in range(len(lines)):
264-
if string.split(lines[i])[:2] in matches:
265-
return lines, i
264+
if pat.match(lines[i]): return lines, i
266265
else: raise IOError, 'could not find class definition'
267266

268267
if ismethod(object):
@@ -277,8 +276,9 @@ def findsource(object):
277276
if not hasattr(object, 'co_firstlineno'):
278277
raise IOError, 'could not find function definition'
279278
lnum = object.co_firstlineno - 1
279+
pat = re.compile(r'^\s*def\s')
280280
while lnum > 0:
281-
if string.split(lines[lnum])[:1] == ['def']: break
281+
if pat.match(lines[lnum]): break
282282
lnum = lnum - 1
283283
return lines, lnum
284284

0 commit comments

Comments
 (0)