[Python-checkins] CVS: python/dist/src/Lib inspect.py,1.11,1.12
Ka-Ping Yee
ping@users.sourceforge.net
Tue, 10 Apr 2001 04:43:02 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv3100
Modified Files:
inspect.py
Log Message:
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Index: inspect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/inspect.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** inspect.py 2001/03/23 15:29:59 1.11
--- inspect.py 2001/04/10 11:43:00 1.12
***************
*** 193,196 ****
--- 193,211 ----
'function, traceback, frame, or code object'
+ def getmoduleinfo(path):
+ """Get the module name, suffix, mode, and module type for a given file."""
+ filename = os.path.basename(path)
+ suffixes = map(lambda (suffix, mode, mtype):
+ (-len(suffix), suffix, mode, mtype), imp.get_suffixes())
+ suffixes.sort() # try longest suffixes first, in case they overlap
+ for neglen, suffix, mode, mtype in suffixes:
+ if filename[neglen:] == suffix:
+ return filename[:neglen], suffix, mode, mtype
+
+ def getmodulename(path):
+ """Return the module name for a given file, or None."""
+ info = getmoduleinfo(path)
+ if info: return info[0]
+
def getsourcefile(object):
"""Return the Python source file an object was defined in, if it exists."""