Simplified code for readability

Splitted import compatibility function definition for better readability

Simplified code for python 3.3 load_module implementation
This commit is contained in:
Aldo Stracquadanio
2013-03-23 23:16:06 +00:00
parent be8ef33b69
commit 3ef564847d

View File

@@ -19,41 +19,52 @@ is_py3k = sys.hexversion >= 0x03000000
is_py33 = sys.hexversion >= 0x03030000
is_py25 = sys.hexversion < 0x02060000
if is_py33:
def find_module(string, path=None):
importing = None
if path is not None:
importing = importlib.find_loader(string, path)
if importing is None:
raise ImportError
def find_module_py33(string, path=None):
returning = (None, None, None)
importing = None
if path is not None:
importing = importlib.find_loader(string, path)
else:
importing = importlib.find_loader(string)
if importing is None:
raise ImportError
try:
filename = importing.get_filename(string)
if filename and os.path.exists(filename):
returning = (open(filename, 'U'), filename, False)
else:
importing = importlib.find_loader(string)
returning = (None, filename, False)
except AttributeError:
returning = (None, importing.load_module(string).__name__, False)
returning = (None, None, None)
try:
filename = importing.get_filename(string)
if filename and os.path.exists(filename):
returning = (open(filename, 'U'), filename, False)
else:
returning = (None, filename, False)
except AttributeError:
if importing is None:
returning = (None, None, False)
else:
returning = (None, importing.load_module(string).__name__, False)
return returning
else:
def find_module(string, path=None):
importing = None
if path is None:
importing = imp.find_module(string)
else:
importing = imp.find_module(string, path)
return returning
returning = (importing[0], importing[1], importing[2][2] == imp.PKG_DIRECTORY)
def find_module_pre_py33(string, path=None):
importing = None
if path is None:
importing = imp.find_module(string)
else:
importing = imp.find_module(string, path)
return returning
return (importing[0], importing[1], importing[2][2] == imp.PKG_DIRECTORY)
def find_module(string, path=None):
"""Provides information about a module.
This function isolates the differences in importing libraries introduced with
python 3.3 on; it gets a module name and optionally a path. It will return a
tuple containin an open file for the module (if not builtin), the filename
or the name of the module if it is a builtin one and a boolean indicating
ig the module is contained in a package."""
returning = (None, None, None)
if is_py33:
returning = find_module_py33(string, path)
else:
returning = find_module_pre_py33(string, path)
return returning
# next was defined in python 2.6, in python 3 obj.next won't be possible
# anymore