Simplified module_has_submodule on Python >= 3.3.

Stopped using the imp module on Python >= 3.3. Refs #21628.
This commit is contained in:
Aymeric Augustin 2014-06-07 21:55:16 +02:00
parent 47a9347b98
commit d7f1f316bc
1 changed files with 77 additions and 57 deletions

View File

@ -1,7 +1,6 @@
from __future__ import absolute_import # Avoid importing `importlib` from this package. from __future__ import absolute_import # Avoid importing `importlib` from this package.
import copy import copy
import imp
from importlib import import_module from importlib import import_module
import os import os
import sys import sys
@ -87,6 +86,27 @@ def autodiscover_modules(*args, **kwargs):
raise raise
if sys.version_info[:2] >= (3, 3):
if sys.version_info[:2] >= (3, 4):
from importlib.util import find_spec as importlib_find
else:
from importlib import find_loader as importlib_find
def module_has_submodule(package, module_name):
"""See if 'module' is in 'package'."""
try:
package_name = package.__name__
package_path = package.__path__
except AttributeError:
# package isn't a package.
return False
full_module_name = package_name + '.' + module_name
return importlib_find(full_module_name, package_path) is not None
else:
import imp
def module_has_submodule(package, module_name): def module_has_submodule(package, module_name):
"""See if 'module' is in 'package'.""" """See if 'module' is in 'package'."""
name = ".".join([package.__name__, module_name]) name = ".".join([package.__name__, module_name])