mirror of https://github.com/python/cpython.git
Add support for dotted module names to readmodule().
This commit is contained in:
parent
1c5fb1cd1b
commit
7a840e8d50
|
@ -65,13 +65,22 @@ def __init__(self, module, name, super, file, lineno):
|
||||||
def _addmethod(self, name, lineno):
|
def _addmethod(self, name, lineno):
|
||||||
self.methods[name] = lineno
|
self.methods[name] = lineno
|
||||||
|
|
||||||
def readmodule(module, path = []):
|
def readmodule(module, path=[], inpackage=0):
|
||||||
'''Read a module file and return a dictionary of classes.
|
'''Read a module file and return a dictionary of classes.
|
||||||
|
|
||||||
Search for MODULE in PATH and sys.path, read and parse the
|
Search for MODULE in PATH and sys.path, read and parse the
|
||||||
module and return a dictionary with one entry for each class
|
module and return a dictionary with one entry for each class
|
||||||
found in the module.'''
|
found in the module.'''
|
||||||
|
|
||||||
|
i = string.rfind(module, '.')
|
||||||
|
if i >= 0:
|
||||||
|
# Dotted module name
|
||||||
|
package = module[:i]
|
||||||
|
submodule = module[i+1:]
|
||||||
|
parent = readmodule(package, path, inpackage)
|
||||||
|
child = readmodule(submodule, parent['__path__'], 1)
|
||||||
|
return child
|
||||||
|
|
||||||
if _modules.has_key(module):
|
if _modules.has_key(module):
|
||||||
# we've seen this module before...
|
# we've seen this module before...
|
||||||
return _modules[module]
|
return _modules[module]
|
||||||
|
@ -83,21 +92,20 @@ def readmodule(module, path = []):
|
||||||
|
|
||||||
# search the path for the module
|
# search the path for the module
|
||||||
f = None
|
f = None
|
||||||
suffixes = imp.get_suffixes()
|
if inpackage:
|
||||||
for dir in path + sys.path:
|
try:
|
||||||
for suff, mode, type in suffixes:
|
f, file, (suff, mode, type) = \
|
||||||
file = os.path.join(dir, module + suff)
|
imp.find_module(module, path)
|
||||||
try:
|
except ImportError:
|
||||||
f = open(file, mode)
|
f = None
|
||||||
except IOError:
|
if f is None:
|
||||||
pass
|
fullpath = path + sys.path
|
||||||
else:
|
f, file, (suff, mode, type) = imp.find_module(module, fullpath)
|
||||||
# found the module
|
if type == imp.PKG_DIRECTORY:
|
||||||
break
|
dict = {'__path__': [file]}
|
||||||
if f:
|
_modules[module] = dict
|
||||||
break
|
# XXX Should we recursively look for submodules?
|
||||||
if not f:
|
return dict
|
||||||
raise IOError, 'module ' + module + ' not found'
|
|
||||||
if type != imp.PY_SOURCE:
|
if type != imp.PY_SOURCE:
|
||||||
# not Python source, can't do anything with this module
|
# not Python source, can't do anything with this module
|
||||||
f.close()
|
f.close()
|
||||||
|
@ -130,7 +138,7 @@ def readmodule(module, path = []):
|
||||||
try:
|
try:
|
||||||
# recursively read the
|
# recursively read the
|
||||||
# imported module
|
# imported module
|
||||||
d = readmodule(n, path)
|
d = readmodule(n, path, inpackage)
|
||||||
except:
|
except:
|
||||||
print 'module',n,'not found'
|
print 'module',n,'not found'
|
||||||
pass
|
pass
|
||||||
|
@ -142,7 +150,7 @@ def readmodule(module, path = []):
|
||||||
names = string.splitfields(res.group('imp'), ',')
|
names = string.splitfields(res.group('imp'), ',')
|
||||||
try:
|
try:
|
||||||
# recursively read the imported module
|
# recursively read the imported module
|
||||||
d = readmodule(mod, path)
|
d = readmodule(mod, path, inpackage)
|
||||||
except:
|
except:
|
||||||
print 'module',mod,'not found'
|
print 'module',mod,'not found'
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue