]> granicus.if.org Git - python/commitdiff
Fix getcomments() so that it doesn't fail with TypeErrors.
authorJeremy Hylton <jeremy@alum.mit.edu>
Thu, 28 Mar 2002 23:01:56 +0000 (23:01 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Thu, 28 Mar 2002 23:01:56 +0000 (23:01 +0000)
It appears that getcomments() can get called for classes defined in
C.  Since these don't have source code, it can't do anything useful.
A function buried many levels deep was raising a TypeError that was
not caught.

Who knows why this broke...

Lib/inspect.py

index 175f5d6eb17f434d2a79034782a5acfe58a0deda..2b28d8ebe3e31be65dcf73926ea264e42196868c 100644 (file)
@@ -416,9 +416,14 @@ def findsource(object):
     raise IOError, 'could not find code object'
 
 def getcomments(object):
-    """Get lines of comments immediately preceding an object's source code."""
-    try: lines, lnum = findsource(object)
-    except IOError: return None
+    """Get lines of comments immediately preceding an object's source code.
+
+    Returns None when source can't be found.
+    """
+    try:
+        lines, lnum = findsource(object)
+    except (IOError, TypeError):
+        return None
 
     if ismodule(object):
         # Look for a comment block at the top of the file.