[Python-checkins] CVS: python/dist/src/Lib inspect.py,1.5,1.6
Ka-Ping Yee
ping@users.sourceforge.net
Thu, 01 Mar 2001 18:08:55 -0800
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv9814
Modified Files:
inspect.py
Log Message:
Clarify the purpose of getsourcefile().
Add getabsfile() for getting a most-normalized path.
Index: inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/inspect.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** inspect.py 2001/03/02 01:19:39 1.5
--- inspect.py 2001/03/02 02:08:53 1.6
***************
*** 170,174 ****
def getfile(object):
! """Try to guess which (text or binary) file an object was defined in."""
if ismodule(object):
if hasattr(object, '__file__'):
--- 170,174 ----
def getfile(object):
! """Work out which source or compiled file an object was defined in."""
if ismodule(object):
if hasattr(object, '__file__'):
***************
*** 193,196 ****
--- 193,211 ----
'function, traceback, frame, or code object'
+ def getsourcefile(object):
+ """Return the Python source file an object was defined in, if it exists."""
+ filename = getfile(object)
+ 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 = {}
***************
*** 200,204 ****
return sys.modules.get(object.__module__)
try:
! file = os.path.abspath(getsourcefile(object))
except TypeError:
return None
--- 215,219 ----
return sys.modules.get(object.__module__)
try:
! file = getabsfile(object)
except TypeError:
return None
***************
*** 207,212 ****
for module in sys.modules.values():
if hasattr(module, '__file__'):
! modulesbyfile[
! os.path.abspath(getsourcefile(module))] = module.__name__
if modulesbyfile.has_key(file):
return sys.modules[modulesbyfile[file]]
--- 222,226 ----
for module in sys.modules.values():
if hasattr(module, '__file__'):
! modulesbyfile[getabsfile(module)] = module.__name__
if modulesbyfile.has_key(file):
return sys.modules[modulesbyfile[file]]
***************
*** 222,232 ****
except AttributeError: pass
- def getsourcefile(object):
- """Try to guess which Python source file an object was defined in."""
- filename = getfile(object)
- if filename[-4:] == '.pyc':
- filename = filename[:-4] + '.py'
- return filename
-
def findsource(object):
"""Return the entire source file and starting line number for an object.
--- 236,239 ----
***************
*** 572,576 ****
lines = lines[start:start+context]
index = lineno - 1 - start
! except:
lines = index = None
else:
--- 579,583 ----
lines = lines[start:start+context]
index = lineno - 1 - start
! except IOError:
lines = index = None
else: