]> granicus.if.org Git - python/commitdiff
Bug #1550524: better heuristics to find correct class definition
authorGeorg Brandl <georg@python.org>
Thu, 12 Oct 2006 09:20:36 +0000 (09:20 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 12 Oct 2006 09:20:36 +0000 (09:20 +0000)
in inspect.findsource().
 (backport from rev. 52299)

Lib/inspect.py

index ba2021ab949eba56678e1e12d8625f3516317ee4..986a415e2c510a206ac4ff559210ae2db68315bc 100644 (file)
@@ -472,9 +472,24 @@ def findsource(object):
 
     if isclass(object):
         name = object.__name__
-        pat = re.compile(r'^\s*class\s*' + name + r'\b')
+        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
+        # make some effort to find the best matching class definition:
+        # use the one with the least indentation, which is the one
+        # that's most probably not inside a function definition.
+        candidates = []
         for i in range(len(lines)):
-            if pat.match(lines[i]): return lines, i
+            match = pat.match(lines[i])
+            if match:
+                # if it's at toplevel, it's already the best one
+                if lines[i][0] == 'c':
+                    return lines, i
+                # else add whitespace to candidate list
+                candidates.append((match.group(1), i))
+        if candidates:
+            # this will sort by whitespace, and by line number,
+            # less whitespace first
+            candidates.sort()
+            return lines, candidates[0][1]
         else:
             raise IOError('could not find class definition')