[Python-checkins] CVS: python/dist/src/Lib inspect.py,1.6,1.7

Ka-Ping Yee ping@users.sourceforge.net
Thu, 01 Mar 2001 21:50:36 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv7296

Modified Files:
	inspect.py 
Log Message:
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
    as long as the filename also doesn't end in a suffix that indicates
    a binary file (according to the flags in imp.get_suffixes()).

Shrink try...except clauses and replace some of them with explicit checks.



Index: inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/inspect.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** inspect.py	2001/03/02 02:08:53	1.6
--- inspect.py	2001/03/02 05:50:34	1.7
***************
*** 198,215 ****
      if string.lower(filename[-4:]) in ['.pyc', '.pyo']:
          filename = filename[:-4] + '.py'
!     if string.lower(filename[-3:]) == '.py' and os.path.exists(filename):
          return filename
  
  def getabsfile(object):
!     """Return an absolute path to the source file or compiled file for an object.
  
!     The idea is for each object to have a unique origin, so this routine normalizes
!     the result as much as possible."""
!     return os.path.normcase(os.path.abspath(getsourcefile(object) or getfile(object)))
  
  modulesbyfile = {}
  
  def getmodule(object):
!     """Try to guess which module an object was defined in."""
      if isclass(object):
          return sys.modules.get(object.__module__)
--- 198,220 ----
      if string.lower(filename[-4:]) in ['.pyc', '.pyo']:
          filename = filename[:-4] + '.py'
!     for suffix, mode, kind in imp.get_suffixes():
!         if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
!             # Looks like a binary file.  We want to only return a text file.
!             return None
!     if os.path.exists(filename):
          return filename
  
  def getabsfile(object):
!     """Return an absolute path to the source or compiled file for an object.
  
!     The idea is for each object to have a unique origin, so this routine
!     normalizes the result as much as possible."""
!     return os.path.normcase(
!         os.path.abspath(getsourcefile(object) or getfile(object)))
  
  modulesbyfile = {}
  
  def getmodule(object):
!     """Return the module an object was defined in, or None if not found."""
      if isclass(object):
          return sys.modules.get(object.__module__)
***************
*** 226,238 ****
          return sys.modules[modulesbyfile[file]]
      main = sys.modules['__main__']
!     try:
          mainobject = getattr(main, object.__name__)
!         if mainobject is object: return main
!     except AttributeError: pass
      builtin = sys.modules['__builtin__']
!     try:
          builtinobject = getattr(builtin, object.__name__)
!         if builtinobject is object: return builtin
!     except AttributeError: pass
  
  def findsource(object):
--- 231,243 ----
          return sys.modules[modulesbyfile[file]]
      main = sys.modules['__main__']
!     if hasattr(main, object.__name__):
          mainobject = getattr(main, object.__name__)
!         if mainobject is object:
!             return main
      builtin = sys.modules['__builtin__']
!     if hasattr(builtin, object.__name__):
          builtinobject = getattr(builtin, object.__name__)
!         if builtinobject is object:
!             return builtin
  
  def findsource(object):
***************
*** 245,252 ****
      try:
          file = open(getsourcefile(object))
-         lines = file.readlines()
-         file.close()
      except (TypeError, IOError):
          raise IOError, 'could not get source code'
  
      if ismodule(object):
--- 250,257 ----
      try:
          file = open(getsourcefile(object))
      except (TypeError, IOError):
          raise IOError, 'could not get source code'
+     lines = file.readlines()
+     file.close()
  
      if ismodule(object):
***************
*** 270,287 ****
          object = object.f_code
      if iscode(object):
!         try:
!             lnum = object.co_firstlineno - 1
!         except AttributeError:
              raise IOError, 'could not find function definition'
!         else:
!             while lnum > 0:
!                 if string.split(lines[lnum])[:1] == ['def']: break
!                 lnum = lnum - 1
!             return lines, lnum
  
  def getcomments(object):
      """Get lines of comments immediately preceding an object's source code."""
      try: lines, lnum = findsource(object)
!     except: return None
  
      if ismodule(object):
--- 275,290 ----
          object = object.f_code
      if iscode(object):
!         if not hasattr(object, 'co_firstlineno'):
              raise IOError, 'could not find function definition'
!         lnum = object.co_firstlineno - 1
!         while lnum > 0:
!             if string.split(lines[lnum])[:1] == ['def']: break
!             lnum = lnum - 1
!         return lines, lnum
  
  def getcomments(object):
      """Get lines of comments immediately preceding an object's source code."""
      try: lines, lnum = findsource(object)
!     except IOError: return None
  
      if ismodule(object):
***************
*** 575,584 ****
          try:
              lines, lnum = findsource(frame)
              start = max(start, 1)
              start = min(start, len(lines) - context)
              lines = lines[start:start+context]
              index = lineno - 1 - start
-         except IOError:
-             lines = index = None
      else:
          lines = index = None
--- 578,588 ----
          try:
              lines, lnum = findsource(frame)
+         except IOError:
+             lines = index = None
+         else:
              start = max(start, 1)
              start = min(start, len(lines) - context)
              lines = lines[start:start+context]
              index = lineno - 1 - start
      else:
          lines = index = None